diff --git a/.gitignore b/.gitignore index b037a556..f5f037e5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ bin/ obj/ *.user *.suo -*.DotSettings *~ *.swp *.orig diff --git a/TestStack.BDDfy.Tests/HumanizerTests.cs b/TestStack.BDDfy.Tests/NetToStringTests.cs similarity index 57% rename from TestStack.BDDfy.Tests/HumanizerTests.cs rename to TestStack.BDDfy.Tests/NetToStringTests.cs index 90c6ee5f..516af371 100644 --- a/TestStack.BDDfy.Tests/HumanizerTests.cs +++ b/TestStack.BDDfy.Tests/NetToStringTests.cs @@ -5,13 +5,13 @@ namespace TestStack.BDDfy.Tests { [TestFixture] - public class HumanizerTests + public class NetToStringTests { [Test] public void PascalCaseInputStringIsTurnedIntoSentence() { Assert.That( - Configurator.Scanners.Humanize("PascalCaseInputStringIsTurnedIntoSentence"), + Configurator.Scanners.Humanize("PascalCaseInputStringIsTurnedIntoSentence"), Is.EqualTo("Pascal case input string is turned into sentence")); } @@ -37,7 +37,7 @@ public void WhenInputStringEndWithANumber_ThenNumberIsDealtWithLikeAWord() public void UnderscoredInputStringIsTurnedIntoSentence() { Assert.That( - Configurator.Scanners.Humanize("Underscored_input_string_is_turned_into_sentence"), + Configurator.Scanners.Humanize("Underscored_input_string_is_turned_into_sentence"), Is.EqualTo("Underscored input string is turned into sentence")); } @@ -45,24 +45,29 @@ public void UnderscoredInputStringIsTurnedIntoSentence() public void UnderscoredInputStringPreservesCasing() { Assert.That( - Configurator.Scanners.Humanize("Underscored_input_String_is_turned_INTO_sentence"), + Configurator.Scanners.Humanize("Underscored_input_String_is_turned_INTO_sentence"), Is.EqualTo("Underscored input String is turned INTO sentence")); } [Test] public void OneLetterWordInTheBeginningOfStringIsTurnedIntoAWord() { - Assert.That( - Configurator.Scanners.Humanize("XIsFirstPlayer"), - Is.EqualTo("X is first player")); + Assert.That(Configurator.Scanners.Humanize("XIsFirstPlayer"), Is.EqualTo("X is first player")); } - [Test] - public void CanDealWithExampleStepNames() + [TestCase("GivenThereAre__start__Cucumbers", "Given there are cucumbers")] + [TestCase("Given_there_are__start__cucumbers", "Given there are cucumbers")] + [TestCase("GivenMethodTaking__ExampleInt__", "Given method taking ")] + [TestCase("Given_method_taking__ExampleInt__", "Given method taking ")] + [TestCase("__starting__with_example", " with example")] + [TestCase("__starting__WithExample", " with example")] + [TestCase("WhenMethod__takes____two__parameters", "When method parameters")] + [TestCase("When_method__takes____two__parameters", "When method parameters")] + [TestCase("When_method_takes__one__and__two__parameters", "When method takes and parameters")] + [TestCase("WhenMethodTakes__one__and__two__parameters", "When method takes and parameters")] + public void CanDealWithExampleStepNames(string stepName, string expectedStepTitle) { - NetToString.Convert("GivenThereAre__start__Cucumbers").ShouldBe("Given there are cucumbers"); - NetToString.Convert("Given_there_are__start__cucumbers").ShouldBe("Given there are cucumbers"); - NetToString.Convert("GivenMethodTaking__ExampleInt__").ShouldBe("Given method taking "); + NetToString.Convert(stepName).ShouldBe(expectedStepTitle, Case.Sensitive); } } } \ No newline at end of file diff --git a/TestStack.BDDfy.Tests/TestStack.BDDfy.Tests.csproj b/TestStack.BDDfy.Tests/TestStack.BDDfy.Tests.csproj index eb6ba4bf..ca6a4d92 100644 --- a/TestStack.BDDfy.Tests/TestStack.BDDfy.Tests.csproj +++ b/TestStack.BDDfy.Tests/TestStack.BDDfy.Tests.csproj @@ -104,7 +104,7 @@ Code - + diff --git a/TestStack.BDDfy/NetToString.cs b/TestStack.BDDfy/NetToString.cs index 0e36f416..d857feba 100644 --- a/TestStack.BDDfy/NetToString.cs +++ b/TestStack.BDDfy/NetToString.cs @@ -14,32 +14,15 @@ static string FromPascalCase(string name) new List(), (list, currentChar) => { - if (currentChar == ' ') - { - list.Add(currentChar); - return list; - } - - if(list.Count == 0) - { + if (currentChar == ' ' || list.Count == 0) list.Add(currentChar); - return list; - } - - var lastCharacterInTheList = list[list.Count - 1]; - if (lastCharacterInTheList != ' ') + else { - if(char.IsDigit(lastCharacterInTheList)) - { - if (char.IsLetter(currentChar)) - list.Add(' '); - } - else if (!char.IsLower(currentChar)) + if(ShouldAddSpace(list[list.Count - 1], currentChar)) list.Add(' '); + list.Add(char.ToLower(currentChar)); } - list.Add(char.ToLower(currentChar)); - return list; }); @@ -47,19 +30,43 @@ static string FromPascalCase(string name) return result.Replace(" i ", " I "); // I is an exception } - public static readonly Func Convert = name => + private static bool ShouldAddSpace(char lastChar, char currentChar) { - if (name.Contains("__")) + if (lastChar == ' ') + return false; + + if (char.IsDigit(lastChar)) { - // hacking the crap out of it for now - name = Regex.Replace(name, "__(\\w+)__", " <$1> "); - return FromPascalCase(name).Replace("_", "").Replace(" >", ">").Replace("< ", "<").TrimEnd(); + if (char.IsLetter(currentChar)) + return true; + + return false; } + if (!char.IsLower(currentChar) && currentChar != '>' && lastChar != '<') + return true; + + return false; + } + + public static readonly Func Convert = name => + { + if (name.Contains("__")) + return ExampleTitle(name); + if (name.Contains("_")) return FromUnderscoreSeparatedWords(name); return FromPascalCase(name); }; + + private static string ExampleTitle(string name) + { + name = Regex.Replace(name, "__([a-zA-Z]+)__", " <$1> "); + + // for when there are two consequetive example placeholders in the word; e.g. Given__one____two__parameters + name = name.Replace(" ", " "); + return Convert(name).Trim(); + } } } \ No newline at end of file diff --git a/TestStack.BDDfy/Scanners/ScenarioScanners/FluentScenarioScanner.cs b/TestStack.BDDfy/Scanners/ScenarioScanners/FluentScenarioScanner.cs index 3aaef337..3652e2c1 100644 --- a/TestStack.BDDfy/Scanners/ScenarioScanners/FluentScenarioScanner.cs +++ b/TestStack.BDDfy/Scanners/ScenarioScanners/FluentScenarioScanner.cs @@ -45,7 +45,7 @@ private static string GetTitleFromMethodNameInStackTrace(object testObject) if (initiatingFrame == null) return null; - return NetToString.Convert(initiatingFrame.GetMethod().Name); + return Configurator.Scanners.Humanize(initiatingFrame.GetMethod().Name); } } } diff --git a/TestStack.BDDfy/Scanners/ScenarioScanners/ReflectiveScenarioScanner.cs b/TestStack.BDDfy/Scanners/ScenarioScanners/ReflectiveScenarioScanner.cs index f175c9b1..7f85490a 100644 --- a/TestStack.BDDfy/Scanners/ScenarioScanners/ReflectiveScenarioScanner.cs +++ b/TestStack.BDDfy/Scanners/ScenarioScanners/ReflectiveScenarioScanner.cs @@ -51,7 +51,7 @@ public virtual IEnumerable Scan(ITestContext testContext) static string GetScenarioText(Type scenarioType) { - return NetToString.Convert(scenarioType.Name); + return Configurator.Scanners.Humanize(scenarioType.Name); } protected virtual IEnumerable ScanScenarioForSteps(ITestContext testContext) diff --git a/TestStack.BDDfy/Scanners/StepScanners/ExecutableAttribute/ExecutableAttributeStepScanner.cs b/TestStack.BDDfy/Scanners/StepScanners/ExecutableAttribute/ExecutableAttributeStepScanner.cs index 3d8e9b7b..66d1fe5f 100644 --- a/TestStack.BDDfy/Scanners/StepScanners/ExecutableAttribute/ExecutableAttributeStepScanner.cs +++ b/TestStack.BDDfy/Scanners/StepScanners/ExecutableAttribute/ExecutableAttributeStepScanner.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Reflection; using System.Text.RegularExpressions; +using TestStack.BDDfy.Configuration; namespace TestStack.BDDfy { @@ -33,7 +34,7 @@ public IEnumerable Scan(ITestContext testContext, MethodInfo candidateMeth var stepTitle = new StepTitle(executableAttribute.StepTitle); if(string.IsNullOrEmpty(stepTitle)) - stepTitle = new StepTitle(NetToString.Convert(candidateMethod.Name)); + stepTitle = new StepTitle(Configurator.Scanners.Humanize(candidateMethod.Name)); var stepAsserts = IsAssertingByAttribute(candidateMethod); @@ -76,7 +77,7 @@ public IEnumerable Scan(ITestContext testContext, MethodInfo method, Examp string stepTitle = executableAttribute.StepTitle; if (string.IsNullOrEmpty(stepTitle)) - stepTitle = NetToString.Convert(method.Name); + stepTitle = Configurator.Scanners.Humanize(method.Name); var stepAsserts = IsAssertingByAttribute(method); var methodParameters = method.GetParameters(); diff --git a/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs b/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs index e13c8ad7..33e7510d 100644 --- a/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs +++ b/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs @@ -4,6 +4,7 @@ using System.Reflection; using System.Linq; using System.Threading.Tasks; +using TestStack.BDDfy.Configuration; namespace TestStack.BDDfy { @@ -136,7 +137,7 @@ private StepTitle CreateTitle(string stepTextTemplate, bool includeInputsInStepT { var flatInputArray = inputArguments.FlattenArrays(); - var stepTitle = NetToString.Convert(methodInfo.Name); + var stepTitle = Configurator.Scanners.Humanize(methodInfo.Name); if (!string.IsNullOrEmpty(stepTextTemplate)) stepTitle = string.Format(stepTextTemplate, flatInputArray); else if (includeInputsInStepTitle) diff --git a/TestStack.BDDfy/Scanners/StepScanners/MethodName/MethodNameStepScanner.cs b/TestStack.BDDfy/Scanners/StepScanners/MethodName/MethodNameStepScanner.cs index 4bd66315..874f8ff4 100644 --- a/TestStack.BDDfy/Scanners/StepScanners/MethodName/MethodNameStepScanner.cs +++ b/TestStack.BDDfy/Scanners/StepScanners/MethodName/MethodNameStepScanner.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Reflection; using System.Linq; +using TestStack.BDDfy.Configuration; namespace TestStack.BDDfy { @@ -122,7 +123,7 @@ private string GetStepTitle(MethodInfo method, object testObject, RunStepWithArg private string GetStepTitleFromMethodName(MethodInfo method, RunStepWithArgsAttribute argAttribute) { - var methodName = _stepTextTransformer(NetToString.Convert(method.Name)); + var methodName = _stepTextTransformer(Configurator.Scanners.Humanize(method.Name)); object[] inputs = null; if (argAttribute != null && argAttribute.InputArguments != null) diff --git a/TestStack.BDDfy/Scanners/StoryMetadata.cs b/TestStack.BDDfy/Scanners/StoryMetadata.cs index d68fe9e6..3f28909f 100644 --- a/TestStack.BDDfy/Scanners/StoryMetadata.cs +++ b/TestStack.BDDfy/Scanners/StoryMetadata.cs @@ -1,4 +1,5 @@ using System; +using TestStack.BDDfy.Configuration; namespace TestStack.BDDfy { @@ -11,7 +12,7 @@ public StoryMetadata(Type storyType, StoryNarrativeAttribute narrative) public StoryMetadata(Type storyType, string narrative1, string narrative2, string narrative3, string title = null, string titlePrefix = null) { - Title = title ?? NetToString.Convert(storyType.Name); + Title = title ?? Configurator.Scanners.Humanize(storyType.Name); TitlePrefix = titlePrefix ?? "Story: "; Type = storyType; diff --git a/TestStack.BDDfy/TestStack.BDDfy.csproj.DotSettings b/TestStack.BDDfy/TestStack.BDDfy.csproj.DotSettings new file mode 100644 index 00000000..80b4ef71 --- /dev/null +++ b/TestStack.BDDfy/TestStack.BDDfy.csproj.DotSettings @@ -0,0 +1,9 @@ + + True + True + True + True + True + True + True + True \ No newline at end of file