Skip to content

HTTPS clone URL

Subversion checkout URL

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