Esempio n. 1
0
        protected TestGenerator CreateTestGenerator(ProjectSettings projectSettings)
        {
            Configuration.SpecFlowConfiguration generatorSpecFlowConfiguration = ConfigurationLoader.GetDefault();
            CodeDomHelper            codeDomHelper            = new CodeDomHelper(CodeDomProviderLanguage.CSharp);
            UnitTestFeatureGenerator unitTestFeatureGenerator = new UnitTestFeatureGenerator(new NUnitTestGeneratorProvider(codeDomHelper), codeDomHelper, generatorSpecFlowConfiguration, new DecoratorRegistryStub());

            var gherkinParserFactory = new SpecFlowGherkinParserFactory();

            var generatorRegistryStub = new Mock <IFeatureGeneratorRegistry>();

            generatorRegistryStub.Setup(r => r.CreateGenerator(It.IsAny <SpecFlowDocument>())).Returns(unitTestFeatureGenerator);

            return(new TestGenerator(generatorSpecFlowConfiguration, projectSettings, TestHeaderWriterStub.Object, TestUpToDateCheckerStub.Object, generatorRegistryStub.Object, codeDomHelper, gherkinParserFactory));
        }
Esempio n. 2
0
        public void Test1()
        {
            CodeDomProvider  codeProvider = new VBCodeProvider();
            var              specFlowGherkinParserFactory = new SpecFlowGherkinParserFactory();
            var              parser = specFlowGherkinParserFactory.Create(new CultureInfo("en-gb"));
            SpecFlowDocument document;

            using (Stream stream = typeof(TestClassGeneratorTests).Assembly.GetManifestResourceStream("PB.SpecFlowMaster.Tests.SpecFlowTarget.feature.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    document = parser.Parse(reader,
                                            Path.Combine(TestContext.CurrentContext.TestDirectory, @"SpecFlowTarget.feature.txt"));
                }

            var codeNamespace = new CodeNamespace();
            var testClass     = new CodeTypeDeclaration("FeatureTest");

            codeNamespace.Types.Add(testClass);
            var context = new TestClassGenerationContext(
                unitTestGeneratorProvider: new NUnit3TestGeneratorProvider(new CodeDomHelper(codeProvider)),
                document: document,
                ns: codeNamespace,
                testClass: testClass,
                testRunnerField: null,
                testClassInitializeMethod: null,
                testClassCleanupMethod: null,
                testInitializeMethod: null,
                testCleanupMethod: null,
                scenarioInitializeMethod: null,
                scenarioStartMethod: null,
                scenarioCleanupMethod: null,
                featureBackgroundMethod: null,
                generateRowTests: false
                );

            var objectContainer = new ObjectContainer();

            objectContainer.RegisterInstanceAs(new FeatureMetadataProvider());
            objectContainer.Resolve <FeatureMetadataProvider>()[context.Document] =
                FeatureMetadata.GetFeatureMetadata(context.Document);

            var target = new MasterClassGenerator(
                objectContainer,
                MasterClassGenerator.CreateContextFromOriginContext(context, context.UnitTestGeneratorProvider),
                new CodeDomHelper(codeProvider));

            target.Generate();

            using (var outputWriter = new StringWriter())
            {
                using (codeProvider)
                {
                    var options = new CodeGeneratorOptions
                    {
                        BracingStyle = "C",
                    };

                    codeProvider.GenerateCodeFromNamespace(codeNamespace, outputWriter, options);

                    outputWriter.Flush();
                    var generatedTestCode = outputWriter.ToString();
                }
            }
        }