Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 227 lines (195 sloc) 4.419 kB
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
1 use v6;
2
3 use Test;
4 # Test of PRE and POST traits
5 #
4e5f604 [t/spec] fix some smartlinks
moritz authored
6 # L<S04/Phasers/"assert precondition at every block ">
832f178 [t/spec] fix more smartlinks; down to 33 broken links
moritz authored
7 # L<S06/Subroutine traits/PRE/POST>
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
8 #
9 # TODO:
10 # * Multiple inheritance + PRE/POST blocks
11
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
12 plan 25;
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
13
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
14 sub foo(Int $i) {
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
15 PRE {
16 $i < 5
17 }
18 return 1;
19 }
20
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
21 sub bar(Int $i) {
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
22 return 1;
23 POST {
24 $i < 5;
25 }
26 }
27
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
28 lives_ok { foo(2) }, 'sub with PRE compiles and runs';
29 lives_ok { bar(3) }, 'sub with POST compiles and runs';
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
30
77ec25f @coke pugs fudge
coke authored
31 #?pugs todo
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
32 dies_ok { foo(10) }, 'Violated PRE throws (catchable) exception';
33 dies_ok { bar(10) }, 'Violated POST throws (catchable) exception';
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
34
35 # multiple PREs und POSTs
36
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
37 sub baz (Int $i) {
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
38 PRE {
39 $i > 0
40 }
41 PRE {
42 $i < 23
43 }
44 return 1;
45 }
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
46 lives_ok { baz(2) }, 'sub with two PREs compiles and runs';
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
47
77ec25f @coke pugs fudge
coke authored
48 #?pugs todo
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
49 dies_ok { baz(-1)}, 'sub with two PREs fails when first is violated';
77ec25f @coke pugs fudge
coke authored
50 #?pugs todo
dca6a92 [t/spec] fixed syntax error in pre-post.t, caught by STD.pm++
moritz authored
51 dies_ok { baz(42)}, 'sub with two PREs fails when second is violated';
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
52
53
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
54 sub qox (Int $i) {
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
55 return 1;
56 POST {
57 $i > 0
58 }
59 POST {
60 $i < 42
61 }
62 }
63
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
64 lives_ok({ qox(23) }, "sub with two POSTs compiles and runs");
77ec25f @coke pugs fudge
coke authored
65 #?pugs todo
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
66 dies_ok( { qox(-1) }, "sub with two POSTs fails if first POST is violated");
67 dies_ok( { qox(123)}, "sub with two POSTs fails if second POST is violated");
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
68
69 # inheritance
70
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
71 class PRE_Parent {
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
72 method test(Int $i) {
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
73 PRE {
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
74 $i < 23
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
75 }
76 return 1;
77 }
78 }
79
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
80 class PRE_Child is PRE_Parent {
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
81 method test(Int $i){
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
82 PRE {
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
83 $i > 0;
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
84 }
85 return 1;
86 }
87 }
88
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
89 my $foo = PRE_Child.new;
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
90
77ec25f @coke pugs fudge
coke authored
91 #?pugs todo
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
92 lives_ok { $foo.test(5) }, 'PRE in methods compiles and runs';
93 dies_ok { $foo.test(-42) }, 'PRE in child throws';
5f0bdab @coke niecza unfudge
coke authored
94 #?niecza todo 'PRE inheritance'
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
95 dies_ok { $foo.test(78) }, 'PRE in parent throws';
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
96
97
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
98 class POST_Parent {
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
99 method test(Int $i) {
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
100 return 1;
101 POST {
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
102 $i > 23
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
103 }
104 }
105 }
106
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
107 class POST_Child is POST_Parent {
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
108 method test(Int $i){
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
109 return 1;
110 POST {
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
111 $i < -23
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
112 }
113 }
114 }
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
115 my $mp = POST_Child.new;
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
116
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
117 #?niecza 2 skip 'unspecced'
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
118 lives_ok { $mp.test(-42) }, "It's enough if we satisfy one of the POST blocks (Child)";
77ec25f @coke pugs fudge
coke authored
119 #?pugs todo
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
120 lives_ok { $mp.test(42) }, "It's enough if we satisfy one of the POST blocks (Parent)";
e704067 [t/spec] some more variable declaration fixes
moritz authored
121 dies_ok { $mp.test(12) }, 'Violating poth POST blocks throws an error';
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
122
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
123 class Another {
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
124 method test(Int $x) {
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
125 return 3 * $x;
126 POST {
127 $_ > 4
128 }
129 }
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
130 }
131
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
132 my $pt = Another.new;
77ec25f @coke pugs fudge
coke authored
133 #?pugs todo
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correc…
moritz authored
134 lives_ok { $pt.test(2) }, 'POST receives return value as $_ (succeess)';
135 dies_ok { $pt.test(1) }, 'POST receives return value as $_ (failure)';
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
136
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
137 {
138 my $str;
139 {
140 PRE { $str ~= '('; 1 }
141 POST { $str ~= ')'; 1 }
142 $str ~= 'x';
143 }
77ec25f @coke pugs fudge
coke authored
144 #?pugs todo
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
145 is $str, '(x)', 'PRE and POST run on ordinary blocks';
146 }
147
148 {
149 my $str;
150 {
151 POST { $str ~= ')'; 1 }
152 LEAVE { $str ~= ']' }
153 ENTER { $str ~= '[' }
154 PRE { $str ~= '('; 1 }
155 $str ~= 'x';
156 }
77ec25f @coke pugs fudge
coke authored
157 #?pugs todo
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
158 is $str, '([x])', 'PRE/POST run outside ENTER/LEAVE';
159 }
160
161 {
162 my $str;
163 try {
164 {
165 PRE { $str ~= '('; 0 }
166 PRE { $str ~= '*'; 1 }
167 ENTER { $str ~= '[' }
168 $str ~= 'x';
169 LEAVE { $str ~= ']' }
170 POST { $str ~= ')'; 1 }
171 }
172 }
77ec25f @coke pugs fudge
coke authored
173 #?pugs todo
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
174 is $str, '(', 'failing PRE runs nothing else';
175 }
176
5f0bdab @coke niecza unfudge
coke authored
177 #?niecza todo 'I think POST runs LIFO by spec?'
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
178 {
179 my $str;
180 try {
181 {
182 POST { $str ~= 'x'; 0 }
183 LEAVE { $str ~= 'y' }
184 POST { $str ~= 'z'; 1 }
185 }
186 }
77ec25f @coke pugs fudge
coke authored
187 #?pugs todo
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
188 is $str, 'yx', 'failing POST runs LEAVE but not more POSTs';
189 }
190
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
191 #?niecza skip 'unspecced'
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
192 {
193 my $str;
194 try {
195 POST { $str ~= $! // '<undef>'; 1 }
196 die 'foo';
197 }
77ec25f @coke pugs fudge
coke authored
198 #?pugs todo
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
199 ok $str ~~ /foo/, 'POST runs on exception, with correct $!';
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
200 }
201
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
202 #?niecza skip 'unspecced'
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
203 {
204 my $str;
205 try {
fd2754c [t/spec/S04] change ?: to ??!!
lwall authored
206 POST { $str ~= (defined $! ?? 'yes' !! 'no'); 1 }
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
207 try { die 'foo' }
fd2754c [t/spec/S04] change ?: to ??!!
lwall authored
208 $str ~= (defined $! ?? 'aye' !! 'nay');
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
209 }
77ec25f @coke pugs fudge
coke authored
210 #?pugs todo
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
211 is $str, 'ayeno', 'POST has undefined $! on no exception';
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
212 }
213
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
214 #?niecza skip 'unspecced'
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
215 {
216 try {
217 POST { 0 }
218 die 'foo';
219 }
77ec25f @coke pugs fudge
coke authored
220 #?pugs todo
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
221 ok $! ~~ /foo/, 'failing POST on exception doesn\'t replace $!';
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
222 # XXX
223 # is $!.pending.[-1], 'a POST exception', 'does push onto $!.pending';
224 }
225
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
226 # vim: ft=perl6
Something went wrong with that request. Please try again.