1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
package org.tap4j.representer; |
25 | |
|
26 | |
import java.io.PrintWriter; |
27 | |
import java.io.StringWriter; |
28 | |
import java.util.List; |
29 | |
import java.util.Map; |
30 | |
|
31 | |
import org.tap4j.model.BailOut; |
32 | |
import org.tap4j.model.Comment; |
33 | |
import org.tap4j.model.Footer; |
34 | |
import org.tap4j.model.Header; |
35 | |
import org.tap4j.model.Plan; |
36 | |
import org.tap4j.model.TapElement; |
37 | |
import org.tap4j.model.TestResult; |
38 | |
import org.tap4j.model.TestSet; |
39 | |
import org.yaml.snakeyaml.DumperOptions; |
40 | |
import org.yaml.snakeyaml.DumperOptions.LineBreak; |
41 | |
import org.yaml.snakeyaml.Yaml; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class Tap13Representer implements Representer { |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | private static final CharSequence LINE_SEPARATOR = "\n"; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private org.tap4j.representer.DumperOptions options; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 0 | private Yaml yaml = null; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public Tap13Representer() { |
69 | 0 | this(new org.tap4j.representer.DumperOptions()); |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public Tap13Representer(org.tap4j.representer.DumperOptions options) { |
76 | 0 | super(); |
77 | 0 | this.options = options; |
78 | 0 | if (options.isPrintDiagnostics()) { |
79 | 0 | final DumperOptions yamlDumperOptions = new DumperOptions(); |
80 | 0 | yamlDumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
81 | 0 | yamlDumperOptions.setLineBreak(LineBreak.getPlatformLineBreak()); |
82 | 0 | yamlDumperOptions.setExplicitStart(true); |
83 | 0 | yamlDumperOptions.setExplicitEnd(true); |
84 | 0 | yaml = new Yaml(yamlDumperOptions); |
85 | |
} |
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public org.tap4j.representer.DumperOptions getOptions() { |
92 | 0 | return options; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public String representData(TestSet testSet) { |
99 | 0 | StringWriter sw = new StringWriter(); |
100 | 0 | PrintWriter pw = new PrintWriter(sw); |
101 | 0 | printHeader(pw, testSet.getHeader()); |
102 | 0 | printPlan(pw, testSet.getPlan()); |
103 | 0 | for (TapElement tapLine : testSet.getTapLines()) { |
104 | 0 | printTapLine(pw, tapLine); |
105 | 0 | } |
106 | 0 | printFooter(pw, testSet.getFooter()); |
107 | 0 | return sw.toString(); |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
protected void printTapLine(PrintWriter pw, TapElement tapResult) { |
115 | 0 | if (tapResult instanceof BailOut) { |
116 | 0 | printBailOut(pw, (BailOut) tapResult); |
117 | 0 | } else if (tapResult instanceof Comment) { |
118 | 0 | printComment(pw, (Comment) tapResult); |
119 | 0 | pw.append(LINE_SEPARATOR); |
120 | 0 | } else if (tapResult instanceof TestResult) { |
121 | 0 | printTestResult(pw, (TestResult) tapResult); |
122 | |
} |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
protected void printTestResult(PrintWriter pw, TestResult testResult) { |
130 | 0 | if (testResult.getSubtest() != null) { |
131 | 0 | int indent = this.options.getIndent(); |
132 | 0 | int spaces = this.options.getSpaces(); |
133 | 0 | this.options.setIndent(indent + spaces); |
134 | 0 | pw.append(this.representData(testResult.getSubtest())); |
135 | 0 | this.options.setIndent(indent); |
136 | |
} |
137 | 0 | printFiller(pw); |
138 | 0 | pw.append(testResult.getStatus().toString()); |
139 | 0 | pw.append(' ' + Integer.toString(testResult.getTestNumber())); |
140 | 0 | if (testResult.getDescription() != null && testResult.getDescription().trim().length() > 0) { |
141 | 0 | pw.append(' ' + testResult.getDescription()); |
142 | |
} |
143 | 0 | if (testResult.getDirective() != null) { |
144 | 0 | pw.append(" # " |
145 | 0 | + testResult.getDirective().getDirectiveValue().toString()); |
146 | 0 | String reason = testResult.getDirective().getReason(); |
147 | 0 | if (reason != null && reason.trim().length() > 0) { |
148 | 0 | pw.append(' ' + testResult.getDirective().getReason()); |
149 | |
} |
150 | |
} |
151 | 0 | List<Comment> comments = testResult.getComments(); |
152 | 0 | if (comments.size() > 0) { |
153 | 0 | for (Comment comment : comments) { |
154 | 0 | if (comment.isInline()) { |
155 | 0 | pw.append(' '); |
156 | 0 | printComment(pw, comment); |
157 | |
} else { |
158 | 0 | pw.append(LINE_SEPARATOR); |
159 | 0 | printComment(pw, comment); |
160 | |
} |
161 | 0 | } |
162 | |
} |
163 | 0 | printDiagnostic(pw, testResult); |
164 | 0 | pw.append(LINE_SEPARATOR); |
165 | 0 | } |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
protected void printBailOut(PrintWriter pw, BailOut bailOut) { |
172 | 0 | printFiller(pw); |
173 | 0 | pw.append("Bail out!"); |
174 | 0 | if (bailOut.getReason() != null) { |
175 | 0 | pw.append(' ' + bailOut.getReason()); |
176 | |
} |
177 | 0 | if (bailOut.getComment() != null) { |
178 | 0 | pw.append(' '); |
179 | 0 | printComment(pw, bailOut.getComment()); |
180 | |
} |
181 | 0 | printDiagnostic(pw, bailOut); |
182 | 0 | pw.append(LINE_SEPARATOR); |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
protected void printFooter(PrintWriter pw, Footer footer) { |
190 | 0 | if (footer != null) { |
191 | 0 | printFiller(pw); |
192 | 0 | pw.append("TAP " + footer.getText()); |
193 | 0 | if (footer.getComment() != null) { |
194 | 0 | pw.append(' '); |
195 | 0 | printComment(pw, footer.getComment()); |
196 | |
} |
197 | 0 | printDiagnostic(pw, footer); |
198 | 0 | pw.append(LINE_SEPARATOR); |
199 | |
} |
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
protected void printPlan(PrintWriter pw, Plan plan) { |
207 | 0 | if (plan != null) { |
208 | 0 | printFiller(pw); |
209 | 0 | pw.append(plan.getInitialTestNumber() |
210 | |
+ ".." |
211 | 0 | + plan.getLastTestNumber()); |
212 | 0 | if (plan.getSkip() != null) { |
213 | 0 | pw.append(" skip "); |
214 | 0 | pw.append(plan.getSkip().getReason()); |
215 | |
} |
216 | 0 | if (plan.getComment() != null) { |
217 | 0 | pw.append(' '); |
218 | 0 | this.printComment(pw, plan.getComment()); |
219 | |
} |
220 | 0 | printDiagnostic(pw, plan); |
221 | 0 | pw.append(LINE_SEPARATOR); |
222 | |
} else { |
223 | 0 | if (options.isAllowEmptyTestPlan() == Boolean.FALSE) { |
224 | 0 | throw new RepresenterException("Missing required TAP Plan"); |
225 | |
} |
226 | |
} |
227 | 0 | } |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
protected void printHeader(PrintWriter pw, Header header) { |
234 | 0 | if (header != null) { |
235 | 0 | printFiller(pw); |
236 | 0 | pw.append("TAP version " + header.getVersion()); |
237 | 0 | if (header.getComment() != null) { |
238 | 0 | pw.append(' '); |
239 | 0 | this.printComment(pw, header.getComment()); |
240 | |
} |
241 | 0 | printDiagnostic(pw, header); |
242 | 0 | pw.append(LINE_SEPARATOR); |
243 | |
} |
244 | 0 | } |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
protected void printComment(PrintWriter pw, Comment comment) { |
251 | 0 | pw.append("# " + comment.getText()); |
252 | 0 | } |
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
protected void printDiagnostic(PrintWriter pw, TapElement tapElement) { |
261 | 0 | if (this.yaml != null) { |
262 | 0 | Map<String, Object> diagnostic = tapElement.getDiagnostic(); |
263 | 0 | if (diagnostic != null && !diagnostic.isEmpty()) { |
264 | 0 | String diagnosticText = yaml.dump(diagnostic); |
265 | 0 | diagnosticText = diagnosticText.replaceAll("((?m)^)", " "); |
266 | 0 | pw.append(LINE_SEPARATOR); |
267 | 0 | printFiller(pw); |
268 | 0 | pw.append(diagnosticText); |
269 | |
} |
270 | |
} |
271 | 0 | } |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
protected void printFiller(PrintWriter pw) { |
279 | 0 | if (this.options.getIndent() > 0) { |
280 | 0 | for (int i = 0; i < options.getIndent(); i++) { |
281 | 0 | pw.append(' '); |
282 | |
} |
283 | |
} |
284 | 0 | } |
285 | |
|
286 | |
} |