3 # Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
5 # So far there are tests for the following prototypes.
6 # none, () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@)
8 # It is impossible to test every prototype that can be specified, but
9 # we should test as many as we can.
24 my $p = prototype(shift);
26 my $what = defined $c ? '(' . $p . ')' : 'no prototype';
28 print '# Testing ',$what,"\n";
31 if((defined($p) && defined($c) && $p ne $c)
32 || (defined($p) != defined($c)));
33 printf "ok %d\n",$i++;
44 testing \&no_proto, undef;
47 print "# \@_ = (",join(",",@_),")\n";
51 print "not " unless 0 == no_proto();
52 printf "ok %d\n",$i++;
54 print "not " unless 1 == no_proto(5);
55 printf "ok %d\n",$i++;
57 print "not " unless 4 == &no_proto;
58 printf "ok %d\n",$i++;
60 print "not " unless 1 == no_proto +6;
61 printf "ok %d\n",$i++;
63 print "not " unless 4 == no_proto(@_);
64 printf "ok %d\n",$i++;
71 testing \&no_args, '';
74 print "# \@_ = (",join(",",@_),")\n";
78 print "not " unless 0 == no_args();
79 printf "ok %d\n",$i++;
81 print "not " unless 0 == no_args;
82 printf "ok %d\n",$i++;
84 print "not " unless 5 == no_args +5;
85 printf "ok %d\n",$i++;
87 print "not " unless 4 == &no_args;
88 printf "ok %d\n",$i++;
90 print "not " unless 2 == &no_args(1,2);
91 printf "ok %d\n",$i++;
94 print "not " unless $@;
95 printf "ok %d\n",$i++;
101 testing \&one_args, '$';
104 print "# \@_ = (",join(",",@_),")\n";
108 print "not " unless 1 == one_args(1);
109 printf "ok %d\n",$i++;
111 print "not " unless 1 == one_args +5;
112 printf "ok %d\n",$i++;
114 print "not " unless 4 == &one_args;
115 printf "ok %d\n",$i++;
117 print "not " unless 2 == &one_args(1,2);
118 printf "ok %d\n",$i++;
120 eval "one_args(1,2)";
121 print "not " unless $@;
122 printf "ok %d\n",$i++;
125 print "not " unless $@;
126 printf "ok %d\n",$i++;
129 print "# \@_ = (",join(",",@_),")\n";
130 print "not " unless @_ == 1 && $_[0] == 4;
131 printf "ok %d\n",$i++;
140 testing \&over_one_args, '$@';
142 sub over_one_args ($@) {
143 print "# \@_ = (",join(",",@_),")\n";
147 print "not " unless 1 == over_one_args(1);
148 printf "ok %d\n",$i++;
150 print "not " unless 2 == over_one_args(1,2);
151 printf "ok %d\n",$i++;
153 print "not " unless 1 == over_one_args +5;
154 printf "ok %d\n",$i++;
156 print "not " unless 4 == &over_one_args;
157 printf "ok %d\n",$i++;
159 print "not " unless 2 == &over_one_args(1,2);
160 printf "ok %d\n",$i++;
162 print "not " unless 5 == &over_one_args(1,@_);
163 printf "ok %d\n",$i++;
165 eval "over_one_args()";
166 print "not " unless $@;
167 printf "ok %d\n",$i++;
169 sub over_one_a_args ($@) {
170 print "# \@_ = (",join(",",@_),")\n";
171 print "not " unless @_ >= 1 && $_[0] == 4;
172 printf "ok %d\n",$i++;
176 over_one_a_args(@_,1);
177 over_one_a_args(@_,1,2);
178 over_one_a_args(@_,@_);
184 testing \&scalar_and_hash, '$%';
186 sub scalar_and_hash ($%) {
187 print "# \@_ = (",join(",",@_),")\n";
191 print "not " unless 1 == scalar_and_hash(1);
192 printf "ok %d\n",$i++;
194 print "not " unless 3 == scalar_and_hash(1,2,3);
195 printf "ok %d\n",$i++;
197 print "not " unless 1 == scalar_and_hash +5;
198 printf "ok %d\n",$i++;
200 print "not " unless 4 == &scalar_and_hash;
201 printf "ok %d\n",$i++;
203 print "not " unless 2 == &scalar_and_hash(1,2);
204 printf "ok %d\n",$i++;
206 print "not " unless 5 == &scalar_and_hash(1,@_);
207 printf "ok %d\n",$i++;
209 eval "scalar_and_hash()";
210 print "not " unless $@;
211 printf "ok %d\n",$i++;
213 sub scalar_and_hash_a ($@) {
214 print "# \@_ = (",join(",",@_),")\n";
215 print "not " unless @_ >= 1 && $_[0] == 4;
216 printf "ok %d\n",$i++;
219 scalar_and_hash_a(@_);
220 scalar_and_hash_a(@_,1);
221 scalar_and_hash_a(@_,1,2);
222 scalar_and_hash_a(@_,@_);
228 testing \&one_or_two, '$;$';
230 sub one_or_two ($;$) {
231 print "# \@_ = (",join(",",@_),")\n";
235 print "not " unless 1 == one_or_two(1);
236 printf "ok %d\n",$i++;
238 print "not " unless 2 == one_or_two(1,3);
239 printf "ok %d\n",$i++;
241 print "not " unless 1 == one_or_two +5;
242 printf "ok %d\n",$i++;
244 print "not " unless 4 == &one_or_two;
245 printf "ok %d\n",$i++;
247 print "not " unless 3 == &one_or_two(1,2,3);
248 printf "ok %d\n",$i++;
250 print "not " unless 5 == &one_or_two(1,@_);
251 printf "ok %d\n",$i++;
254 print "not " unless $@;
255 printf "ok %d\n",$i++;
257 eval "one_or_two(1,2,3)";
258 print "not " unless $@;
259 printf "ok %d\n",$i++;
261 sub one_or_two_a ($;$) {
262 print "# \@_ = (",join(",",@_),")\n";
263 print "not " unless @_ >= 1 && $_[0] == 4;
264 printf "ok %d\n",$i++;
275 testing \&a_sub, '&';
278 print "# \@_ = (",join(",",@_),")\n";
282 sub tmp_sub_1 { printf "ok %d\n",$i++ }
284 a_sub { printf "ok %d\n",$i++ };
287 @array = ( \&tmp_sub_1 );
289 print "not " unless $@;
290 printf "ok %d\n",$i++;
296 testing \&a_subx, '\&';
299 print "# \@_ = (",join(",",@_),")\n";
303 sub tmp_sub_2 { printf "ok %d\n",$i++ }
306 @array = ( \&tmp_sub_2 );
307 eval 'a_subx @array';
308 print "not " unless $@;
309 printf "ok %d\n",$i++;
315 testing \&sub_aref, '&\@';
318 print "# \@_ = (",join(",",@_),")\n";
319 my($sub,$array) = @_;
320 print "not " unless @_ == 2 && @{$array} == 4;
321 print map { &{$sub}($_) } @{$array}
324 @array = (qw(O K)," ", $i++);
325 sub_aref { lc shift } @array;
332 testing \&sub_array, '&@';
335 print "# \@_ = (",join(",",@_),")\n";
336 print "not " unless @_ == 5;
338 print map { &{$sub}($_) } @_
341 @array = (qw(O K)," ", $i++);
342 sub_array { lc shift } @array;
343 sub_array { lc shift } ('O', 'K', ' ', $i++);
350 testing \&a_hash, '%';
353 print "# \@_ = (",join(",",@_),")\n";
357 print "not " unless 1 == a_hash 'a';
358 printf "ok %d\n",$i++;
360 print "not " unless 2 == a_hash 'a','b';
361 printf "ok %d\n",$i++;
367 testing \&a_hash_ref, '\%';
369 sub a_hash_ref (\%) {
370 print "# \@_ = (",join(",",@_),")\n";
371 print "not " unless ref($_[0]) && $_[0]->{'a'};
372 printf "ok %d\n",$i++;
378 print "not " unless $hash{'b'} == 2;
379 printf "ok %d\n",$i++;
385 testing \&array_ref_plus, '\@@';
387 sub array_ref_plus (\@@) {
388 print "# \@_ = (",join(",",@_),")\n";
389 print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
390 printf "ok %d\n",$i++;
391 @{$_[0]} = (qw(ok)," ",$i++,"\n");
396 array_ref_plus @array, @more; }
397 print "not " unless @array == 4;
401 print "not " if defined prototype('CORE::print');
402 print "ok ", $i++, "\n";
404 print "not " if defined prototype('CORE::system');
405 print "ok ", $i++, "\n";
407 print "# CORE::open => ($p)\nnot " if ($p = prototype('CORE::open')) ne '*;$@';
408 print "ok ", $i++, "\n";
410 print "# CORE:Foo => ($p), \$@ => `$@'\nnot "
411 if defined ($p = eval { prototype('CORE::Foo') or 1 }) or $@ !~ /^Can't find an opnumber/;
412 print "ok ", $i++, "\n";
414 # correctly note too-short parameter lists that don't end with '$',
415 # a possible regression.
419 print "not " unless $@ =~ /^Not enough/;
420 print "ok ", $i++, "\n";
424 print "not " unless $@ =~ /^Not enough/;
425 print "ok ", $i++, "\n";
428 *X::foo3 = sub {'ok'};
429 print "# $@not " unless eval {X->foo3} eq 'ok';
430 print "ok ", $i++, "\n";
433 *X::foo4 = sub ($) {'ok'};
434 print "not " unless X->foo4 eq 'ok';
435 print "ok ", $i++, "\n";
437 # test if the (*) prototype allows barewords, constants, scalar expressions,
438 # globs and globrefs (just as CORE::open() does), all under stricture
439 sub star (*&) { &{$_[1]} }
440 sub star2 (**&) { &{$_[2]} }
442 sub Bar::BAZ { "quuz" }
445 print "not " unless $_[0] eq 'FOO';
446 print "ok $i - star FOO\n";
449 print "not " unless $_[0] eq 'FOO';
450 print "ok $i - star(FOO)\n";
453 print "not " unless $_[0] eq 'FOO';
454 print qq/ok $i - star "FOO"\n/;
457 print "not " unless $_[0] eq 'FOO';
458 print qq/ok $i - star("FOO")\n/;
461 print "not " unless $_[0] eq 'FOO';
462 print "ok $i - star \$star\n";
465 print "not " unless $_[0] eq 'FOO';
466 print "ok $i - star(\$star)\n";
469 print "not " unless $_[0] eq \*FOO;
470 print "ok $i - star *FOO\n";
473 print "not " unless $_[0] eq \*FOO;
474 print "ok $i - star(*FOO)\n";
477 print "not " unless $_[0] eq \*FOO;
478 print "ok $i - star \\*FOO\n";
481 print "not " unless $_[0] eq \*FOO;
482 print "ok $i - star(\\*FOO)\n";
484 star2 FOO, BAR, sub {
485 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
486 print "ok $i - star2 FOO, BAR\n";
488 star2(Bar::BAZ, FOO, sub {
489 print "not " unless $_[0] eq 'Bar::BAZ' and $_[1] eq 'FOO';
490 print "ok $i - star2(Bar::BAZ, FOO)\n"
492 star2 BAR(), FOO, sub {
493 print "not " unless $_[0] eq 'quux' and $_[1] eq 'FOO';
494 print "ok $i - star2 BAR(), FOO\n"
496 star2(FOO, BAR(), sub {
497 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'quux';
498 print "ok $i - star2(FOO, BAR())\n";
500 star2 "FOO", "BAR", sub {
501 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
502 print qq/ok $i - star2 "FOO", "BAR"\n/;
504 star2("FOO", "BAR", sub {
505 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
506 print qq/ok $i - star2("FOO", "BAR")\n/;
508 star2 $star, $star, sub {
509 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
510 print "ok $i - star2 \$star, \$star\n";
512 star2($star, $star, sub {
513 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
514 print "ok $i - star2(\$star, \$star)\n";
516 star2 *FOO, *BAR, sub {
517 print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
518 print "ok $i - star2 *FOO, *BAR\n";
520 star2(*FOO, *BAR, sub {
521 print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
522 print "ok $i - star2(*FOO, *BAR)\n";
524 star2 \*FOO, \*BAR, sub {
526 print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
527 print "ok $i - star2 \*FOO, \*BAR\n";
529 star2(\*FOO, \*BAR, sub {
531 print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
532 print "ok $i - star2(\*FOO, \*BAR)\n";
535 # test scalarref prototype
537 print "not " unless ref $_[0];
538 print "ok $_[1] - sreftest\n";
542 sreftest my $sref, $i++;
543 sreftest($helem{$i}, $i++);
544 sreftest $aelem[0], $i++;
547 # test prototypes when they are evaled and there is a syntax error
548 # Byacc generates the string "syntax error". Bison gives the
549 # string "parse error".
551 for my $p ( "", qw{ () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@) } ) {
552 no warnings 'prototype';
553 my $eval = "sub evaled_subroutine $p { &void *; }";
555 print "# eval[$eval]\nnot " unless $@ && $@ =~ /(parse|syntax) error/i;
556 print "ok ", $i++, "\n";
560 print "not " unless prototype "CORE::substr" eq '$$;$$';
561 print "ok ", $i++, "\n";
563 # recv takes a scalar reference for its second argument
564 print "not " unless prototype "CORE::recv" eq '*\\$$$';
565 print "ok ", $i++, "\n";
571 sub mysub { print "not calling mysub I hope\n" }
574 sub myref (\[$@%&*]) { print "# $_[0]\n"; return "$_[0]" }
576 print "not " unless myref($myvar) =~ /^SCALAR\(/;
577 print "ok ", $i++, "\n";
578 print "not " unless myref(@myarray) =~ /^ARRAY\(/;
579 print "ok ", $i++, "\n";
580 print "not " unless myref(%myhash) =~ /^HASH\(/;
581 print "ok ", $i++, "\n";
582 print "not " unless myref(&mysub) =~ /^CODE\(/;
583 print "ok ", $i++, "\n";
584 print "not " unless myref(*myglob) =~ /^GLOB\(/;
585 print "ok ", $i++, "\n";
587 eval q/sub multi1 (\[%@]) { 1 } multi1 $myvar;/;
588 print "not " unless $@ =~ /Type of arg 1 to main::multi1 must be one of/;
589 print "ok ", $i++, "\n";
590 eval q/sub multi2 (\[$*&]) { 1 } multi2 @myarray;/;
591 print "not " unless $@ =~ /Type of arg 1 to main::multi2 must be one of/;
592 print "ok ", $i++, "\n";
593 eval q/sub multi3 (\[$@]) { 1 } multi3 %myhash;/;
594 print "not " unless $@ =~ /Type of arg 1 to main::multi3 must be one of/;
595 print "ok ", $i++, "\n";
596 eval q/sub multi4 ($\[%]) { 1 } multi4 1, &mysub;/;
597 print "not " unless $@ =~ /Type of arg 2 to main::multi4 must be one of/;
598 print "ok ", $i++, "\n";
599 eval q/sub multi5 (\[$@]$) { 1 } multi5 *myglob;/;
600 print "not " unless $@ =~ /Type of arg 1 to main::multi5 must be one of/
601 && $@ =~ /Not enough arguments/;
602 print "ok ", $i++, "\n";
605 # check that obviously bad prototypes are getting warnings
607 use warnings 'syntax';
609 local $SIG{__WARN__} = sub { $warn .= join("",@_) };
611 eval 'sub badproto (@bar) { 1; }';
612 print "not " unless $warn =~ /Illegal character in prototype for main::badproto : \@bar/;
613 print "ok ", $i++, "\n";
615 eval 'sub badproto2 (bar) { 1; }';
616 print "not " unless $warn =~ /Illegal character in prototype for main::badproto2 : bar/;
617 print "ok ", $i++, "\n";
619 eval 'sub badproto3 (&$bar$@) { 1; }';
620 print "not " unless $warn =~ /Illegal character in prototype for main::badproto3 : &\$bar\$\@/;
621 print "ok ", $i++, "\n";
623 eval 'sub badproto4 (@ $b ar) { 1; }';
624 print "not " unless $warn =~ /Illegal character in prototype for main::badproto4 : \@\$bar/;
625 print "ok ", $i++, "\n";
628 # make sure whitespace in prototypes works
629 eval "sub good (\$\t\$\n\$) { 1; }";
631 print "ok ", $i++, "\n";
633 # Ought to fail, doesn't in 5.8.1.
634 eval 'sub bug (\[%@]) { } my $array = [0 .. 1]; bug %$array;';
635 print "not " unless $@ =~ /Not a HASH reference/;
636 print "ok ", $i++, "\n";