Skip to content

Commit

Permalink
add generated code attribute/region to the generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed May 3, 2010
1 parent 7f75fe4 commit 310f3ea
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 8 deletions.
44 changes: 44 additions & 0 deletions Generator/CodeDomHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.CodeDom.Compiler;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace TechTalk.SpecFlow.Generator
{
Expand Down Expand Up @@ -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;
}
}
}
20 changes: 14 additions & 6 deletions Generator/SpecFlowGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IUnitTestGeneratorProvider>(project.GeneratorConfiguration.GeneratorUnitTestProviderType);
codeDomHelper.InjectIfRequired(generatorProvider);

Expand Down Expand Up @@ -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 = @"------------------------------------------------------------------------------
<auto-generated>
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Generator/SpecFlowUnitTestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Generator/UnitTestProvider/XUnitTestGeneratorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Tests/FeatureTests/ExternalSteps/ExternalSteps.feature.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Tests/RuntimeTests/ExecutionTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 310f3ea

Please sign in to comment.