Skip to content

Commit

Permalink
Fixing issue TestStack#226. Now handles numbers and throws for some o…
Browse files Browse the repository at this point in the history
…ther characters that are not supported in examples.
  • Loading branch information
Johan Bergens committed May 2, 2016
1 parent b799fa7 commit 7ab77d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/TestStack.BDDfy.Tests/NetToStringTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Shouldly;
using TestStack.BDDfy.Configuration;
using Xunit;
Expand Down Expand Up @@ -55,6 +56,8 @@ public void OneLetterWordInTheBeginningOfStringIsTurnedIntoAWord()
[Theory]
[InlineData("GivenThereAre__start__Cucumbers", "Given there are <start> cucumbers")]
[InlineData("Given_there_are__start__cucumbers", "Given there are <start> cucumbers")]
[InlineData("GivenThereAre__count1__Cucumbers", "Given there are <count 1> cucumbers")]
[InlineData("Given_there_are__count2__cucumbers", "Given there are <count2> cucumbers")] // The spacing rules for numbers are not consequential
[InlineData("GivenMethodTaking__ExampleInt__", "Given method taking <example int>")]
[InlineData("Given_method_taking__ExampleInt__", "Given method taking <ExampleInt>")]
[InlineData("__starting__with_example", "<starting> with example")]
Expand All @@ -67,5 +70,16 @@ public void CanDealWithExampleStepNames(string stepName, string expectedStepTitl
{
NetToString.Convert(stepName).ShouldBe(expectedStepTitle, Case.Sensitive);
}

[Theory]
[InlineData("GivenThereAre__två__Cucumbers", "Given there are <två> cucumbers")]
public void ReportsIllegalExampleStepNames(string stepName, string expectedStepTitle) {
var exception = Record.Exception(() => {
NetToString.Convert(stepName).ShouldBe(expectedStepTitle, Case.Sensitive);
});

Assert.NotNull(exception);
Assert.IsType<ArgumentException>(exception);
}
}
}
19 changes: 12 additions & 7 deletions src/TestStack.BDDfy/NetToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ private static bool ShouldAddSpace(char lastChar, char currentChar)
return false;
}

public static readonly Func<string, string> Convert = name =>
{
if (name.Contains("__"))
return ExampleTitle(name);
public static readonly Func<string, string> Convert = name => {
return name.Contains("__") ? ExampleTitle(name) : ConvertNonExample(name);
};

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

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

private static string ExampleTitle(string name)
{
name = Regex.Replace(name, "__([a-zA-Z]+)__", " <$1> ");
// Compare contains("__") with a regex match
string newName = Regex.Replace(name, "__([a-zA-Z][a-zA-Z0-9]*)__", " <$1> ");

if (newName == name) {
throw new ArgumentException("Illegal example title in name '" + name + "'!");
}

// for when there are two consequetive example placeholders in the word; e.g. Given__one____two__parameters
name = name.Replace(" ", " ");
return Convert(name).Trim();
newName = newName.Replace(" ", " ");
return Convert(newName).Trim();
}
}
}

0 comments on commit 7ab77d0

Please sign in to comment.