Skip to content

Commit

Permalink
Merge remote branch 'mbunit/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed Jun 4, 2010
2 parents 03a9852 + 593a62e commit 7b69438
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Generator/Configuration/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ private void SetUnitTestDefaultsByName(string name)
case "nunit":
GeneratorUnitTestProviderType = typeof(NUnitTestConverter);
break;
case "mbunit":
GeneratorUnitTestProviderType = typeof(MbUnitTestGeneratorProvider);
break;
case "xunit":
GeneratorUnitTestProviderType = typeof(XUnitTestGeneratorProvider);
break;
Expand Down
1 change: 1 addition & 0 deletions Generator/TechTalk.SpecFlow.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SpecFlowGenerator.cs" />
<Compile Include="SpecFlowUnitTestConverter.cs" />
<Compile Include="UnitTestProvider\MbUnitTestGeneratorProvider.cs" />
<Compile Include="UnitTestProvider\XUnitTestGeneratorProvider.cs" />
<Compile Include="UnitTestProvider\IUnitTestGeneratorProvider.cs" />
<Compile Include="UnitTestProvider\MsTestGeneratorProvider.cs" />
Expand Down
104 changes: 104 additions & 0 deletions Generator/UnitTestProvider/MbUnitTestGeneratorProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;

namespace TechTalk.SpecFlow.Generator.UnitTestProvider
{
public class MbUnitTestGeneratorProvider : IUnitTestGeneratorProvider
{
private const string TESTFIXTURE_ATTR = "MbUnit.Framework.TestFixtureAttribute";
private const string TEST_ATTR = "MbUnit.Framework.TestAttribute";
private const string CATEGORY_ATTR = "MbUnit.Framework.CategoryAttribute";
private const string TESTSETUP_ATTR = "MbUnit.Framework.SetUpAttribute";
private const string TESTFIXTURESETUP_ATTR = "MbUnit.Framework.FixtureSetUpAttribute";
private const string TESTFIXTURETEARDOWN_ATTR = "MbUnit.Framework.FixtureTearDownAttribute";
private const string TESTTEARDOWN_ATTR = "MbUnit.Framework.TearDownAttribute";
private const string IGNORE_ATTR = "MbUnit.Framework.IgnoreAttribute";
private const string DESCRIPTION_ATTR = "MbUnit.Framework.DescriptionAttribute";

public void SetTestFixture(CodeTypeDeclaration typeDeclaration, string title, string description)
{
typeDeclaration.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(TESTFIXTURE_ATTR)));

SetDescription(typeDeclaration.CustomAttributes, title);
}

private void SetDescription(CodeAttributeDeclarationCollection customAttributes, string description)
{
customAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(DESCRIPTION_ATTR),
new CodeAttributeArgument(
new CodePrimitiveExpression(description))));
}

private void SetCategories(CodeAttributeDeclarationCollection customAttributes, IEnumerable<string> categories)
{
foreach (var category in categories)
{
customAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(CATEGORY_ATTR),
new CodeAttributeArgument(
new CodePrimitiveExpression(category))));
}
}

public void SetTestFixtureCategories(CodeTypeDeclaration typeDeclaration, IEnumerable<string> categories)
{
SetCategories(typeDeclaration.CustomAttributes, categories);
}

public void SetTest(CodeMemberMethod memberMethod, string title)
{
memberMethod.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(TEST_ATTR)));

SetDescription(memberMethod.CustomAttributes, title);
}

public void SetTestCategories(CodeMemberMethod memberMethod, IEnumerable<string> categories)
{
SetCategories(memberMethod.CustomAttributes, categories);
}

public void SetTestSetup(CodeMemberMethod memberMethod)
{
memberMethod.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(TESTSETUP_ATTR)));
}

public void SetTestFixtureSetup(CodeMemberMethod memberMethod)
{
memberMethod.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(TESTFIXTURESETUP_ATTR)));
}

public void SetTestFixtureTearDown(CodeMemberMethod memberMethod)
{
memberMethod.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(TESTFIXTURETEARDOWN_ATTR)));
}

public void SetTestTearDown(CodeMemberMethod memberMethod)
{
memberMethod.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(TESTTEARDOWN_ATTR)));
}

public void SetIgnore(CodeTypeMember codeTypeMember)
{
codeTypeMember.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(IGNORE_ATTR)));
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Configuration/RuntimeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ private void SetUnitTestDefaultsByName(string name)
case "nunit":
RuntimeUnitTestProviderType = typeof(NUnitRuntimeProvider);
break;
case "mbunit":
RuntimeUnitTestProviderType = typeof(MbUnitRuntimeProvider);
break;
case "xunit":
RuntimeUnitTestProviderType = typeof(XUnitRuntimeProvider);
break;
Expand Down
1 change: 1 addition & 0 deletions Runtime/TechTalk.SpecFlow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Compile Include="Tracing\NullListener.cs" />
<Compile Include="Tracing\StepDefinitonSkeletonProvider.cs" />
<Compile Include="Tracing\StepFormatter.cs" />
<Compile Include="UnitTestProvider\MbUnitRuntimeProvider.cs" />
<Compile Include="UnitTestProvider\XUnitRuntimeProvider.cs" />
<Compile Include="UnitTestProvider\MsTestRuntimeProvider.cs" />
<Compile Include="UnitTestProvider\NUnitRuntimeProvider.cs" />
Expand Down
37 changes: 37 additions & 0 deletions Runtime/UnitTestProvider/MbUnitRuntimeProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;

namespace TechTalk.SpecFlow.UnitTestProvider
{
public class MbUnitRuntimeProvider : IUnitTestRuntimeProvider
{
private const string MSTEST_ASSEMBLY = "MbUnit";
private const string ASSERT_TYPE = "MbUnit.Framework.Assert";

private Action<string> assertInconclusive;

#region IUnitTestRuntimeProvider Members

public void TestInconclusive(string message)
{
if (assertInconclusive == null)
{
assertInconclusive = UnitTestRuntimeProviderHelper
.GetAssertMethod(MSTEST_ASSEMBLY, ASSERT_TYPE, "Inconclusive");
}

assertInconclusive(message);
}

public void TestIgnore(string message)
{
TestInconclusive(message); // there is no dynamic "Ignore" in mstest
}

public bool DelayedFixtureTearDown
{
get { return true; }
}

#endregion
}
}
1 change: 1 addition & 0 deletions Tests/ParserTests/ParserTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="SuccessfulMbUnitGenerationTest.cs" />
<Compile Include="SuccessfulXUnitGenerationTest.cs" />
<Compile Include="SuccessfulGenerationTest.cs" />
<Compile Include="SuccessfulParsingTest.cs" />
Expand Down
183 changes: 183 additions & 0 deletions Tests/ParserTests/SuccessfulMbUnitGenerationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.CSharp;
using NUnit.Framework;
using TechTalk.SpecFlow.Generator;
using TechTalk.SpecFlow.Generator.UnitTestProvider;
using TechTalk.SpecFlow.Parser;
using TechTalk.SpecFlow.Parser.SyntaxElements;

namespace ParserTests
{
[TestFixture]
public class SuccessfulMbUnitGenerationTest
{
private void CompareWithExpectedResult(Feature feature, string expectedResultFileName)
{
string expected = TestFileHelper.ReadFile(expectedResultFileName);
string got = GenerateCodeFromFeature(feature);

Assert.AreEqual(expected, got);
}

private void GenerateCodeFromFeature(Feature feature, TextWriter writer)
{
var codeDomHelper = new CodeDomHelper(GenerationTargetLanguage.CSharp);
var mbUnitTestGeneratorProvider = new MbUnitTestGeneratorProvider();
var converter = new SpecFlowUnitTestConverter(mbUnitTestGeneratorProvider, codeDomHelper, true);
CodeNamespace codeNamespace = converter.GenerateUnitTestFixture(feature, "TestClassName", "Target.Namespace");

var codeProvider = new CSharpCodeProvider();
var options = new CodeGeneratorOptions();
codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, options);
}

private void GenerateCodeFromFeature(Feature feature, string fileName)
{
using (var writer = new StreamWriter(fileName, false, Encoding.UTF8))
{
GenerateCodeFromFeature(feature, writer);
}
}

private string GenerateCodeFromFeature(Feature feature)
{
using (var writer = new Utf8StringWriter())
{
GenerateCodeFromFeature(feature, writer);
return writer.ToString();
}
}

[Test]
public void CanGenerateButFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "but.feature"));
}

[Test]
public void CanGenerateCommentsFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "comments.feature"));
}

[Test]
public void CanGenerateFeatureheaderFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "featureheader.feature"));
}

[Test, TestCaseSource(typeof (TestFileHelper), "GetTestFiles")]
public void CanGenerateFromFile(string fileName)
{
Console.WriteLine(fileName);
var parser = new SpecFlowLangParser(new CultureInfo("en-US"));
using (var reader = new StreamReader(fileName))
{
Feature feature = parser.Parse(reader);
Assert.IsNotNull(feature);

string generatedCode = GenerateCodeFromFeature(feature);
Assert.IsNotNull(generatedCode);

// to regenerate the expected result file:
//GenerateCodeFromFeature(feature, fileName + ".cs");

//CompareWithExpectedResult(feature, fileName + ".cs");
}
}

[Test]
public void CanGenerateFromFiles()
{
foreach (string testFile in TestFileHelper.GetTestFiles())
{
CanGenerateFromFile(testFile);
}
}

[Test]
public void CanGenerateGivenWhenThenDuplicationFeatureFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "givenwhenthenduplication.feature"));
}

[Test]
public void CanGenerateMixedGWTFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "mixedgivenwhenthen.feature"));
}

[Test]
public void CanGenerateMultilineargumentFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "multilineargument.feature"));
}

[Test]
public void CanGenerateMultilinetitleFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "multilinetitle.feature"));
}

[Test]
public void CanGenerateScneriooutlineFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "scenariooutline.feature"));
}

[Test]
public void CanGenerateSimpleFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "simple.feature"));
}

[Test]
public void CanGenerateTableargumentFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "tableargument.feature"));
}

[Test]
public void CanGenerateTagsFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "tags.feature"));
}

[Test]
public void CanGenerateWhitespacesFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "whitespaces.feature"));
}

[Test]
public void CanGeneratebackgroundFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "background.feature"));
}

[Test]
public void CanGeneratebackgroundWithTitleFeature()
{
string folder = Path.GetFullPath(Path.Combine(TestFileHelper.GetProjectLocation(), "TestFiles"));
CanGenerateFromFile(Path.Combine(folder, "background_withtitle.feature"));
}
}
}
9 changes: 5 additions & 4 deletions Tests/RuntimeTests/ConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public void CanLoadConfigFromConfigFile()
}

[Test]
public void CanLoadConfigFromString()
{
const string configString = @"<specFlow>
[TestCase(@"<specFlow>
<language feature=""en"" tool=""en"" />
<unitTestProvider name=""NUnit""
Expand All @@ -38,7 +36,10 @@ public void CanLoadConfigFromString()
minTracedDuration=""0:0:0.1""
listener=""TechTalk.SpecFlow.Tracing.DefaultListener, TechTalk.SpecFlow""
/>
</specFlow>";
</specFlow>")]
public void CanLoadConfigFromString(string configString)
{
//const string configString = ;

var runtimeConfig = RuntimeConfiguration.LoadFromConfigFile(
ConfigurationSectionHandler.CreateFromXml(configString));
Expand Down

0 comments on commit 7b69438

Please sign in to comment.