Skip to content

Commit 49585ea

Browse files
SonOfSardaarGurpreet Singh
andauthored
Feature/configurable humanizer (#290)
* Allow setting up custom Humanizer * Change DefaultHumanizer class to public --------- Co-authored-by: Gurpreet Singh <[email protected]>
1 parent 13d9ace commit 49585ea

18 files changed

+59
-75
lines changed

src/TestStack.BDDfy.Tests/NetToStringTests.cs renamed to src/TestStack.BDDfy.Tests/DefaultHumanizerTests.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,53 @@
55

66
namespace TestStack.BDDfy.Tests
77
{
8-
public class NetToStringTests
8+
public class DefaultHumanizerTests
99
{
10+
private static DefaultHumanizer Humanizer => new();
11+
1012
[Fact]
1113
public void PascalCaseInputStringIsTurnedIntoSentence()
1214
{
13-
Configurator.Scanners.Humanize("PascalCaseInputStringIsTurnedIntoSentence")
15+
Humanizer.Humanize("PascalCaseInputStringIsTurnedIntoSentence")
1416
.ShouldBe("Pascal case input string is turned into sentence");
1517
}
1618

1719
[Fact]
1820
public void WhenInputStringContainsConsequtiveCaptialLetters_ThenTheyAreTurnedIntoOneLetterWords()
1921
{
20-
Configurator.Scanners.Humanize("WhenIUseAnInputAHere").ShouldBe("When I use an input a here");
22+
Humanizer.Humanize("WhenIUseAnInputAHere").ShouldBe("When I use an input a here");
2123
}
2224

2325
[Fact]
2426
public void WhenInputStringStartsWithANumber_ThenNumberIsDealtWithLikeAWord()
2527
{
26-
Configurator.Scanners.Humanize("10NumberIsInTheBegining").ShouldBe("10 number is in the begining");
28+
Humanizer.Humanize("10NumberIsInTheBegining").ShouldBe("10 number is in the begining");
2729
}
2830

2931
[Fact]
3032
public void WhenInputStringEndWithANumber_ThenNumberIsDealtWithLikeAWord()
3133
{
32-
Configurator.Scanners.Humanize("NumberIsAtTheEnd100").ShouldBe("Number is at the end 100");
34+
Humanizer.Humanize("NumberIsAtTheEnd100").ShouldBe("Number is at the end 100");
3335
}
3436

3537
[Fact]
3638
public void UnderscoredInputStringIsTurnedIntoSentence()
3739
{
38-
Configurator.Scanners.Humanize("Underscored_input_string_is_turned_into_sentence")
40+
Humanizer.Humanize("Underscored_input_string_is_turned_into_sentence")
3941
.ShouldBe("Underscored input string is turned into sentence");
4042
}
4143

4244
[Fact]
4345
public void UnderscoredInputStringPreservesCasing()
4446
{
45-
Configurator.Scanners.Humanize("Underscored_input_String_is_turned_INTO_sentence")
47+
Humanizer.Humanize("Underscored_input_String_is_turned_INTO_sentence")
4648
.ShouldBe("Underscored input String is turned INTO sentence");
4749
}
4850

4951
[Fact]
5052
public void OneLetterWordInTheBeginningOfStringIsTurnedIntoAWord()
5153
{
52-
Configurator.Scanners.Humanize("XIsFirstPlayer").ShouldBe("X is first player");
54+
Humanizer.Humanize("XIsFirstPlayer").ShouldBe("X is first player");
5355
}
5456

5557
[Theory]
@@ -67,14 +69,14 @@ public void OneLetterWordInTheBeginningOfStringIsTurnedIntoAWord()
6769
[InlineData("WhenMethodTakes__one__and__two__parameters", "When method takes <one> and <two> parameters")]
6870
public void CanDealWithExampleStepNames(string stepName, string expectedStepTitle)
6971
{
70-
NetToString.Convert(stepName).ShouldBe(expectedStepTitle);
72+
Humanizer.Humanize(stepName).ShouldBe(expectedStepTitle);
7173
}
7274

7375
[Theory]
7476
[InlineData("GivenThereAre__två__Cucumbers", "Given there are <två> cucumbers")]
7577
public void ReportsIllegalExampleStepNames(string stepName, string expectedStepTitle) {
7678
var exception = Record.Exception(() => {
77-
NetToString.Convert(stepName).ShouldBe(expectedStepTitle);
79+
Humanizer.Humanize(stepName).ShouldBe(expectedStepTitle);
7880
});
7981

8082
exception.ShouldNotBeNull();

src/TestStack.BDDfy.Tests/Scanner/FluentScanner/BDDfyUsingFluentApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public void WhenTitleIsNotProvidedItIsFetchedFromMethodName()
279279
.BDDfy();
280280

281281
var scenario = story.Scenarios.First();
282-
scenario.Title.ShouldBe(Configurator.Scanners.Humanize(nameof(WhenTitleIsNotProvidedItIsFetchedFromMethodName)));
282+
scenario.Title.ShouldBe(Configurator.Humanizer.Humanize(nameof(WhenTitleIsNotProvidedItIsFetchedFromMethodName)));
283283
}
284284

285285
[Fact]

src/TestStack.BDDfy.Tests/Scanner/ReflectiveScanner/WhenCombinationOfExecutableAttributeAndMethodNamingConventionIsUsed.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ public void LegacyConsecutiveAssertionStepIsScanned()
125125

126126
void VerifyStepAndItsProperties(Expression<Action> stepMethodAction, ExecutionOrder expectedOrder, int expectedCount = 1)
127127
{
128-
var matchingSteps = _scenario.Steps.Where(s => s.Title.Trim() == Configurator.Scanners.Humanize(Helpers.GetMethodInfo(stepMethodAction).Name));
128+
var matchingSteps = _scenario.Steps.Where(s => s.Title.Trim() == Configurator.Humanizer.Humanize(Helpers.GetMethodInfo(stepMethodAction).Name));
129129
matchingSteps.Count().ShouldBe(expectedCount);
130130
matchingSteps.All(s => s.ExecutionOrder == expectedOrder).ShouldBe(true);
131131
}
132132

133133
[Fact]
134134
public void IgnoredMethodShouldNotBeAddedToSteps()
135135
{
136-
var matchingSteps = _scenario.Steps.Where(s => s.Title == Configurator.Scanners.Humanize(Helpers.GetMethodInfo(() => _sut.ThenIAmNotAStep()).Name));
136+
var matchingSteps = _scenario.Steps.Where(s => s.Title == Configurator.Humanizer.Humanize(Helpers.GetMethodInfo(() => _sut.ThenIAmNotAStep()).Name));
137137
matchingSteps.ShouldBeEmpty();
138138
}
139139
}

src/TestStack.BDDfy.Tests/Scanner/ReflectiveScanner/WhenMethodNamesFollowNamingConventionsOtherThanGivenWhenThen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void IncorrectSpecificationStepIsNotAdded()
8989

9090
void AssertSpecificationStepIsScannedProperly(Expression<Action> getSpecMethod)
9191
{
92-
var specMethods = _steps.Where(s => s.Title.Trim() == Configurator.Scanners.Humanize(Helpers.GetMethodInfo(getSpecMethod).Name));
92+
var specMethods = _steps.Where(s => s.Title.Trim() == Configurator.Humanizer.Humanize(Helpers.GetMethodInfo(getSpecMethod).Name));
9393
specMethods.Count().ShouldBe(1);
9494
var specStep = specMethods.First();
9595
specStep.Asserts.ShouldBe(false);

src/TestStack.BDDfy.Tests/Scanner/ReflectiveScanner/WhenStepsAreDefinedInABaseClass.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ Scenario Scenario
4141
[RunStepWithArgs("ThenInTheBaseClass")]
4242
void ThenTheFollowingStepFromBaseClassIsScanned(string stepName)
4343
{
44-
Scenario.Steps.Count(s => s.Title == Configurator.Scanners.Humanize(stepName)).ShouldBe(1);
44+
Scenario.Steps.Count(s => s.Title == Configurator.Humanizer.Humanize(stepName)).ShouldBe(1);
4545
}
4646

4747
[RunStepWithArgs("GivenInTheSubClass")]
4848
[RunStepWithArgs("WhenInTheSubClass")]
4949
[RunStepWithArgs("ThenInTheSubClass")]
5050
void ThenTheFollowingStepFromSubClassScanned(string stepName)
5151
{
52-
Scenario.Steps.Count(s => s.Title == Configurator.Scanners.Humanize(stepName)).ShouldBe(1);
52+
Scenario.Steps.Count(s => s.Title == Configurator.Humanizer.Humanize(stepName)).ShouldBe(1);
5353
}
5454

5555
[Fact]

src/TestStack.BDDfy.Tests/Scanner/ReflectiveScanner/WhenTestClassUsesExecutableAttributes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public WhenTestClassUsesExecutableAttributes()
6868

6969
private static string GetStepTextFromMethodName(Expression<Action> methodInfoAction)
7070
{
71-
return Configurator.Scanners.Humanize(Helpers.GetMethodInfo(methodInfoAction).Name);
71+
return Configurator.Humanizer.Humanize(Helpers.GetMethodInfo(methodInfoAction).Name);
7272
}
7373

7474
[Fact]

src/TestStack.BDDfy/BDDfyExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public static Engine LazyBDDfy<TStory>(this object testObject, string scenarioTi
5252
/// <returns></returns>
5353
public static Story BDDfy(this object testObject, string scenarioTitle = null, [System.Runtime.CompilerServices.CallerMemberName] string caller = null)
5454
{
55-
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Scanners.Humanize(caller)).Run();
55+
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Humanizer.Humanize(caller)).Run();
5656
}
5757

5858
public static Engine LazyBDDfy(this object testObject, string scenarioTitle = null, [System.Runtime.CompilerServices.CallerMemberName] string caller = null)
5959
{
60-
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Scanners.Humanize(caller));
60+
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Humanizer.Humanize(caller));
6161
}
6262

6363
/// <summary>
@@ -71,13 +71,13 @@ public static Engine LazyBDDfy(this object testObject, string scenarioTitle = nu
7171
public static Story BDDfy<TStory>(this object testObject, string scenarioTitle = null, [System.Runtime.CompilerServices.CallerMemberName] string caller = null)
7272
where TStory : class
7373
{
74-
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Scanners.Humanize(caller), typeof(TStory)).Run();
74+
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Humanizer.Humanize(caller), typeof(TStory)).Run();
7575
}
7676

7777
public static Engine LazyBDDfy<TStory>(this object testObject, string scenarioTitle = null, [System.Runtime.CompilerServices.CallerMemberName] string caller = null)
7878
where TStory : class
7979
{
80-
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Scanners.Humanize(caller), typeof(TStory));
80+
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Humanizer.Humanize(caller), typeof(TStory));
8181
}
8282
#endif
8383

src/TestStack.BDDfy/Configuration/Configurator.cs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,18 @@ namespace TestStack.BDDfy.Configuration
22
{
33
public static class Configurator
44
{
5-
static Configurator()
6-
{
7-
AsyncVoidSupportEnabled = true;
8-
}
5+
public static bool AsyncVoidSupportEnabled { get; set; } = true;
96

10-
public static bool AsyncVoidSupportEnabled { get; set; }
7+
public static Processors Processors { get; } = new();
118

12-
private static readonly Processors ProcessorsFactory = new();
13-
public static Processors Processors
14-
{
15-
get { return ProcessorsFactory; }
16-
}
9+
public static BatchProcessors BatchProcessors { get; } = new();
1710

18-
private static readonly BatchProcessors BatchProcessorFactory = new();
19-
public static BatchProcessors BatchProcessors
20-
{
21-
get { return BatchProcessorFactory; }
22-
}
11+
public static Scanners Scanners { get; } = new();
2312

24-
private static readonly Scanners ScannersFactory = new();
25-
public static Scanners Scanners
26-
{
27-
get { return ScannersFactory; }
28-
}
13+
public static IKeyGenerator IdGenerator { get; set; } = new SequentialKeyGenerator();
2914

30-
private static IKeyGenerator _idGenerator = new SequentialKeyGenerator();
31-
public static IKeyGenerator IdGenerator
32-
{
33-
get { return _idGenerator; }
34-
set { _idGenerator = value; }
35-
}
36-
37-
38-
private static IStepExecutor _stepExecutor = new StepExecutor();
39-
public static IStepExecutor StepExecutor
40-
{
41-
get { return _stepExecutor; }
42-
set { _stepExecutor = value; }
43-
}
15+
public static IStepExecutor StepExecutor { get; set; } = new StepExecutor();
4416

17+
public static IHumanizer Humanizer { get; set; } = new DefaultHumanizer();
4518
}
4619
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TestStack.BDDfy.Configuration
2+
{
3+
public interface IHumanizer
4+
{
5+
string Humanize(string input);
6+
}
7+
}

src/TestStack.BDDfy/Configuration/Scanners.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,5 @@ public IEnumerable<IStepScanner> GetStepScanners(object objectUnderTest)
3535
}
3636

3737
public Func<IStoryMetadataScanner> StoryMetadataScanner = () => new StoryAttributeMetadataScanner();
38-
39-
public Func<string, string> Humanize = NetToString.Convert;
4038
}
4139
}

0 commit comments

Comments
 (0)