diff --git a/CHANGELOG.md b/CHANGELOG.md index 98a4a9751..b92f12c9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * Support 'Order' parameter for `StepArgumentTransformationAttribute` to prioritize execution (#185) * Upgrade to Gherkin v29 from v19 (see [Gherkin changelog](https://github.com/cucumber/gherkin/blob/main/CHANGELOG.md)) (#205, #240) * Added registration of `ReqnrollFeatureFiles` for Rider/ReSharper Build (#231) +* Added option to override regex group matching behavior (#243) ## Bug fixes: @@ -16,7 +17,7 @@ * Fix: Process cannot access the file when building a multi-target project (#197) * Fix: Project dependencies transiently refer to System.Net.Http <= v4.3.0 that has high severity security vulnerability (#240) -*Contributors of this release (in alphabetical order):* @ajeckmans, @cimnine, @gasparnagy, @obligaron, @runnerok, @stbychkov +*Contributors of this release (in alphabetical order):* @ajeckmans, @cimnine, @gasparnagy, @obligaron, @olegKoshmeliuk, @runnerok, @stbychkov # v2.0.3 - 2024-06-10 diff --git a/Reqnroll/Infrastructure/DefaultDependencyProvider.cs b/Reqnroll/Infrastructure/DefaultDependencyProvider.cs index fc21b19a6..eb489540e 100644 --- a/Reqnroll/Infrastructure/DefaultDependencyProvider.cs +++ b/Reqnroll/Infrastructure/DefaultDependencyProvider.cs @@ -47,6 +47,7 @@ public virtual void RegisterGlobalContainerDefaults(ObjectContainer container) container.RegisterTypeAs(); container.RegisterTypeAs(); container.RegisterTypeAs(); + container.RegisterTypeAs(); #pragma warning disable CS0618 container.RegisterTypeAs(); #pragma warning restore CS0618 diff --git a/Reqnroll/Infrastructure/MatchArgumentCalculator.cs b/Reqnroll/Infrastructure/MatchArgumentCalculator.cs new file mode 100644 index 000000000..07af450d4 --- /dev/null +++ b/Reqnroll/Infrastructure/MatchArgumentCalculator.cs @@ -0,0 +1,26 @@ +using Reqnroll.Bindings; +using System.Linq; +using System.Text.RegularExpressions; + +namespace Reqnroll.Infrastructure +{ + public interface IMatchArgumentCalculator + { + object[] CalculateArguments(Match match, StepInstance stepInstance, IStepDefinitionBinding stepDefinitionBinding); + } + + public class MatchArgumentCalculator : IMatchArgumentCalculator + { + public object[] CalculateArguments(Match match, StepInstance stepInstance, IStepDefinitionBinding stepDefinitionBinding) + { + var regexArgs = match.Groups.Cast().Skip(1).Where(g => g.Success).Select(g => g.Value); + var arguments = regexArgs.Cast().ToList(); + if (stepInstance.MultilineTextArgument != null) + arguments.Add(stepInstance.MultilineTextArgument); + if (stepInstance.TableArgument != null) + arguments.Add(stepInstance.TableArgument); + + return arguments.ToArray(); + } + } +} diff --git a/Reqnroll/Infrastructure/StepDefinitionMatchService.cs b/Reqnroll/Infrastructure/StepDefinitionMatchService.cs index 76177a2ce..7e434ec4d 100644 --- a/Reqnroll/Infrastructure/StepDefinitionMatchService.cs +++ b/Reqnroll/Infrastructure/StepDefinitionMatchService.cs @@ -30,24 +30,14 @@ public class StepDefinitionMatchService : IStepDefinitionMatchService private readonly IBindingRegistry _bindingRegistry; private readonly IStepArgumentTypeConverter _stepArgumentTypeConverter; private readonly IErrorProvider _errorProvider; + private readonly IMatchArgumentCalculator _matchArgumentCalculator; - public StepDefinitionMatchService(IBindingRegistry bindingRegistry, IStepArgumentTypeConverter stepArgumentTypeConverter, IErrorProvider errorProvider) + public StepDefinitionMatchService(IBindingRegistry bindingRegistry, IStepArgumentTypeConverter stepArgumentTypeConverter, IErrorProvider errorProvider, IMatchArgumentCalculator matchArgumentCalculator ) { _bindingRegistry = bindingRegistry; _stepArgumentTypeConverter = stepArgumentTypeConverter; _errorProvider = errorProvider; - } - - private object[] CalculateArguments(Match match, StepInstance stepInstance) - { - var regexArgs = match.Groups.Cast().Skip(1).Where(g => g.Success).Select(g => g.Value); - var arguments = regexArgs.Cast().ToList(); - if (stepInstance.MultilineTextArgument != null) - arguments.Add(stepInstance.MultilineTextArgument); - if (stepInstance.TableArgument != null) - arguments.Add(stepInstance.TableArgument); - - return arguments.ToArray(); + _matchArgumentCalculator = matchArgumentCalculator; } private bool CanConvertArg(object value, IBindingType typeToConvertTo, CultureInfo bindingCulture) @@ -85,7 +75,7 @@ public BindingMatch Match(IStepDefinitionBinding stepDefinitionBinding, StepInst if (useScopeMatching && stepDefinitionBinding.IsScoped && stepInstance.StepContext != null && !stepDefinitionBinding.BindingScope.Match(stepInstance.StepContext, out scopeMatches)) return BindingMatch.NonMatching; - var arguments = match == null ? Array.Empty() : CalculateArguments(match, stepInstance); + var arguments = match == null ? Array.Empty() : _matchArgumentCalculator.CalculateArguments(match, stepInstance, stepDefinitionBinding); if (useParamMatching) { diff --git a/Tests/Reqnroll.RuntimeTests/Infrastructure/StepDefinitionMatchServiceTest.cs b/Tests/Reqnroll.RuntimeTests/Infrastructure/StepDefinitionMatchServiceTest.cs index 5433e65f2..7d9121f2d 100644 --- a/Tests/Reqnroll.RuntimeTests/Infrastructure/StepDefinitionMatchServiceTest.cs +++ b/Tests/Reqnroll.RuntimeTests/Infrastructure/StepDefinitionMatchServiceTest.cs @@ -31,7 +31,7 @@ public StepDefinitionMatchServiceTest() private StepDefinitionMatchService CreateSUT() { - return new StepDefinitionMatchService(bindingRegistryMock.Object, stepArgumentTypeConverterMock.Object, new StubErrorProvider()); + return new StepDefinitionMatchService(bindingRegistryMock.Object, stepArgumentTypeConverterMock.Object, new StubErrorProvider(), new MatchArgumentCalculator()); } private static BindingMethod CreateBindingMethod(string name = "dummy")