3 # Test the core keywords.
5 # Initially this test file just checked that CORE::foo got correctly
6 # deparsed as CORE::foo, hence the name. It's since been expanded
7 # to fully test both CORE:: verses none, plus that any arguments
8 # are correctly deparsed. It also cross-checks against regen/keywords.pl
9 # to make sure we've tested all keywords, and with the correct strength.
11 # A keyword can be either weak or strong. Strong keywords can never be
12 # overridden, while weak ones can. So deparsing of weak keywords depends
13 # on whether a sub of that name has been created:
15 # for both: keyword(..) deparsed as keyword(..)
16 # for weak: CORE::keyword(..) deparsed as CORE::keyword(..)
17 # for strong: CORE::keyword(..) deparsed as keyword(..)
19 # Three permutations of lex/nonlex args are checked for:
22 # foo(my $a,$b,$c,...)
23 # my ($a,$b,$c,...); foo($a,$b,$c,...)
25 # Note that tests for prefixing feature.pm-enabled keywords with CORE:: when
26 # feature.pm is not enabled are in deparse.t, as they fit that format better.
31 if (($Config::Config{extensions} !~ /\bB\b/) ){
32 print "1..0 # Skip -- Perl configured without B module\n";
41 use feature (sprintf(":%vd", $^V)); # to avoid relying on the feature
43 no warnings 'experimental::autoderef';
45 my $deparse = new B::Deparse;
50 # for a given keyword, create a sub of that name, then
51 # deparse "() = $expr", and see if it matches $expected_expr
54 my ($keyword, $expr, $expected_expr, $lexsub) = @_;
56 $expected_expr //= $expr;
60 # lex=0: () = foo($a,$b,$c)
61 # lex=1: my ($a,$b); () = foo($a,$b,$c)
62 # lex=2: () = foo(my $a,$b,$c)
63 for my $lex (0, 1, 2) {
65 next if $keyword =~ /local|our|state|my/;
67 my $vars = $lex == 1 ? 'my($a, $b, $c, $d, $e);' . "\n " : "";
71 if ($expr =~ 'CORE::do') {
72 # do foo() is a syntax error, so B::Deparse emits
73 # do (foo()), but does not distinguish between foo and my,
74 # because it is too complicated.
77 s/\$a/$repl/ for $expr, $expected_expr;
80 my $desc = "$keyword: lex=$lex $expr => $expected_expr";
81 $desc .= " (lex sub)" if $lexsub;
87 no warnings 'experimental::lexical_subs';
88 use feature 'lexical_subs';
91 eval "sub { state sub $keyword; ${vars}() = $expr }"
98 $code_ref = eval "no strict 'vars'; sub { ${vars}() = $expr }"
102 my $got_text = $deparse->coderef2text($code_ref);
104 unless ($got_text =~ /
105 package (?:lexsub)?test;
106 BEGIN \{\$\{\^WARNING_BITS} = "[^"]*"}
107 use strict 'refs', 'subs';
112 ::diag("couldn't extract line from boilerplate\n");
118 is $got_expr, $expected_expr, $desc;
123 # Deparse can't distinguish 'and' from '&&' etc
124 my %infix_map = qw(and && or ||);
127 # test a keyword that is a binary infix operator, like 'cmp'.
128 # $parens - "$a op $b" is deparsed as "($a op $b)"
129 # $strong - keyword is strong
131 sub do_infix_keyword {
132 my ($keyword, $parens, $strong) = @_;
133 $SEEN_STRENGH{$keyword} = $strong;
134 my $expr = "(\$a $keyword \$b)";
135 my $nkey = $infix_map{$keyword} // $keyword;
136 my $expr = "(\$a $keyword \$b)";
137 my $exp = "\$a $nkey \$b";
138 $exp = "($exp)" if $parens;
140 # with infix notation, a keyword is always interpreted as core,
141 # so no need for Deparse to disambiguate with CORE::
142 testit $keyword, "(\$a CORE::$keyword \$b)", $exp;
143 testit $keyword, "(\$a $keyword \$b)", $exp;
144 testit $keyword, "(\$a CORE::$keyword \$b)", $exp, 1;
145 testit $keyword, "(\$a $keyword \$b)", $exp, 1;
147 # B::Deparse fully qualifies any sub whose name is a keyword,
148 # imported or not, since the importedness may not be reproduced by
149 # the deparsed code. x is special.
150 my $pre = "test::" x ($keyword ne 'x');
151 testit $keyword, "$keyword(\$a, \$b)", "$pre$keyword(\$a, \$b);";
153 testit $keyword, "$keyword(\$a, \$b)", "$keyword(\$a, \$b);", 1;
156 # test a keyword that is as tandard op/function, like 'index(...)'.
157 # narg - how many args to test it with
158 # $parens - "foo $a, $b" is deparsed as "foo($a, $b)"
159 # $dollar - an extra '$_' arg will appear in the deparsed output
160 # $strong - keyword is strong
164 my ($keyword, $narg, $parens, $dollar, $strong) = @_;
166 $SEEN_STRENGH{$keyword} = $strong;
168 for my $core (0,1) { # if true, add CORE:: to keyword being deparsed
169 for my $lexsub (0,1) { # if true, define lex sub
171 for my $do_exp(0, 1) { # first create expr, then expected-expr
172 my @args = map "\$$_", (undef,"a".."z")[1..$narg];
174 if $dollar && $do_exp && ($strong && !$lexsub or $core);
175 my $args = join(', ', @args);
176 # XXX $lex_parens is temporary, until lex subs are
179 !$core && $do_exp && $lexsub && $keyword ne 'map';
180 $args = ((!$core && !$strong) || $parens || $lex_parens)
182 : @args ? " $args" : "";
183 push @code, (($core && !($do_exp && $strong))
187 : $do_exp && !$core && !$strong ? "test::" : "")
190 # code[0]: to run; code[1]: expected
191 testit $keyword, @code, $lexsub;
203 die "not 3 fields" unless @fields == 3;
204 my ($keyword, $args, $flags) = @fields;
206 $args = '012' if $args eq '@';
208 my $parens = $flags =~ s/p//;
209 my $invert1 = $flags =~ s/1//;
210 my $dollar = $flags =~ s/\$//;
211 my $strong = $flags =~ s/\+//;
212 die "unrecognised flag(s): '$flags'" unless $flags =~ /^-?$/;
214 if ($args eq 'B') { # binary infix
215 die "$keyword: binary (B) op can't have '\$' flag\\n" if $dollar;
216 die "$keyword: binary (B) op can't have '1' flag\\n" if $invert1;
217 do_infix_keyword($keyword, $parens, $strong);
220 my @narg = split //, $args;
221 for my $n (0..$#narg) {
222 my $narg = $narg[$n];
224 $p = !$p if ($n == 0 && $invert1);
225 do_std_keyword($keyword, $narg, $p, (!$n && $dollar), $strong);
233 testit dbmopen => 'CORE::dbmopen(%foo, $bar, $baz);';
234 testit dbmclose => 'CORE::dbmclose %foo;';
236 testit delete => 'CORE::delete $h{\'foo\'};', 'delete $h{\'foo\'};';
237 testit delete => 'CORE::delete $h{\'foo\'};', undef, 1;
238 testit delete => 'CORE::delete @h{\'foo\'};', undef, 1;
239 testit delete => 'CORE::delete $h[0];', undef, 1;
240 testit delete => 'CORE::delete @h[0];', undef, 1;
241 testit delete => 'delete $h{\'foo\'};', 'delete $h{\'foo\'};';
243 # do is listed as strong, but only do { block } is strong;
244 # do $file is weak, so test it separately here
245 testit do => 'CORE::do $a;';
246 testit do => 'do $a;', 'test::do($a);';
247 testit do => 'CORE::do { 1 }',
249 testit do => 'CORE::do { 1 }',
250 "CORE::do {\n 1\n };", 1;
251 testit do => 'do { 1 };',
254 testit each => 'CORE::each %bar;';
256 testit eof => 'CORE::eof();';
258 testit exists => 'CORE::exists $h{\'foo\'};', 'exists $h{\'foo\'};';
259 testit exists => 'CORE::exists $h{\'foo\'};', undef, 1;
260 testit exists => 'CORE::exists &foo;', undef, 1;
261 testit exists => 'CORE::exists $h[0];', undef, 1;
262 testit exists => 'exists $h{\'foo\'};', 'exists $h{\'foo\'};';
264 testit exec => 'CORE::exec($foo $bar);';
266 testit glob => 'glob;', 'glob($_);';
267 testit glob => 'CORE::glob;', 'CORE::glob($_);';
268 testit glob => 'glob $a;', 'glob($a);';
269 testit glob => 'CORE::glob $a;', 'CORE::glob($a);';
271 testit grep => 'CORE::grep { $a } $b, $c', 'grep({$a;} $b, $c);';
273 testit keys => 'CORE::keys %bar;';
275 testit map => 'CORE::map { $a } $b, $c', 'map({$a;} $b, $c);';
277 testit not => '3 unless CORE::not $a && $b;';
279 testit readline => 'CORE::readline $a . $b;';
281 testit readpipe => 'CORE::readpipe $a + $b;';
283 testit reverse => 'CORE::reverse sort(@foo);';
285 # note that the test does '() = split...' which is why the
286 # limit is optimised to 1
287 testit split => 'split;', q{split(' ', $_, 1);};
288 testit split => 'CORE::split;', q{split(' ', $_, 1);};
289 testit split => 'split $a;', q{split(/$a/u, $_, 1);};
290 testit split => 'CORE::split $a;', q{split(/$a/u, $_, 1);};
291 testit split => 'split $a, $b;', q{split(/$a/u, $b, 1);};
292 testit split => 'CORE::split $a, $b;', q{split(/$a/u, $b, 1);};
293 testit split => 'split $a, $b, $c;', q{split(/$a/u, $b, $c);};
294 testit split => 'CORE::split $a, $b, $c;', q{split(/$a/u, $b, $c);};
296 testit sub => 'CORE::sub { $a, $b }',
297 "sub {\n \$a, \$b;\n }\n ;";
299 testit system => 'CORE::system($foo $bar);';
301 testit values => 'CORE::values %bar;';
304 # XXX These are deparsed wrapped in parens.
305 # whether they should be, I don't know!
307 testit dump => '(CORE::dump);';
308 testit dump => '(CORE::dump FOO);';
309 testit goto => '(CORE::goto);', '(goto);';
310 testit goto => '(CORE::goto FOO);', '(goto FOO);';
311 testit last => '(CORE::last);', '(last);';
312 testit last => '(CORE::last FOO);', '(last FOO);';
313 testit next => '(CORE::next);', '(next);';
314 testit next => '(CORE::next FOO);', '(next FOO);';
315 testit redo => '(CORE::redo);', '(redo);';
316 testit redo => '(CORE::redo FOO);', '(redo FOO);';
317 testit redo => '(CORE::redo);', '(redo);';
318 testit redo => '(CORE::redo FOO);', '(redo FOO);';
319 testit return => '(return);', '(return);';
320 testit return => '(CORE::return);', '(return);';
322 # these are the keywords I couldn't think how to test within this framework
324 my %not_tested = map { $_ => 1} qw(
367 # Sanity check against keyword data:
368 # make sure we haven't missed any keywords,
369 # and that we got the strength right.
373 skip "sanity checks when not PERL_CORE", 1 unless defined $ENV{PERL_CORE};
375 my $file = '../regen/keywords.pl';
377 if (open my $fh, '<', $file) {
382 next unless /^([+\-])(\w+)$/;
383 my ($strength, $key) = ($1, $2);
384 $strength = ($strength eq '+') ? 1 : 0;
386 if (!$SEEN{$key} && !$not_tested{$key}) {
387 diag("keyword '$key' seen in $file, but not tested here!!");
390 if (exists $SEEN_STRENGH{$key} and $SEEN_STRENGH{$key} != $strength) {
391 diag("keyword '$key' strengh as seen in $file doen't match here!!");
397 diag("Can't open $file: $!");
402 diag("Saw $count keywords: less than 200!");
405 ok($pass, "sanity checks");
416 # * one of more digits indictating which lengths of args the function accepts,
417 # * or 'B' to indiate a binary infix operator,
418 # * or '@' to indicate a list function.
420 # Flags consists of the following (or '-' if no flags):
421 # + : strong keyword: can't be overrriden
422 # p : the args are parenthesised on deparsing;
423 # 1 : parenthesising of 1st arg length is inverted
424 # so '234 p1' means: foo a1,a2; foo(a1,a2,a3); foo(a1,a2,a3,a4)
425 # $ : on the first argument length, there is an implicit extra
426 # '$_' arg which will appear on deparsing;
427 # e.g. 12p$ will be tested as: foo(a1); foo(a1,a2);
428 # and deparsed as: foo(a1, $_); foo(a1,a2);
430 # XXX Note that we really should get this data from regen/keywords.pl
431 # and regen/opcodes (augmented if necessary), rather than duplicating it
459 # dbmopen handled specially
460 # dbmclose handled specially
462 # delete handled specially
464 # do handled specially
465 # dump handled specially
466 each 1 - # also tested specially
473 eof 01 - # also tested specially
477 exec @ p1 # also tested specially
478 # exists handled specially
514 # given handled specially
515 grep 123 p+ # also tested specially
516 # glob handled specially
517 # goto handled specially
525 keys 1 - # also tested specially
527 # last handled specially
540 map 123 p+ # also tested specially
546 my 123 p+ # skip with 0 args, as my() => ()
548 # next handled specially
549 # not handled specially
555 our 123 p+ # skip with 0 args, as our() => ()
568 # readline handled specially
570 # readpipe handled specially
572 # redo handled specially
575 # XXX This code prints 'Undefined subroutine &main::require called':
576 # use subs (); import subs 'require';
577 # eval q[no strict 'vars'; sub { () = require; }]; print $@;
581 # return handled specially
582 reverse @ p1 # also tested specially
615 # split handled specially
621 state 123 p+ # skip with 0 args, as state() => ()
623 # sub handled specially
630 system @ p1 # also tested specially
648 values 1 - # also tested specially