Skip to content

Commit 9107453

Browse files
committed
Merge pull request #119 from TestStack/nettostring
NetToString clean up and bug fixes
2 parents 9319ba9 + 9dea591 commit 9107453

11 files changed

Lines changed: 71 additions & 47 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ bin/
33
obj/
44
*.user
55
*.suo
6-
*.DotSettings
76
*~
87
*.swp
98
*.orig
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
namespace TestStack.BDDfy.Tests
66
{
77
[TestFixture]
8-
public class HumanizerTests
8+
public class NetToStringTests
99
{
1010
[Test]
1111
public void PascalCaseInputStringIsTurnedIntoSentence()
1212
{
1313
Assert.That(
14-
Configurator.Scanners.Humanize("PascalCaseInputStringIsTurnedIntoSentence"),
14+
Configurator.Scanners.Humanize("PascalCaseInputStringIsTurnedIntoSentence"),
1515
Is.EqualTo("Pascal case input string is turned into sentence"));
1616
}
1717

@@ -37,32 +37,37 @@ public void WhenInputStringEndWithANumber_ThenNumberIsDealtWithLikeAWord()
3737
public void UnderscoredInputStringIsTurnedIntoSentence()
3838
{
3939
Assert.That(
40-
Configurator.Scanners.Humanize("Underscored_input_string_is_turned_into_sentence"),
40+
Configurator.Scanners.Humanize("Underscored_input_string_is_turned_into_sentence"),
4141
Is.EqualTo("Underscored input string is turned into sentence"));
4242
}
4343

4444
[Test]
4545
public void UnderscoredInputStringPreservesCasing()
4646
{
4747
Assert.That(
48-
Configurator.Scanners.Humanize("Underscored_input_String_is_turned_INTO_sentence"),
48+
Configurator.Scanners.Humanize("Underscored_input_String_is_turned_INTO_sentence"),
4949
Is.EqualTo("Underscored input String is turned INTO sentence"));
5050
}
5151

5252
[Test]
5353
public void OneLetterWordInTheBeginningOfStringIsTurnedIntoAWord()
5454
{
55-
Assert.That(
56-
Configurator.Scanners.Humanize("XIsFirstPlayer"),
57-
Is.EqualTo("X is first player"));
55+
Assert.That(Configurator.Scanners.Humanize("XIsFirstPlayer"), Is.EqualTo("X is first player"));
5856
}
5957

60-
[Test]
61-
public void CanDealWithExampleStepNames()
58+
[TestCase("GivenThereAre__start__Cucumbers", "Given there are <start> cucumbers")]
59+
[TestCase("Given_there_are__start__cucumbers", "Given there are <start> cucumbers")]
60+
[TestCase("GivenMethodTaking__ExampleInt__", "Given method taking <example int>")]
61+
[TestCase("Given_method_taking__ExampleInt__", "Given method taking <ExampleInt>")]
62+
[TestCase("__starting__with_example", "<starting> with example")]
63+
[TestCase("__starting__WithExample", "<starting> with example")]
64+
[TestCase("WhenMethod__takes____two__parameters", "When method <takes> <two> parameters")]
65+
[TestCase("When_method__takes____two__parameters", "When method <takes> <two> parameters")]
66+
[TestCase("When_method_takes__one__and__two__parameters", "When method takes <one> and <two> parameters")]
67+
[TestCase("WhenMethodTakes__one__and__two__parameters", "When method takes <one> and <two> parameters")]
68+
public void CanDealWithExampleStepNames(string stepName, string expectedStepTitle)
6269
{
63-
NetToString.Convert("GivenThereAre__start__Cucumbers").ShouldBe("Given there are <start> cucumbers");
64-
NetToString.Convert("Given_there_are__start__cucumbers").ShouldBe("Given there are <start> cucumbers");
65-
NetToString.Convert("GivenMethodTaking__ExampleInt__").ShouldBe("Given method taking <example int>");
70+
NetToString.Convert(stepName).ShouldBe(expectedStepTitle, Case.Sensitive);
6671
}
6772
}
6873
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
<Compile Include="Scanner\FluentScanner\WhenStepsAreScannedUsingFluentScanner.cs">
105105
<SubType>Code</SubType>
106106
</Compile>
107-
<Compile Include="HumanizerTests.cs" />
107+
<Compile Include="NetToStringTests.cs" />
108108
<Compile Include="Reporters\Diagnostics\DiagnosticsReportBuilderTests.cs" />
109109
<Compile Include="Reporters\Diagnostics\WhenBuildingReportDiagnostics.cs" />
110110
<Compile Include="Reporters\Diagnostics\DiagnosticsReporterTests.cs" />

TestStack.BDDfy/NetToString.cs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,59 @@ static string FromPascalCase(string name)
1414
new List<char>(),
1515
(list, currentChar) =>
1616
{
17-
if (currentChar == ' ')
18-
{
19-
list.Add(currentChar);
20-
return list;
21-
}
22-
23-
if(list.Count == 0)
24-
{
17+
if (currentChar == ' ' || list.Count == 0)
2518
list.Add(currentChar);
26-
return list;
27-
}
28-
29-
var lastCharacterInTheList = list[list.Count - 1];
30-
if (lastCharacterInTheList != ' ')
19+
else
3120
{
32-
if(char.IsDigit(lastCharacterInTheList))
33-
{
34-
if (char.IsLetter(currentChar))
35-
list.Add(' ');
36-
}
37-
else if (!char.IsLower(currentChar))
21+
if(ShouldAddSpace(list[list.Count - 1], currentChar))
3822
list.Add(' ');
23+
list.Add(char.ToLower(currentChar));
3924
}
4025

41-
list.Add(char.ToLower(currentChar));
42-
4326
return list;
4427
});
4528

4629
var result = new string(chars.ToArray());
4730
return result.Replace(" i ", " I "); // I is an exception
4831
}
4932

50-
public static readonly Func<string, string> Convert = name =>
33+
private static bool ShouldAddSpace(char lastChar, char currentChar)
5134
{
52-
if (name.Contains("__"))
35+
if (lastChar == ' ')
36+
return false;
37+
38+
if (char.IsDigit(lastChar))
5339
{
54-
// hacking the crap out of it for now
55-
name = Regex.Replace(name, "__(\\w+)__", " <$1> ");
56-
return FromPascalCase(name).Replace("_", "").Replace(" >", ">").Replace("< ", "<").TrimEnd();
40+
if (char.IsLetter(currentChar))
41+
return true;
42+
43+
return false;
5744
}
5845

46+
if (!char.IsLower(currentChar) && currentChar != '>' && lastChar != '<')
47+
return true;
48+
49+
return false;
50+
}
51+
52+
public static readonly Func<string, string> Convert = name =>
53+
{
54+
if (name.Contains("__"))
55+
return ExampleTitle(name);
56+
5957
if (name.Contains("_"))
6058
return FromUnderscoreSeparatedWords(name);
6159

6260
return FromPascalCase(name);
6361
};
62+
63+
private static string ExampleTitle(string name)
64+
{
65+
name = Regex.Replace(name, "__([a-zA-Z]+)__", " <$1> ");
66+
67+
// 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();
70+
}
6471
}
6572
}

TestStack.BDDfy/Scanners/ScenarioScanners/FluentScenarioScanner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static string GetTitleFromMethodNameInStackTrace(object testObject)
4545
if (initiatingFrame == null)
4646
return null;
4747

48-
return NetToString.Convert(initiatingFrame.GetMethod().Name);
48+
return Configurator.Scanners.Humanize(initiatingFrame.GetMethod().Name);
4949
}
5050
}
5151
}

TestStack.BDDfy/Scanners/ScenarioScanners/ReflectiveScenarioScanner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public virtual IEnumerable<Scenario> Scan(ITestContext testContext)
5151

5252
static string GetScenarioText(Type scenarioType)
5353
{
54-
return NetToString.Convert(scenarioType.Name);
54+
return Configurator.Scanners.Humanize(scenarioType.Name);
5555
}
5656

5757
protected virtual IEnumerable<Step> ScanScenarioForSteps(ITestContext testContext)

TestStack.BDDfy/Scanners/StepScanners/ExecutableAttribute/ExecutableAttributeStepScanner.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Reflection;
44
using System.Text.RegularExpressions;
5+
using TestStack.BDDfy.Configuration;
56

67
namespace TestStack.BDDfy
78
{
@@ -33,7 +34,7 @@ public IEnumerable<Step> Scan(ITestContext testContext, MethodInfo candidateMeth
3334

3435
var stepTitle = new StepTitle(executableAttribute.StepTitle);
3536
if(string.IsNullOrEmpty(stepTitle))
36-
stepTitle = new StepTitle(NetToString.Convert(candidateMethod.Name));
37+
stepTitle = new StepTitle(Configurator.Scanners.Humanize(candidateMethod.Name));
3738

3839
var stepAsserts = IsAssertingByAttribute(candidateMethod);
3940

@@ -76,7 +77,7 @@ public IEnumerable<Step> Scan(ITestContext testContext, MethodInfo method, Examp
7677

7778
string stepTitle = executableAttribute.StepTitle;
7879
if (string.IsNullOrEmpty(stepTitle))
79-
stepTitle = NetToString.Convert(method.Name);
80+
stepTitle = Configurator.Scanners.Humanize(method.Name);
8081

8182
var stepAsserts = IsAssertingByAttribute(method);
8283
var methodParameters = method.GetParameters();

TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55
using System.Linq;
66
using System.Threading.Tasks;
7+
using TestStack.BDDfy.Configuration;
78

89
namespace TestStack.BDDfy
910
{
@@ -136,7 +137,7 @@ private StepTitle CreateTitle(string stepTextTemplate, bool includeInputsInStepT
136137
{
137138

138139
var flatInputArray = inputArguments.FlattenArrays();
139-
var stepTitle = NetToString.Convert(methodInfo.Name);
140+
var stepTitle = Configurator.Scanners.Humanize(methodInfo.Name);
140141

141142
if (!string.IsNullOrEmpty(stepTextTemplate)) stepTitle = string.Format(stepTextTemplate, flatInputArray);
142143
else if (includeInputsInStepTitle)

TestStack.BDDfy/Scanners/StepScanners/MethodName/MethodNameStepScanner.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Reflection;
44
using System.Linq;
5+
using TestStack.BDDfy.Configuration;
56

67
namespace TestStack.BDDfy
78
{
@@ -122,7 +123,7 @@ private string GetStepTitle(MethodInfo method, object testObject, RunStepWithArg
122123

123124
private string GetStepTitleFromMethodName(MethodInfo method, RunStepWithArgsAttribute argAttribute)
124125
{
125-
var methodName = _stepTextTransformer(NetToString.Convert(method.Name));
126+
var methodName = _stepTextTransformer(Configurator.Scanners.Humanize(method.Name));
126127
object[] inputs = null;
127128

128129
if (argAttribute != null && argAttribute.InputArguments != null)

TestStack.BDDfy/Scanners/StoryMetadata.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using TestStack.BDDfy.Configuration;
23

34
namespace TestStack.BDDfy
45
{
@@ -11,7 +12,7 @@ public StoryMetadata(Type storyType, StoryNarrativeAttribute narrative)
1112

1213
public StoryMetadata(Type storyType, string narrative1, string narrative2, string narrative3, string title = null, string titlePrefix = null)
1314
{
14-
Title = title ?? NetToString.Convert(storyType.Name);
15+
Title = title ?? Configurator.Scanners.Humanize(storyType.Name);
1516
TitlePrefix = titlePrefix ?? "Story: ";
1617
Type = storyType;
1718

0 commit comments

Comments
 (0)