forked from cucumber/cucumber-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStats.java
executable file
·180 lines (158 loc) · 6.26 KB
/
Stats.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package cucumber.runtime;
import gherkin.formatter.AnsiFormats;
import gherkin.formatter.Format;
import gherkin.formatter.Formats;
import gherkin.formatter.MonochromeFormats;
import gherkin.formatter.model.Result;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
class Stats {
public static final long ONE_SECOND = 1000000000;
public static final long ONE_MINUTE = 60 * ONE_SECOND;
public static final String PENDING = "pending";
private SubCounts scenarioSubCounts = new SubCounts();
private SubCounts stepSubCounts = new SubCounts();
private long totalDuration = 0;
private Formats formats;
private Locale locale;
private List<String> failedScenarios = new ArrayList<String>();
private List<String> pendingScenarios = new ArrayList<String>();
private List<String> undefinedScenarios = new ArrayList<String>();
private List<String> passedScenarios = new ArrayList<String>();
public Stats(boolean monochrome) {
this(monochrome, Locale.getDefault());
}
public Stats(boolean monochrome, Locale locale) {
this.locale = locale;
if (monochrome) {
formats = new MonochromeFormats();
} else {
formats = new AnsiFormats();
}
}
public void printStats(PrintStream out, boolean isStrict) {
printNonZeroResultScenarios(out, isStrict);
if (stepSubCounts.getTotal() == 0) {
out.println("0 Scenarios");
out.println("0 Steps");
} else {
printScenarioCounts(out);
printStepCounts(out);
}
printDuration(out);
}
private void printStepCounts(PrintStream out) {
out.print(stepSubCounts.getTotal());
out.print(" Steps (");
printSubCounts(out, stepSubCounts);
out.println(")");
}
private void printScenarioCounts(PrintStream out) {
out.print(scenarioSubCounts.getTotal());
out.print(" Scenarios (");
printSubCounts(out, scenarioSubCounts);
out.println(")");
}
private void printSubCounts(PrintStream out, SubCounts subCounts) {
boolean addComma = false;
addComma = printSubCount(out, subCounts.failed, Result.FAILED, addComma);
addComma = printSubCount(out, subCounts.skipped, Result.SKIPPED.getStatus(), addComma);
addComma = printSubCount(out, subCounts.pending, PENDING, addComma);
addComma = printSubCount(out, subCounts.undefined, Result.UNDEFINED.getStatus(), addComma);
addComma = printSubCount(out, subCounts.passed, Result.PASSED, addComma);
}
private boolean printSubCount(PrintStream out, int count, String type, boolean addComma) {
if (count != 0) {
if (addComma) {
out.print(", ");
}
Format format = formats.get(type);
out.print(format.text(count + " " + type));
addComma = true;
}
return addComma;
}
private void printDuration(PrintStream out) {
out.print(String.format("%dm", (totalDuration / ONE_MINUTE)));
DecimalFormat format = new DecimalFormat("0.000", new DecimalFormatSymbols(locale));
out.println(format.format(((double) (totalDuration % ONE_MINUTE)) / ONE_SECOND) + "s");
}
private void printNonZeroResultScenarios(PrintStream out, boolean isStrict) {
printScenarios(out, failedScenarios, Result.FAILED);
if (isStrict) {
printScenarios(out, pendingScenarios, PENDING);
printScenarios(out, undefinedScenarios, Result.UNDEFINED.getStatus());
}
}
private void printScenarios(PrintStream out, List<String> scenarios, String type) {
Format format = formats.get(type);
if (!scenarios.isEmpty()) {
out.println(format.text(capitalizeFirstLetter(type) + " scenarios:"));
}
for (String scenario : scenarios) {
String[] parts = scenario.split("#");
out.print(format.text(parts[0]));
for (int i = 1; i < parts.length; ++i) {
out.println("#" + parts[i]);
}
}
if (!scenarios.isEmpty()) {
out.println();
}
}
private String capitalizeFirstLetter(String type) {
return type.substring(0, 1).toUpperCase(locale) + type.substring(1);
}
public void addStep(Result result) {
addResultToSubCount(stepSubCounts, result.getStatus());
addTime(result.getDuration());
}
public void addScenario(String resultStatus) {
addResultToSubCount(scenarioSubCounts, resultStatus);
}
public void addHookTime(Long duration) {
addTime(duration);
}
private void addTime(Long duration) {
totalDuration += duration != null ? duration : 0;
}
private void addResultToSubCount(SubCounts subCounts, String resultStatus) {
if (resultStatus.equals(Result.FAILED)) {
subCounts.failed++;
} else if (resultStatus.equals(PENDING)) {
subCounts.pending++;
} else if (resultStatus.equals(Result.UNDEFINED.getStatus())) {
subCounts.undefined++;
} else if (resultStatus.equals(Result.SKIPPED.getStatus())) {
subCounts.skipped++;
} else if (resultStatus.equals(Result.PASSED)) {
subCounts.passed++;
}
}
public void addScenario(String resultStatus, String scenarioDesignation) {
addResultToSubCount(scenarioSubCounts, resultStatus);
if (resultStatus.equals(Result.FAILED)) {
failedScenarios.add(scenarioDesignation);
} else if (resultStatus.equals(PENDING)) {
pendingScenarios.add(scenarioDesignation);
} else if (resultStatus.equals(Result.UNDEFINED.getStatus())) {
undefinedScenarios.add(scenarioDesignation);
} else if (resultStatus.equals(Result.PASSED)) {
passedScenarios.add(scenarioDesignation);
}
}
class SubCounts {
public int passed = 0;
public int failed = 0;
public int skipped = 0;
public int pending = 0;
public int undefined = 0;
public int getTotal() {
return passed + failed + skipped + pending + undefined;
}
}
}