Skip to content

Commit

Permalink
Fix for 81 - Cucumber Expression using Enums errors when two enums ex…
Browse files Browse the repository at this point in the history
…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
clrudolphi committed Apr 16, 2024
1 parent 4f620a0 commit 791b0c5
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
test run (global) context instead of the test thread context.
* Support for PriorityAttribute in MsTest adapter
* Support for Scenario Outline / DataRowAttribute in MsTest adapter
* Fix for #81 in which Cucumber Expressions fail when two enums with the same short name (differing namespaces) are used as parameters

# v1.0.1 - 2024-02-16

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,23 @@ private IEnumerable<ICucumberExpressionParameterTypeTransformation> GetEnumTypes
{
yield return new BuiltInCucumberExpressionParameterTypeTransformation(
CucumberExpressionParameterType.MatchAllRegex,
enumParameterType);
enumParameterType,
enumParameterType.Type.FullName);
}
}

public IParameterType LookupByTypeName(string name)
{
if (_parameterTypesByName.Value.TryGetValue(name, out var parameterType))
return parameterType;
//enum keys contain the Fullname of the type, try matching on the short name:
var matchingEnums = _parameterTypesByName.Value.Where(kvp => kvp.Value.ParameterType.IsEnum && (kvp.Key.EndsWith("." + name) || kvp.Key.EndsWith("+" + name))).ToArray();
if (matchingEnums.Length == 0) { return null; }
if (matchingEnums.Length == 1) { return matchingEnums[0].Value; }
if (matchingEnums.Length > 1)
{
throw new ReqnrollException($"Ambiguous enum in cucumber expression. Multiple enums share the same short name '{name}'. Use the enum's full name in the cucumber expression or define a [StepArgumentTransformation] with the chosen type and the short name.");
}
return null;
}

Expand Down
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) { }
}
}

0 comments on commit 791b0c5

Please sign in to comment.