Skip to content

Commit 5b71bec

Browse files
committed
Added option to (sub) order the steps using the attributes
1 parent c4baec5 commit 5b71bec

File tree

6 files changed

+224
-4
lines changed

6 files changed

+224
-4
lines changed
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NUnit.Framework;
6+
using TestStack.BDDfy.Configuration;
7+
using TestStack.BDDfy.Core;
8+
using TestStack.BDDfy.Scanners;
9+
using TestStack.BDDfy.Scanners.ScenarioScanners;
10+
using TestStack.BDDfy.Scanners.StepScanners.ExecutableAttribute;
11+
using TestStack.BDDfy.Scanners.StepScanners.ExecutableAttribute.GwtAttributes;
12+
13+
namespace TestStack.BDDfy.Tests.Scanner
14+
{
15+
[TestFixture]
16+
public class ExecutableAttributeOrderOrdersTheStepsCorrectly
17+
{
18+
private TypeWithOrderedAttribute _typeWithAttribute;
19+
private List<ExecutionStep> _steps;
20+
21+
private class TypeWithOrderedAttribute
22+
{
23+
24+
25+
[AndThen(Order = 2)]
26+
public void AndThen2() { }
27+
28+
[AndThen(Order = -3)]
29+
public void AndThenNeg3() { }
30+
31+
[AndThen]
32+
public void AndThen0() { }
33+
34+
[AndThen(Order = 2)]
35+
public void AndThen2Again() { }
36+
37+
[Then(Order = 1)]
38+
public void Then1() { }
39+
40+
[Then(Order = 3)]
41+
public void Then3() { }
42+
43+
[Given(Order = 1)]
44+
public void Given1() { }
45+
46+
[Given(Order = 3)]
47+
public void Given3() { }
48+
49+
[AndGiven]
50+
public void AndGiven0() { }
51+
52+
[AndGiven(Order = 2)]
53+
public void AndGiven2() { }
54+
55+
[AndGiven(Order = -3)]
56+
public void AndGivenNeg3() { }
57+
58+
[AndGiven(Order = 2)]
59+
public void AndGiven2Again() { }
60+
61+
62+
[When(Order = 1)]
63+
public void When1() { }
64+
65+
[When(Order = 3)]
66+
public void When3() { }
67+
68+
[AndWhen(Order = 2)]
69+
public void AndWhen2() { }
70+
71+
[AndWhen(Order = -3)]
72+
public void AndWhenNeg3() { }
73+
74+
[AndWhen]
75+
public void AndWhen0() { }
76+
77+
[AndWhen(Order = 2)]
78+
public void AndWhen2Again() { }
79+
80+
}
81+
82+
[SetUp]
83+
public void WhenStep_TestClassHasAttributes()
84+
{
85+
var testObject = new TypeWithOrderedAttribute();
86+
var stepScanners = Configurator.Scanners.GetStepScanners(testObject).ToArray();
87+
var scanner = new ReflectiveScenarioScanner(stepScanners);
88+
var scenario = scanner.Scan(testObject);
89+
_steps = scenario.Steps;
90+
}
91+
92+
[Test]
93+
public void Step0IsGiven1()
94+
{
95+
Assert.AreEqual("Given 1", _steps[0].StepTitle);
96+
}
97+
98+
[Test]
99+
public void Step1IsGiven3()
100+
{
101+
Assert.AreEqual("Given 3", _steps[1].StepTitle);
102+
}
103+
104+
105+
[Test]
106+
public void Step2IsAndGivenNeg3()
107+
{
108+
Assert.AreEqual("And given neg 3", _steps[2].StepTitle);
109+
}
110+
111+
[Test]
112+
public void Step3IsAndGiven0()
113+
{
114+
Assert.AreEqual("And given 0", _steps[3].StepTitle);
115+
}
116+
117+
118+
[Test]
119+
public void Step4AndGiven2()
120+
{
121+
Assert.AreEqual("And given 2", _steps[4].StepTitle);
122+
}
123+
124+
[Test]
125+
public void Step5AndGiven2Again()
126+
{
127+
Assert.AreEqual("And given 2 again", _steps[5].StepTitle);
128+
}
129+
130+
131+
[Test]
132+
public void Step6IsWhen1()
133+
{
134+
Assert.AreEqual("When 1", _steps[6].StepTitle);
135+
}
136+
137+
[Test]
138+
public void Step7IsWhen3()
139+
{
140+
Assert.AreEqual("When 3", _steps[7].StepTitle);
141+
}
142+
143+
144+
[Test]
145+
public void Step8IsAndWhenNeg3()
146+
{
147+
Assert.AreEqual("And when neg 3", _steps[8].StepTitle);
148+
}
149+
150+
[Test]
151+
public void Step9IsAndWhen0()
152+
{
153+
Assert.AreEqual("And when 0", _steps[9].StepTitle);
154+
}
155+
156+
157+
[Test]
158+
public void Step10AndWhen2()
159+
{
160+
Assert.AreEqual("And when 2", _steps[10].StepTitle);
161+
}
162+
163+
[Test]
164+
public void Step11AndWhen2Again()
165+
{
166+
Assert.AreEqual("And when 2 again", _steps[11].StepTitle);
167+
}
168+
169+
170+
[Test]
171+
public void Step12IsThen1()
172+
{
173+
Assert.AreEqual("Then 1", _steps[12].StepTitle);
174+
}
175+
176+
[Test]
177+
public void Step13IsThen3()
178+
{
179+
Assert.AreEqual("Then 3", _steps[13].StepTitle);
180+
}
181+
182+
183+
[Test]
184+
public void Step14IsAndThenNeg3()
185+
{
186+
Assert.AreEqual("And then neg 3", _steps[14].StepTitle);
187+
}
188+
189+
[Test]
190+
public void Step15IsAndThen0()
191+
{
192+
Assert.AreEqual("And then 0", _steps[15].StepTitle);
193+
}
194+
195+
196+
[Test]
197+
public void Step16AndThen2()
198+
{
199+
Assert.AreEqual("And then 2", _steps[16].StepTitle);
200+
}
201+
202+
[Test]
203+
public void Step17AndThen2Again()
204+
{
205+
Assert.AreEqual("And then 2 again", _steps[17].StepTitle);
206+
}
207+
}
208+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<SubType>Code</SubType>
7070
</Compile>
7171
<Compile Include="HumanizerTests.cs" />
72+
<Compile Include="Scanner\ExecutableAttributeOrderOrdersTheStepsCorrectly.cs" />
7273
<Compile Include="Scanner\PropertiesAreNotConsideredAsStepsEvenWhenTheirNameMatchesConventions.cs" />
7374
<Compile Include="Scanner\ExpressionExtensionsTests.cs" />
7475
<Compile Include="Scanner\StepScannerExtensions.cs" />

TestStack.BDDfy/Core/ExecutionStep.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public ExecutionStep(
5353
public StepExecutionResult Result { get; set; }
5454
public Exception Exception { get; set; }
5555
public ExecutionOrder ExecutionOrder { get; private set; }
56+
public int ExecutionSubOrder { get; set; }
5657

5758
public void Execute(object testObject)
5859
{

TestStack.BDDfy/Core/Scenario.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class Scenario
3434
public Scenario(object testObject, IEnumerable<ExecutionStep> steps, string scenarioText)
3535
{
3636
TestObject = testObject;
37-
_steps = steps.OrderBy(o => o.ExecutionOrder).ToList();
37+
_steps = steps.OrderBy(o => o.ExecutionOrder).ThenBy(o => o.ExecutionSubOrder).ToList();
3838
Id = Guid.NewGuid();
3939

4040
Title = scenarioText;
@@ -46,7 +46,7 @@ public Scenario(object testObject, IEnumerable<ExecutionStep> steps, string scen
4646
public Guid Id { get; private set; }
4747

4848
private readonly List<ExecutionStep> _steps;
49-
public IEnumerable<ExecutionStep> Steps
49+
public List<ExecutionStep> Steps
5050
{
5151
get { return _steps; }
5252
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ public ExecutableAttribute(ExecutionOrder order, string stepTitle)
4040
public ExecutionOrder ExecutionOrder { get; private set; }
4141
public bool Asserts { get; set; }
4242
public string StepTitle { get; set; }
43+
public int Order { get; set; }
4344
}
4445
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public IEnumerable<ExecutionStep> Scan(object testObject, MethodInfo candidateMe
6666
var runStepWithArgsAttributes = (RunStepWithArgsAttribute[])candidateMethod.GetCustomAttributes(typeof(RunStepWithArgsAttribute), false);
6767
if (runStepWithArgsAttributes.Length == 0)
6868
{
69-
yield return new ExecutionStep(GetStepAction(candidateMethod), stepTitle, stepAsserts, executableAttribute.ExecutionOrder, true);
69+
yield return
70+
new ExecutionStep(GetStepAction(candidateMethod), stepTitle, stepAsserts, executableAttribute.ExecutionOrder, true)
71+
{
72+
ExecutionSubOrder = executableAttribute.Order
73+
};
7074
}
7175

7276
foreach (var runStepWithArgsAttribute in runStepWithArgsAttributes)
@@ -81,7 +85,12 @@ public IEnumerable<ExecutionStep> Scan(object testObject, MethodInfo candidateMe
8185
else if (!string.IsNullOrEmpty(executableAttribute.StepTitle))
8286
methodName = string.Format(executableAttribute.StepTitle, flatInput);
8387

84-
yield return new ExecutionStep(GetStepAction(candidateMethod, inputArguments), methodName, stepAsserts, executableAttribute.ExecutionOrder, true);
88+
yield return
89+
new ExecutionStep(GetStepAction(candidateMethod, inputArguments), methodName, stepAsserts,
90+
executableAttribute.ExecutionOrder, true)
91+
{
92+
ExecutionSubOrder = executableAttribute.Order
93+
};
8594
}
8695
}
8796

0 commit comments

Comments
 (0)