forked from rakudo/rakudo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Test.pm
255 lines (199 loc) · 6.54 KB
/
Test.pm
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
# Copyright (C) 2007, The Perl Foundation.
# $Id: Test.pm 34904 2009-01-03 23:24:38Z masak $
## This is a temporary Test.pm to get us started until we get pugs's Test.pm
## working. It's shamelessly stolen & adapted from MiniPerl6 in the pugs repo.
# globals to keep track of our tests
our $num_of_tests_run = 0;
our $num_of_tests_failed = 0;
our $todo_upto_test_num = 0;
our $todo_reason = '';
our $num_of_tests_planned;
our $no_plan;
our $die_on_fail;
our $*WARNINGS = 0;
# for running the test suite multiple times in the same process
our $testing_started;
## test functions
# Compare numeric values with approximation
sub approx ($x, $y) {
my $epsilon = 0.00001;
my $diff = abs($x - $y);
($diff < $epsilon);
}
# you can call die_on_fail; to turn it on and die_on_fail(0) to turn it off
sub die_on_fail($fail=1) {
$die_on_fail = $fail;
}
# "plan 'no_plan';" is now "plan *;"
multi sub plan(Whatever $plan) is export() {
$no_plan = 1;
}
multi sub plan($number_of_tests) is export() {
$testing_started = 1;
$num_of_tests_planned = $number_of_tests;
say '1..' ~ $number_of_tests;
}
multi sub pass($desc) is export() {
proclaim(1, $desc);
}
multi sub ok(Object $cond, $desc) is export() {
proclaim($cond, $desc);
}
multi sub ok(Object $cond) is export() { ok($cond, ''); }
multi sub nok(Object $cond, $desc) is export() {
proclaim(!$cond, $desc);
}
multi sub nok(Object $cond) is export() { nok(!$cond, ''); }
multi sub is(Object $got, Object $expected, $desc) is export() {
my $test = $got eq $expected;
proclaim($test, $desc);
}
multi sub is(Object $got, Object $expected) is export() { is($got, $expected, ''); }
multi sub isnt(Object $got, Object $expected, $desc) is export() {
my $test = !($got eq $expected);
proclaim($test, $desc);
}
multi sub isnt(Object $got, Object $expected) is export() { isnt($got, $expected, ''); }
multi sub is_approx(Object $got, Object $expected, $desc) is export() {
my $test = abs($got - $expected) <= 0.00001;
proclaim($test, $desc);
}
multi sub is_approx($got, $expected) is export() { is_approx($got, $expected, ''); }
multi sub todo($reason, $count) is export() {
$todo_upto_test_num = $num_of_tests_run + $count;
$todo_reason = '# TODO ' ~ $reason;
}
multi sub todo($reason) is export() {
$todo_upto_test_num = $num_of_tests_run + 1;
$todo_reason = '# TODO ' ~ $reason;
}
multi sub skip() is export() { proclaim(1, "# SKIP"); }
multi sub skip($reason) is export() { proclaim(1, "# SKIP " ~ $reason); }
multi sub skip($count, $reason) is export() {
for 1..$count {
proclaim(1, "# SKIP " ~ $reason);
}
}
multi sub skip_rest() is export() {
skip($num_of_tests_planned - $num_of_tests_run, "");
}
multi sub skip_rest($reason) is export() {
skip($num_of_tests_planned - $num_of_tests_run, $reason);
}
sub diag($message) is export() { say '# '~$message; }
multi sub flunk($reason) is export() { proclaim(0, "flunk $reason")}
multi sub isa_ok(Object $var,$type) is export() {
ok($var.isa($type), "The object is-a '$type'");
}
multi sub isa_ok(Object $var,$type, $msg) is export() { ok($var.isa($type), $msg); }
multi sub dies_ok(Callable $closure, $reason) is export() {
try {
$closure();
}
proclaim((defined $!), $reason);
}
multi sub dies_ok(Callable $closure) is export() {
dies_ok($closure, '');
}
multi sub lives_ok(Callable $closure, $reason) is export() {
try {
$closure();
}
proclaim((not defined $!), $reason);
}
multi sub lives_ok(Callable $closure) is export() {
lives_ok($closure, '');
}
multi sub eval_dies_ok(Str $code, $reason) is export() {
proclaim((defined eval_exception($code)), $reason);
}
multi sub eval_dies_ok(Str $code) is export() {
eval_dies_ok($code, '');
}
multi sub eval_lives_ok(Str $code, $reason) is export() {
proclaim((not defined eval_exception($code)), $reason);
}
multi sub eval_lives_ok(Str $code) is export() {
eval_lives_ok($code, '');
}
multi sub is_deeply(Object $this, Object $that, $reason) {
my $val = _is_deeply( $this, $that );
proclaim($val, $reason);
}
multi sub is_deeply(Object $this, Object $that) {
my $val = _is_deeply( $this, $that );
proclaim($val, '');
}
sub _is_deeply(Object $this, Object $that) {
if $this ~~ List && $that ~~ List {
return if +$this.values != +$that.values;
for $this Z $that -> $a, $b {
return if ! _is_deeply( $a, $b );
}
return True;
}
elsif $this ~~ Hash && $that ~~ Hash {
return if +$this.keys != +$that.keys;
for $this.keys.sort Z $that.keys.sort -> $a, $b {
return if $a ne $b;
return if ! _is_deeply( $this{$a}, $that{$b} );
}
return True;
}
elsif $this ~~ Str | Num | Int && $that ~~ Str | Num | Int {
return $this eq $that;
}
elsif $this ~~ Pair && $that ~~ Pair {
return $this.key eq $that.key
&& _is_deeply( $this.value, $this.value );
}
elsif $this ~~ undef && $that ~~ undef && $this.WHAT eq $that.WHAT {
return True;
}
return;
}
## 'private' subs
sub eval_exception($code) {
my $eval_exception;
try { eval ($code); $eval_exception = $! }
$eval_exception // $!;
}
sub proclaim($cond, $desc) {
$testing_started = 1;
$num_of_tests_run = $num_of_tests_run + 1;
unless $cond {
print "not ";
$num_of_tests_failed = $num_of_tests_failed + 1
unless $num_of_tests_run <= $todo_upto_test_num;
}
print "ok ", $num_of_tests_run, " - ", $desc;
if $todo_reason and $num_of_tests_run <= $todo_upto_test_num {
print $todo_reason;
}
print "\n";
if !$cond && $die_on_fail && !$todo_reason {
die "Test failed. Stopping test";
}
# must clear this between tests
$todo_reason = '';
}
END {
# until END blocks can access compile-time symbol tables of outer scopes,
# we need these declarations
our $testing_started;
our $num_of_tests_planned;
our $num_of_tests_run;
our $num_of_tests_failed;
our $no_plan;
if $no_plan {
$num_of_tests_planned = $num_of_tests_run;
say "1..$num_of_tests_planned";
}
if ($testing_started and $num_of_tests_planned != $num_of_tests_run) { ##Wrong quantity of tests
diag("Looks like you planned $num_of_tests_planned tests, but ran $num_of_tests_run");
}
if ($testing_started and $num_of_tests_failed) {
diag("Looks like you failed $num_of_tests_failed tests of $num_of_tests_run");
}
}
# vim: ft=perl6