Skip to content

Commit

Permalink
Added option to override regex group matching behavior (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
olegKoshmeliuk authored Aug 30, 2024
1 parent c85984f commit 812ffc8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
* 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:

* Fix: Reqnroll.Autofac: Objects registered in the global container cannot be relsolved in BeforeTestRun/AfterTestRun hooks (#183)
* 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

Expand Down
1 change: 1 addition & 0 deletions Reqnroll/Infrastructure/DefaultDependencyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public virtual void RegisterGlobalContainerDefaults(ObjectContainer container)
container.RegisterTypeAs<CucumberExpressionStepDefinitionBindingBuilderFactory, ICucumberExpressionStepDefinitionBindingBuilderFactory>();
container.RegisterTypeAs<CucumberExpressionDetector, ICucumberExpressionDetector>();
container.RegisterTypeAs<StepDefinitionRegexCalculator, IStepDefinitionRegexCalculator>();
container.RegisterTypeAs<MatchArgumentCalculator, IMatchArgumentCalculator>();
#pragma warning disable CS0618
container.RegisterTypeAs<BindingInvoker, IBindingInvoker>();
#pragma warning restore CS0618
Expand Down
26 changes: 26 additions & 0 deletions Reqnroll/Infrastructure/MatchArgumentCalculator.cs
Original file line number Diff line number Diff line change
@@ -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<Group>().Skip(1).Where(g => g.Success).Select(g => g.Value);
var arguments = regexArgs.Cast<object>().ToList();
if (stepInstance.MultilineTextArgument != null)
arguments.Add(stepInstance.MultilineTextArgument);
if (stepInstance.TableArgument != null)
arguments.Add(stepInstance.TableArgument);

return arguments.ToArray();
}
}
}
18 changes: 4 additions & 14 deletions Reqnroll/Infrastructure/StepDefinitionMatchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Group>().Skip(1).Where(g => g.Success).Select(g => g.Value);
var arguments = regexArgs.Cast<object>().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)
Expand Down Expand Up @@ -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<object>() : CalculateArguments(match, stepInstance);
var arguments = match == null ? Array.Empty<object>() : _matchArgumentCalculator.CalculateArguments(match, stepInstance, stepDefinitionBinding);

if (useParamMatching)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 812ffc8

Please sign in to comment.