Skip to content

Commit 7ab77d0

Browse files
author
Johan Bergens
committed
Fixing issue #226. Now handles numbers and throws for some other characters that are not supported in examples.
1 parent b799fa7 commit 7ab77d0

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-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
}

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)