Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 176 lines (155 sloc) 5.113 kb
39924ff @sorear Remove lexical package fudge
authored
1 my module Test;
74d317f @sorear Implement source position display on test failure
authored
2
3 constant $?TRANSPARENT = 1;
4
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
5 class Builder {
031dc9f @sorear Implement todo()
authored
6 has $!current-test;
636186e @pmurias [Test.pm6] having both a plan and done_testing only produces one test pl...
pmurias authored
7 has $!set-plan;
031dc9f @sorear Implement todo()
authored
8 has $!todo-up-to;
9 has $!todo-reason;
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
10
11 method new() {
12 $*TEST-BUILDER;
74d317f @sorear Implement source position display on test failure
authored
13 }
64fc764 @sorear Factor out ok and plan into Test.pm6
authored
14
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
15 method blame() {
16 my $frame = caller;
17 while $frame.hints('$?TRANSPARENT') {
18 $frame = $frame.caller;
19 }
02a9152 @sorear Use !method and listop ~ in Test.pm6
authored
20 $frame.file ~ " line " ~ $frame.line;
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
21 }
22
02a9152 @sorear Use !method and listop ~ in Test.pm6
authored
23 method !output($text) {
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
24 say $text;
25 }
26
27 method reset() {
031dc9f @sorear Implement todo()
authored
28 $!current-test = 1;
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
29 }
64fc764 @sorear Factor out ok and plan into Test.pm6
authored
30
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
31 method note($m) {
02a9152 @sorear Use !method and listop ~ in Test.pm6
authored
32 self!output("# " ~ $m);
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
33 0;
34 }
35
36 method ok($bool, $tag) {
37 my $not = $bool ?? "" !! "not ";
b2c363d @pmurias add flunk and pass to Test.pm6 and make the $tag in is and ok optional
pmurias authored
38 my $desc;
39 if $tag {
ac814f7 @coke escape "#" in TAP test descriptions
coke authored
40 $desc = " - " ~ $tag.subst('#', '\#').split("\n").join("\n#");
b2c363d @pmurias add flunk and pass to Test.pm6 and make the $tag in is and ok optional
pmurias authored
41 } else {
42 $desc = '';
43 }
031dc9f @sorear Implement todo()
authored
44 if $!todo-up-to >= $!current-test {
45 $desc ~= " # TODO $!todo-reason";
46 }
47 self!output($not ~ "ok " ~ $!current-test++ ~ $desc);
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
48 if !$bool { self.note(self.blame); }
49 }
50
031dc9f @sorear Implement todo()
authored
51 method todo($reason, $count) {
52 $!todo-reason = $reason;
53 $!todo-up-to = $!current-test + $count - 1; # todo(1) should stop after cur
811d5d9 @sorear Add a few more test functions, test files
authored
54 }
031dc9f @sorear Implement todo()
authored
55
811d5d9 @sorear Add a few more test functions, test files
authored
56 method skip($reason) {
031dc9f @sorear Implement todo()
authored
57 self!output("ok {$!current-test++} # SKIP $reason");
811d5d9 @sorear Add a few more test functions, test files
authored
58 }
59
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
60 method expected-tests($num) {
02a9152 @sorear Use !method and listop ~ in Test.pm6
authored
61 self!output("1.." ~ $num);
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
62 }
616bc42 @sorear Add plan * and done_testing to Test.pm6
authored
63
64 method plan($x) {
636186e @pmurias [Test.pm6] having both a plan and done_testing only produces one test pl...
pmurias authored
65 $!set-plan = 1;
9788237 @sorear Secondary fixes revealed by (regression testing)++
authored
66 if $x ~~ Cool {
67 self.expected-tests(+$x);
616bc42 @sorear Add plan * and done_testing to Test.pm6
authored
68 } else {
69 die "Invalid argument to plan";
70 }
71 }
72
025e408 @pmurias [Test.pm6] remove &done-testing and &done_testing
pmurias authored
73 method done {
636186e @pmurias [Test.pm6] having both a plan and done_testing only produces one test pl...
pmurias authored
74 if !($!set-plan) {
031dc9f @sorear Implement todo()
authored
75 self!output("1.." ~ ($!current-test - 1));
636186e @pmurias [Test.pm6] having both a plan and done_testing only produces one test pl...
pmurias authored
76 }
616bc42 @sorear Add plan * and done_testing to Test.pm6
authored
77 }
64fc764 @sorear Factor out ok and plan into Test.pm6
authored
78 }
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
79
11e843d @sorear CREATE gets proper P6 semantics, add bless()
authored
80 $GLOBAL::TEST-BUILDER = Builder.bless(*);
f506534 @sorear Refactor Test.pm6 to use an inner Builder
authored
81 $GLOBAL::TEST-BUILDER.reset;
82
a73f026 @sorear Fix parsing $::(), ignore "is dynamic" on variables
authored
83 sub cmp_ok(\$a, $fn, \$b, $tag?) is export { ok($fn($a, $b), $tag); }
811d5d9 @sorear Add a few more test functions, test files
authored
84 sub ok(\$bool, $tag?) is export { $*TEST-BUILDER.ok(?$bool, $tag) }
85 sub nok(\$bool, $tag?) is export { $*TEST-BUILDER.ok(!$bool, $tag) }
4024e22 @sorear Add a bunch of binding tests to spectest.data
authored
86 sub skip_rest($tag?) is export { } #OK
d22c6cb @sorear Tweaks to Test to handle junctions better, fake &WHAT and &HOW
authored
87 sub pass($tag?) is export { $*TEST-BUILDER.ok(1, $tag); True }
b2c363d @pmurias add flunk and pass to Test.pm6 and make the $tag in is and ok optional
pmurias authored
88 sub flunk($tag?) is export { $*TEST-BUILDER.ok(0, $tag) }
d22c6cb @sorear Tweaks to Test to handle junctions better, fake &WHAT and &HOW
authored
89 sub isa_ok(Mu $obj, Mu $type, $tag?) is export { $*TEST-BUILDER.ok($obj.^isa($type), $tag) }
93cb7df @sorear Initial prototype (Perl6) implementation of hyperops
authored
90 sub is_deeply($a,$b,$c) is export { is $a.perl, $b.perl, $c }
f727731 @sorear Mark more setting functions as Nil-transparent, and implement Parcel/Lis...
authored
91 sub is(\$got, \$expected, $tag?) is export {
f828727 @pmurias [Test.pm6] is prints out the incorrect value on failure
pmurias authored
92
93 # avoid comparing twice
d22c6cb @sorear Tweaks to Test to handle junctions better, fake &WHAT and &HOW
authored
94 my $equal = (~$got) eq (~$expected);
f828727 @pmurias [Test.pm6] is prints out the incorrect value on failure
pmurias authored
95
96 $*TEST-BUILDER.ok($equal, $tag);
97 if !$equal {
98 $*TEST-BUILDER.note(' Failed test');
d22c6cb @sorear Tweaks to Test to handle junctions better, fake &WHAT and &HOW
authored
99 $*TEST-BUILDER.note(' got: ' ~ ~$got);
100 $*TEST-BUILDER.note(' expected: ' ~ ~$expected);
f828727 @pmurias [Test.pm6] is prints out the incorrect value on failure
pmurias authored
101 }
102 }
d22c6cb @sorear Tweaks to Test to handle junctions better, fake &WHAT and &HOW
authored
103 sub isnt(Mu $got, Mu $expected, $tag?) is export { $*TEST-BUILDER.ok($got ne $expected, $tag) }
252c6a5 @sorear Implement {,eval_}{succeed,fail}s_ok per flussence++
authored
104 # Runs $code, trapping various failure modes and returning applicable.
65df472 @sorear Reimplement lives_ok and friends using CATCH/CONTROL. Fix exposed bugs.
authored
105 sub no-control($code) {
252c6a5 @sorear Implement {,eval_}{succeed,fail}s_ok per flussence++
authored
106 my ($died, $warned);
65df472 @sorear Reimplement lives_ok and friends using CATCH/CONTROL. Fix exposed bugs.
authored
107 {
252c6a5 @sorear Implement {,eval_}{succeed,fail}s_ok per flussence++
authored
108 CATCH { default { $died = True } }
109 CONTROL {
110 if .[0] == 11 {
111 $warned = True;
112 return; # NIECZA - causes &warn to return
113 }
114 when .[0] != 11 { $died = True } # exits block
115 }
65df472 @sorear Reimplement lives_ok and friends using CATCH/CONTROL. Fix exposed bugs.
authored
116 $code.();
117 }
252c6a5 @sorear Implement {,eval_}{succeed,fail}s_ok per flussence++
authored
118 $died ?? "die" !! $warned ?? "warn" !! "";
65df472 @sorear Reimplement lives_ok and friends using CATCH/CONTROL. Fix exposed bugs.
authored
119 }
811d5d9 @sorear Add a few more test functions, test files
authored
120 sub lives_ok($code,$why?) is export {
252c6a5 @sorear Implement {,eval_}{succeed,fail}s_ok per flussence++
authored
121 $*TEST-BUILDER.ok(no-control($code) ne "die", $why);
811d5d9 @sorear Add a few more test functions, test files
authored
122 }
123 sub dies_ok($code,$why?) is export {
252c6a5 @sorear Implement {,eval_}{succeed,fail}s_ok per flussence++
authored
124 $*TEST-BUILDER.ok(no-control($code) eq "die", $why);
125 }
126 sub succeeds_ok($code,$why?,:$ignore = ()) is export {
127 $*TEST-BUILDER.ok(?(no-control($code) eq any("", @$ignore)), $why);
128 }
129 sub fails_ok($code,$why?,:$expect = <die warn fail>) is export {
130 $*TEST-BUILDER.ok(?(no-control($code) eq any(@$expect)), $why);
131 }
132 sub eval_lives_ok($code,$why?) is export {
133 $*TEST-BUILDER.ok(no-control({ eval $code }) ne "die", $why);
134 }
135 sub eval_dies_ok($code,$why?) is export {
136 $*TEST-BUILDER.ok(no-control({ eval $code }) eq "die", $why);
e9691cc @sorear xx and Whatever/Array, fudging eval_*_ok to choke on control exceptions
authored
137 }
252c6a5 @sorear Implement {,eval_}{succeed,fail}s_ok per flussence++
authored
138 sub eval_succeeds_ok($code,$why?,:$ignore = ()) is export {
139 $*TEST-BUILDER.ok(?(no-control({ eval $code }) eq any("", @$ignore)), $why);
980923f @sorear Implement eval_dies_ok, eval_lives_ok
authored
140 }
252c6a5 @sorear Implement {,eval_}{succeed,fail}s_ok per flussence++
authored
141 sub eval_fails_ok($code,$why?,:$expect = <die warn fail>) is export {
142 $*TEST-BUILDER.ok(?(no-control({ eval $code }) eq any(@$expect)), $why);
980923f @sorear Implement eval_dies_ok, eval_lives_ok
authored
143 }
4024e22 @sorear Add a bunch of binding tests to spectest.data
authored
144 sub diag($str) is export { $*TEST-BUILDER.note($str) }
49b11af @sorear Add $*ARGFILES, -n and -p option handling
authored
145 sub is_approx(Mu $got, Mu $expected, $desc = '') is export {
146 my $test = ($got - $expected).abs <= 1/100000;
147 $*TEST-BUILDER.ok(?$test, $desc);
148 unless $test {
149 $*TEST-BUILDER.note("got: $got");
150 $*TEST-BUILDER.note("expected: $expected");
151 }
152 ?$test;
153 }
031dc9f @sorear Implement todo()
authored
154 sub todo($reason, $count = 1) is export { $*TEST-BUILDER.todo($reason, $count) }
616bc42 @sorear Add plan * and done_testing to Test.pm6
authored
155 sub plan($num) is export { $*TEST-BUILDER.plan($num) }
9f058da @sorear [v6] Add a few unit tests for CgOp
authored
156 sub done() is export { $*TEST-BUILDER.done }
0711b24 @sorear Track &skip API change
authored
157 sub skip($reason,$number) is export {
811d5d9 @sorear Add a few more test functions, test files
authored
158 $*TEST-BUILDER.skip($reason) for ^$number;
75090a8 @pmurias added skip to Test
pmurias authored
159 }
a8f84cb @sorear Implement <before> and family
authored
160
161 # TODO standardize me
162 sub rxtest($rgx, $rgxname, @y, @n) is export {
163 for @y {
164 my $k = $_ ~~ Pair ?? $_.key !! $_;
165 my $v = $_ ~~ Pair ?? $_.value !! $_;
166 ok $k ~~ $rgx, "$rgxname ~~ $v";
167 }
168 for @n {
169 my $k = $_ ~~ Pair ?? $_.key !! $_;
170 my $v = $_ ~~ Pair ?? $_.value !! $_;
171 ok !($k ~~ $rgx), "$rgxname !~~ $v";
172 }
173 }
174
175
Something went wrong with that request. Please try again.