1
1
package cucumber .runtime ;
2
2
3
3
import cucumber .api .Result ;
4
+ import cucumber .api .event .EventHandler ;
5
+ import cucumber .api .event .EventListener ;
6
+ import cucumber .api .event .EventPublisher ;
7
+ import cucumber .api .event .TestCaseFinished ;
8
+ import cucumber .api .event .TestRunFinished ;
9
+ import cucumber .api .event .TestRunStarted ;
10
+ import cucumber .api .event .TestStepFinished ;
4
11
import cucumber .runtime .formatter .AnsiFormats ;
5
12
import cucumber .runtime .formatter .Format ;
6
13
import cucumber .runtime .formatter .Formats ;
13
20
import java .util .List ;
14
21
import java .util .Locale ;
15
22
16
- class Stats {
23
+ class Stats implements EventListener {
17
24
public static final long ONE_SECOND = 1000000000 ;
18
25
public static final long ONE_MINUTE = 60 * ONE_SECOND ;
26
+ private static final byte ERRORS = 0x1 ;
19
27
private SubCounts scenarioSubCounts = new SubCounts ();
20
28
private SubCounts stepSubCounts = new SubCounts ();
29
+ private long startTime = 0 ;
21
30
private long totalDuration = 0 ;
22
31
private Formats formats ;
23
32
private Locale locale ;
24
- private List <String > failedScenarios = new ArrayList <String >();
33
+ private final List <String > failedScenarios = new ArrayList <String >();
25
34
private List <String > ambiguousScenarios = new ArrayList <String >();
26
- private List <String > pendingScenarios = new ArrayList <String >();
27
- private List <String > undefinedScenarios = new ArrayList <String >();
35
+ private final List <String > pendingScenarios = new ArrayList <String >();
36
+ private final List <String > undefinedScenarios = new ArrayList <String >();
37
+ private final List <Throwable > errors = new ArrayList <Throwable >();
38
+ private final EventHandler <TestRunStarted > testRunStartedHandler = new EventHandler <TestRunStarted >() {
39
+ @ Override
40
+ public void receive (TestRunStarted event ) {
41
+ setStartTime (event .getTimeStamp ());
42
+ }
43
+ };
44
+ private final EventHandler <TestStepFinished > stepFinishedHandler = new EventHandler <TestStepFinished >() {
45
+ @ Override
46
+ public void receive (TestStepFinished event ) {
47
+ Result result = event .result ;
48
+ if (result .getError () != null ) {
49
+ addError (result .getError ());
50
+ }
51
+ if (!event .testStep .isHook ()) {
52
+ addStep (result .getStatus ());
53
+ }
54
+ }
55
+ };
56
+ private final EventHandler <TestCaseFinished > testCaseFinishedHandler = new EventHandler <TestCaseFinished >() {
57
+ @ Override
58
+ public void receive (TestCaseFinished event ) {
59
+ addScenario (event .result .getStatus (), event .testCase .getScenarioDesignation ());
60
+ }
61
+ };
62
+ private final EventHandler <TestRunFinished > testRunFinishedHandler = new EventHandler <TestRunFinished >() {
63
+ @ Override
64
+ public void receive (TestRunFinished event ) {
65
+ setFinishTime (event .getTimeStamp ());
66
+ }
67
+ };
28
68
29
69
public Stats (boolean monochrome ) {
30
70
this (monochrome , Locale .getDefault ());
@@ -39,6 +79,27 @@ public Stats(boolean monochrome, Locale locale) {
39
79
}
40
80
}
41
81
82
+
83
+ @ Override
84
+ public void setEventPublisher (EventPublisher publisher ) {
85
+ publisher .registerHandlerFor (TestRunStarted .class , testRunStartedHandler );
86
+ publisher .registerHandlerFor (TestStepFinished .class , stepFinishedHandler );
87
+ publisher .registerHandlerFor (TestCaseFinished .class , testCaseFinishedHandler );
88
+ publisher .registerHandlerFor (TestRunFinished .class , testRunFinishedHandler );
89
+ }
90
+
91
+ public List <Throwable > getErrors () {
92
+ return errors ;
93
+ }
94
+
95
+ public byte exitStatus (boolean isStrict ) {
96
+ byte result = 0x0 ;
97
+ if (!failedScenarios .isEmpty () || (isStrict && (!pendingScenarios .isEmpty () || !undefinedScenarios .isEmpty ()))) {
98
+ result |= ERRORS ;
99
+ }
100
+ return result ;
101
+ }
102
+
42
103
public void printStats (PrintStream out , boolean isStrict ) {
43
104
printNonZeroResultScenarios (out , isStrict );
44
105
if (stepSubCounts .getTotal () == 0 ) {
@@ -119,21 +180,20 @@ private void printScenarios(PrintStream out, List<String> scenarios, Result.Type
119
180
}
120
181
}
121
182
122
- public void addStep (Result result ) {
123
- addResultToSubCount (stepSubCounts , result .getStatus ());
124
- addTime (result .getDuration ());
183
+ void addStep (Result .Type resultStatus ) {
184
+ addResultToSubCount (stepSubCounts , resultStatus );
125
185
}
126
186
127
- public void addScenario ( Result . Type resultStatus ) {
128
- addResultToSubCount ( scenarioSubCounts , resultStatus );
187
+ private void addError ( Throwable error ) {
188
+ errors . add ( error );
129
189
}
130
190
131
- public void addHookTime (Long duration ) {
132
- addTime ( duration ) ;
191
+ void setStartTime (Long startTime ) {
192
+ this . startTime = startTime ;
133
193
}
134
194
135
- private void addTime (Long duration ) {
136
- totalDuration += duration != null ? duration : 0 ;
195
+ void setFinishTime (Long finishTime ) {
196
+ this . totalDuration = finishTime - startTime ;
137
197
}
138
198
139
199
private void addResultToSubCount (SubCounts subCounts , Result .Type resultStatus ) {
@@ -158,7 +218,7 @@ private void addResultToSubCount(SubCounts subCounts, Result.Type resultStatus)
158
218
}
159
219
}
160
220
161
- public void addScenario (Result .Type resultStatus , String scenarioDesignation ) {
221
+ void addScenario (Result .Type resultStatus , String scenarioDesignation ) {
162
222
addResultToSubCount (scenarioSubCounts , resultStatus );
163
223
switch (resultStatus ) {
164
224
case FAILED :
@@ -178,7 +238,7 @@ public void addScenario(Result.Type resultStatus, String scenarioDesignation) {
178
238
}
179
239
}
180
240
181
- class SubCounts {
241
+ static class SubCounts {
182
242
public int passed = 0 ;
183
243
public int failed = 0 ;
184
244
public int ambiguous = 0 ;
0 commit comments