Skip to content

Commit

Permalink
Allow the use of custom subclasses of StepDefinitionBaseAttribute. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dnperfors committed May 5, 2020
1 parent 7355ab8 commit 23083ea
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion TechTalk.SpecFlow/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class BindingAttribute : Attribute
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public abstract class StepDefinitionBaseAttribute : Attribute
{
internal StepDefinitionType[] Types { get; private set; }
public StepDefinitionType[] Types { get; private set; }
public string Regex { get; set; }

/// <summary>
Expand Down
17 changes: 2 additions & 15 deletions TechTalk.SpecFlow/Bindings/Discovery/BindingSourceProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ private bool IsBindingType(BindingSourceType bindingSourceType)

private bool IsStepDefinitionAttribute(BindingSourceAttribute attribute)
{
return
typeof(GivenAttribute).IsAssignableFrom(attribute.AttributeType) ||
typeof(WhenAttribute).IsAssignableFrom(attribute.AttributeType) ||
typeof(ThenAttribute).IsAssignableFrom(attribute.AttributeType) ||
typeof(StepDefinitionAttribute).IsAssignableFrom(attribute.AttributeType);
return typeof(StepDefinitionBaseAttribute).IsAssignableFrom(attribute.AttributeType);
}

private bool IsHookAttribute(BindingSourceAttribute attribute)
Expand Down Expand Up @@ -301,16 +297,7 @@ protected virtual bool ValidateStepArgumentTransformation(BindingSourceMethod bi

private IEnumerable<StepDefinitionType> GetStepDefinitionTypes(BindingSourceAttribute stepDefinitionAttribute)
{
if (typeof(GivenAttribute).IsAssignableFrom(stepDefinitionAttribute.AttributeType))
return new[] { StepDefinitionType.Given };
if (typeof(WhenAttribute).IsAssignableFrom(stepDefinitionAttribute.AttributeType))
return new[] { StepDefinitionType.When };
if (typeof(ThenAttribute).IsAssignableFrom(stepDefinitionAttribute.AttributeType))
return new[] { StepDefinitionType.Then };
if (typeof(StepDefinitionAttribute).IsAssignableFrom(stepDefinitionAttribute.AttributeType))
return new[] { StepDefinitionType.Given, StepDefinitionType.When, StepDefinitionType.Then };

return new StepDefinitionType[0];
return stepDefinitionAttribute.NamedAttributeValues["Types"].GetValue<IEnumerable<StepDefinitionType>>();
}

private void ApplyForScope(BindingScope[] scopes, Action<BindingScope> action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ public class SpecFlowAttributesFilter : ISpecFlowAttributesFilter
private readonly IReadOnlyCollection<Type> _validAttributeTypes = new[]
{
typeof(BindingAttribute),
typeof(GivenAttribute),
typeof(WhenAttribute),
typeof(ThenAttribute),
typeof(HookAttribute),
typeof(StepDefinitionAttribute),
typeof(StepDefinitionBaseAttribute),
typeof(StepArgumentTransformationAttribute),
typeof(TableAliasesAttribute),
typeof(ScopeAttribute),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,23 @@ public void DannSollteEtwasPassieren()
}
}

[Binding]
public class BindingClassWithCustomStepDefinitionAttribute
{
public class GivenAndWhenAttribute : StepDefinitionBaseAttribute
{
public GivenAndWhenAttribute(string regex)
: base(regex, new[] { StepDefinitionType.Given, StepDefinitionType.When } )
{
}
}

[GivenAndWhen("given and when")]
public void GivenAndWhen()
{
}
}

[Fact]
public void ShouldFindBinding_WithDefaultOrder()
{
Expand Down Expand Up @@ -361,5 +378,18 @@ public void ShouldFindStepDefinitionsWithTranslatedAttributes()
Assert.Equal(1, bindingSourceProcessorStub.StepDefinitionBindings.Count(b => b.StepDefinitionType == StepDefinitionType.When));
Assert.Equal(1, bindingSourceProcessorStub.StepDefinitionBindings.Count(b => b.StepDefinitionType == StepDefinitionType.Then));
}

[Fact]
public void ShouldFindStepDefinitionsWithCustomAttribute()
{
var builder = new RuntimeBindingRegistryBuilder(bindingSourceProcessorStub, new SpecFlowAttributesFilter());

builder.BuildBindingsFromType(typeof(BindingClassWithCustomStepDefinitionAttribute));

Assert.Equal(2, bindingSourceProcessorStub.StepDefinitionBindings.Count);
Assert.Equal(1, bindingSourceProcessorStub.StepDefinitionBindings.Count(b => b.StepDefinitionType == StepDefinitionType.Given));
Assert.Equal(1, bindingSourceProcessorStub.StepDefinitionBindings.Count(b => b.StepDefinitionType == StepDefinitionType.When));
Assert.Equal(0, bindingSourceProcessorStub.StepDefinitionBindings.Count(b => b.StepDefinitionType == StepDefinitionType.Then));
}
}
}
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
Changes since 3.1.97

Features:
+ Allow the use of custom subclasses of StepDefinitionBaseAttribute.

Fixes for Development:
+ Changed unit tests to ignore custom user overrides to data formatting in en-US locale

API Changes:
+ Now allows override of method categories in NUnit to allow for plugin development on this function


Changes since 3.1.89

Fixes:
Expand Down

0 comments on commit 23083ea

Please sign in to comment.