Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP. xUnit Collection name and Collection fixture class full name tags #1430 #1833

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using TechTalk.SpecFlow.Generator.CodeDom;
using BoDi;
using TechTalk.SpecFlow.Utils;
using System.Text.RegularExpressions;

namespace TechTalk.SpecFlow.Generator.UnitTestProvider
{
Expand All @@ -18,7 +17,16 @@ public class XUnit2TestGeneratorProvider : XUnitTestGeneratorProvider
private const string OUTPUT_INTERFACE_PARAMETER_NAME = "testOutputHelper";
private const string OUTPUT_INTERFACE_FIELD_NAME = "_testOutputHelper";
private const string FIXTUREDATA_PARAMETER_NAME = "fixtureData";

private const string COLLECTION_TAG = "xunit:collection";
private static readonly Regex _collectionNameRegex =
new Regex($@"{COLLECTION_TAG}\(""([^""']+?)""\)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

private const string COLLECTION_FIXTURE_CLASS_FULL_NAME_TAG = "xunit:collectionFixtureClassFullName";
private static readonly Regex _collectionFixtureClassFullNameRegex =
new Regex($@"{COLLECTION_FIXTURE_CLASS_FULL_NAME_TAG}\(""([\w.]+?)""\)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private const string COLLECTION_FIXTURE_FIELD_NAME = "_collectionFixture";
private string _collectionFixtureClassFullName;

public XUnit2TestGeneratorProvider(CodeDomHelper codeDomHelper)
:base(codeDomHelper)
{
Expand Down Expand Up @@ -71,14 +79,28 @@ public override void SetRow(TestClassGenerationContext generationContext, CodeMe
protected override void SetTestConstructor(TestClassGenerationContext generationContext, CodeConstructor ctorMethod) {
ctorMethod.Parameters.Add(
new CodeParameterDeclarationExpression((CodeTypeReference)generationContext.CustomData[FIXTUREDATA_PARAMETER_NAME], FIXTUREDATA_PARAMETER_NAME));

ctorMethod.Parameters.Add(
new CodeParameterDeclarationExpression(OUTPUT_INTERFACE, OUTPUT_INTERFACE_PARAMETER_NAME));

ctorMethod.Statements.Add(
new CodeAssignStatement(
new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), OUTPUT_INTERFACE_FIELD_NAME),
new CodeVariableReferenceExpression(OUTPUT_INTERFACE_PARAMETER_NAME)));

if (_collectionFixtureClassFullName != null)
{
const string CollectionFixtureParameterName = "collectionFixture";
const string CollectionFixtureFieldName = "_collectionFixture";

ctorMethod.Parameters.Add(
new CodeParameterDeclarationExpression(_collectionFixtureClassFullName, CollectionFixtureParameterName));
generationContext.TestClass.Members.Add(new CodeMemberField(_collectionFixtureClassFullName, CollectionFixtureFieldName));
ctorMethod.Statements.Add(
new CodeAssignStatement(
new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), CollectionFixtureFieldName),
new CodeVariableReferenceExpression(CollectionFixtureParameterName)));
}

base.SetTestConstructor(generationContext, ctorMethod);
}

Expand Down Expand Up @@ -108,10 +130,49 @@ public override void SetTestMethodIgnore(TestClassGenerationContext generationCo
);
}
}

public override void SetTestClassCategories(TestClassGenerationContext generationContext, IEnumerable<string> featureCategories)
{
var categories = featureCategories.ToArray();
SetTestClassCollectionIfNeeded(generationContext, categories);
base.SetTestClassCategories(generationContext, categories);
}

private void SetTestClassCollectionIfNeeded(TestClassGenerationContext generationContext, string[] categories)
{
var collectionName = FindTagSingleValue(categories, _collectionNameRegex);
var collectionFixtureClassFullName = FindTagSingleValue(categories, _collectionFixtureClassFullNameRegex);
if (collectionName == null && collectionFixtureClassFullName == null)
{
return;
}

if (collectionName != null && collectionFixtureClassFullName != null)
{
throw new TestGeneratorException(
$"It's not allowed to specify both {COLLECTION_TAG} and {COLLECTION_FIXTURE_CLASS_FULL_NAME_TAG} tags for a feature.");
}

if (collectionName != null)
{
CodeDomHelper.AddAttribute(
generationContext.TestClass,
COLLECTION_ATTRIBUTE,
new CodeAttributeArgument(new CodePrimitiveExpression(collectionName)));
return;
}

_collectionFixtureClassFullName = collectionFixtureClassFullName;
}


public override void SetTestClassParallelize(TestClassGenerationContext generationContext)
private static string FindTagSingleValue(string[] categories, Regex tagMatcher)
{
CodeDomHelper.AddAttribute(generationContext.TestClass, COLLECTION_ATTRIBUTE, new CodeAttributeArgument(new CodePrimitiveExpression(Guid.NewGuid())));
return categories
.Select(category => tagMatcher.Match(category))
.Where(match => match.Success)
.Select(match => match.Value)
.SingleOrDefault();
}

public override void FinalizeTestClass(TestClassGenerationContext generationContext)
Expand All @@ -120,16 +181,28 @@ public override void FinalizeTestClass(TestClassGenerationContext generationCont

// testRunner.ScenarioContext.ScenarioContainer.RegisterInstanceAs<ITestOutputHelper>(_testOutputHelper);
generationContext.ScenarioInitializeMethod.Statements.Add(
new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
RegisterInstanceInContainer(generationContext, OUTPUT_INTERFACE, OUTPUT_INTERFACE_FIELD_NAME));

if (_collectionFixtureClassFullName != null)
{
// testRunner.ScenarioContext.ScenarioContainer.RegisterInstanceAs<CollectionFixture>(_collectionFixture);
generationContext.ScenarioInitializeMethod.Statements.Add(
RegisterInstanceInContainer(generationContext, _collectionFixtureClassFullName, COLLECTION_FIXTURE_FIELD_NAME));
}
}

private static CodeMethodInvokeExpression RegisterInstanceInContainer(TestClassGenerationContext generationContext, string type, string fieldName)
{
return new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(
new CodeFieldReferenceExpression(null, generationContext.TestRunnerField.Name),
nameof(ScenarioContext)),
nameof(ScenarioContext.ScenarioContainer)),
nameof(IObjectContainer.RegisterInstanceAs),
new CodeTypeReference(OUTPUT_INTERFACE)),
new CodeVariableReferenceExpression(OUTPUT_INTERFACE_FIELD_NAME)));
new CodeFieldReferenceExpression(null, generationContext.TestRunnerField.Name),
nameof(ScenarioContext)),
nameof(ScenarioContext.ScenarioContainer)),
nameof(IObjectContainer.RegisterInstanceAs),
new CodeTypeReference(type)),
new CodeVariableReferenceExpression(fieldName));
}

protected override bool IsTestMethodAlreadyIgnored(CodeMemberMethod testMethod)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void SetTestClass(TestClassGenerationContext generationContext, string fe
// xUnit does not use an attribute for the TestFixture, all public classes are potential fixtures
}

public void SetTestClassCategories(TestClassGenerationContext generationContext, IEnumerable<string> featureCategories)
public virtual void SetTestClassCategories(TestClassGenerationContext generationContext, IEnumerable<string> featureCategories)
{
// Set Category trait which can be used with the /trait or /-trait xunit flags to include/exclude tests
foreach (string str in featureCategories)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,23 @@ Scenario: Should be able to log custom messages using context injection
When I execute the tests
Then the execution summary should contain
| Succeeded |
| 1 |
| 1 |

Scenario: Should be able to set collection attribute
Given there is a SpecFlow project
And a scenario 'Simple Scenario' as with collection attribute 'sample'
"""
When I do something
"""
And the following step definition
"""
[When(@"I do something")]
public void WhenIDoSomething()
{
ScenarioContext.Current.ScenarioContainer.Resolve<Xunit.Abstractions.ITestOutputHelper>().WriteLine("hello");
}
"""
When I execute the tests
Then the execution summary should contain
| Succeeded |
| 1 |
11 changes: 11 additions & 0 deletions Tests/TechTalk.SpecFlow.Specs/StepDefinitions/FeatureFileSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public void GivenAScenarioSimpleScenarioAs(string title, string scenarioContent)
");
}

[Given(@"a scenario '(.*)' as with collection attribute '(.*)'")]
public void GivenAScenarioAsWithCollectionAttribute(string title, string collection, string scenarioContent)
{
_projectsDriver.AddFeatureFile(
$@"@xunit:collection({collection})
Feature: Feature {Guid.NewGuid()}
Scenario: {title}
{scenarioContent.Replace("'''", "\"\"\"")}
");
}

[Given(@"there is a scenario in a feature file")]
public void GivenThereIsAScenarioInAFeatureFile()
{
Expand Down