-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for 81 - Cucumber Expression using Enums errors when two enums ex…
…ist with the same short name (#100) * Fix for 81 - Cucumber Expression using Enums errors when two enums with the same short name exist as parameters to cucumber expressions. This fix causes the CucumberExpressionParameterTypeRegistry to use the FQN of the enum Types used as Cucumber Expression parameters. * Change Log * Modified CucumberExpressionParameterTypeRegistry to lookup enums by type FullName. * Code clean-up per PR code review #100 (review) * Clean-up per PR code review. * Added to test to confirm creation of InValid StepDefinitionBindings and BindingRegistry in InValid state when ambiguous enum cucumber parameter expressions are present. Modified Error message for formatting.
- Loading branch information
1 parent
4f620a0
commit 791b0c5
Showing
3 changed files
with
153 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 142 additions & 22 deletions
164
...RuntimeTests/Bindings/CucumberExpressions/CucumberExpressionParameterTypeRegistryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,154 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using FluentAssertions; | ||
using Reqnroll.Bindings; | ||
using Reqnroll.Bindings.CucumberExpressions; | ||
using Reqnroll.Bindings.Discovery; | ||
using Reqnroll.Bindings.Reflection; | ||
using Reqnroll.Infrastructure; | ||
using Xunit; | ||
|
||
namespace Reqnroll.RuntimeTests.Bindings.CucumberExpressions; | ||
namespace Reqnroll.RuntimeTests.Bindings.CucumberExpressions { | ||
|
||
public class CucumberExpressionParameterTypeRegistryTests | ||
{ | ||
// Most of the logic in CucumberExpressionParameterTypeRegistry can only be tested in integration of a cucumber expression match, | ||
// so those are tested in the CucumberExpressionIntegrationTests class. | ||
|
||
private BindingRegistry _bindingRegistry; | ||
private CucumberExpressionParameterTypeRegistry CreateSut() | ||
public class CucumberExpressionParameterTypeRegistryTests | ||
{ | ||
_bindingRegistry = new BindingRegistry(); | ||
return new CucumberExpressionParameterTypeRegistry(_bindingRegistry); | ||
} | ||
// Most of the logic in CucumberExpressionParameterTypeRegistry can only be tested in integration of a cucumber expression match, | ||
// so those are tested in the CucumberExpressionIntegrationTests class. | ||
|
||
private BindingRegistry _bindingRegistry; | ||
private CucumberExpressionParameterTypeRegistry CreateSut() | ||
{ | ||
_bindingRegistry = new BindingRegistry(); | ||
return new CucumberExpressionParameterTypeRegistry(_bindingRegistry); | ||
} | ||
|
||
[Fact] | ||
public void Should_provide_string_type() | ||
{ | ||
var sut = CreateSut(); | ||
var paramType = sut.LookupByTypeName("string"); | ||
|
||
// The regex '.*' provided by the CucumberExpressionParameterTypeRegistry is fake and | ||
// will be ignored because of the special string type handling implemented in ReqnrollCucumberExpression. | ||
// See ReqnrollCucumberExpression.HandleStringType for detailed explanation. | ||
paramType.Should().NotBeNull(); | ||
paramType.RegexStrings.Should().HaveCount(1); | ||
paramType.RegexStrings[0].Should().Be(".*"); | ||
} | ||
|
||
public class SampleEnumUsingClass | ||
{ | ||
public void MethodUsingSampleColorEnum1(SampleColorEnum color) { } | ||
} | ||
[Fact] | ||
public void Should_not_error_on_multiple_enums_of_the_same_name() | ||
{ | ||
var sut = CreateSut(); | ||
IBindingMethod enumUsingBindingMethod1 = new RuntimeBindingMethod(typeof(SampleEnumUsingClass).GetMethod(nameof(SampleEnumUsingClass.MethodUsingSampleColorEnum1))); | ||
sut.OnBindingMethodProcessed(enumUsingBindingMethod1); | ||
IBindingMethod enumUsingBindingMethod2 = new RuntimeBindingMethod(typeof(CucumberAddtionalExpressions.EnumCucumberExpressions).GetMethod(nameof(CucumberAddtionalExpressions.EnumCucumberExpressions.MethodUsingSampleColorEnum2))); | ||
sut.OnBindingMethodProcessed(enumUsingBindingMethod2); | ||
var paramTypes = sut.GetParameterTypes().Where(pt => pt.ParameterType.IsEnum).ToList(); | ||
|
||
paramTypes.Should().HaveCount(2); | ||
} | ||
|
||
[Fact] | ||
public void ParameterTypeRegistry_should_identify_error_when_given_multiple_bindings_with_an_ambiguous_enum_as_a_parameter() | ||
{ | ||
var expression = "I have {SampleColorEnum} cucumbers in my belly"; | ||
var containerBuilder = new ContainerBuilder(new CucumberExpressionIntegrationTests.TestDependencyProvider()); | ||
var globalContainer = containerBuilder.CreateGlobalContainer(GetType().Assembly); | ||
|
||
[Fact] | ||
public void Should_provide_string_type() | ||
var bindingSourceProcessor = globalContainer.Resolve<IRuntimeBindingSourceProcessor>(); | ||
|
||
var bindingRegistry = globalContainer.Resolve<IBindingRegistry>(); | ||
|
||
// set up first method binding that uses an ambiguous enum parameter | ||
var bindingSourceMethod = new BindingSourceMethod | ||
{ | ||
BindingMethod = new RuntimeBindingMethod(typeof(CucumberExpressionIntegrationTests.SampleBindings).GetMethod(nameof(CucumberExpressionIntegrationTests.SampleBindings.StepDefWithEnumParam))), | ||
IsPublic = true, | ||
Attributes = new[] | ||
{ | ||
new BindingSourceAttribute | ||
{ | ||
AttributeType = new RuntimeBindingType(typeof(GivenAttribute)), | ||
AttributeValues = new IBindingSourceAttributeValueProvider[] | ||
{ | ||
new BindingSourceAttributeValueProvider(expression) | ||
} | ||
} | ||
} | ||
}; | ||
bindingSourceProcessor.ProcessType( | ||
new BindingSourceType | ||
{ | ||
BindingType = new RuntimeBindingType(typeof(CucumberExpressionIntegrationTests.SampleBindings)), | ||
Attributes = new[] | ||
{ | ||
new BindingSourceAttribute | ||
{ | ||
AttributeType = new RuntimeBindingType(typeof(BindingAttribute)) | ||
} | ||
}, | ||
IsPublic = true, | ||
IsClass = true | ||
}); | ||
bindingSourceProcessor.ProcessMethod(bindingSourceMethod); | ||
|
||
// set up second method binding that uses an ambiguous enum parameter | ||
var second_bindingSourceMethod = new BindingSourceMethod | ||
{ | ||
BindingMethod = new RuntimeBindingMethod(typeof(CucumberAddtionalExpressions.EnumCucumberExpressions).GetMethod(nameof(CucumberAddtionalExpressions.EnumCucumberExpressions.MethodUsingSampleColorEnum2))), | ||
IsPublic = true, | ||
Attributes = new[] | ||
{ | ||
new BindingSourceAttribute | ||
{ | ||
AttributeType = new RuntimeBindingType(typeof(GivenAttribute)), | ||
AttributeValues = new IBindingSourceAttributeValueProvider[] | ||
{ | ||
new BindingSourceAttributeValueProvider(expression) | ||
} | ||
} | ||
} | ||
}; | ||
bindingSourceProcessor.ProcessType( | ||
new BindingSourceType | ||
{ | ||
BindingType = new RuntimeBindingType(typeof(CucumberAddtionalExpressions.EnumCucumberExpressions)), | ||
Attributes = new[] | ||
{ | ||
new BindingSourceAttribute | ||
{ | ||
AttributeType = new RuntimeBindingType(typeof(BindingAttribute)) | ||
} | ||
}, | ||
IsPublic = true, | ||
IsClass = true | ||
}); | ||
bindingSourceProcessor.ProcessMethod(second_bindingSourceMethod); | ||
|
||
|
||
bindingSourceProcessor.BuildingCompleted(); | ||
|
||
bindingRegistry.IsValid.Should().BeFalse(); | ||
var stepDefs = bindingRegistry.GetStepDefinitions().ToArray(); | ||
stepDefs.Count().Should().Be(2); | ||
stepDefs.All(sd => sd.SourceExpression == expression).Should().BeTrue(); | ||
stepDefs.All(sd => sd.IsValid == false).Should().BeTrue(); | ||
stepDefs.All(sd => sd.ErrorMessage.StartsWith("Ambiguous enum")).Should().BeTrue(); | ||
} | ||
} | ||
} | ||
namespace Reqnroll.RuntimeTests.Bindings.CucumberAddtionalExpressions | ||
{ | ||
public class EnumCucumberExpressions | ||
{ | ||
var sut = CreateSut(); | ||
var paramType = sut.LookupByTypeName("string"); | ||
|
||
// The regex '.*' provided by the CucumberExpressionParameterTypeRegistry is fake and | ||
// will be ignored because of the special string type handling implemented in ReqnrollCucumberExpression. | ||
// See ReqnrollCucumberExpression.HandleStringType for detailed explanation. | ||
paramType.Should().NotBeNull(); | ||
paramType.RegexStrings.Should().HaveCount(1); | ||
paramType.RegexStrings[0].Should().Be(".*"); | ||
public enum SampleColorEnum { Yellow, Brown }; | ||
|
||
public void MethodUsingSampleColorEnum2(SampleColorEnum color) { } | ||
} | ||
} |