-
Notifications
You must be signed in to change notification settings - Fork 1
/
JTap.java
500 lines (354 loc) · 9.37 KB
/
JTap.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import java.io.* ;
public class JTap {
final private String version = "1.0" ;
private boolean plan_set = false ;
private boolean no_plan = false ;
private boolean skip_all = false ;
private boolean test_died = false ;
private int expected_tests = 0 ;
private int executed_tests = 0 ;
private int failed_tests = 0 ;
private boolean exit = true ;
private String todo = null ;
private PrintStream out = null ;
private PrintStream err = null ;
public JTap(){
this(true) ;
}
public JTap(boolean really_exit){
out = System.out ;
err = System.err ;
exit = really_exit ;
}
synchronized public int plan_no_plan(){
if (plan_set){
die("You tried to plan twice!") ;
}
plan_set = true ;
no_plan = true ;
return 1 ;
}
synchronized public int plan_skip_all(String reason){
if (plan_set){
die("You tried to plan twice!") ;
}
print_plan(0, "Skip " + reason) ;
skip_all = true ;
plan_set = true ;
exit(0) ;
return 0 ;
}
synchronized public int plan_tests(int tests){
if (plan_set){
die("You tried to plan twice!") ;
}
if (tests == 0){
die("You said to run 0 tests! You've got to run something.") ;
}
print_plan(tests) ;
expected_tests = tests ;
plan_set = true ;
return tests ;
}
private void print_plan(int expected){
print_plan(expected, null) ;
}
synchronized private void print_plan(int expected_tests, String directive){
out.print("1.." + expected_tests) ;
if (directive != null){
out.print(" # " + directive) ;
}
out.print("\n") ;
out.flush() ;
}
public boolean pass(String name){
return ok(true, name) ;
}
public boolean fail(String name){
return ok(false, name) ;
}
public boolean ok(boolean result){
return ok(result, null) ;
}
/*
This is the workhorse method that actually
prints the tests result.
*/
synchronized public boolean ok(boolean result, String name){
if (! plan_set){
die("You tried to run a test without a plan! Gotta have a plan.") ;
}
executed_tests++ ;
if (name != null) {
if (name.matches("[\\d\\s]+")){
diag(" You named your test '" + name
+ "'. You shouldn't use numbers for your test names.") ;
diag(" Very confusing.") ;
}
}
if (! result){
out.print("not ") ;
failed_tests++ ;
}
out.print("ok " + executed_tests) ;
if (name != null) {
out.print(" - ") ;
out.print(name.replaceAll("#", "\\\\#")) ;
}
if (todo != null){
out.print(" # TODO " + todo) ;
if (! result){
failed_tests-- ;
}
}
out.print("\n") ;
out.flush() ;
if (! result){
Throwable t = new Throwable() ;
StackTraceElement stack[] = t.getStackTrace() ;
String file = null ;
String clas = null ;
String func = null ;
int line = 0 ;
try {
for (int i = 0 ; i < stack.length ; i++){
Class c = Class.forName(stack[i].getClassName()) ;
if (! JTap.class.isAssignableFrom(c)){
// We are outside a JTap object, so this is probably the callpoint
file = stack[i].getFileName() ;
clas = c.getName() ;
func = stack[i].getMethodName() ;
line = stack[i].getLineNumber() ;
break ;
}
}
}
catch (Exception e){
e.printStackTrace() ;
}
if (name != null){
diag(" Failed " + (todo == null ? "" : "(TODO) ") + "test '" + name + "'") ;
diag(" in " + file + ":" + func + "() at line " + line + ".") ;
}
else {
diag(" Failed " + (todo == null ? "" : "(TODO) ") + "test in " + file + ":" + func + "() at line " + line + ".") ;
}
}
return result ;
}
private boolean equals(Object result, Object expected){
boolean r ;
if ((result == null)&&(expected == null)){
r = true ;
}
else if ((result == null)||(expected == null)){
r = false ;
}
else {
r = result.equals(expected) ;
}
return r ;
}
private boolean matches(Object result, String pattern){
boolean r ;
if ((result == null)||(pattern == null)){
r = false ;
}
else {
r = result.toString().matches(pattern) ;
}
return r ;
}
private void is_diag(Object result, Object expected){
diag(" got: '" + result + "'") ;
diag(" expected: '" + expected + "'") ;
}
public boolean is(Object result, Object expected){
return is(result, expected, null) ;
}
public boolean is(Object result, Object expected, String name){
boolean r = ok(equals(result, expected), name) ;
if (! r){
is_diag(result, expected) ;
}
return r ;
}
public boolean is(long result, long expected){
return is(new Long(result), new Long(expected)) ;
}
public boolean is(long result, long expected, String name){
return is(new Long(result), new Long(expected), name) ;
}
public boolean is(double result, double expected){
return is(new Double(result), new Double(expected)) ;
}
public boolean is(double result, double expected, String name){
return is(new Double(result), new Double(expected), name) ;
}
public boolean isnt(Object result, Object expected){
return isnt(result, expected, null) ;
}
public boolean isnt(Object result, Object expected, String name){
boolean r = ok(! equals(result, expected), name) ;
if (! r){
is_diag(result, expected) ;
}
return r ;
}
public boolean isnt(long result, long expected){
return isnt(new Long(result), new Long(expected)) ;
}
public boolean isnt(long result, long expected, String name){
return isnt(new Long(result), new Long(expected), name) ;
}
public boolean isnt(double result, double expected){
return isnt(new Double(result), new Double(expected)) ;
}
public boolean isnt(double result, double expected, String name){
return isnt(new Double(result), new Double(expected), name) ;
}
public boolean like(Object result, String pattern){
return like(result, pattern, null) ;
}
public boolean like(Object result, String pattern, String name){
boolean r = ok(matches(result, pattern), name) ;
if (! r){
diag(" " + result + " doesn't match '" + pattern + "'") ;
}
return r ;
}
public boolean unlike(Object result, String pattern){
return unlike(result, pattern, null) ;
}
public boolean unlike(Object result, String pattern, String name){
boolean r = ok(! matches(result, pattern), name) ;
if (! r){
diag(" " + result + " matches '" + pattern + "'") ;
}
return r ;
}
public boolean isa_ok(Object o, Class c){
return isa_ok(o, c, null) ;
}
public boolean isa_ok(Object o, Class c, String name){
boolean r = false ;
if ((o == null)||(c == null)){
r = false ;
}
else {
r = ok(c.isInstance(o), name) ;
}
if (! r){
diag(" Object isn't a '" + c.getName() + "' it's a '" + o.getClass().getName() + "'") ;
}
return r ;
}
synchronized public void skip(String reason){
skip(reason, 1) ;
}
synchronized public void skip(String reason, int n){
for (int i = 0 ; i < n ; i++){
executed_tests++ ;
out.print("ok " + executed_tests + " # skip " + reason + "\n") ;
out.flush() ;
}
throw new JTapSkipException(reason) ;
}
synchronized public void todo_start(String reason){
if (reason.equals("")){
reason = null ;
}
todo = reason ;
}
synchronized public void todo_end(){
todo = null ;
}
synchronized public boolean diag(String msg){
if (msg != null){
String lines[] = msg.split("\n") ;
StringBuffer buf = new StringBuffer() ;
for (int i = 0 ; i < lines.length ; i++){
buf.append("# " + lines[i] + "\n") ;
}
out.print(buf) ;
out.flush() ;
}
return false ;
}
synchronized private void die(String reason){
err.println(reason) ;
test_died = true ;
exit(255) ;
}
synchronized public void BAIL_OUT(String reason){
out.println("Bail out! " + reason) ;
out.flush() ;
exit(255) ;
}
private int cleanup(){
int rc = 0 ;
if (! plan_set){
diag("Looks like your test died before it could output anything.") ;
return rc ;
}
if (test_died){
diag("Looks like your test died just after " + executed_tests + ".") ;
return rc ;
}
if ((! skip_all)&&(no_plan)){
print_plan(executed_tests) ;
}
if ((! no_plan)&&(expected_tests < executed_tests)) {
diag("Looks like you planned " + expected_tests + " test" + (expected_tests > 1 ? "s" : "") + " but ran "
+ (executed_tests - expected_tests) + " extra.") ;
rc = -1 ;
}
if ((! no_plan)&&(expected_tests > executed_tests)) {
diag("Looks like you planned " + expected_tests + " test" + (expected_tests > 1 ? "s" : "") + " but only ran "
+ executed_tests + ".") ;
}
if (failed_tests > 0){
diag("Looks like you failed " + failed_tests + " test" + (failed_tests > 1 ? "s" : "") + " of " + executed_tests + ".") ;
}
return rc ;
}
synchronized public int exit_status(){
if ((no_plan)||(! plan_set)){
return failed_tests ;
}
if (expected_tests < executed_tests){
return executed_tests - expected_tests ;
}
return failed_tests + (expected_tests - executed_tests) ;
}
synchronized public void exit(){
exit(exit_status()) ;
}
synchronized private void exit(int rc){
int alt_rc = cleanup() ;
if (alt_rc != 0){
rc = alt_rc ;
}
if (exit){
System.exit(rc) ;
}
else {
throw new JTapExitException(rc) ;
}
}
}
class JTapException extends RuntimeException {
JTapException(String msg){
super(msg) ;
}
}
class JTapExitException extends JTapException {
JTapExitException(int rc){
super("exit " + rc) ;
}
}
class JTapSkipException extends JTapException {
JTapSkipException(String reason){
super("skip " + reason) ;
}
}