| 1 | # vim: ft=perl6 | ||
| 2 | module Test; | ||
| 3 | my $failed = 0; | ||
| 4 | my $num_of_tests_planned; | ||
| 5 | my $num_of_tests_run = 0; | ||
| 6 | |||
| 7 | sub plan($n) is export { | ||
| 8 | $num_of_tests_planned = $n; | ||
| 9 | say("1..$n"); | ||
| 10 | } | ||
| 11 | |||
| 12 | sub skip($reason, $count=1) is export { | ||
| 13 | my $i = 1; | ||
| 14 | while $i <= $count { proclaim(1, "# SKIP " ~ $reason); $i = $i + 1; } | ||
| 15 | } | ||
| 16 | |||
| 17 | sub skip_rest($reason = '<unknown>') is export { | ||
| 18 | skip($reason, $num_of_tests_planned - $num_of_tests_run); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub proclaim($cond, $desc) { | ||
| 22 | unless ?$cond { | ||
| 23 | print "not "; | ||
| 24 | $failed++; | ||
| 25 | } | ||
| 26 | $num_of_tests_run++; | ||
| 27 | print "ok $num_of_tests_run"; | ||
| 28 | if defined $desc { | ||
| 29 | print " - $desc\n"; | ||
| 30 | } else { | ||
| 31 | print "\n"; | ||
| 32 | } | ||
| 33 | return $cond; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub ok($cond, $desc) is export { | ||
| 37 | proclaim(?$cond, $desc); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub nok($cond, $desc=undef) is export { | ||
| 41 | proclaim(!$cond, $desc); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub is($x,$y, $desc=undef) is export { | ||
| 45 | if (!proclaim($x eq $y, $desc)) { | ||
| 46 | print " GOT: $x\n"; | ||
| 47 | print " EXPECTED: $y\n"; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | sub isnt($x,$y, $desc=undef) is export { | ||
| 52 | if (!proclaim($x ne $y, $desc)) { | ||
| 53 | print " GOT: $x\n"; | ||
| 54 | print " EXPECTED: $y\n"; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | sub isa_ok($x, $y, $desc=undef) is export { | ||
| 59 | proclaim($x.isa($y), $desc); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub lives_ok($closure, $reason='') is export { | ||
| 63 | try { | ||
| 64 | $closure(); | ||
| 65 | }; | ||
| 66 | proclaim((not defined $!), $reason); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub dies_ok($closure, $reason='') is export { | ||
| 70 | try { | ||
| 71 | $closure(); | ||
| 72 | }; | ||
| 73 | proclaim((defined $!), $reason); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub eval_dies_ok($code, $reason=undef) is export { | ||
| 77 | my $ee = eval_exception($code); | ||
| 78 | if defined $ee { | ||
| 79 | proclaim(1, $reason); | ||
| 80 | } else { | ||
| 81 | proclaim(0, $reason); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | sub eval_lives_ok($code, $reason=undef) is export { | ||
| 86 | my $ee = eval_exception($code); | ||
| 87 | if defined $ee { | ||
| 88 | proclaim(0, $reason); | ||
| 89 | } else { | ||
| 90 | proclaim(1, $reason); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | sub force_todo(*@ary) { | ||
| 95 | ... | ||
| 96 | } | ||
| 97 | |||
| 98 | sub eval_exception($code) { | ||
| 99 | try { | ||
| 100 | eval $code; | ||
| 101 | }; | ||
| 102 | $!; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub done() is export { | ||
| 106 | say("1..$num_of_tests_run"); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub pass($reason='passed') is export { | ||
| 110 | proclaim(1, $reason); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub flunk($reason) is export { | ||
| 114 | proclaim(0, "flunk $reason"); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub diag($msg) is export { | ||
| 118 | $*ERR.say("# " ~ $msg); | ||
| 119 | } | ||
| 120 | |||
| 121 | END { | ||
| 122 | exit 1 if $failed > 0; | ||
| 123 | } |