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:: versus 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
44 my $deparse = new B::Deparse;
49 # for a given keyword, create a sub of that name, then
50 # deparse "() = $expr", and see if it matches $expected_expr
53 my ($keyword, $expr, $expected_expr, $lexsub) = @_;
55 $expected_expr //= $expr;
59 # lex=0: () = foo($a,$b,$c)
60 # lex=1: my ($a,$b); () = foo($a,$b,$c)
61 # lex=2: () = foo(my $a,$b,$c)
62 for my $lex (0, 1, 2) {
64 next if $keyword =~ /local|our|state|my/;
66 my $vars = $lex == 1 ? 'my($a, $b, $c, $d, $e);' . "\n " : "";
70 if ($expr =~ 'CORE::do') {
71 # do foo() is a syntax error, so B::Deparse emits
72 # do (foo()), but does not distinguish between foo and my,
73 # because it is too complicated.
76 s/\$a/$repl/ for $expr, $expected_expr;
79 my $desc = "$keyword: lex=$lex $expr => $expected_expr";
80 $desc .= " (lex sub)" if $lexsub;
87 no warnings 'experimental::lexical_subs';
88 use feature 'lexical_subs';
90 $code = "sub { state sub $keyword; ${vars}() = $expr }";
91 $code_ref = eval $code
98 $code = "no strict 'vars'; sub { ${vars}() = $expr }";
99 $code_ref = eval $code
100 or die "$@ in $expr";
103 my $got_text = $deparse->coderef2text($code_ref);
105 unless ($got_text =~ /
106 package (?:lexsub)?test;
107 (?: BEGIN \{\$\{\^WARNING_BITS\} = "[^"]+"\}
108 )? use strict 'refs', 'subs';
110 (?: (?:CORE::)?state sub \w+;
111 )? \Q$vars\E\(\) = (.*)
114 ::diag("couldn't extract line from boilerplate\n");
120 is $got_expr, $expected_expr, $desc
121 or ::diag("ORIGINAL CODE:\n$code");;
126 # Deparse can't distinguish 'and' from '&&' etc
127 my %infix_map = qw(and && or ||);
130 # test a keyword that is a binary infix operator, like 'cmp'.
131 # $parens - "$a op $b" is deparsed as "($a op $b)"
132 # $strong - keyword is strong
134 sub do_infix_keyword {
135 my ($keyword, $parens, $strong) = @_;
136 $SEEN_STRENGH{$keyword} = $strong;
137 my $expr = "(\$a $keyword \$b)";
138 my $nkey = $infix_map{$keyword} // $keyword;
139 my $expr = "(\$a $keyword \$b)";
140 my $exp = "\$a $nkey \$b";
141 $exp = "($exp)" if $parens;
143 # with infix notation, a keyword is always interpreted as core,
144 # so no need for Deparse to disambiguate with CORE::
145 testit $keyword, "(\$a CORE::$keyword \$b)", $exp;
146 testit $keyword, "(\$a $keyword \$b)", $exp;
147 testit $keyword, "(\$a CORE::$keyword \$b)", $exp, 1;
148 testit $keyword, "(\$a $keyword \$b)", $exp, 1;
150 # B::Deparse fully qualifies any sub whose name is a keyword,
151 # imported or not, since the importedness may not be reproduced by
152 # the deparsed code. x is special.
153 my $pre = "test::" x ($keyword ne 'x');
154 testit $keyword, "$keyword(\$a, \$b)", "$pre$keyword(\$a, \$b);";
156 testit $keyword, "$keyword(\$a, \$b)", "$keyword(\$a, \$b);", 1;
159 # test a keyword that is as tandard op/function, like 'index(...)'.
160 # narg - how many args to test it with
161 # $parens - "foo $a, $b" is deparsed as "foo($a, $b)"
162 # $dollar - an extra '$_' arg will appear in the deparsed output
163 # $strong - keyword is strong
167 my ($keyword, $narg, $parens, $dollar, $strong) = @_;
169 $SEEN_STRENGH{$keyword} = $strong;
171 for my $core (0,1) { # if true, add CORE:: to keyword being deparsed
172 for my $lexsub (0,1) { # if true, define lex sub
174 for my $do_exp(0, 1) { # first create expr, then expected-expr
175 my @args = map "\$$_", (undef,"a".."z")[1..$narg];
177 if $dollar && $do_exp && ($strong && !$lexsub or $core);
178 my $args = join(', ', @args);
179 # XXX $lex_parens is temporary, until lex subs are
182 !$core && $do_exp && $lexsub && $keyword ne 'map';
183 $args = ((!$core && !$strong) || $parens || $lex_parens)
185 : @args ? " $args" : "";
186 push @code, (($core && !($do_exp && $strong))
190 : $do_exp && !$core && !$strong ? "test::" : "")
193 # code[0]: to run; code[1]: expected
194 testit $keyword, @code, $lexsub;
206 die "not 3 fields" unless @fields == 3;
207 my ($keyword, $args, $flags) = @fields;
209 $args = '012' if $args eq '@';
211 my $parens = $flags =~ s/p//;
212 my $invert1 = $flags =~ s/1//;
213 my $dollar = $flags =~ s/\$//;
214 my $strong = $flags =~ s/\+//;
215 die "unrecognised flag(s): '$flags'" unless $flags =~ /^-?$/;
217 if ($args eq 'B') { # binary infix
218 die "$keyword: binary (B) op can't have '\$' flag\\n" if $dollar;
219 die "$keyword: binary (B) op can't have '1' flag\\n" if $invert1;
220 do_infix_keyword($keyword, $parens, $strong);
223 my @narg = split //, $args;
224 for my $n (0..$#narg) {
225 my $narg = $narg[$n];
227 $p = !$p if ($n == 0 && $invert1);
228 do_std_keyword($keyword, $narg, $p, (!$n && $dollar), $strong);
236 testit dbmopen => 'CORE::dbmopen(%foo, $bar, $baz);';
237 testit dbmclose => 'CORE::dbmclose %foo;';
239 testit delete => 'CORE::delete $h{\'foo\'};', 'delete $h{\'foo\'};';
240 testit delete => 'CORE::delete $h{\'foo\'};', undef, 1;
241 testit delete => 'CORE::delete @h{\'foo\'};', undef, 1;
242 testit delete => 'CORE::delete $h[0];', undef, 1;
243 testit delete => 'CORE::delete @h[0];', undef, 1;
244 testit delete => 'delete $h{\'foo\'};', 'delete $h{\'foo\'};';
246 # do is listed as strong, but only do { block } is strong;
247 # do $file is weak, so test it separately here
248 testit do => 'CORE::do $a;';
249 testit do => 'do $a;', 'test::do($a);';
250 testit do => 'CORE::do { 1 }',
252 testit do => 'CORE::do { 1 }',
253 "CORE::do {\n 1\n };", 1;
254 testit do => 'do { 1 };',
257 testit each => 'CORE::each %bar;';
258 testit each => 'CORE::each @foo;';
260 testit eof => 'CORE::eof();';
262 testit exists => 'CORE::exists $h{\'foo\'};', 'exists $h{\'foo\'};';
263 testit exists => 'CORE::exists $h{\'foo\'};', undef, 1;
264 testit exists => 'CORE::exists &foo;', undef, 1;
265 testit exists => 'CORE::exists $h[0];', undef, 1;
266 testit exists => 'exists $h{\'foo\'};', 'exists $h{\'foo\'};';
268 testit exec => 'CORE::exec($foo $bar);';
270 testit glob => 'glob;', 'glob($_);';
271 testit glob => 'CORE::glob;', 'CORE::glob($_);';
272 testit glob => 'glob $a;', 'glob($a);';
273 testit glob => 'CORE::glob $a;', 'CORE::glob($a);';
275 testit grep => 'CORE::grep { $a } $b, $c', 'grep({$a;} $b, $c);';
277 testit keys => 'CORE::keys %bar;';
278 testit keys => 'CORE::keys @bar;';
280 testit map => 'CORE::map { $a } $b, $c', 'map({$a;} $b, $c);';
282 testit not => '3 unless CORE::not $a && $b;';
284 testit pop => 'CORE::pop @foo;';
286 testit push => 'CORE::push @foo;', 'CORE::push(@foo);';
287 testit push => 'CORE::push @foo, 1;', 'CORE::push(@foo, 1);';
288 testit push => 'CORE::push @foo, 1, 2;', 'CORE::push(@foo, 1, 2);';
290 testit readline => 'CORE::readline $a . $b;';
292 testit readpipe => 'CORE::readpipe $a + $b;';
294 testit reverse => 'CORE::reverse sort(@foo);';
296 testit shift => 'CORE::shift @foo;';
298 testit splice => q{CORE::splice @foo;}, q{CORE::splice(@foo);};
299 testit splice => q{CORE::splice @foo, 0;}, q{CORE::splice(@foo, 0);};
300 testit splice => q{CORE::splice @foo, 0, 1;}, q{CORE::splice(@foo, 0, 1);};
301 testit splice => q{CORE::splice @foo, 0, 1, 'a';}, q{CORE::splice(@foo, 0, 1, 'a');};
302 testit splice => q{CORE::splice @foo, 0, 1, 'a', 'b';}, q{CORE::splice(@foo, 0, 1, 'a', 'b');};
304 # note that the test does '() = split...' which is why the
305 # limit is optimised to 1
306 testit split => 'split;', q{split(' ', $_, 1);};
307 testit split => 'CORE::split;', q{split(' ', $_, 1);};
308 testit split => 'split $a;', q{split(/$a/u, $_, 1);};
309 testit split => 'CORE::split $a;', q{split(/$a/u, $_, 1);};
310 testit split => 'split $a, $b;', q{split(/$a/u, $b, 1);};
311 testit split => 'CORE::split $a, $b;', q{split(/$a/u, $b, 1);};
312 testit split => 'split $a, $b, $c;', q{split(/$a/u, $b, $c);};
313 testit split => 'CORE::split $a, $b, $c;', q{split(/$a/u, $b, $c);};
315 testit sub => 'CORE::sub { $a, $b }',
316 "sub {\n \$a, \$b;\n }\n ;";
318 testit system => 'CORE::system($foo $bar);';
320 testit unshift => 'CORE::unshift @foo;', 'CORE::unshift(@foo);';
321 testit unshift => 'CORE::unshift @foo, 1;', 'CORE::unshift(@foo, 1);';
322 testit unshift => 'CORE::unshift @foo, 1, 2;', 'CORE::unshift(@foo, 1, 2);';
324 testit values => 'CORE::values %bar;';
325 testit values => 'CORE::values @foo;';
328 # XXX These are deparsed wrapped in parens.
329 # whether they should be, I don't know!
331 testit dump => '(CORE::dump);';
332 testit dump => '(CORE::dump FOO);';
333 testit goto => '(CORE::goto);', '(goto);';
334 testit goto => '(CORE::goto FOO);', '(goto FOO);';
335 testit last => '(CORE::last);', '(last);';
336 testit last => '(CORE::last FOO);', '(last FOO);';
337 testit next => '(CORE::next);', '(next);';
338 testit next => '(CORE::next FOO);', '(next FOO);';
339 testit redo => '(CORE::redo);', '(redo);';
340 testit redo => '(CORE::redo FOO);', '(redo FOO);';
341 testit redo => '(CORE::redo);', '(redo);';
342 testit redo => '(CORE::redo FOO);', '(redo FOO);';
343 testit return => '(return);', '(return);';
344 testit return => '(CORE::return);', '(return);';
346 # these are the keywords I couldn't think how to test within this framework
348 my %not_tested = map { $_ => 1} qw(
391 # Sanity check against keyword data:
392 # make sure we haven't missed any keywords,
393 # and that we got the strength right.
397 skip "sanity checks when not PERL_CORE", 1 unless defined $ENV{PERL_CORE};
399 my $file = '../regen/keywords.pl';
401 if (open my $fh, '<', $file) {
406 next unless /^([+\-])(\w+)$/;
407 my ($strength, $key) = ($1, $2);
408 $strength = ($strength eq '+') ? 1 : 0;
410 if (!$SEEN{$key} && !$not_tested{$key}) {
411 diag("keyword '$key' seen in $file, but not tested here!!");
414 if (exists $SEEN_STRENGH{$key} and $SEEN_STRENGH{$key} != $strength) {
415 diag("keyword '$key' strengh as seen in $file doen't match here!!");
421 diag("Can't open $file: $!");
426 diag("Saw $count keywords: less than 200!");
429 ok($pass, "sanity checks");
440 # * one of more digits indictating which lengths of args the function accepts,
441 # * or 'B' to indiate a binary infix operator,
442 # * or '@' to indicate a list function.
444 # Flags consists of the following (or '-' if no flags):
445 # + : strong keyword: can't be overrriden
446 # p : the args are parenthesised on deparsing;
447 # 1 : parenthesising of 1st arg length is inverted
448 # so '234 p1' means: foo a1,a2; foo(a1,a2,a3); foo(a1,a2,a3,a4)
449 # $ : on the first argument length, there is an implicit extra
450 # '$_' arg which will appear on deparsing;
451 # e.g. 12p$ will be tested as: foo(a1); foo(a1,a2);
452 # and deparsed as: foo(a1, $_); foo(a1,a2);
454 # XXX Note that we really should get this data from regen/keywords.pl
455 # and regen/opcodes (augmented if necessary), rather than duplicating it
483 # dbmopen handled specially
484 # dbmclose handled specially
486 # delete handled specially
488 # do handled specially
489 # dump handled specially
490 # each handled specially
497 eof 01 - # also tested specially
501 exec @ p1 # also tested specially
502 # exists handled specially
538 # given handled specially
539 grep 123 p+ # also tested specially
540 # glob handled specially
541 # goto handled specially
549 # keys handled specially
551 # last handled specially
564 map 123 p+ # also tested specially
570 my 123 p+ # skip with 0 args, as my() => ()
572 # next handled specially
573 # not handled specially
579 our 123 p+ # skip with 0 args, as our() => ()
582 pop 0 1 # also tested specially
587 # push handled specially
592 # readline handled specially
594 # readpipe handled specially
596 # redo handled specially
599 # XXX This code prints 'Undefined subroutine &main::require called':
600 # use subs (); import subs 'require';
601 # eval q[no strict 'vars'; sub { () = require; }]; print $@;
605 # return handled specially
606 reverse @ p1 # also tested specially
628 shift 0 1 # also tested specially
639 # split handled specially
640 # splice handled specially
645 state 123 p1+ # skip with 0 args, as state() => ()
647 # sub handled specially
654 system @ p1 # also tested specially
669 # unshift handled specially
672 # values handled specially