Skip to content

Commit 42f9fe0

Browse files
committed
Merge pull request #225 from jbergens/master
Fixes #226
2 parents 603ed9a + 7ab77d0 commit 42f9fe0

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

src/TestStack.BDDfy.Tests/NetToStringTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Shouldly;
23
using TestStack.BDDfy.Configuration;
34
using Xunit;
@@ -55,6 +56,8 @@ public void OneLetterWordInTheBeginningOfStringIsTurnedIntoAWord()
5556
[Theory]
5657
[InlineData("GivenThereAre__start__Cucumbers", "Given there are <start> cucumbers")]
5758
[InlineData("Given_there_are__start__cucumbers", "Given there are <start> cucumbers")]
59+
[InlineData("GivenThereAre__count1__Cucumbers", "Given there are <count 1> cucumbers")]
60+
[InlineData("Given_there_are__count2__cucumbers", "Given there are <count2> cucumbers")] // The spacing rules for numbers are not consequential
5861
[InlineData("GivenMethodTaking__ExampleInt__", "Given method taking <example int>")]
5962
[InlineData("Given_method_taking__ExampleInt__", "Given method taking <ExampleInt>")]
6063
[InlineData("__starting__with_example", "<starting> with example")]
@@ -67,5 +70,16 @@ public void CanDealWithExampleStepNames(string stepName, string expectedStepTitl
6770
{
6871
NetToString.Convert(stepName).ShouldBe(expectedStepTitle, Case.Sensitive);
6972
}
73+
74+
[Theory]
75+
[InlineData("GivenThereAre__två__Cucumbers", "Given there are <två> cucumbers")]
76+
public void ReportsIllegalExampleStepNames(string stepName, string expectedStepTitle) {
77+
var exception = Record.Exception(() => {
78+
NetToString.Convert(stepName).ShouldBe(expectedStepTitle, Case.Sensitive);
79+
});
80+
81+
Assert.NotNull(exception);
82+
Assert.IsType<ArgumentException>(exception);
83+
}
7084
}
7185
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+

2+
Scenario: Fluent can be used with examples
3+
Given method taking <example int a>
4+
When empty method
5+
Then all is good
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+

2+
Scenario: Fluent can be used with examples ending in a number
3+
Given method taking <example int 1>
4+
When empty method
5+
Then all is good
6+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using ApprovalTests;
4+
using Shouldly;
5+
using TestStack.BDDfy.Reporters;
6+
using Xunit;
7+
8+
namespace TestStack.BDDfy.Tests.Scanner.Examples
9+
{
10+
public class FluentWithExamplesAtEnd
11+
{
12+
[Fact]
13+
public void FluentCanBeUsedWithExamples()
14+
{
15+
var story = this
16+
.Given(_ => MethodTaking__ExampleIntA__(1), false)
17+
.When(_ => WhenEmptyMethod())
18+
.Then(_ => ThenAllIsGood())
19+
.BDDfy();
20+
21+
var textReporter = new TextReporter();
22+
textReporter.Process(story);
23+
Approvals.Verify(textReporter.ToString());
24+
}
25+
26+
// This test crashes BDDfy or causes an infinite loop
27+
[Fact]
28+
public void FluentCanBeUsedWithExamplesEndingInANumber() {
29+
var story = this
30+
.Given(_ => MethodTaking__ExampleInt1__(1), false)
31+
.When(_ => WhenEmptyMethod())
32+
.Then(_ => ThenAllIsGood())
33+
.BDDfy();
34+
35+
var textReporter = new TextReporter();
36+
textReporter.Process(story);
37+
Approvals.Verify(textReporter.ToString());
38+
}
39+
40+
private void ThenAllIsGood()
41+
{
42+
}
43+
44+
private void WhenEmptyMethod()
45+
{
46+
}
47+
48+
// Ending an example name with a number seems to cause problems in BDDfy
49+
private void MethodTaking__ExampleInt1__(int exampleInt)
50+
{
51+
exampleInt.ShouldBeInRange(1, 2);
52+
}
53+
54+
private void MethodTaking__ExampleIntA__(int exampleInt) {
55+
exampleInt.ShouldBeInRange(1, 2);
56+
}
57+
}
58+
}

src/TestStack.BDDfy.Tests/TestStack.BDDfy.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<Compile Include="Reporters\Html\TestableHtmlReporter.cs" />
8888
<Compile Include="Reporters\ReportApprover.cs" />
8989
<Compile Include="Scanner\Examples\ExampleActionTests.cs" />
90+
<Compile Include="Scanner\Examples\FluentWithExamplesAtEnd.cs" />
9091
<Compile Include="Scanner\FluentScanner\ComplexStepsTests.cs" />
9192
<Compile Include="Scanner\FluentScanner\DoesNotConflictWithnSubstitute.cs" />
9293
<Compile Include="Scanner\Examples\FluentWithExamples.cs" />

src/TestStack.BDDfy/NetToString.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ private static bool ShouldAddSpace(char lastChar, char currentChar)
4949
return false;
5050
}
5151

52-
public static readonly Func<string, string> Convert = name =>
53-
{
54-
if (name.Contains("__"))
55-
return ExampleTitle(name);
52+
public static readonly Func<string, string> Convert = name => {
53+
return name.Contains("__") ? ExampleTitle(name) : ConvertNonExample(name);
54+
};
5655

56+
private static readonly Func<string, string> ConvertNonExample = name => {
5757
if (name.Contains("_"))
5858
return FromUnderscoreSeparatedWords(name);
5959

@@ -62,11 +62,16 @@ private static bool ShouldAddSpace(char lastChar, char currentChar)
6262

6363
private static string ExampleTitle(string name)
6464
{
65-
name = Regex.Replace(name, "__([a-zA-Z]+)__", " <$1> ");
65+
// Compare contains("__") with a regex match
66+
string newName = Regex.Replace(name, "__([a-zA-Z][a-zA-Z0-9]*)__", " <$1> ");
67+
68+
if (newName == name) {
69+
throw new ArgumentException("Illegal example title in name '" + name + "'!");
70+
}
6671

6772
// for when there are two consequetive example placeholders in the word; e.g. Given__one____two__parameters
68-
name = name.Replace(" ", " ");
69-
return Convert(name).Trim();
73+
newName = newName.Replace(" ", " ");
74+
return Convert(newName).Trim();
7075
}
7176
}
7277
}

0 commit comments

Comments
 (0)