Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 172 lines (147 sloc) 3.531 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
2023f06 @moritz test blockless PRE and POST
moritz authored
9 plan 22;
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
10
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
11 sub foo(Int $i) {
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
12 PRE {
13 $i < 5
14 }
15 return 1;
16 }
17
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correctio...
moritz authored
18 sub bar(Int $i) {
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
19 return 1;
20 POST {
21 $i < 5;
22 }
23 }
24
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correctio...
moritz authored
25 lives_ok { foo(2) }, 'sub with PRE compiles and runs';
26 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
27
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correctio...
moritz authored
28 dies_ok { foo(10) }, 'Violated PRE throws (catchable) exception';
29 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
30
31 # multiple PREs und POSTs
32
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
33 sub baz (Int $i) {
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
34 PRE {
35 $i > 0
36 }
37 PRE {
38 $i < 23
39 }
40 return 1;
41 }
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correctio...
moritz authored
42 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
43
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correctio...
moritz authored
44 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
45 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
46
47
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
48 sub qox (Int $i) {
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
49 return 1;
50 POST {
51 $i > 0
52 }
53 POST {
54 $i < 42
55 }
56 }
57
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correctio...
moritz authored
58 lives_ok({ qox(23) }, "sub with two POSTs compiles and runs");
59 dies_ok( { qox(-1) }, "sub with two POSTs fails if first POST is violated");
60 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
61
62
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correctio...
moritz authored
63 class Another {
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
64 method test(Int $x) {
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correctio...
moritz authored
65 return 3 * $x;
66 POST {
67 $_ > 4
68 }
69 }
72237dd [t] one more closure_traits move, a bit more TODO (also added to TASKS)
moritz authored
70 }
71
e7ca07b [t] move some tests to spec/, re-worked pre-post.t, some minor correctio...
moritz authored
72 my $pt = Another.new;
73 lives_ok { $pt.test(2) }, 'POST receives return value as $_ (succeess)';
74 dies_ok { $pt.test(1) }, 'POST receives return value as $_ (failure)';
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
75
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
76 {
77 my $str;
78 {
79 PRE { $str ~= '('; 1 }
80 POST { $str ~= ')'; 1 }
81 $str ~= 'x';
82 }
83 is $str, '(x)', 'PRE and POST run on ordinary blocks';
84 }
85
86 {
87 my $str;
88 {
89 POST { $str ~= ')'; 1 }
90 LEAVE { $str ~= ']' }
91 ENTER { $str ~= '[' }
92 PRE { $str ~= '('; 1 }
93 $str ~= 'x';
94 }
95 is $str, '([x])', 'PRE/POST run outside ENTER/LEAVE';
96 }
97
98 {
99 my $str;
100 try {
101 {
102 PRE { $str ~= '('; 0 }
103 PRE { $str ~= '*'; 1 }
104 ENTER { $str ~= '[' }
105 $str ~= 'x';
106 LEAVE { $str ~= ']' }
107 POST { $str ~= ')'; 1 }
108 }
109 }
25668e6 @coke Prefer RT #XXXXXX
coke authored
110 #?rakudo.moar todo 'failing PRE block should not run any other phasers, RT #121531'
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
111 is $str, '(', 'failing PRE runs nothing else';
112 }
113
114 {
115 my $str;
116 try {
117 {
eb002d6 @jnthn Correct and unfudge a test; POST runs LIFO.
jnthn authored
118 POST { $str ~= 'z'; 1 }
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
119 POST { $str ~= 'x'; 0 }
120 LEAVE { $str ~= 'y' }
121 }
122 }
123 is $str, 'yx', 'failing POST runs LEAVE but not more POSTs';
124 }
125
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
126 #?niecza skip 'unspecced'
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
127 {
128 my $str;
129 try {
130 POST { $str ~= $! // '<undef>'; 1 }
131 die 'foo';
132 }
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
133 ok $str ~~ /foo/, 'POST runs on exception, with correct $!';
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
134 }
135
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
136 #?niecza skip 'unspecced'
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
137 #?rakudo todo 'POST and exceptions RT #124961'
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
138 {
139 my $str;
140 try {
fd2754c [t/spec/S04] change ?: to ??!!
lwall authored
141 POST { $str ~= (defined $! ?? 'yes' !! 'no'); 1 }
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
142 try { die 'foo' }
fd2754c [t/spec/S04] change ?: to ??!!
lwall authored
143 $str ~= (defined $! ?? 'aye' !! 'nay');
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
144 }
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
145 is $str, 'ayeno', 'POST has undefined $! on no exception';
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
146 }
147
7d8245f @sorear [S04-phasers] Fudge pre-post, enter-leave, and keep-undo for niecza
sorear authored
148 #?niecza skip 'unspecced'
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored
149 #?rakudo skip "POST and exceptions RT #124962"
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
150 {
151 try {
152 POST { 0 }
153 die 'foo';
154 }
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
155 ok $! ~~ /foo/, 'failing POST on exception doesn\'t replace $!';
59ffe4c [t/spec] Tests for r28005 (closure traits and exceptions).
benmorrow authored
156 # XXX
157 # is $!.pending.[-1], 'a POST exception', 'does push onto $!.pending';
158 }
159
2023f06 @moritz test blockless PRE and POST
moritz authored
160 {
161 my sub blockless($x) {
162 PRE $x >= 0;
163 POST $_ == 4;
164 return $x;
165 }
166 lives_ok { blockless(4) }, 'blockless PRE/POST (+)';
167 dies_ok { blockless -4 }, 'blockless PRE/POST (-, 1)';
168 dies_ok { blockless 14 }, 'blockless PRE/POST (-, 2)';
169 }
170
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
171 # vim: ft=perl6
Something went wrong with that request. Please try again.