This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #46947] Parse method-BLOCK arguments as a term
[perl5.git] / ext / B / t / optree_specials.t
1 #!./perl
2
3 # This tests the B:: module(s) with CHECK, BEGIN, END and INIT blocks. The
4 # text excerpts below marked with "# " in front are the expected output. They
5 # are there twice, EOT for threading, and EONT for a non-threading Perl. The
6 # output is matched losely. If the match fails even though the "got" and
7 # "expected" output look exactly the same, then watch for trailing, invisible
8 # spaces.
9
10 BEGIN {
11     unshift @INC, 't';
12     require Config;
13     if (($Config::Config{'extensions'} !~ /\bB\b/) ){
14         print "1..0 # Skip -- Perl configured without B module\n";
15         exit 0;
16     }
17 }
18
19 # import checkOptree(), and %gOpts (containing test state)
20 use OptreeCheck;        # ALSO DOES @ARGV HANDLING !!!!!!
21 use Config;
22
23 plan tests => 15;
24
25 require_ok("B::Concise");
26
27 my $out = runperl(
28     switches => ["-MO=Concise,BEGIN,CHECK,INIT,END,-exec"],
29     prog => q{$a=$b && print q/foo/},
30     stderr => 1 );
31
32 #print "out:$out\n";
33
34 my $src = q[our ($beg, $chk, $init, $end, $uc) = qq{'foo'}; BEGIN { $beg++ } CHECK { $chk++ } INIT { $init++ } END { $end++ } UNITCHECK {$uc++}];
35
36
37 checkOptree ( name      => 'BEGIN',
38               bcopts    => 'BEGIN',
39               prog      => $src,
40               strip_open_hints => 1,
41               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
42 # BEGIN 1:
43 # a  <1> leavesub[1 ref] K/REFC,1 ->(end)
44 # -     <@> lineseq KP ->a
45 # 1        <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$ ->2
46 # 3        <1> require sK/1 ->4
47 # 2           <$> const[PV "strict.pm"] s/BARE ->3
48 # 4        <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$ ->5
49 # -        <@> lineseq K ->-
50 # -           <0> null ->5
51 # 9           <1> entersub[t1] KS*/TARG,2 ->a
52 # 5              <0> pushmark s ->6
53 # 6              <$> const[PV "strict"] sM ->7
54 # 7              <$> const[PV "refs"] sM ->8
55 # 8              <$> method_named[PV "unimport"] ->9
56 # BEGIN 2:
57 # k  <1> leavesub[1 ref] K/REFC,1 ->(end)
58 # -     <@> lineseq K ->k
59 # b        <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$ ->c
60 # d        <1> require sK/1 ->e
61 # c           <$> const[PV "strict.pm"] s/BARE ->d
62 # e        <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$ ->f
63 # -        <@> lineseq K ->-
64 # -           <0> null ->f
65 # j           <1> entersub[t1] KS*/TARG,2 ->k
66 # f              <0> pushmark s ->g
67 # g              <$> const[PV "strict"] sM ->h
68 # h              <$> const[PV "refs"] sM ->i
69 # i              <$> method_named[PV "unimport"] ->j
70 # BEGIN 3:
71 # u  <1> leavesub[1 ref] K/REFC,1 ->(end)
72 # -     <@> lineseq KP ->u
73 # l        <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$ ->m
74 # n        <1> require sK/1 ->o
75 # m           <$> const[PV "warnings.pm"] s/BARE ->n
76 # o        <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$ ->p
77 # -        <@> lineseq K ->-
78 # -           <0> null ->p
79 # t           <1> entersub[t1] KS*/TARG,2 ->u
80 # p              <0> pushmark s ->q
81 # q              <$> const[PV "warnings"] sM ->r
82 # r              <$> const[PV "qw"] sM ->s
83 # s              <$> method_named[PV "unimport"] ->t
84 # BEGIN 4:
85 # y  <1> leavesub[1 ref] K/REFC,1 ->(end)
86 # -     <@> lineseq KP ->y
87 # v        <;> nextstate(main 2 -e:1) v:>,<,%,{ ->w
88 # x        <1> postinc[t3] sK/1 ->y
89 # -           <1> ex-rv2sv sKRM/1 ->x
90 # w              <#> gvsv[*beg] s ->x
91 EOT_EOT
92 # BEGIN 1:
93 # a  <1> leavesub[1 ref] K/REFC,1 ->(end)
94 # -     <@> lineseq KP ->a
95 # 1        <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$ ->2
96 # 3        <1> require sK/1 ->4
97 # 2           <$> const(PV "strict.pm") s/BARE ->3
98 # 4        <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$ ->5
99 # -        <@> lineseq K ->-
100 # -           <0> null ->5
101 # 9           <1> entersub[t1] KS*/TARG,2 ->a
102 # 5              <0> pushmark s ->6
103 # 6              <$> const(PV "strict") sM ->7
104 # 7              <$> const(PV "refs") sM ->8
105 # 8              <$> method_named(PV "unimport") ->9
106 # BEGIN 2:
107 # k  <1> leavesub[1 ref] K/REFC,1 ->(end)
108 # -     <@> lineseq K ->k
109 # b        <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$ ->c
110 # d        <1> require sK/1 ->e
111 # c           <$> const(PV "strict.pm") s/BARE ->d
112 # e        <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$ ->f
113 # -        <@> lineseq K ->-
114 # -           <0> null ->f
115 # j           <1> entersub[t1] KS*/TARG,2 ->k
116 # f              <0> pushmark s ->g
117 # g              <$> const(PV "strict") sM ->h
118 # h              <$> const(PV "refs") sM ->i
119 # i              <$> method_named(PV "unimport") ->j
120 # BEGIN 3:
121 # u  <1> leavesub[1 ref] K/REFC,1 ->(end)
122 # -     <@> lineseq KP ->u
123 # l        <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$ ->m
124 # n        <1> require sK/1 ->o
125 # m           <$> const(PV "warnings.pm") s/BARE ->n
126 # o        <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$ ->p
127 # -        <@> lineseq K ->-
128 # -           <0> null ->p
129 # t           <1> entersub[t1] KS*/TARG,2 ->u
130 # p              <0> pushmark s ->q
131 # q              <$> const(PV "warnings") sM ->r
132 # r              <$> const(PV "qw") sM ->s
133 # s              <$> method_named(PV "unimport") ->t
134 # BEGIN 4:
135 # y  <1> leavesub[1 ref] K/REFC,1 ->(end)
136 # -     <@> lineseq KP ->y
137 # v        <;> nextstate(main 2 -e:1) v:>,<,%,{ ->w
138 # x        <1> postinc[t2] sK/1 ->y
139 # -           <1> ex-rv2sv sKRM/1 ->x
140 # w              <$> gvsv(*beg) s ->x
141 EONT_EONT
142
143
144 checkOptree ( name      => 'END',
145               bcopts    => 'END',
146               prog      => $src,
147               strip_open_hints => 1,
148               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
149 # END 1:
150 # 4  <1> leavesub[1 ref] K/REFC,1 ->(end)
151 # -     <@> lineseq KP ->4
152 # 1        <;> nextstate(main 5 -e:6) v:>,<,%,{ ->2
153 # 3        <1> postinc[t3] sK/1 ->4
154 # -           <1> ex-rv2sv sKRM/1 ->3
155 # 2              <#> gvsv[*end] s ->3
156 EOT_EOT
157 # END 1:
158 # 4  <1> leavesub[1 ref] K/REFC,1 ->(end)
159 # -     <@> lineseq KP ->4
160 # 1        <;> nextstate(main 5 -e:6) v:>,<,%,{ ->2
161 # 3        <1> postinc[t2] sK/1 ->4
162 # -           <1> ex-rv2sv sKRM/1 ->3
163 # 2              <$> gvsv(*end) s ->3
164 EONT_EONT
165
166
167 checkOptree ( name      => 'CHECK',
168               bcopts    => 'CHECK',
169               prog      => $src,
170               strip_open_hints => 1,
171               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
172 # CHECK 1:
173 # 4  <1> leavesub[1 ref] K/REFC,1 ->(end)
174 # -     <@> lineseq KP ->4
175 # 1        <;> nextstate(main 3 -e:4) v:>,<,%,{ ->2
176 # 3        <1> postinc[t3] sK/1 ->4
177 # -           <1> ex-rv2sv sKRM/1 ->3
178 # 2              <#> gvsv[*chk] s ->3
179 EOT_EOT
180 # CHECK 1:
181 # 4  <1> leavesub[1 ref] K/REFC,1 ->(end)
182 # -     <@> lineseq KP ->4
183 # 1        <;> nextstate(main 3 -e:4) v:>,<,%,{ ->2
184 # 3        <1> postinc[t2] sK/1 ->4
185 # -           <1> ex-rv2sv sKRM/1 ->3
186 # 2              <$> gvsv(*chk) s ->3
187 EONT_EONT
188
189 checkOptree ( name      => 'UNITCHECK',
190               bcopts=> 'UNITCHECK',
191               prog      => $src,
192               strip_open_hints => 1,
193               expect=> <<'EOT_EOT', expect_nt => <<'EONT_EONT');
194 # UNITCHECK 1:
195 # 4  <1> leavesub[1 ref] K/REFC,1 ->(end)
196 # -     <@> lineseq KP ->4
197 # 1        <;> nextstate(main 3 -e:4) v:>,<,%,{ ->2
198 # 3        <1> postinc[t3] sK/1 ->4
199 # -           <1> ex-rv2sv sKRM/1 ->3
200 # 2              <#> gvsv[*uc] s ->3
201 EOT_EOT
202 # UNITCHECK 1:
203 # 4  <1> leavesub[1 ref] K/REFC,1 ->(end)
204 # -     <@> lineseq KP ->4
205 # 1        <;> nextstate(main 3 -e:4) v:>,<,%,{ ->2
206 # 3        <1> postinc[t2] sK/1 ->4
207 # -           <1> ex-rv2sv sKRM/1 ->3
208 # 2              <$> gvsv(*uc) s ->3
209 EONT_EONT
210
211 checkOptree ( name      => 'INIT',
212               bcopts    => 'INIT',
213               #todo     => 'get working',
214               prog      => $src,
215               strip_open_hints => 1,
216               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
217 # INIT 1:
218 # 4  <1> leavesub[1 ref] K/REFC,1 ->(end)
219 # -     <@> lineseq KP ->4
220 # 1        <;> nextstate(main 4 -e:5) v:>,<,%,{ ->2
221 # 3        <1> postinc[t3] sK/1 ->4
222 # -           <1> ex-rv2sv sKRM/1 ->3
223 # 2              <#> gvsv[*init] s ->3
224 EOT_EOT
225 # INIT 1:
226 # 4  <1> leavesub[1 ref] K/REFC,1 ->(end)
227 # -     <@> lineseq KP ->4
228 # 1        <;> nextstate(main 4 -e:5) v:>,<,%,{ ->2
229 # 3        <1> postinc[t2] sK/1 ->4
230 # -           <1> ex-rv2sv sKRM/1 ->3
231 # 2              <$> gvsv(*init) s ->3
232 EONT_EONT
233
234
235 checkOptree ( name      => 'all of BEGIN END INIT CHECK UNITCHECK -exec',
236               bcopts    => [qw/ BEGIN END INIT CHECK UNITCHECK -exec /],
237               prog      => $src,
238               strip_open_hints => 1,
239               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
240 # BEGIN 1:
241 # 1  <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$
242 # 2  <$> const[PV "strict.pm"] s/BARE
243 # 3  <1> require sK/1
244 # 4  <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$
245 # 5  <0> pushmark s
246 # 6  <$> const[PV "strict"] sM
247 # 7  <$> const[PV "refs"] sM
248 # 8  <$> method_named[PV "unimport"] 
249 # 9  <1> entersub[t1] KS*/TARG,2
250 # a  <1> leavesub[1 ref] K/REFC,1
251 # BEGIN 2:
252 # b  <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$
253 # c  <$> const[PV "strict.pm"] s/BARE
254 # d  <1> require sK/1
255 # e  <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$
256 # f  <0> pushmark s
257 # g  <$> const[PV "strict"] sM
258 # h  <$> const[PV "refs"] sM
259 # i  <$> method_named[PV "unimport"] 
260 # j  <1> entersub[t1] KS*/TARG,2
261 # k  <1> leavesub[1 ref] K/REFC,1
262 # BEGIN 3:
263 # l  <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$
264 # m  <$> const[PV "warnings.pm"] s/BARE
265 # n  <1> require sK/1
266 # o  <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$
267 # p  <0> pushmark s
268 # q  <$> const[PV "warnings"] sM
269 # r  <$> const[PV "qw"] sM
270 # s  <$> method_named[PV "unimport"] 
271 # t  <1> entersub[t1] KS*/TARG,2
272 # u  <1> leavesub[1 ref] K/REFC,1
273 # BEGIN 4:
274 # v  <;> nextstate(main 2 -e:1) v:>,<,%,{
275 # w  <#> gvsv[*beg] s
276 # x  <1> postinc[t3] sK/1
277 # y  <1> leavesub[1 ref] K/REFC,1
278 # END 1:
279 # z  <;> nextstate(main 5 -e:1) v:>,<,%,{
280 # 10 <#> gvsv[*end] s
281 # 11 <1> postinc[t3] sK/1
282 # 12 <1> leavesub[1 ref] K/REFC,1
283 # INIT 1:
284 # 13 <;> nextstate(main 4 -e:1) v:>,<,%,{
285 # 14 <#> gvsv[*init] s
286 # 15 <1> postinc[t3] sK/1
287 # 16 <1> leavesub[1 ref] K/REFC,1
288 # CHECK 1:
289 # 17 <;> nextstate(main 3 -e:1) v:>,<,%,{
290 # 18 <#> gvsv[*chk] s
291 # 19 <1> postinc[t3] sK/1
292 # 1a <1> leavesub[1 ref] K/REFC,1
293 # UNITCHECK 1:
294 # 1b <;> nextstate(main 6 -e:1) v:>,<,%,{
295 # 1c <#> gvsv[*uc] s
296 # 1d <1> postinc[t3] sK/1
297 # 1e <1> leavesub[1 ref] K/REFC,1
298 EOT_EOT
299 # BEGIN 1:
300 # 1  <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$
301 # 2  <$> const(PV "strict.pm") s/BARE
302 # 3  <1> require sK/1
303 # 4  <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$
304 # 5  <0> pushmark s
305 # 6  <$> const(PV "strict") sM
306 # 7  <$> const(PV "refs") sM
307 # 8  <$> method_named(PV "unimport") 
308 # 9  <1> entersub[t1] KS*/TARG,2
309 # a  <1> leavesub[1 ref] K/REFC,1
310 # BEGIN 2:
311 # b  <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$
312 # c  <$> const(PV "strict.pm") s/BARE
313 # d  <1> require sK/1
314 # e  <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$
315 # f  <0> pushmark s
316 # g  <$> const(PV "strict") sM
317 # h  <$> const(PV "refs") sM
318 # i  <$> method_named(PV "unimport") 
319 # j  <1> entersub[t1] KS*/TARG,2
320 # k  <1> leavesub[1 ref] K/REFC,1
321 # BEGIN 3:
322 # l  <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$
323 # m  <$> const(PV "warnings.pm") s/BARE
324 # n  <1> require sK/1
325 # o  <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$
326 # p  <0> pushmark s
327 # q  <$> const(PV "warnings") sM
328 # r  <$> const(PV "qw") sM
329 # s  <$> method_named(PV "unimport") 
330 # t  <1> entersub[t1] KS*/TARG,2
331 # u  <1> leavesub[1 ref] K/REFC,1
332 # BEGIN 4:
333 # v  <;> nextstate(main 2 -e:1) v:>,<,%,{
334 # w  <$> gvsv(*beg) s
335 # x  <1> postinc[t2] sK/1
336 # y  <1> leavesub[1 ref] K/REFC,1
337 # END 1:
338 # z  <;> nextstate(main 5 -e:1) v:>,<,%,{
339 # 10 <$> gvsv(*end) s
340 # 11 <1> postinc[t2] sK/1
341 # 12 <1> leavesub[1 ref] K/REFC,1
342 # INIT 1:
343 # 13 <;> nextstate(main 4 -e:1) v:>,<,%,{
344 # 14 <$> gvsv(*init) s
345 # 15 <1> postinc[t2] sK/1
346 # 16 <1> leavesub[1 ref] K/REFC,1
347 # CHECK 1:
348 # 17 <;> nextstate(main 3 -e:1) v:>,<,%,{
349 # 18 <$> gvsv(*chk) s
350 # 19 <1> postinc[t2] sK/1
351 # 1a <1> leavesub[1 ref] K/REFC,1
352 # UNITCHECK 1:
353 # 1b <;> nextstate(main 6 -e:1) v:>,<,%,{
354 # 1c <$> gvsv(*uc) s
355 # 1d <1> postinc[t2] sK/1
356 # 1e <1> leavesub[1 ref] K/REFC,1
357 EONT_EONT
358
359
360 # perl "-I../lib" -MO=Concise,BEGIN,CHECK,INIT,END,-exec -e '$a=$b && print q/foo/'
361
362
363
364 checkOptree ( name      => 'regression test for patch 25352',
365               bcopts    => [qw/ BEGIN END INIT CHECK -exec /],
366               prog      => 'print q/foo/',
367               expect    => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
368 # BEGIN 1:
369 # 1  <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$
370 # 2  <$> const[PV "strict.pm"] s/BARE
371 # 3  <1> require sK/1
372 # 4  <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$
373 # 5  <0> pushmark s
374 # 6  <$> const[PV "strict"] sM
375 # 7  <$> const[PV "refs"] sM
376 # 8  <$> method_named[PV "unimport"] 
377 # 9  <1> entersub[t1] KS*/TARG,2
378 # a  <1> leavesub[1 ref] K/REFC,1
379 # BEGIN 2:
380 # b  <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$
381 # c  <$> const[PV "strict.pm"] s/BARE
382 # d  <1> require sK/1
383 # e  <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$
384 # f  <0> pushmark s
385 # g  <$> const[PV "strict"] sM
386 # h  <$> const[PV "refs"] sM
387 # i  <$> method_named[PV "unimport"] 
388 # j  <1> entersub[t1] KS*/TARG,2
389 # k  <1> leavesub[1 ref] K/REFC,1
390 # BEGIN 3:
391 # l  <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$
392 # m  <$> const[PV "warnings.pm"] s/BARE
393 # n  <1> require sK/1
394 # o  <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$
395 # p  <0> pushmark s
396 # q  <$> const[PV "warnings"] sM
397 # r  <$> const[PV "qw"] sM
398 # s  <$> method_named[PV "unimport"] 
399 # t  <1> entersub[t1] KS*/TARG,2
400 # u  <1> leavesub[1 ref] K/REFC,1
401 EOT_EOT
402 # BEGIN 1:
403 # 1  <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$
404 # 2  <$> const(PV "strict.pm") s/BARE
405 # 3  <1> require sK/1
406 # 4  <;> nextstate(B::Concise -275 Concise.pm:356) v:*,&,{,x*,x&,x$,$
407 # 5  <0> pushmark s
408 # 6  <$> const(PV "strict") sM
409 # 7  <$> const(PV "refs") sM
410 # 8  <$> method_named(PV "unimport") 
411 # 9  <1> entersub[t1] KS*/TARG,2
412 # a  <1> leavesub[1 ref] K/REFC,1
413 # BEGIN 2:
414 # b  <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$
415 # c  <$> const(PV "strict.pm") s/BARE
416 # d  <1> require sK/1
417 # e  <;> nextstate(B::Concise -265 Concise.pm:367) v:*,&,x*,x&,x$,$
418 # f  <0> pushmark s
419 # g  <$> const(PV "strict") sM
420 # h  <$> const(PV "refs") sM
421 # i  <$> method_named(PV "unimport") 
422 # j  <1> entersub[t1] KS*/TARG,2
423 # k  <1> leavesub[1 ref] K/REFC,1
424 # BEGIN 3:
425 # l  <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$
426 # m  <$> const(PV "warnings.pm") s/BARE
427 # n  <1> require sK/1
428 # o  <;> nextstate(B::Concise -254 Concise.pm:386) v:*,&,{,x*,x&,x$,$
429 # p  <0> pushmark s
430 # q  <$> const(PV "warnings") sM
431 # r  <$> const(PV "qw") sM
432 # s  <$> method_named(PV "unimport") 
433 # t  <1> entersub[t1] KS*/TARG,2
434 # u  <1> leavesub[1 ref] K/REFC,1
435 EONT_EONT