Remaining nit in the deparsing of reversed foreach loops
[p5sagit/p5-mst-13.2.git] / ext / B / t / optree_concise.t
CommitLineData
724aa791 1#!perl
2
3BEGIN {
4 chdir 't';
5 @INC = ('../lib', '../ext/B/t');
9cd8f857 6 require Config;
7 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
8 print "1..0 # Skip -- Perl configured without B module\n";
9 exit 0;
10 }
8dd2f9d4 11 if ($Config::Config{'extensions'} !~ /\bData\/Dumper\b/) {
2799c206 12 print
13 "1..0 # Skip: Data::Dumper was not built, needed by OptreeCheck\n";
14 exit 0;
2799c206 15 }
724aa791 16 require './test.pl';
17}
18
19# import checkOptree(), and %gOpts (containing test state)
20use OptreeCheck; # ALSO DOES @ARGV HANDLING !!!!!!
2ce64696 21use Config;
724aa791 22
5e251bf1 23plan tests => 24;
2ce64696 24SKIP: {
4837be01 25skip "no perlio in this build", 24 unless $Config::Config{useperlio};
724aa791 26
27$SIG{__WARN__} = sub {
28 my $err = shift;
29 $err =~ m/Subroutine re::(un)?install redefined/ and return;
30};
31#################################
32pass("CANONICAL B::Concise EXAMPLE");
33
34checkOptree ( name => 'canonical example w -basic',
35 bcopts => '-basic',
36 code => sub{$a=$b+42},
37 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
38# 7 <1> leavesub[\d+ refs?] K/REFC,1 ->(end)
39# - <@> lineseq KP ->7
40# 1 <;> nextstate(foo bar) v ->2
41# 6 <2> sassign sKS/2 ->7
42# 4 <2> add[t\d+] sK/2 ->5
43# - <1> ex-rv2sv sK/1 ->3
44# 2 <#> gvsv[*b] s ->3
45# 3 <$> const[IV 42] s ->4
46# - <1> ex-rv2sv sKRM*/1 ->6
47# 5 <#> gvsv[*a] s ->6
48EOT_EOT
49# 7 <1> leavesub[1 ref] K/REFC,1 ->(end)
50# - <@> lineseq KP ->7
51# 1 <;> nextstate(main 60 optree_concise.t:122) v ->2
52# 6 <2> sassign sKS/2 ->7
53# 4 <2> add[t1] sK/2 ->5
54# - <1> ex-rv2sv sK/1 ->3
55# 2 <$> gvsv(*b) s ->3
56# 3 <$> const(IV 42) s ->4
57# - <1> ex-rv2sv sKRM*/1 ->6
58# 5 <$> gvsv(*a) s ->6
59EONT_EONT
60
61checkOptree ( name => 'canonical example w -exec',
62 bcopts => '-exec',
63 code => sub{$a=$b+42},
64 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
724aa791 65# 1 <;> nextstate(main 61 optree_concise.t:139) v
66# 2 <#> gvsv[*b] s
67# 3 <$> const[IV 42] s
68# 4 <2> add[t3] sK/2
69# 5 <#> gvsv[*a] s
70# 6 <2> sassign sKS/2
71# 7 <1> leavesub[1 ref] K/REFC,1
72EOT_EOT
724aa791 73# 1 <;> nextstate(main 61 optree_concise.t:139) v
74# 2 <$> gvsv(*b) s
75# 3 <$> const(IV 42) s
76# 4 <2> add[t1] sK/2
77# 5 <$> gvsv(*a) s
78# 6 <2> sassign sKS/2
79# 7 <1> leavesub[1 ref] K/REFC,1
80EONT_EONT
81
724aa791 82#################################
83pass("B::Concise OPTION TESTS");
84
85checkOptree ( name => '-base3 sticky-exec',
86 bcopts => '-base3',
87 code => sub{$a=$b+42},
88 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
724aa791 891 <;> dbstate(main 24 optree_concise.t:132) v
902 <#> gvsv[*b] s
9110 <$> const[IV 42] s
9211 <2> add[t3] sK/2
9312 <#> gvsv[*a] s
9420 <2> sassign sKS/2
cc02ea56 9521 <1> leavesub[1 ref] K/REFC,1
724aa791 96EOT_EOT
724aa791 97# 1 <;> nextstate(main 62 optree_concise.t:161) v
98# 2 <$> gvsv(*b) s
99# 10 <$> const(IV 42) s
100# 11 <2> add[t1] sK/2
101# 12 <$> gvsv(*a) s
102# 20 <2> sassign sKS/2
103# 21 <1> leavesub[1 ref] K/REFC,1
104EONT_EONT
105
106checkOptree ( name => 'sticky-base3, -basic over sticky-exec',
107 bcopts => '-basic',
108 code => sub{$a=$b+42},
109 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
11021 <1> leavesub[1 ref] K/REFC,1 ->(end)
111- <@> lineseq KP ->21
1121 <;> nextstate(main 32 optree_concise.t:164) v ->2
11320 <2> sassign sKS/2 ->21
11411 <2> add[t3] sK/2 ->12
115- <1> ex-rv2sv sK/1 ->10
1162 <#> gvsv[*b] s ->10
11710 <$> const[IV 42] s ->11
118- <1> ex-rv2sv sKRM*/1 ->20
11912 <#> gvsv[*a] s ->20
120EOT_EOT
121# 21 <1> leavesub[1 ref] K/REFC,1 ->(end)
122# - <@> lineseq KP ->21
123# 1 <;> nextstate(main 63 optree_concise.t:186) v ->2
124# 20 <2> sassign sKS/2 ->21
125# 11 <2> add[t1] sK/2 ->12
126# - <1> ex-rv2sv sK/1 ->10
127# 2 <$> gvsv(*b) s ->10
128# 10 <$> const(IV 42) s ->11
129# - <1> ex-rv2sv sKRM*/1 ->20
130# 12 <$> gvsv(*a) s ->20
131EONT_EONT
132
133checkOptree ( name => '-base4',
134 bcopts => [qw/ -basic -base4 /],
135 code => sub{$a=$b+42},
136 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
13713 <1> leavesub[1 ref] K/REFC,1 ->(end)
138- <@> lineseq KP ->13
1391 <;> nextstate(main 26 optree_concise.t:145) v ->2
14012 <2> sassign sKS/2 ->13
14110 <2> add[t3] sK/2 ->11
142- <1> ex-rv2sv sK/1 ->3
1432 <#> gvsv[*b] s ->3
1443 <$> const[IV 42] s ->10
145- <1> ex-rv2sv sKRM*/1 ->12
14611 <#> gvsv[*a] s ->12
147EOT_EOT
148# 13 <1> leavesub[1 ref] K/REFC,1 ->(end)
149# - <@> lineseq KP ->13
150# 1 <;> nextstate(main 64 optree_concise.t:193) v ->2
151# 12 <2> sassign sKS/2 ->13
152# 10 <2> add[t1] sK/2 ->11
153# - <1> ex-rv2sv sK/1 ->3
154# 2 <$> gvsv(*b) s ->3
155# 3 <$> const(IV 42) s ->10
156# - <1> ex-rv2sv sKRM*/1 ->12
157# 11 <$> gvsv(*a) s ->12
158EONT_EONT
159
160checkOptree ( name => "restore -base36 default",
161 bcopts => [qw/ -basic -base36 /],
162 code => sub{$a},
163 crossfail => 1,
164 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
1653 <1> leavesub[1 ref] K/REFC,1 ->(end)
166- <@> lineseq KP ->3
1671 <;> nextstate(main 27 optree_concise.t:161) v ->2
168- <1> ex-rv2sv sK/1 ->-
1692 <#> gvsv[*a] s ->3
170EOT_EOT
171# 3 <1> leavesub[1 ref] K/REFC,1 ->(end)
172# - <@> lineseq KP ->3
173# 1 <;> nextstate(main 65 optree_concise.t:210) v ->2
174# - <1> ex-rv2sv sK/1 ->-
175# 2 <$> gvsv(*a) s ->3
176EONT_EONT
177
178checkOptree ( name => "terse basic",
179 bcopts => [qw/ -basic -terse /],
180 code => sub{$a},
181 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
182UNOP (0x82b0918) leavesub [1]
183 LISTOP (0x82b08d8) lineseq
184 COP (0x82b0880) nextstate
185 UNOP (0x82b0860) null [15]
186 PADOP (0x82b0840) gvsv GV (0x82a818c) *a
187EOT_EOT
188# UNOP (0x8282310) leavesub [1]
189# LISTOP (0x82822f0) lineseq
190# COP (0x82822b8) nextstate
191# UNOP (0x812fc20) null [15]
192# SVOP (0x812fc00) gvsv GV (0x814692c) *a
193EONT_EONT
194
195checkOptree ( name => "sticky-terse exec",
196 bcopts => [qw/ -exec /],
197 code => sub{$a},
198 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
724aa791 199COP (0x82b0d70) nextstate
200PADOP (0x82b0d30) gvsv GV (0x82a818c) *a
201UNOP (0x82b0e08) leavesub [1]
202EOT_EOT
724aa791 203# COP (0x82828e0) nextstate
204# SVOP (0x82828a0) gvsv GV (0x814692c) *a
205# UNOP (0x8282938) leavesub [1]
206EONT_EONT
207
208pass("OPTIONS IN CMDLINE MODE");
209
210checkOptree ( name => 'cmdline invoke -basic works',
211 prog => 'sort @a',
212 #bcopts => '-basic', # default
213 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
214# 7 <@> leave[1 ref] vKP/REFC ->(end)
215# 1 <0> enter ->2
216# 2 <;> nextstate(main 1 -e:1) v ->3
217# 6 <@> sort vK ->7
218# 3 <0> pushmark s ->4
219# 5 <1> rv2av[t2] lK/1 ->6
220# 4 <#> gv[*a] s ->5
221EOT_EOT
222# 7 <@> leave[1 ref] vKP/REFC ->(end)
223# 1 <0> enter ->2
224# 2 <;> nextstate(main 1 -e:1) v ->3
225# 6 <@> sort vK ->7
226# 3 <0> pushmark s ->4
227# 5 <1> rv2av[t1] lK/1 ->6
228# 4 <$> gv(*a) s ->5
229EONT_EONT
230
231checkOptree ( name => 'cmdline invoke -exec works',
232 prog => 'sort @a',
233 bcopts => '-exec',
234 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
2351 <0> enter
2362 <;> nextstate(main 1 -e:1) v
2373 <0> pushmark s
2384 <#> gv[*a] s
2395 <1> rv2av[t2] lK/1
2406 <@> sort vK
2417 <@> leave[1 ref] vKP/REFC
242EOT_EOT
243# 1 <0> enter
244# 2 <;> nextstate(main 1 -e:1) v
245# 3 <0> pushmark s
246# 4 <$> gv(*a) s
247# 5 <1> rv2av[t1] lK/1
248# 6 <@> sort vK
249# 7 <@> leave[1 ref] vKP/REFC
250EONT_EONT
251
5e251bf1 252;
253$DB::single=1;
254checkOptree
255 ( name => 'cmdline self-strict compile err using prog',
256 prog => 'use strict; sort @a',
257 bcopts => [qw/ -basic -concise -exec /],
258 errs => 'Global symbol "@a" requires explicit package name at .*? line 1.',
259 );
724aa791 260
5e251bf1 261checkOptree
262 ( name => 'cmdline self-strict compile err using code',
263 code => 'use strict; sort @a',
264 bcopts => [qw/ -basic -concise -exec /],
265 #noanchors => 1,
266 errs => 'Global symbol "@a" requires explicit package name at .*? line 1.',
267 );
724aa791 268
5e251bf1 269checkOptree
270 ( name => 'useless use of sort in void context',
271 prog => 'our @a; sort @a',
272 bcopts => [qw/ -basic -concise -exec /],
273 errs => 'Useless use of sort in void context at -e line 1.',
274 );
275
276checkOptree
277 ( name => 'cmdline -basic -concise -exec works',
278 prog => 'our @a; sort @a',
279 bcopts => [qw/ -basic -concise -exec /],
280 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
724aa791 281# 1 <0> enter
282# 2 <;> nextstate(main 1 -e:1) v
283# 3 <#> gv[*a] s
284# 4 <1> rv2av[t3] vK/OURINTR,1
285# 5 <;> nextstate(main 2 -e:1) v
286# 6 <0> pushmark s
287# 7 <#> gv[*a] s
288# 8 <1> rv2av[t5] lK/1
289# 9 <@> sort vK
290# a <@> leave[1 ref] vKP/REFC
291EOT_EOT
292# 1 <0> enter
293# 2 <;> nextstate(main 1 -e:1) v
294# 3 <$> gv(*a) s
295# 4 <1> rv2av[t2] vK/OURINTR,1
296# 5 <;> nextstate(main 2 -e:1) v
297# 6 <0> pushmark s
298# 7 <$> gv(*a) s
299# 8 <1> rv2av[t3] lK/1
300# 9 <@> sort vK
301# a <@> leave[1 ref] vKP/REFC
302EONT_EONT
303
304
305#################################
306pass("B::Concise STYLE/CALLBACK TESTS");
307
308use B::Concise qw( walk_output add_style set_style_standard add_callback );
309
310# new relative style, added by set_up_relative_test()
311@stylespec =
312 ( "#hyphseq2 (*( (x( ;)x))*)<#classsym> "
313 . "#exname#arg(?([#targarglife])?)~#flags(?(/#privateb)?)(x(;~->#next)x) "
314 . "(x(;~=> #extra)x)\n" # new 'variable' used here
315
316 , " (*( )*) goto #seq\n"
cc02ea56 317 , "(?(<#seq>)?)#exname#arg(?([#targarglife])?)"
724aa791 318 #. "(x(;~=> #extra)x)\n" # new 'variable' used here
319 );
320
321sub set_up_relative_test {
322 # add a new style, and a callback which adds an 'extra' property
323
324 add_style ( "relative" => @stylespec );
325 #set_style_standard ( "relative" );
326
327 add_callback
328 ( sub {
329 my ($h, $op, $format, $level, $style) = @_;
330
331 # callback marks up const ops
332 $h->{arg} .= ' CALLBACK' if $h->{name} eq 'const';
333 $h->{extra} = '';
334
cc02ea56 335 if ($lastnext and $$lastnext != $$op) {
336 $h->{goto} = ($h->{seq} eq '-')
337 ? 'unresolved' : $h->{seq};
338 }
339
724aa791 340 # 2 style specific behaviors
341 if ($style eq 'relative') {
342 $h->{extra} = 'RELATIVE';
343 $h->{arg} .= ' RELATIVE' if $h->{name} eq 'leavesub';
344 }
345 elsif ($style eq 'scope') {
346 # supress printout entirely
347 $$format="" unless grep { $h->{name} eq $_ } @scopeops;
348 }
349 });
350}
351
352#################################
353set_up_relative_test();
354pass("set_up_relative_test, new callback installed");
355
356checkOptree ( name => 'callback used, independent of style',
357 bcopts => [qw/ -concise -exec /],
358 code => sub{$a=$b+42},
359 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
724aa791 3601 <;> nextstate(main 76 optree_concise.t:337) v
3612 <#> gvsv[*b] s
3623 <$> const[IV 42] CALLBACK s
3634 <2> add[t3] sK/2
3645 <#> gvsv[*a] s
3656 <2> sassign sKS/2
3667 <1> leavesub[1 ref] K/REFC,1
367EOT_EOT
368# 1 <;> nextstate(main 455 optree_concise.t:328) v
369# 2 <$> gvsv(*b) s
370# 3 <$> const(IV 42) CALLBACK s
371# 4 <2> add[t1] sK/2
372# 5 <$> gvsv(*a) s
373# 6 <2> sassign sKS/2
374# 7 <1> leavesub[1 ref] K/REFC,1
375EONT_EONT
376
377checkOptree ( name => "new 'relative' style, -exec mode",
378 bcopts => [qw/ -basic -relative /],
379 code => sub{$a=$b+42},
380 crossfail => 1,
381 #retry => 1,
382 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
cc02ea56 3837 <1> leavesub RELATIVE[1 ref] K ->(end) => RELATIVE
384- <@> lineseq KP ->7 => RELATIVE
3851 <;> nextstate(main 49 optree_concise.t:309) v ->2 => RELATIVE
3866 <2> sassign sKS ->7 => RELATIVE
3874 <2> add[t3] sK ->5 => RELATIVE
388- <1> ex-rv2sv sK ->3 => RELATIVE
3892 <#> gvsv[*b] s ->3 => RELATIVE
3903 <$> const[IV 42] CALLBACK s ->4 => RELATIVE
391- <1> ex-rv2sv sKRM* ->6 => RELATIVE
3925 <#> gvsv[*a] s ->6 => RELATIVE
724aa791 393EOT_EOT
cc02ea56 394# 7 <1> leavesub RELATIVE[1 ref] K ->(end) => RELATIVE
395# - <@> lineseq KP ->7 => RELATIVE
396# 1 <;> nextstate(main 77 optree_concise.t:353) v ->2 => RELATIVE
397# 6 <2> sassign sKS ->7 => RELATIVE
398# 4 <2> add[t1] sK ->5 => RELATIVE
399# - <1> ex-rv2sv sK ->3 => RELATIVE
400# 2 <$> gvsv(*b) s ->3 => RELATIVE
401# 3 <$> const(IV 42) CALLBACK s ->4 => RELATIVE
402# - <1> ex-rv2sv sKRM* ->6 => RELATIVE
403# 5 <$> gvsv(*a) s ->6 => RELATIVE
724aa791 404EONT_EONT
405
406checkOptree ( name => "both -exec -relative",
407 bcopts => [qw/ -exec -relative /],
408 code => sub{$a=$b+42},
409 crossfail => 1,
410 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
724aa791 4111 <;> nextstate(main 50 optree_concise.t:326) v
4122 <#> gvsv[*b] s
4133 <$> const[IV 42] CALLBACK s
4144 <2> add[t3] sK
4155 <#> gvsv[*a] s
4166 <2> sassign sKS
4177 <1> leavesub RELATIVE[1 ref] K
418EOT_EOT
419# 1 <;> nextstate(main 78 optree_concise.t:371) v
420# 2 <$> gvsv(*b) s
421# 3 <$> const(IV 42) CALLBACK s
422# 4 <2> add[t1] sK
423# 5 <$> gvsv(*a) s
424# 6 <2> sassign sKS
425# 7 <1> leavesub RELATIVE[1 ref] K
426EONT_EONT
427
428#################################
429
430@scopeops = qw( leavesub enter leave nextstate );
431add_style
432 ( 'scope' # concise copy
433 , "#hyphseq2 (*( (x( ;)x))*)<#classsym> "
434 . "#exname#arg(?([#targarglife])?)~#flags(?(/#private)?)(x(;~->#next)x) "
435 , " (*( )*) goto #seq\n"
436 , "(?(<#seq>)?)#exname#arg(?([#targarglife])?)"
437 );
438
439checkOptree ( name => "both -exec -scope",
440 bcopts => [qw/ -exec -scope /],
441 code => sub{$a=$b+42},
442 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
724aa791 4431 <;> nextstate(main 50 optree_concise.t:337) v
4447 <1> leavesub[1 ref] K/REFC,1
445EOT_EOT
724aa791 4461 <;> nextstate(main 75 optree_concise.t:396) v
4477 <1> leavesub[1 ref] K/REFC,1
448EONT_EONT
449
450
451checkOptree ( name => "both -basic -scope",
452 bcopts => [qw/ -basic -scope /],
453 code => sub{$a=$b+42},
454 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
4557 <1> leavesub[1 ref] K/REFC,1 ->(end)
4561 <;> nextstate(main 51 optree_concise.t:347) v ->2
457EOT_EOT
4587 <1> leavesub[1 ref] K/REFC,1 ->(end)
4591 <;> nextstate(main 76 optree_concise.t:407) v ->2
460EONT_EONT
461
2ce64696 462} #skip
724aa791 463