Skip to content

Commit 99008e6

Browse files
committed
implemented ButGiven, ButWhen and ButThen attribs & conventions
1 parent 7be8ed2 commit 99008e6

File tree

6 files changed

+91
-2
lines changed

6 files changed

+91
-2
lines changed

TestStack.BDDfy.Tests/Scanner/ReflectiveScanner/WhenTestClassFollowsGivenWhenThenNamingConvention.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@ public void Setup(){}
1717
public void AndThenAnotherThingIsTrue() {}
1818
public void AndWhenSomethingElseHappens() {}
1919
public void And_When_another_THING_Happens() {}
20+
public void ButWhenSomethingDoesNotHappen() { }
21+
public void But_When_another_THING_does_not_happen() { }
2022
public void GivenSomeState() {}
2123
public void AndSomethingElseToo() { }
2224
public void WhenSomethingHappens() {}
2325
public void AndGivenAnotherState() { }
2426
public void And_Given_Some_OTHER_state() { }
2527
public void AndGiven_some_other_initial_state() { }
28+
public void ButGiven_some_initial_state_is_not_set() { }
2629
public void ThenSomethingIsTrue(){}
2730
public void TearDown(){}
2831
public void And_YET_another_thing(){}
2932
public void AndThen_something_else() {}
3033
public void And_then_there_was_that_one_time() {}
3134
public void But_some_condition_is_not_true() {}
3235
public void ButSomeOtherConditionIsNotTrue() {}
36+
public void ButThenSomeOtherConditionIsNotTrueEither() {}
37+
public void But_then_again_some_condition_is_not_true() { }
38+
public void But_Given_Some_OTHER_state_is_not_set() { }
3339
}
3440

3541
[Test]
@@ -39,16 +45,20 @@ public void VerifyScannedSteps()
3945
_steps = new DefaultMethodNameStepScanner().Scan(TestContext.GetContext(_typeWithoutAttribute)).ToList();
4046
int stepIndex = 0;
4147

42-
Assert.That(_steps.Count, Is.EqualTo(18));
48+
Assert.That(_steps.Count, Is.EqualTo(24));
4349
AssertStep(_steps[stepIndex++], "Establish context", ExecutionOrder.Initialize, false, false);
4450
AssertStep(_steps[stepIndex++], "Setup", ExecutionOrder.Initialize, false, false);
4551
AssertStep(_steps[stepIndex++], "Given some state", ExecutionOrder.SetupState);
4652
AssertStep(_steps[stepIndex++], "And another state", ExecutionOrder.ConsecutiveSetupState);
4753
AssertStep(_steps[stepIndex++], "And Some OTHER state", ExecutionOrder.ConsecutiveSetupState);
4854
AssertStep(_steps[stepIndex++], "And some other initial state", ExecutionOrder.ConsecutiveSetupState);
55+
AssertStep(_steps[stepIndex++], "But some initial state is not set", ExecutionOrder.ConsecutiveSetupState);
56+
AssertStep(_steps[stepIndex++], "But Some OTHER state is not set", ExecutionOrder.ConsecutiveSetupState);
4957
AssertStep(_steps[stepIndex++], "When something happens", ExecutionOrder.Transition);
5058
AssertStep(_steps[stepIndex++], "And something else happens", ExecutionOrder.ConsecutiveTransition);
5159
AssertStep(_steps[stepIndex++], "And another THING Happens", ExecutionOrder.ConsecutiveTransition);
60+
AssertStep(_steps[stepIndex++], "But something does not happen", ExecutionOrder.ConsecutiveTransition);
61+
AssertStep(_steps[stepIndex++], "But another THING does not happen", ExecutionOrder.ConsecutiveTransition);
5262
AssertStep(_steps[stepIndex++], "Then something is true", ExecutionOrder.Assertion, true);
5363
AssertStep(_steps[stepIndex++], "And another thing is true", ExecutionOrder.ConsecutiveAssertion, true);
5464
AssertStep(_steps[stepIndex++], "And something else too", ExecutionOrder.ConsecutiveAssertion, true);
@@ -57,6 +67,8 @@ public void VerifyScannedSteps()
5767
AssertStep(_steps[stepIndex++], "And there was that one time", ExecutionOrder.ConsecutiveAssertion, true);
5868
AssertStep(_steps[stepIndex++], "But some condition is not true", ExecutionOrder.ConsecutiveAssertion, true);
5969
AssertStep(_steps[stepIndex++], "But some other condition is not true", ExecutionOrder.ConsecutiveAssertion, true);
70+
AssertStep(_steps[stepIndex++], "But some other condition is not true either", ExecutionOrder.ConsecutiveAssertion, true);
71+
AssertStep(_steps[stepIndex++], "But again some condition is not true", ExecutionOrder.ConsecutiveAssertion, true);
6072
AssertStep(_steps[stepIndex++], "Tear down", ExecutionOrder.TearDown, asserts:false, shouldReport:false);
6173
}
6274

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@ public void ThenIShouldNotBeReturnedBecauseIDoNotHaveAttributes() { }
2727
[When(StepTitle = MethodTextForWhenSomethingHappens)]
2828
public void WhenStep() { }
2929

30+
public void ButWhenSomethingDoesNotHappen() { }
31+
32+
[ButWhen]
33+
public void AndSomethingHasNotHappened() { }
3034
public void WhenStep_NoAttributeIsProvided() { }
3135

3236
[Given]
3337
public void Given() { }
3438

3539
public void GivenWithoutAttribute() { }
40+
41+
[ButGiven]
42+
public void SetupShouldAvoidSomethings() { }
43+
44+
public void ButGivenSomethingIsNotSetup() {}
3645

3746
[AndWhen]
3847
public void TheOtherPartOfWhen() { }
@@ -62,7 +71,7 @@ private static string GetStepTextFromMethodName(Action methodInfoAction)
6271
[Test]
6372
public void DecoratedMethodsAreReturned()
6473
{
65-
Assert.That(_steps.Count, Is.EqualTo(8));
74+
Assert.That(_steps.Count, Is.EqualTo(10));
6675
}
6776

6877
[Test]
@@ -83,6 +92,15 @@ public void AndGiven()
8392
Assert.That(step.Title.Trim(), Is.EqualTo(GetStepTextFromMethodName(_typeWithAttribute.SomeOtherPartOfTheGiven)));
8493
}
8594

95+
[Test]
96+
public void ButGiven()
97+
{
98+
var step = _steps.Single(s => s.Title.Trim() == "Setup should avoid somethings");
99+
Assert.That(step.ExecutionOrder, Is.EqualTo(ExecutionOrder.ConsecutiveSetupState));
100+
Assert.IsFalse(step.Asserts);
101+
Assert.That(step.Title.Trim(), Is.EqualTo(GetStepTextFromMethodName(_typeWithAttribute.SetupShouldAvoidSomethings)));
102+
}
103+
86104
[Test]
87105
public void When()
88106
{
@@ -101,6 +119,15 @@ public void TheOtherPartOfWhen()
101119
Assert.IsFalse(step.Asserts);
102120
}
103121

122+
[Test]
123+
public void ButWhen()
124+
{
125+
var step = _steps.Single(s => s.Title.Trim() == "And something has not happened");
126+
Assert.That(step.ExecutionOrder, Is.EqualTo(ExecutionOrder.ConsecutiveTransition));
127+
Assert.That(step.Title.Trim(), Is.EqualTo(GetStepTextFromMethodName(_typeWithAttribute.AndSomethingHasNotHappened)));
128+
Assert.IsFalse(step.Asserts);
129+
}
130+
104131
[Test]
105132
public void ThenStepsWithArgs()
106133
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace TestStack.BDDfy
2+
{
3+
public class ButGivenAttribute : ExecutableAttribute
4+
{
5+
public ButGivenAttribute() : this(null) { }
6+
7+
public ButGivenAttribute(string stepTitle)
8+
: base(ExecutionOrder.ConsecutiveSetupState, stepTitle)
9+
{
10+
}
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace TestStack.BDDfy
2+
{
3+
public class ButWhenAttribute : ExecutableAttribute
4+
{
5+
public ButWhenAttribute() : this(null) { }
6+
7+
public ButWhenAttribute(string stepTitle)
8+
: base(ExecutionOrder.ConsecutiveTransition, stepTitle)
9+
{
10+
}
11+
}
12+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ public DefaultMethodNameStepScanner()
1212
AddMatcher(new MethodNameMatcher(s => s.StartsWith("Given", StringComparison.OrdinalIgnoreCase), ExecutionOrder.SetupState));
1313
AddMatcher(new MethodNameMatcher(s => s.StartsWith("AndGiven", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveSetupState));
1414
AddMatcher(new MethodNameMatcher(s => s.StartsWith("And_Given_", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveSetupState));
15+
AddMatcher(new MethodNameMatcher(s => s.StartsWith("But_Given_", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveSetupState));
16+
AddMatcher(new MethodNameMatcher(s => s.StartsWith("ButGiven", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveSetupState));
1517
AddMatcher(new MethodNameMatcher(s => s.StartsWith("When", StringComparison.OrdinalIgnoreCase), ExecutionOrder.Transition));
1618
AddMatcher(new MethodNameMatcher(s => s.StartsWith("AndWhen", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveTransition));
1719
AddMatcher(new MethodNameMatcher(s => s.StartsWith("And_When_", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveTransition));
20+
AddMatcher(new MethodNameMatcher(s => s.StartsWith("But_When_", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveTransition));
21+
AddMatcher(new MethodNameMatcher(s => s.StartsWith("ButWhen", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveTransition));
1822
AddMatcher(new MethodNameMatcher(s => s.StartsWith("Then", StringComparison.OrdinalIgnoreCase), ExecutionOrder.Assertion) { Asserts = true });
1923
AddMatcher(new MethodNameMatcher(s => s.StartsWith("And", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveAssertion) { Asserts = true });
2024
AddMatcher(new MethodNameMatcher(s => s.StartsWith("AndThen", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveAssertion) { Asserts = true });
2125
AddMatcher(new MethodNameMatcher(s => s.StartsWith("And_Then_", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveAssertion) { Asserts = true });
2226
AddMatcher(new MethodNameMatcher(s => s.StartsWith("But", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveAssertion) { Asserts = true });
27+
AddMatcher(new MethodNameMatcher(s => s.StartsWith("But_Then_", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveAssertion) { Asserts = true });
28+
AddMatcher(new MethodNameMatcher(s => s.StartsWith("ButThen", StringComparison.OrdinalIgnoreCase), ExecutionOrder.ConsecutiveAssertion) { Asserts = true });
2329
AddMatcher(new MethodNameMatcher(s => s.StartsWith("TearDown", StringComparison.OrdinalIgnoreCase), ExecutionOrder.TearDown) { ShouldReport = false });
2430
}
2531

@@ -28,21 +34,39 @@ static string CleanupTheStepText(string stepText)
2834
if (stepText.StartsWith("and given ", StringComparison.OrdinalIgnoreCase))
2935
return stepText.Remove("and ".Length, "given ".Length);
3036

37+
if (stepText.StartsWith("but given ", StringComparison.OrdinalIgnoreCase))
38+
return stepText.Remove("but ".Length, "given ".Length);
39+
3140
if (stepText.StartsWith("and when ", StringComparison.OrdinalIgnoreCase))
3241
return stepText.Remove("and ".Length, "when ".Length);
3342

43+
if (stepText.StartsWith("but when ", StringComparison.OrdinalIgnoreCase))
44+
return stepText.Remove("but ".Length, "when ".Length);
45+
3446
if (stepText.StartsWith("and then ", StringComparison.OrdinalIgnoreCase))
3547
return stepText.Remove("and ".Length, "then ".Length);
3648

49+
if (stepText.StartsWith("but then ", StringComparison.OrdinalIgnoreCase))
50+
return stepText.Remove("but ".Length, "then ".Length);
51+
3752
if (stepText.StartsWith("AndGiven ", StringComparison.OrdinalIgnoreCase))
3853
return stepText.Remove("and".Length, "given".Length);
3954

55+
if (stepText.StartsWith("ButGiven ", StringComparison.OrdinalIgnoreCase))
56+
return stepText.Remove("but".Length, "given".Length);
57+
4058
if (stepText.StartsWith("AndWhen ", StringComparison.OrdinalIgnoreCase))
4159
return stepText.Remove("and".Length, "when".Length);
4260

61+
if (stepText.StartsWith("ButWhen ", StringComparison.OrdinalIgnoreCase))
62+
return stepText.Remove("but".Length, "when".Length);
63+
4364
if (stepText.StartsWith("AndThen ", StringComparison.OrdinalIgnoreCase))
4465
return stepText.Remove("and".Length, "then".Length);
4566

67+
if (stepText.StartsWith("ButThen ", StringComparison.OrdinalIgnoreCase))
68+
return stepText.Remove("but".Length, "then".Length);
69+
4670
return stepText;
4771
}
4872
}

TestStack.BDDfy/TestStack.BDDfy.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@
116116
<Compile Include="Scanners\StepScanners\Examples\ExampleValue.cs" />
117117
<Compile Include="Scanners\StepScanners\Examples\UnassignableExampleException.cs" />
118118
<Compile Include="Scanners\StepScanners\ExecutableAttribute\GwtAttributes\ButAttribute.cs" />
119+
<Compile Include="Scanners\StepScanners\ExecutableAttribute\GwtAttributes\ButGivenAttribute.cs" />
120+
<Compile Include="Scanners\StepScanners\ExecutableAttribute\GwtAttributes\ButWhenAttribute.cs" />
119121
<Compile Include="Scanners\StepScanners\WithTagsExtensions.cs" />
120122
<Compile Include="Scanners\StepScanners\Fluent\FluentApi.cs">
121123
<AutoGen>True</AutoGen>

0 commit comments

Comments
 (0)