3 # This file tests the results of calling subroutines in the CORE::
4 # namespace with ampersand syntax. In other words, it tests the bodies of
5 # the subroutines themselves, not the ops that they might inline themselves
6 # as when called as barewords.
8 # coreinline.t tests the inlining of these subs as ops. Since it was
9 # convenient, I also put the prototype and undefinedness checking in that
10 # file, even though those have nothing to do with inlining. (coreinline.t
11 # reads the list in keywords.pl, which is why it’s convenient.)
19 # Since tests inside evals can too easily fail silently, we cannot rely
20 # on done_testing. It’s much easier to count the tests as we go than to
21 # declare the plan up front, so this script ends with a test that makes
22 # sure the right number of tests have happened.
25 &is(map(@$_ ? "[@{[map $_//'~~u~~', @$_]}]" : 'nought', @_[0,1]), $_[2]);
29 readpipe => 'quoted execution (``, qx)',
30 ref => 'reference-type operator',
33 return $op_desc{$_[0]} || $_[0];
37 # This tests that the &{} syntax respects the number of arguments implied
38 # by the prototype, plus some extra tests for the (_) prototype.
42 # Create an alias, for the caller’s convenience.
43 *{"my$o"} = \&{"CORE::$o"};
45 my $p = prototype "CORE::$o";
50 eval " &CORE::$o(1) ";
51 like $@, qr/^Too many arguments for $o at /, "&$o with too many args";
57 eval " &CORE::$o(1,2) ";
58 my $desc = quotemeta op_desc($o);
59 like $@, qr/^Too many arguments for $desc at /,
60 "&$o with too many args";
66 my($in,$out) = @_; # for testing implied $_
68 # Since we have $in and $out values, we might as well test basic amper-
71 is &{"CORE::$o"}($in), $out, "&$o";
72 lis [&{"CORE::$o"}($in)], [$out], "&$o in list context";
75 is &{"CORE::$o"}(), $out, "&$o with no args";
77 # Since there is special code to deal with lexical $_, make sure it
82 is &{"CORE::$o"}(), $out, "&$o with no args uses lexical \$_";
84 # Make sure we get the right pad under recursion
89 is &{"CORE::$o"}(), $out,
90 "&$o with no args uses the right lexical \$_ under recursion";
99 is "CORE::$o"->(), $out, "&$o with the right lexical \$_ in an eval"
102 elsif ($p =~ '^([$*]+);?\z') { # Fixed-length $$$ or ***
103 my $args = length $1;
105 eval " &CORE::$o((1)x($args-1)) ";
106 like $@, qr/^Not enough arguments for $o at /, "&$o with too few args";
107 eval " &CORE::$o((1)x($args+1)) ";
108 like $@, qr/^Too many arguments for $o at /, "&$o with too many args";
112 die "Please add tests for the $p prototype";
116 test_proto '__FILE__';
117 test_proto '__LINE__';
118 test_proto '__PACKAGE__';
120 is file(), 'frob' , '__FILE__ does check its caller' ; ++ $tests;
121 is line(), 5 , '__LINE__ does check its caller' ; ++ $tests;
122 is pakg(), 'stribble', '__PACKAGE__ does check its caller'; ++ $tests;
124 test_proto 'abs', -5, 5;
128 is &CORE::accept(qw{foo bar}), undef, "&accept";
129 lis [&{"CORE::accept"}(qw{foo bar})], [undef], "&accept in list context";
131 &myaccept(my $foo, my $bar);
132 is ref $foo, 'GLOB', 'CORE::accept autovivifies its first argument';
133 is $bar, undef, 'CORE::accept does not autovivify its second argument';
136 eval { 'myaccept'->($foo, $bar) };
137 like $@, qr/^Can't use an undefined value as a symbol reference at/,
138 'CORE::accept will not accept undef 2nd arg under strict';
139 is ref $foo, 'GLOB', 'CORE::accept autovivs its first arg under strict';
147 is &CORE::bind('foo', 'bear'), undef, "&bind";
148 lis [&CORE::bind('foo', 'bear')], [undef], "&bind in list context";
149 eval { &mybind(my $foo, "bear") };
150 like $@, qr/^Bad symbol for filehandle at/,
151 'CORE::bind dies with undef first arg';
162 is $tmp, undef, '&break';
165 test_proto 'chr', 5, "\5";
168 test_proto 'closedir';
170 is &CORE::closedir(foo), undef, '&CORE::closedir';
171 lis [&CORE::closedir(foo)], [undef], '&CORE::closedir in list context';
173 test_proto 'connect';
175 is &CORE::connect('foo','bar'), undef, '&connect';
176 lis [&myconnect('foo','bar')], [undef], '&connect in list context';
178 test_proto 'continue';
190 test_proto $_ for qw(
191 endgrent endhostent endnetent endprotoent endpwent endservent
200 is &CORE::fileno(\*STDIN), fileno STDIN, '&CORE::fileno';
201 lis [&CORE::fileno(\*STDIN)], [fileno STDIN], '&CORE::fileno in list cx';
206 test_proto "get$_" for qw '
207 grent grgid grnam hostbyaddr hostbyname hostent login netbyaddr netbyname
208 netent peername ppid priority protobyname protobynumber protoent
209 pwent pwnam pwuid servbyname servbyport servent sockname sockopt
212 test_proto 'hex', ff=>255;
213 test_proto 'int', 1.5=>1;
215 test_proto 'lc', 'A', 'a';
216 test_proto 'lcfirst', 'AA', 'aA';
217 test_proto 'length', 'aaa', 3;
221 test_proto "msg$_" for qw( ctl get rcv snd );
225 is &mynot(1), !1, '¬';
226 lis [&mynot(0)], [!0], '¬ in list context';
228 test_proto 'oct', '666', 438;
229 test_proto 'opendir';
230 test_proto 'ord', chr(64), 64;
232 test_proto 'quotemeta', '$', '\$';
233 test_proto 'readdir';
234 test_proto 'readlink';
235 test_proto 'readpipe';
237 use if !is_miniperl, File::Spec::Functions, qw "catfile";
238 use if !is_miniperl, File::Temp, 'tempdir';
244 my $dir = tempdir(uc cleanup => 1);
245 my $tmpfilenam = catfile $dir, 'aaa';
246 open my $fh, ">", $tmpfilenam or die "cannot open $tmpfilenam: $!";
247 close $fh or die "cannot close $tmpfilenam: $!";
248 &myrename("$tmpfilenam", $tmpfilenam = catfile $dir,'bbb');
249 ok open(my $fh, '>', $tmpfilenam), '&rename';
252 test_proto 'ref', [], 'ARRAY';
253 test_proto 'rewinddir';
260 open my $fh, "<", \"misled" or die $!;
262 is <$fh>, 'sled', '&seek in action';
265 test_proto 'seekdir';
266 test_proto "sem$_" for qw "ctl get op";
268 test_proto "set$_" for qw '
269 grent hostent netent priority protoent pwent servent sockopt
272 test_proto "shm$_" for qw "ctl get read write";
273 test_proto 'shutdown';
275 test_proto "socket$_" for "", "pair";
276 test_proto 'sqrt', 4, 2;
277 test_proto 'symlink';
278 test_proto 'sysseek';
279 test_proto 'telldir';
283 like &mytime, '^\d+\z', '&time in scalar context';
284 like join('-', &mytime), '^\d+\z', '&time in list context';
288 like &mytimes, '^[\d.]+\z', '× in scalar context';
289 like join('-',&mytimes), '^[\d.]+-[\d.]+-[\d.]+-[\d.]+\z',
290 '× in list context';
292 test_proto 'uc', 'aa', 'AA';
293 test_proto 'ucfirst', 'aa', "Aa";
297 is &myvec("foo", 0, 4), 6, '&vec';
298 lis [&myvec("foo", 0, 4)], [6], '&vec in list context';
301 is $tmp, "goo", 'lvalue &vec';
304 test_proto 'waitpid';
306 test_proto 'wantarray';
310 $context = qw[void scalar list][&mywantarray + defined mywantarray()]
313 is $context, 'list', '&wantarray with caller in list context';
315 is($context, 'scalar', '&wantarray with caller in scalar context');
317 is($context, 'void', '&wantarray with caller in void context');
318 lis [&mywantarray],[wantarray], '&wantarray itself in list context';
320 # This is just a check to make sure we have tested everything. If we
321 # haven’t, then either the sub needs to be tested or the list in
325 require File::Spec::Functions;
327 File::Spec::Functions::catfile(
328 File::Spec::Functions::updir,'regen','keywords.pl'
330 open my $kh, $keywords_file
331 or die "$0 cannot open $keywords_file: $!";
333 if (m?__END__?..${\0} and /^[-](.*)/) {
336 $word =~ /^(?:CORE|and|cmp|dump|eq|ge|gt|le|lt|ne|or|x|xor)\z/;
338 ok exists &{"my$word"}
339 || (eval{&{"CORE::$word"}}, $@ =~ /cannot be called directly/),
340 "$word either has been tested or is not ampable";
345 # Add new tests above this line.
347 # ------------ END TESTING ----------- #
349 is curr_test, $tests+1, 'right number of tests';
354 sub file { &CORE::__FILE__ }
355 sub line { &CORE::__LINE__ } # 5
357 sub main::pakg { &CORE::__PACKAGE__ }
359 # Please do not add new tests here.