37 final private String version =
"1.0" ;
39 private boolean plan_set = false ;
40 private boolean no_plan = false ;
41 private boolean skip_all = false ;
42 private boolean test_died = false ;
43 private int expected_tests = 0 ;
44 private int executed_tests = 0 ;
45 private int failed_tests = 0 ;
46 private boolean exit = true ;
47 private String todo = null ;
49 private PrintStream out = null ;
50 private PrintStream err = null ;
58 public JTap(
boolean really_exit){
69 die(
"You tried to plan twice!") ;
81 die(
"You tried to plan twice!") ;
84 print_plan(0,
"Skip " + reason) ;
97 die(
"You tried to plan twice!") ;
101 die(
"You said to run 0 tests! You've got to run something.") ;
105 expected_tests = tests ;
113 private void print_plan(
int expected){
114 print_plan(expected, null) ;
118 synchronized private void print_plan(
int expected_tests, String directive){
119 out.print(
"1.." + expected_tests) ;
120 if (directive != null){
121 out.print(
" # " + directive) ;
130 public boolean pass(String name){
131 return ok(
true, name) ;
135 public boolean fail(String name){
136 return ok(
false, name) ;
140 public boolean ok(
boolean result){
141 return ok(result, null) ;
149 synchronized public boolean ok(
boolean result, String name){
151 die(
"You tried to run a test without a plan! Gotta have a plan.") ;
157 if (name.matches(
"[\\d\\s]+")){
158 diag(
" You named your test '" + name
159 +
"'. You shouldn't use numbers for your test names.") ;
160 diag(
" Very confusing.") ;
168 out.print(
"ok " + executed_tests) ;
172 out.print(name.replaceAll(
"#",
"\\\\#")) ;
176 out.print(
" # TODO " + todo) ;
185 Throwable t =
new Throwable() ;
186 StackTraceElement stack[] = t.getStackTrace() ;
193 for (
int i = 0 ; i < stack.length ; i++){
194 Class c = Class.forName(stack[i].getClassName()) ;
195 if (!
JTap.class.isAssignableFrom(c)){
197 file = stack[i].getFileName() ;
199 func = stack[i].getMethodName() ;
200 line = stack[i].getLineNumber() ;
206 e.printStackTrace() ;
210 diag(
" Failed " + (todo == null ?
"" :
"(TODO) ") +
"test '" + name +
"'") ;
211 diag(
" in " + file +
":" + func +
"() at line " + line +
".") ;
214 diag(
" Failed " + (todo == null ?
"" :
"(TODO) ") +
"test in " + file +
":" + func +
"() at line " + line +
".") ;
222 private boolean equals(Object result, Object expected){
225 if ((result == null)&&(expected == null)){
228 else if ((result == null)||(expected == null)){
232 r = result.equals(expected) ;
239 private boolean matches(Object result, String pattern){
242 if ((result == null)||(pattern == null)){
246 r = result.toString().matches(pattern) ;
253 private void is_diag(Object result, Object expected){
254 diag(
" got: '" + result +
"'") ;
255 diag(
" expected: '" + expected +
"'") ;
259 public boolean is(Object result, Object expected){
260 return is(result, expected, null) ;
264 public boolean is(Object result, Object expected, String name){
265 boolean r =
ok(equals(result, expected), name) ;
267 is_diag(result, expected) ;
273 public boolean is(
long result,
long expected){
274 return is(
new Long(result),
new Long(expected)) ;
278 public boolean is(
long result,
long expected, String name){
279 return is(
new Long(result),
new Long(expected), name) ;
283 public boolean is(
double result,
double expected){
284 return is(
new Double(result),
new Double(expected)) ;
288 public boolean is(
double result,
double expected, String name){
289 return is(
new Double(result),
new Double(expected), name) ;
293 public boolean isnt(Object result, Object expected){
294 return isnt(result, expected, null) ;
298 public boolean isnt(Object result, Object expected, String name){
299 boolean r =
ok(! equals(result, expected), name) ;
301 is_diag(result, expected) ;
307 public boolean isnt(
long result,
long expected){
308 return isnt(
new Long(result),
new Long(expected)) ;
312 public boolean isnt(
long result,
long expected, String name){
313 return isnt(
new Long(result),
new Long(expected), name) ;
317 public boolean isnt(
double result,
double expected){
318 return isnt(
new Double(result),
new Double(expected)) ;
322 public boolean isnt(
double result,
double expected, String name){
323 return isnt(
new Double(result),
new Double(expected), name) ;
327 public boolean like(Object result, String pattern){
328 return like(result, pattern, null) ;
332 public boolean like(Object result, String pattern, String name){
333 boolean r =
ok(matches(result, pattern), name) ;
335 diag(
" " + result +
" doesn't match '" + pattern +
"'") ;
341 public boolean unlike(Object result, String pattern){
342 return unlike(result, pattern, null) ;
346 public boolean unlike(Object result, String pattern, String name){
347 boolean r =
ok(! matches(result, pattern), name) ;
349 diag(
" " + result +
" matches '" + pattern +
"'") ;
355 public boolean isa_ok(Object o, Class c){
356 return isa_ok(o, c, null) ;
360 public boolean isa_ok(Object o, Class c, String name){
362 if ((o == null)||(c == null)){
366 r =
ok(c.isInstance(o), name) ;
369 diag(
" Object isn't a '" + c.getName() +
"' it's a '" + o.getClass().getName() +
"'") ;
378 synchronized public void skip(String reason){
383 synchronized public void skip(String reason,
int n){
384 for (
int i = 0 ; i < n ; i++){
386 out.print(
"ok " + executed_tests +
" # skip " + reason +
"\n") ;
389 throw new JTapSkipException(reason) ;
394 if (reason.equals(
"")){
408 String lines[] = msg.split(
"\n") ;
409 StringBuffer buf =
new StringBuffer() ;
410 for (
int i = 0 ; i < lines.length ; i++){
411 buf.append(
"# " + lines[i] +
"\n") ;
422 synchronized private void die(String reason){
423 err.println(reason) ;
430 out.println(
"Bail out! " + reason) ;
436 private int cleanup(){
440 diag(
"Looks like your test died before it could output anything.") ;
445 diag(
"Looks like your test died just after " + executed_tests +
".") ;
449 if ((! skip_all)&&(no_plan)){
450 print_plan(executed_tests) ;
453 if ((! no_plan)&&(expected_tests < executed_tests)) {
454 diag(
"Looks like you planned " + expected_tests +
" test" + (expected_tests > 1 ?
"s" :
"") +
" but ran " 455 + (executed_tests - expected_tests) +
" extra.") ;
459 if ((! no_plan)&&(expected_tests > executed_tests)) {
460 diag(
"Looks like you planned " + expected_tests +
" test" + (expected_tests > 1 ?
"s" :
"") +
" but only ran " 461 + executed_tests +
".") ;
464 if (failed_tests > 0){
465 diag(
"Looks like you failed " + failed_tests +
" test" + (failed_tests > 1 ?
"s" :
"") +
" of " + executed_tests +
".") ;
473 if ((no_plan)||(! plan_set)){
474 return failed_tests ;
477 if (expected_tests < executed_tests){
478 return executed_tests - expected_tests ;
481 return failed_tests + (expected_tests - executed_tests) ;
485 synchronized public void exit(){
490 synchronized private void exit(
int rc){
491 int alt_rc = cleanup() ;
499 throw new JTapExitException(rc) ;
507 class JTapException
extends RuntimeException {
508 JTapException(String
msg){
516 class JTapExitException
extends JTapException {
517 JTapExitException(
int rc){
518 super(
"exit " + rc) ;
525 class JTapSkipException
extends JTapException {
526 JTapSkipException(String reason){
527 super(
"skip " + reason) ;
boolean is(Object result, Object expected, String name)
synchronized void todo_end()
boolean isnt(double result, double expected, String name)
boolean isnt(Object result, Object expected, String name)
synchronized void skip(String reason, int n)
boolean like(Object result, String pattern, String name)
synchronized boolean ok(boolean result, String name)
JTap(boolean really_exit)
synchronized int plan_tests(int tests)
boolean is(double result, double expected)
boolean is(long result, long expected)
synchronized void BAIL_OUT(String reason)
boolean unlike(Object result, String pattern, String name)
synchronized int plan_no_plan()
synchronized boolean diag(String msg)
boolean is(long result, long expected, String name)
synchronized void todo_start(String reason)
boolean isa_ok(Object o, Class c, String name)
boolean is(Object result, Object expected)
boolean isa_ok(Object o, Class c)
boolean fail(String name)
synchronized int exit_status()
boolean pass(String name)
boolean isnt(long result, long expected, String name)
boolean isnt(Object result, Object expected)
synchronized int plan_skip_all(String reason)
boolean unlike(Object result, String pattern)
boolean ok(boolean result)
boolean like(Object result, String pattern)
boolean isnt(long result, long expected)
boolean is(double result, double expected, String name)
synchronized void skip(String reason)
boolean isnt(double result, double expected)