diff --git a/Generator/CodeDomHelper.cs b/Generator/CodeDomHelper.cs index 9fc8a9c72..62926d5c7 100644 --- a/Generator/CodeDomHelper.cs +++ b/Generator/CodeDomHelper.cs @@ -3,6 +3,7 @@ using System.CodeDom.Compiler; using System.Globalization; using System.Reflection; +using System.Runtime.CompilerServices; namespace TechTalk.SpecFlow.Generator { @@ -114,5 +115,48 @@ public void AddDisableSourceLinePragmaStatement(CodeStatementCollection statemen break; } } + + public CodeStatement GetStartRegionStatement(string regionText) + { + switch (TargetLanguage) + { + case GenerationTargetLanguage.CSharp: + return new CodeSnippetStatement("#region " + regionText); + case GenerationTargetLanguage.VB: + return new CodeSnippetStatement("#Region \"" + regionText + "\""); + } + return new CodeCommentStatement("#region " + regionText); + } + + public CodeStatement GetEndRegionStatement() + { + switch (TargetLanguage) + { + case GenerationTargetLanguage.CSharp: + return new CodeSnippetStatement("#endregion"); + case GenerationTargetLanguage.VB: + return new CodeSnippetStatement("#End Region"); + } + return new CodeCommentStatement("#endregion"); + } + + private Version GetCurrentSpecFlowVersion() + { + return Assembly.GetExecutingAssembly().GetName().Version; + } + + public CodeTypeDeclaration CreateGeneratedTypeDeclaration(string className) + { + var result = new CodeTypeDeclaration(className); + result.CustomAttributes.Add( + new CodeAttributeDeclaration( + new CodeTypeReference(typeof(GeneratedCodeAttribute)), + new CodeAttributeArgument(new CodePrimitiveExpression("TechTalk.SpecFlow")), + new CodeAttributeArgument(new CodePrimitiveExpression(GetCurrentSpecFlowVersion().ToString())))); + result.CustomAttributes.Add( + new CodeAttributeDeclaration( + new CodeTypeReference(typeof(CompilerGeneratedAttribute)))); + return result; + } } } diff --git a/Generator/SpecFlowGenerator.cs b/Generator/SpecFlowGenerator.cs index e30dd1bd1..153b75069 100644 --- a/Generator/SpecFlowGenerator.cs +++ b/Generator/SpecFlowGenerator.cs @@ -115,26 +115,27 @@ public void GenerateTestFile(SpecFlowFeatureFile featureFile, CodeDomProvider co { outputWriter = new HackedWriter(outputWriter); - var codeNamespace = GenerateTestFileCode(featureFile, inputReader, codeProvider); + CodeDomHelper codeDomHelper = new CodeDomHelper(codeProvider); + + var codeNamespace = GenerateTestFileCode(featureFile, inputReader, codeProvider, codeDomHelper); var options = new CodeGeneratorOptions { BracingStyle = "C" }; - AddSpecFlowHeader(codeProvider, outputWriter); + AddSpecFlowHeader(codeProvider, outputWriter, codeDomHelper); codeProvider.GenerateCodeFromNamespace(codeNamespace, outputWriter, options); + AddSpecFlowFooter(codeProvider, outputWriter, codeDomHelper); outputWriter.Flush(); } - public CodeNamespace GenerateTestFileCode(SpecFlowFeatureFile featureFile, TextReader inputReader, CodeDomProvider codeProvider) + public CodeNamespace GenerateTestFileCode(SpecFlowFeatureFile featureFile, TextReader inputReader, CodeDomProvider codeProvider, CodeDomHelper codeDomHelper) { string targetNamespace = GetTargetNamespace(featureFile); SpecFlowLangParser parser = new SpecFlowLangParser(project.GeneratorConfiguration.FeatureLanguage); Feature feature = parser.Parse(inputReader, featureFile.GetFullPath(project)); - CodeDomHelper codeDomHelper = new CodeDomHelper(codeProvider); - IUnitTestGeneratorProvider generatorProvider = ConfigurationServices.CreateInstance(project.GeneratorConfiguration.GeneratorUnitTestProviderType); codeDomHelper.InjectIfRequired(generatorProvider); @@ -168,7 +169,7 @@ private string GetTargetNamespace(SpecFlowFeatureFile featureFile) return targetNamespace; } - private void AddSpecFlowHeader(CodeDomProvider codeProvider, TextWriter outputWriter) + private void AddSpecFlowHeader(CodeDomProvider codeProvider, TextWriter outputWriter, CodeDomHelper codeDomHelper) { var specFlowHeaderTemplate = @"------------------------------------------------------------------------------ @@ -191,6 +192,13 @@ the code is regenerated. { codeProvider.GenerateCodeFromStatement(new CodeCommentStatement(line), outputWriter, null); } + + codeProvider.GenerateCodeFromStatement(codeDomHelper.GetStartRegionStatement("Designer generated code"), outputWriter, null); + } + + private void AddSpecFlowFooter(CodeDomProvider codeProvider, TextWriter outputWriter, CodeDomHelper codeDomHelper) + { + codeProvider.GenerateCodeFromStatement(codeDomHelper.GetEndRegionStatement(), outputWriter, null); } public Version GetCurrentSpecFlowVersion() diff --git a/Generator/SpecFlowUnitTestConverter.cs b/Generator/SpecFlowUnitTestConverter.cs index 1d12bdaeb..41b1da021 100644 --- a/Generator/SpecFlowUnitTestConverter.cs +++ b/Generator/SpecFlowUnitTestConverter.cs @@ -53,7 +53,7 @@ public CodeNamespace GenerateUnitTestFixture(Feature feature, string testClassNa codeNamespace.Imports.Add(new CodeNamespaceImport(SPECFLOW_NAMESPACE)); - var testType = new CodeTypeDeclaration(testClassName); + var testType = codeDomHelper.CreateGeneratedTypeDeclaration(testClassName); testType.IsPartial = true; testType.TypeAttributes |= TypeAttributes.Public; codeNamespace.Types.Add(testType); diff --git a/Generator/UnitTestProvider/XUnitTestGeneratorProvider.cs b/Generator/UnitTestProvider/XUnitTestGeneratorProvider.cs index 6959b8cba..209ae657a 100644 --- a/Generator/UnitTestProvider/XUnitTestGeneratorProvider.cs +++ b/Generator/UnitTestProvider/XUnitTestGeneratorProvider.cs @@ -41,7 +41,7 @@ public void SetTestFixtureSetup(CodeMemberMethod fixtureSetupMethod) fixtureSetupMethod.Attributes |= MemberAttributes.Static; - _currentFixtureTypeDeclaration = new CodeTypeDeclaration("FixtureData"); + _currentFixtureTypeDeclaration = CodeDomHelper.CreateGeneratedTypeDeclaration("FixtureData"); _currentTestTypeDeclaration.Members.Add(_currentFixtureTypeDeclaration); var fixtureDataType = diff --git a/Tests/FeatureTests/CallingStepsFromStepDefinitions/CallingStepsFromStepDefinition.feature.cs b/Tests/FeatureTests/CallingStepsFromStepDefinitions/CallingStepsFromStepDefinition.feature.cs index 656139565..25f50baea 100644 --- a/Tests/FeatureTests/CallingStepsFromStepDefinitions/CallingStepsFromStepDefinition.feature.cs +++ b/Tests/FeatureTests/CallingStepsFromStepDefinitions/CallingStepsFromStepDefinition.feature.cs @@ -8,11 +8,14 @@ // the code is regenerated. // // ------------------------------------------------------------------------------ +#region Designer generated code namespace TechTalk.SpecFlow.FeatureTests.CallingStepsFromStepDefinitions { using TechTalk.SpecFlow; + [System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.2.0.0")] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [NUnit.Framework.TestFixtureAttribute()] [NUnit.Framework.DescriptionAttribute("Calling Steps from StepDefinitions")] public partial class CallingStepsFromStepDefinitionsFeature @@ -87,3 +90,4 @@ public virtual void DoSomethingMeaningful() } } } +#endregion diff --git a/Tests/FeatureTests/ContextInjection/ContextInjection.feature.cs b/Tests/FeatureTests/ContextInjection/ContextInjection.feature.cs index f1f52ec1f..94269a59f 100644 --- a/Tests/FeatureTests/ContextInjection/ContextInjection.feature.cs +++ b/Tests/FeatureTests/ContextInjection/ContextInjection.feature.cs @@ -8,11 +8,14 @@ // the code is regenerated. // // ------------------------------------------------------------------------------ +#region Designer generated code namespace TechTalk.SpecFlow.FeatureTests.ContextInjection { using TechTalk.SpecFlow; + [System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.2.0.0")] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [NUnit.Framework.TestFixtureAttribute()] [NUnit.Framework.DescriptionAttribute("Injecting context into step specifications")] public partial class InjectingContextIntoStepSpecificationsFeature @@ -132,3 +135,4 @@ public virtual void FeatureWithADependentContext() } } } +#endregion diff --git a/Tests/FeatureTests/ExternalSteps/ExternalSteps.feature.cs b/Tests/FeatureTests/ExternalSteps/ExternalSteps.feature.cs index 8389bf338..9b747e21d 100644 --- a/Tests/FeatureTests/ExternalSteps/ExternalSteps.feature.cs +++ b/Tests/FeatureTests/ExternalSteps/ExternalSteps.feature.cs @@ -8,11 +8,14 @@ // the code is regenerated. // // ------------------------------------------------------------------------------ +#region Designer generated code namespace TechTalk.SpecFlow.FeatureTests.ExternalSteps { using TechTalk.SpecFlow; + [System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.2.0.0")] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [NUnit.Framework.TestFixtureAttribute()] [NUnit.Framework.DescriptionAttribute("External Step Definitions")] public partial class ExternalStepDefinitionsFeature @@ -69,3 +72,4 @@ public virtual void StepsDefinedInAnExternalVBProjectAndAnExternalC_SharpProject } } } +#endregion diff --git a/Tests/FeatureTests/StepArgumentTransfomation/StepArgumentTransformation.feature.cs b/Tests/FeatureTests/StepArgumentTransfomation/StepArgumentTransformation.feature.cs index 77f92ac25..b53be37de 100644 --- a/Tests/FeatureTests/StepArgumentTransfomation/StepArgumentTransformation.feature.cs +++ b/Tests/FeatureTests/StepArgumentTransfomation/StepArgumentTransformation.feature.cs @@ -8,11 +8,14 @@ // the code is regenerated. // // ------------------------------------------------------------------------------ +#region Designer generated code namespace TechTalk.SpecFlow.FeatureTests.StepArgumentTransfomation { using TechTalk.SpecFlow; + [System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.2.0.0")] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [NUnit.Framework.TestFixtureAttribute()] [NUnit.Framework.DescriptionAttribute("Step Argument Transformations")] public partial class StepArgumentTransformationsFeature @@ -66,3 +69,4 @@ public virtual void StepsWithNon_StringArguments() } } } +#endregion diff --git a/Tests/RuntimeTests/ExecutionTestBase.cs b/Tests/RuntimeTests/ExecutionTestBase.cs index 90171332a..66117a40b 100644 --- a/Tests/RuntimeTests/ExecutionTestBase.cs +++ b/Tests/RuntimeTests/ExecutionTestBase.cs @@ -187,6 +187,8 @@ private object CompileAndCreateTest(string fileName, Feature feature) compilerParameters.GenerateInMemory = true; compilerParameters.TempFiles.KeepFiles = true; + compilerParameters.ReferencedAssemblies.Add( + TestFileHelper.GetAssemblyPath(typeof (GeneratedCodeAttribute))); //System compilerParameters.ReferencedAssemblies.Add( TestFileHelper.GetAssemblyPath(typeof (TestAttribute))); //NUnit compilerParameters.ReferencedAssemblies.Add( diff --git a/changelog.txt b/changelog.txt index 84a5933c6..8c4bd245b 100644 --- a/changelog.txt +++ b/changelog.txt @@ -14,6 +14,8 @@ New features: + Support for xUnit + Single installer for Visual Studio 2008 and 2010 (Issue 6, 10, 11) + SpecFlow Reporting doesn't work with Firefox (Issue 31) ++ Place GeneratedCodeAttribute and 'Designer generated code' region on generated code to + avoid having this code parsed by code analysis. (Issue 33) 1.2.0 - 2009/11/25