Skip to content

Commit 13d9ace

Browse files
Fix ambiguous header matching in fluent scanner for example tables (#289)
* - added a specific message Added ArgumentException with a message to let user know that parameter name conflicts with header name in ExampleTable * throw exception on ambiguous match for headers * added unit tests for changes --------- Co-authored-by: Fedor <[email protected]>
1 parent 04ca53c commit 13d9ace

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace TestStack.BDDfy.Tests.Scanner.FluentScanner
6+
{
7+
public class AmbiguousHeaderMatchTests
8+
{
9+
private int _count;
10+
11+
[Fact]
12+
public void ThrowsWhenMultipleHeadersMatchParameterName()
13+
{
14+
// Act & Assert
15+
var exception = Should.Throw<AmbiguousMatchException>(() =>
16+
{
17+
this.Given(_ => GivenInput(_count)) // Will try to bind _count to both "count" and "Count" headers
18+
.WithExamples(new ExampleTable("count", "Count") // Deliberately ambiguous headers
19+
{
20+
{ 5, 10 }
21+
})
22+
.BDDfy();
23+
});
24+
25+
exception.Message.ShouldBe("More than one headers for examples, match the parameter 'count' provided for 'GivenInput'");
26+
}
27+
28+
private void GivenInput(int count)
29+
{
30+
// The method exists just to trigger the ambiguous header match
31+
}
32+
}
33+
}

src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,15 @@ private StepTitle CreateTitle(string stepTextTemplate, bool includeInputsInStepT
203203
{
204204
if (_testContext.Examples != null)
205205
{
206-
var matchingHeader = _testContext.Examples.Headers
207-
.SingleOrDefault(header => ExampleTable.HeaderMatches(header, i.ParameterName) ||
208-
ExampleTable.HeaderMatches(header, i.Value.Name));
206+
var matchingHeaders = _testContext.Examples.Headers
207+
.Where(header => ExampleTable.HeaderMatches(header, i.ParameterName) ||
208+
ExampleTable.HeaderMatches(header, i.Value.Name))
209+
.ToList();
210+
211+
if (matchingHeaders.Count > 1)
212+
throw new AmbiguousMatchException ($"More than one headers for examples, match the parameter '{i.ParameterName}' provided for '{methodInfo.Name}'");
213+
214+
var matchingHeader = matchingHeaders.SingleOrDefault();
209215
if (matchingHeader != null)
210216
return string.Format("<{0}>", matchingHeader);
211217
}

0 commit comments

Comments
 (0)