From cb83b762cafa573ef93b1547318ca2cad3589aa4 Mon Sep 17 00:00:00 2001 From: MehdiK Date: Mon, 28 Apr 2014 13:25:12 +0430 Subject: [PATCH 1/4] covered more cases in the NetToString for examples --- ...{HumanizerTests.cs => NetToStringTests.cs} | 29 +++++++++++-------- .../TestStack.BDDfy.Tests.csproj | 2 +- TestStack.BDDfy/NetToString.cs | 17 +++++++---- 3 files changed, 29 insertions(+), 19 deletions(-) rename TestStack.BDDfy.Tests/{HumanizerTests.cs => NetToStringTests.cs} (57%) 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..6f80a3d1 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..306e61ef 100644 --- a/TestStack.BDDfy/NetToString.cs +++ b/TestStack.BDDfy/NetToString.cs @@ -20,7 +20,7 @@ static string FromPascalCase(string name) return list; } - if(list.Count == 0) + if (list.Count == 0) { list.Add(currentChar); return list; @@ -50,16 +50,21 @@ static string FromPascalCase(string name) public static readonly Func Convert = name => { if (name.Contains("__")) - { - // hacking the crap out of it for now - name = Regex.Replace(name, "__(\\w+)__", " <$1> "); - return FromPascalCase(name).Replace("_", "").Replace(" >", ">").Replace("< ", "<").TrimEnd(); - } + 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 FromPascalCase(name).Replace("_", "").Replace(" >", ">").Replace("< ", "<").Trim(); + } } } \ No newline at end of file From 24aff010ad6c4eaf94c972f3899d6aa8a9daa6ca Mon Sep 17 00:00:00 2001 From: MehdiK Date: Mon, 28 Apr 2014 14:21:02 +0430 Subject: [PATCH 2/4] refactored NetToString & fixed a few issues --- TestStack.BDDfy.Tests/NetToStringTests.cs | 10 ++--- TestStack.BDDfy/NetToString.cs | 46 ++++++++++++----------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/TestStack.BDDfy.Tests/NetToStringTests.cs b/TestStack.BDDfy.Tests/NetToStringTests.cs index 6f80a3d1..516af371 100644 --- a/TestStack.BDDfy.Tests/NetToStringTests.cs +++ b/TestStack.BDDfy.Tests/NetToStringTests.cs @@ -58,13 +58,13 @@ public void OneLetterWordInTheBeginningOfStringIsTurnedIntoAWord() [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("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")] + [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(stepName).ShouldBe(expectedStepTitle, Case.Sensitive); diff --git a/TestStack.BDDfy/NetToString.cs b/TestStack.BDDfy/NetToString.cs index 306e61ef..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,6 +30,25 @@ static string FromPascalCase(string name) return result.Replace(" i ", " I "); // I is an exception } + private static bool ShouldAddSpace(char lastChar, char currentChar) + { + if (lastChar == ' ') + return false; + + if (char.IsDigit(lastChar)) + { + 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("__")) @@ -64,7 +66,7 @@ private static string ExampleTitle(string name) // for when there are two consequetive example placeholders in the word; e.g. Given__one____two__parameters name = name.Replace(" ", " "); - return FromPascalCase(name).Replace("_", "").Replace(" >", ">").Replace("< ", "<").Trim(); + return Convert(name).Trim(); } } } \ No newline at end of file From 3873bfd4dc376f946799abb7119e0b6ac75a567a Mon Sep 17 00:00:00 2001 From: MehdiK Date: Mon, 28 Apr 2014 14:26:33 +0430 Subject: [PATCH 3/4] added in dotsettings & excluded a few folders from R# namespace provider --- .gitignore | 1 - TestStack.BDDfy/TestStack.BDDfy.csproj.DotSettings | 9 +++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 TestStack.BDDfy/TestStack.BDDfy.csproj.DotSettings 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/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 From 9dea591021d96d40866c838d5738acb8c0148596 Mon Sep 17 00:00:00 2001 From: MehdiK Date: Mon, 28 Apr 2014 14:34:41 +0430 Subject: [PATCH 4/4] using Configurator.Humanize instead of NetToString everywhere --- .../Scanners/ScenarioScanners/FluentScenarioScanner.cs | 2 +- .../Scanners/ScenarioScanners/ReflectiveScenarioScanner.cs | 2 +- .../ExecutableAttribute/ExecutableAttributeStepScanner.cs | 5 +++-- .../Scanners/StepScanners/Fluent/FluentScanner.cs | 3 ++- .../StepScanners/MethodName/MethodNameStepScanner.cs | 3 ++- TestStack.BDDfy/Scanners/StoryMetadata.cs | 3 ++- 6 files changed, 11 insertions(+), 7 deletions(-) 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;