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.
17 # We need this, as in places we're testing the interaction of prototypes with
26 my $p = prototype(shift);
28 my $what = defined $c ? '(' . $p . ')' : 'no prototype';
30 print '# Testing ',$what,"\n";
33 if((defined($p) && defined($c) && $p ne $c)
34 || (defined($p) != defined($c)));
35 printf "ok %d\n",$i++;
46 testing \&no_proto, undef;
49 print "# \@_ = (",join(",",@_),")\n";
53 print "not " unless 0 == no_proto();
54 printf "ok %d\n",$i++;
56 print "not " unless 1 == no_proto(5);
57 printf "ok %d\n",$i++;
59 print "not " unless 4 == &no_proto;
60 printf "ok %d\n",$i++;
62 print "not " unless 1 == no_proto +6;
63 printf "ok %d\n",$i++;
65 print "not " unless 4 == no_proto(@_);
66 printf "ok %d\n",$i++;
73 testing \&no_args, '';
76 print "# \@_ = (",join(",",@_),")\n";
80 print "not " unless 0 == no_args();
81 printf "ok %d\n",$i++;
83 print "not " unless 0 == no_args;
84 printf "ok %d\n",$i++;
86 print "not " unless 5 == no_args +5;
87 printf "ok %d\n",$i++;
89 print "not " unless 4 == &no_args;
90 printf "ok %d\n",$i++;
92 print "not " unless 2 == &no_args(1,2);
93 printf "ok %d\n",$i++;
96 print "not " unless $@;
97 printf "ok %d\n",$i++;
103 testing \&one_args, '$';
106 print "# \@_ = (",join(",",@_),")\n";
110 print "not " unless 1 == one_args(1);
111 printf "ok %d\n",$i++;
113 print "not " unless 1 == one_args +5;
114 printf "ok %d\n",$i++;
116 print "not " unless 4 == &one_args;
117 printf "ok %d\n",$i++;
119 print "not " unless 2 == &one_args(1,2);
120 printf "ok %d\n",$i++;
122 eval "one_args(1,2)";
123 print "not " unless $@;
124 printf "ok %d\n",$i++;
127 print "not " unless $@;
128 printf "ok %d\n",$i++;
131 print "# \@_ = (",join(",",@_),")\n";
132 print "not " unless @_ == 1 && $_[0] == 4;
133 printf "ok %d\n",$i++;
142 testing \&over_one_args, '$@';
144 sub over_one_args ($@) {
145 print "# \@_ = (",join(",",@_),")\n";
149 print "not " unless 1 == over_one_args(1);
150 printf "ok %d\n",$i++;
152 print "not " unless 2 == over_one_args(1,2);
153 printf "ok %d\n",$i++;
155 print "not " unless 1 == over_one_args +5;
156 printf "ok %d\n",$i++;
158 print "not " unless 4 == &over_one_args;
159 printf "ok %d\n",$i++;
161 print "not " unless 2 == &over_one_args(1,2);
162 printf "ok %d\n",$i++;
164 print "not " unless 5 == &over_one_args(1,@_);
165 printf "ok %d\n",$i++;
167 eval "over_one_args()";
168 print "not " unless $@;
169 printf "ok %d\n",$i++;
171 sub over_one_a_args ($@) {
172 print "# \@_ = (",join(",",@_),")\n";
173 print "not " unless @_ >= 1 && $_[0] == 4;
174 printf "ok %d\n",$i++;
178 over_one_a_args(@_,1);
179 over_one_a_args(@_,1,2);
180 over_one_a_args(@_,@_);
186 testing \&scalar_and_hash, '$%';
188 sub scalar_and_hash ($%) {
189 print "# \@_ = (",join(",",@_),")\n";
193 print "not " unless 1 == scalar_and_hash(1);
194 printf "ok %d\n",$i++;
196 print "not " unless 3 == scalar_and_hash(1,2,3);
197 printf "ok %d\n",$i++;
199 print "not " unless 1 == scalar_and_hash +5;
200 printf "ok %d\n",$i++;
202 print "not " unless 4 == &scalar_and_hash;
203 printf "ok %d\n",$i++;
205 print "not " unless 2 == &scalar_and_hash(1,2);
206 printf "ok %d\n",$i++;
208 print "not " unless 5 == &scalar_and_hash(1,@_);
209 printf "ok %d\n",$i++;
211 eval "scalar_and_hash()";
212 print "not " unless $@;
213 printf "ok %d\n",$i++;
215 sub scalar_and_hash_a ($@) {
216 print "# \@_ = (",join(",",@_),")\n";
217 print "not " unless @_ >= 1 && $_[0] == 4;
218 printf "ok %d\n",$i++;
221 scalar_and_hash_a(@_);
222 scalar_and_hash_a(@_,1);
223 scalar_and_hash_a(@_,1,2);
224 scalar_and_hash_a(@_,@_);
230 testing \&one_or_two, '$;$';
232 sub one_or_two ($;$) {
233 print "# \@_ = (",join(",",@_),")\n";
237 print "not " unless 1 == one_or_two(1);
238 printf "ok %d\n",$i++;
240 print "not " unless 2 == one_or_two(1,3);
241 printf "ok %d\n",$i++;
243 print "not " unless 1 == one_or_two +5;
244 printf "ok %d\n",$i++;
246 print "not " unless 4 == &one_or_two;
247 printf "ok %d\n",$i++;
249 print "not " unless 3 == &one_or_two(1,2,3);
250 printf "ok %d\n",$i++;
252 print "not " unless 5 == &one_or_two(1,@_);
253 printf "ok %d\n",$i++;
256 print "not " unless $@;
257 printf "ok %d\n",$i++;
259 eval "one_or_two(1,2,3)";
260 print "not " unless $@;
261 printf "ok %d\n",$i++;
263 sub one_or_two_a ($;$) {
264 print "# \@_ = (",join(",",@_),")\n";
265 print "not " unless @_ >= 1 && $_[0] == 4;
266 printf "ok %d\n",$i++;
277 testing \&a_sub, '&';
280 print "# \@_ = (",join(",",@_),")\n";
284 sub tmp_sub_1 { printf "ok %d\n",$i++ }
286 a_sub { printf "ok %d\n",$i++ };
289 @array = ( \&tmp_sub_1 );
291 print "not " unless $@;
292 printf "ok %d\n",$i++;
298 testing \&a_subx, '\&';
301 print "# \@_ = (",join(",",@_),")\n";
305 sub tmp_sub_2 { printf "ok %d\n",$i++ }
308 @array = ( \&tmp_sub_2 );
309 eval 'a_subx @array';
310 print "not " unless $@;
311 printf "ok %d\n",$i++;
317 testing \&sub_aref, '&\@';
320 print "# \@_ = (",join(",",@_),")\n";
321 my($sub,$array) = @_;
322 print "not " unless @_ == 2 && @{$array} == 4;
323 print map { &{$sub}($_) } @{$array}
326 @array = (qw(O K)," ", $i++);
327 sub_aref { lc shift } @array;
334 testing \&sub_array, '&@';
337 print "# \@_ = (",join(",",@_),")\n";
338 print "not " unless @_ == 5;
340 print map { &{$sub}($_) } @_
343 @array = (qw(O K)," ", $i++);
344 sub_array { lc shift } @array;
345 sub_array { lc shift } ('O', 'K', ' ', $i++);
352 testing \&a_hash, '%';
355 print "# \@_ = (",join(",",@_),")\n";
359 print "not " unless 1 == a_hash 'a';
360 printf "ok %d\n",$i++;
362 print "not " unless 2 == a_hash 'a','b';
363 printf "ok %d\n",$i++;
369 testing \&a_hash_ref, '\%';
371 sub a_hash_ref (\%) {
372 print "# \@_ = (",join(",",@_),")\n";
373 print "not " unless ref($_[0]) && $_[0]->{'a'};
374 printf "ok %d\n",$i++;
380 print "not " unless $hash{'b'} == 2;
381 printf "ok %d\n",$i++;
387 testing \&array_ref_plus, '\@@';
389 sub array_ref_plus (\@@) {
390 print "# \@_ = (",join(",",@_),")\n";
391 print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
392 printf "ok %d\n",$i++;
393 @{$_[0]} = (qw(ok)," ",$i++,"\n");
398 array_ref_plus @array, @more; }
399 print "not " unless @array == 4;
403 print "not " if defined prototype('CORE::print');
404 print "ok ", $i++, "\n";
406 print "not " if defined prototype('CORE::system');
407 print "ok ", $i++, "\n";
409 print "# CORE::open => ($p)\nnot " if ($p = prototype('CORE::open')) ne '*;$@';
410 print "ok ", $i++, "\n";
412 print "# CORE::Foo => ($p), \$@ => '$@'\nnot "
413 if defined ($p = eval { prototype('CORE::Foo') or 1 }) or $@ !~ /^Can't find an opnumber/;
414 print "ok ", $i++, "\n";
416 eval { prototype("CORE::a\0b") };
417 print "# CORE::a\\0b: \$@ => '$@'\nnot "
418 if $@ !~ /^Can't find an opnumber for "a\0b"/;
419 print "ok ", $i++, "\n";
421 eval { prototype("CORE::\x{100}") };
422 print "# CORE::\\x{100}: => ($p), \$@ => '$@'\nnot "
423 if $@ !~ /^Can't find an opnumber for "\x{100}"/;
424 print "ok ", $i++, "\n";
426 "CORE::Foo" =~ /(.*)/;
427 print "# \$1 containing CORE::Foo => ($p), \$@ => '$@'\nnot "
428 if defined ($p = eval { prototype($1) or 1 })
429 or $@ !~ /^Can't find an opnumber/;
430 print "ok ", $i++, " - \$1 containing CORE::Foo\n";
432 # correctly note too-short parameter lists that don't end with '$',
433 # a possible regression.
437 print "not " unless $@ =~ /^Not enough/;
438 print "ok ", $i++, "\n";
442 print "not " unless $@ =~ /^Not enough/;
443 print "ok ", $i++, "\n";
446 *X::foo3 = sub {'ok'};
447 print "# $@not " unless eval {X->foo3} eq 'ok';
448 print "ok ", $i++, "\n";
451 *X::foo4 = sub ($) {'ok'};
452 print "not " unless X->foo4 eq 'ok';
453 print "ok ", $i++, "\n";
455 # test if the (*) prototype allows barewords, constants, scalar expressions,
456 # globs and globrefs (just as CORE::open() does), all under stricture
457 sub star (*&) { &{$_[1]} }
458 sub star2 (**&) { &{$_[2]} }
460 sub Bar::BAZ { "quuz" }
463 print "not " unless $_[0] eq 'FOO';
464 print "ok $i - star FOO\n";
467 print "not " unless $_[0] eq 'FOO';
468 print "ok $i - star(FOO)\n";
471 print "not " unless $_[0] eq 'FOO';
472 print qq/ok $i - star "FOO"\n/;
475 print "not " unless $_[0] eq 'FOO';
476 print qq/ok $i - star("FOO")\n/;
479 print "not " unless $_[0] eq 'FOO';
480 print "ok $i - star \$star\n";
483 print "not " unless $_[0] eq 'FOO';
484 print "ok $i - star(\$star)\n";
487 print "not " unless $_[0] eq \*FOO;
488 print "ok $i - star *FOO\n";
491 print "not " unless $_[0] eq \*FOO;
492 print "ok $i - star(*FOO)\n";
495 print "not " unless $_[0] eq \*FOO;
496 print "ok $i - star \\*FOO\n";
499 print "not " unless $_[0] eq \*FOO;
500 print "ok $i - star(\\*FOO)\n";
502 star2 FOO, BAR, sub {
503 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
504 print "ok $i - star2 FOO, BAR\n";
506 star2(Bar::BAZ, FOO, sub {
507 print "not " unless $_[0] eq 'Bar::BAZ' and $_[1] eq 'FOO';
508 print "ok $i - star2(Bar::BAZ, FOO)\n"
510 star2 BAR(), FOO, sub {
511 print "not " unless $_[0] eq 'quux' and $_[1] eq 'FOO';
512 print "ok $i - star2 BAR(), FOO\n"
514 star2(FOO, BAR(), sub {
515 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'quux';
516 print "ok $i - star2(FOO, BAR())\n";
518 star2 "FOO", "BAR", sub {
519 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
520 print qq/ok $i - star2 "FOO", "BAR"\n/;
522 star2("FOO", "BAR", sub {
523 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
524 print qq/ok $i - star2("FOO", "BAR")\n/;
526 star2 $star, $star, sub {
527 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
528 print "ok $i - star2 \$star, \$star\n";
530 star2($star, $star, sub {
531 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
532 print "ok $i - star2(\$star, \$star)\n";
534 star2 *FOO, *BAR, sub {
535 print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
536 print "ok $i - star2 *FOO, *BAR\n";
538 star2(*FOO, *BAR, sub {
539 print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
540 print "ok $i - star2(*FOO, *BAR)\n";
542 star2 \*FOO, \*BAR, sub {
544 print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
545 print "ok $i - star2 \*FOO, \*BAR\n";
547 star2(\*FOO, \*BAR, sub {
549 print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
550 print "ok $i - star2(\*FOO, \*BAR)\n";
553 # test scalarref prototype
555 print "not " unless ref $_[0];
556 print "ok $_[1] - sreftest\n";
560 sreftest my $sref, $i++;
561 sreftest($helem{$i}, $i++);
562 sreftest $aelem[0], $i++;
563 sreftest sub { [0] }->()[0], $i++;
564 sreftest my $a = 'quidgley', $i++;
565 print "not " if eval 'return 1; sreftest(3+4)';
566 print "ok ", $i++, ' - \$ with invalid argument', "\n";
571 print "not " unless @_ == 3 && ref $_[0] eq $_[1];
572 print "ok $_[2] - non container test\n";
574 sub quietlazy (+) { return shift(@_) }
576 sub list_or_scalar { wantarray ? (1..10) : [] }
578 my @multiarray = ("a".."z");
579 my %bighash = @multiarray;
580 lazy(\@multiarray, 'ARRAY', $i++);
581 lazy(\%bighash, 'HASH', $i++);
582 lazy({}, 'HASH', $i++);
583 lazy(give_aref, 'ARRAY', $i++);
584 lazy(3, '', $i++); # allowed by prototype, even if runtime error
585 lazy(list_or_scalar, 'ARRAY', $i++); # propagate scalar context
588 # test prototypes when they are evaled and there is a syntax error
589 # Byacc generates the string "syntax error". Bison gives the
590 # string "parse error".
592 for my $p ( "", qw{ () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@) } ) {
594 local $SIG{__WARN__} = sub {
595 my $thiswarn = join("",@_);
596 return if $thiswarn =~ /^Prototype mismatch: sub main::evaled_subroutine/;
599 my $eval = "sub evaled_subroutine $p { &void *; }";
601 print "# eval[$eval]\nnot " unless $@ && $@ =~ /(parse|syntax) error/i;
602 print "ok ", $i++, "\n";
604 print "ok ", $i++, "\n";
606 print "not ok ", $i++, "# $warn \n";
614 sub mysub { print "not calling mysub I hope\n" }
617 sub myref (\[$@%&*]) { print "# $_[0]\n"; return "$_[0]" }
619 print "not " unless myref($myvar) =~ /^SCALAR\(/;
620 print "ok ", $i++, "\n";
621 print "not " unless myref($myvar=7) =~ /^SCALAR\(/;
622 print "ok ", $i++, "\n";
623 print "not " unless myref(@myarray) =~ /^ARRAY\(/;
624 print "ok ", $i++, "\n";
625 print "not " unless myref(%myhash) =~ /^HASH\(/;
626 print "ok ", $i++, "\n";
627 print "not " unless myref(&mysub) =~ /^CODE\(/;
628 print "ok ", $i++, "\n";
629 print "not " unless myref(*myglob) =~ /^GLOB\(/;
630 print "ok ", $i++, "\n";
632 eval q/sub multi1 (\[%@]) { 1 } multi1 $myvar;/;
634 unless $@ =~ /Type of arg 1 to main::multi1 must be one of \[%\@\] /;
635 print "ok ", $i++, "\n";
636 eval q/sub multi2 (\[$*&]) { 1 } multi2 @myarray;/;
638 unless $@ =~ /Type of arg 1 to main::multi2 must be one of \[\$\*&\] /;
639 print "ok ", $i++, "\n";
640 eval q/sub multi3 (\[$@]) { 1 } multi3 %myhash;/;
642 unless $@ =~ /Type of arg 1 to main::multi3 must be one of \[\$\@\] /;
643 print "ok ", $i++, "\n";
644 eval q/sub multi4 ($\[%]) { 1 } multi4 1, &mysub;/;
646 unless $@ =~ /Type of arg 2 to main::multi4 must be one of \[%\] /;
647 print "ok ", $i++, "\n";
648 eval q/sub multi5 (\[$@]$) { 1 } multi5 *myglob;/;
650 unless $@ =~ /Type of arg 1 to main::multi5 must be one of \[\$\@\] /
651 && $@ =~ /Not enough arguments/;
652 print "ok ", $i++, "\n";
655 # check that obviously bad prototypes are getting warnings
659 local $SIG{__WARN__} = sub { $warn .= join("",@_) };
661 eval 'sub badproto (@bar) { 1; }';
662 print "not " unless $warn =~ /Illegal character in prototype for main::badproto : \@bar/;
663 print "ok ", $i++, "\n";
665 eval 'sub badproto2 (bar) { 1; }';
666 print "not " unless $warn =~ /Illegal character in prototype for main::badproto2 : bar/;
667 print "ok ", $i++, "\n";
669 eval 'sub badproto3 (&$bar$@) { 1; }';
670 print "not " unless $warn =~ /Illegal character in prototype for main::badproto3 : &\$bar\$\@/;
671 print "ok ", $i++, "\n";
673 eval 'sub badproto4 (@ $b ar) { 1; }';
674 print "not " unless $warn =~ /Illegal character in prototype for main::badproto4 : \@\$bar/;
675 print "ok ", $i++, "\n";
678 # make sure whitespace in prototypes works
679 eval "sub good (\$\t\$\n\$) { 1; }";
681 print "ok ", $i++, "\n";
683 # Ought to fail, doesn't in 5.8.1.
684 eval 'sub bug (\[%@]) { } my $array = [0 .. 1]; bug %$array;';
685 print "not " unless $@ =~ /Not a HASH reference/;
686 print "ok ", $i++, "\n";
689 # Test that the following prototypes make subs parse as unary functions:
690 # * \sigil \[...] ;$ ;* ;\sigil ;\[...]
692 unless eval 'sub uniproto1 (*) {} uniproto1 $_, 1' or warn $@;
693 print "ok ", $i++, "\n";
695 unless eval 'sub uniproto2 (\$) {} uniproto2 $_, 1' or warn $@;
696 print "ok ", $i++, "\n";
698 unless eval 'sub uniproto3 (\[$%]) {} uniproto3 %_, 1' or warn $@;
699 print "ok ", $i++, "\n";
701 unless eval 'sub uniproto4 (;$) {} uniproto4 $_, 1' or warn $@;
702 print "ok ", $i++, "\n";
704 unless eval 'sub uniproto5 (;*) {} uniproto5 $_, 1' or warn $@;
705 print "ok ", $i++, "\n";
707 unless eval 'sub uniproto6 (;\@) {} uniproto6 @_, 1' or warn $@;
708 print "ok ", $i++, "\n";
710 unless eval 'sub uniproto7 (;\[$%@]) {} uniproto7 @_, 1' or warn $@;
711 print "ok ", $i++, "\n";
713 unless eval 'sub uniproto8 (+) {} uniproto8 $_, 1' or warn $@;
714 print "ok ", $i++, "\n";
716 unless eval 'sub uniproto9 (;+) {} uniproto9 $_, 1' or warn $@;
717 print "ok ", $i++, "\n";
719 # Test that a trailing semicolon makes a sub have listop precedence
720 sub unilist ($;) { $_[0]+1 }
721 sub unilist2(_;) { $_[0]+1 }
722 sub unilist3(;$;) { $_[0]+1 }
723 print "not " unless (unilist 0 || 5) == 6;
724 print "ok ", $i++, "\n";
725 print "not " unless (unilist2 0 || 5) == 6;
726 print "ok ", $i++, "\n";
727 print "not " unless (unilist3 0 || 5) == 6;
728 print "ok ", $i++, "\n";
731 # Lack of prototype on a subroutine definition should override any prototype
732 # on the declaration.
735 local $SIG{__WARN__} = sub {
736 my $thiswarn = join "",@_;
737 if ($thiswarn =~ /^Prototype mismatch: sub main::z_zwap/) {
738 print 'ok ', $i++, "\n";
740 print 'not ok ', $i++, "\n";
741 print STDERR $thiswarn;
745 eval q{sub z_zwap {return @_}};
748 print "not ok ", $i++, "# $@";
750 print "ok ", $i++, "\n";
755 my @got = eval q{z_zwap(@a)};
758 print "not ok ", $i++, " # $@";
760 print "ok ", $i++, "\n";
763 if ("@got" eq "@a") {
764 print "ok ", $i++, "\n";
766 print "not ok ", $i++, " # >@got<\n";