Skip to content

HTTPS clone URL

Subversion checkout URL

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