#
# It is impossible to test every prototype that can be specified, but
# we should test as many as we can.
+#
-use strict;
-
-my $i = 1;
+BEGIN {
+ chdir 't' if -d 't';
+ @INC = '../lib';
+}
-##
-## Something really weird is happening here. Try changing the order
-## of the next three lines, and try moving them to after the definition
-## of testing, some combinations cause the script to fail while
-## running tests on (&\@)
-##
+use strict;
-my %hash;
-my @array;
-@_ = qw(a b c d);
+print "1..140\n";
-print "1..74\n";
+my $i = 1;
sub testing (&$) {
my $p = prototype(shift);
printf "ok %d\n",$i++;
}
+@_ = qw(a b c d);
+my @array;
+my %hash;
##
##
##
##
+testing \&a_subx, '\&';
+
+sub a_subx (\&) {
+ print "# \@_ = (",join(",",@_),")\n";
+ &{$_[0]};
+}
+
+sub tmp_sub_2 { printf "ok %d\n",$i++ }
+a_subx &tmp_sub_2;
+
+@array = ( \&tmp_sub_2 );
+eval 'a_subx @array';
+print "not " unless $@;
+printf "ok %d\n",$i++;
+
+##
+##
+##
+
testing \&sub_aref, '&\@';
sub sub_aref (&\@) {
@array = (qw(O K)," ", $i++);
sub_array { lc shift } @array;
+sub_array { lc shift } ('O', 'K', ' ', $i++);
print "\n";
##
##
##
-testing \&an_array_ref, '\@';
+testing \&array_ref_plus, '\@@';
-sub an_array_ref (\@) {
+sub array_ref_plus (\@@) {
print "# \@_ = (",join(",",@_),")\n";
- print "not " unless ref($_[0]) && 1 == @{$_[0]};
+ print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
printf "ok %d\n",$i++;
@{$_[0]} = (qw(ok)," ",$i++,"\n");
}
@array = ('a');
-an_array_ref @array;
+{ my @more = ('x');
+ array_ref_plus @array, @more; }
print "not " unless @array == 4;
print @array;
+
+my $p;
+print "not " if defined prototype('CORE::print');
+print "ok ", $i++, "\n";
+
+print "not " if defined prototype('CORE::system');
+print "ok ", $i++, "\n";
+
+print "# CORE::open => ($p)\nnot " if ($p = prototype('CORE::open')) ne '*;$@';
+print "ok ", $i++, "\n";
+
+print "# CORE:Foo => ($p), \$@ => `$@'\nnot "
+ if defined ($p = eval { prototype('CORE::Foo') or 1 }) or $@ !~ /^Can't find an opnumber/;
+print "ok ", $i++, "\n";
+
+# correctly note too-short parameter lists that don't end with '$',
+# a possible regression.
+
+sub foo1 ($\@);
+eval q{ foo1 "s" };
+print "not " unless $@ =~ /^Not enough/;
+print "ok ", $i++, "\n";
+
+sub foo2 ($\%);
+eval q{ foo2 "s" };
+print "not " unless $@ =~ /^Not enough/;
+print "ok ", $i++, "\n";
+
+sub X::foo3;
+*X::foo3 = sub {'ok'};
+print "# $@not " unless eval {X->foo3} eq 'ok';
+print "ok ", $i++, "\n";
+
+sub X::foo4 ($);
+*X::foo4 = sub ($) {'ok'};
+print "not " unless X->foo4 eq 'ok';
+print "ok ", $i++, "\n";
+
+# test if the (*) prototype allows barewords, constants, scalar expressions,
+# globs and globrefs (just as CORE::open() does), all under stricture
+sub star (*&) { &{$_[1]} }
+sub star2 (**&) { &{$_[2]} }
+sub BAR { "quux" }
+sub Bar::BAZ { "quuz" }
+my $star = 'FOO';
+star FOO, sub {
+ print "not " unless $_[0] eq 'FOO';
+ print "ok $i - star FOO\n";
+}; $i++;
+star(FOO, sub {
+ print "not " unless $_[0] eq 'FOO';
+ print "ok $i - star(FOO)\n";
+ }); $i++;
+star "FOO", sub {
+ print "not " unless $_[0] eq 'FOO';
+ print qq/ok $i - star "FOO"\n/;
+}; $i++;
+star("FOO", sub {
+ print "not " unless $_[0] eq 'FOO';
+ print qq/ok $i - star("FOO")\n/;
+ }); $i++;
+star $star, sub {
+ print "not " unless $_[0] eq 'FOO';
+ print "ok $i - star \$star\n";
+}; $i++;
+star($star, sub {
+ print "not " unless $_[0] eq 'FOO';
+ print "ok $i - star(\$star)\n";
+ }); $i++;
+star *FOO, sub {
+ print "not " unless $_[0] eq \*FOO;
+ print "ok $i - star *FOO\n";
+}; $i++;
+star(*FOO, sub {
+ print "not " unless $_[0] eq \*FOO;
+ print "ok $i - star(*FOO)\n";
+ }); $i++;
+star \*FOO, sub {
+ print "not " unless $_[0] eq \*FOO;
+ print "ok $i - star \\*FOO\n";
+}; $i++;
+star(\*FOO, sub {
+ print "not " unless $_[0] eq \*FOO;
+ print "ok $i - star(\\*FOO)\n";
+ }); $i++;
+star2 FOO, BAR, sub {
+ print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
+ print "ok $i - star2 FOO, BAR\n";
+}; $i++;
+star2(Bar::BAZ, FOO, sub {
+ print "not " unless $_[0] eq 'Bar::BAZ' and $_[1] eq 'FOO';
+ print "ok $i - star2(Bar::BAZ, FOO)\n"
+ }); $i++;
+star2 BAR(), FOO, sub {
+ print "not " unless $_[0] eq 'quux' and $_[1] eq 'FOO';
+ print "ok $i - star2 BAR(), FOO\n"
+}; $i++;
+star2(FOO, BAR(), sub {
+ print "not " unless $_[0] eq 'FOO' and $_[1] eq 'quux';
+ print "ok $i - star2(FOO, BAR())\n";
+ }); $i++;
+star2 "FOO", "BAR", sub {
+ print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
+ print qq/ok $i - star2 "FOO", "BAR"\n/;
+}; $i++;
+star2("FOO", "BAR", sub {
+ print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
+ print qq/ok $i - star2("FOO", "BAR")\n/;
+ }); $i++;
+star2 $star, $star, sub {
+ print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
+ print "ok $i - star2 \$star, \$star\n";
+}; $i++;
+star2($star, $star, sub {
+ print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
+ print "ok $i - star2(\$star, \$star)\n";
+ }); $i++;
+star2 *FOO, *BAR, sub {
+ print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
+ print "ok $i - star2 *FOO, *BAR\n";
+}; $i++;
+star2(*FOO, *BAR, sub {
+ print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
+ print "ok $i - star2(*FOO, *BAR)\n";
+ }); $i++;
+star2 \*FOO, \*BAR, sub {
+ no strict 'refs';
+ print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
+ print "ok $i - star2 \*FOO, \*BAR\n";
+}; $i++;
+star2(\*FOO, \*BAR, sub {
+ no strict 'refs';
+ print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
+ print "ok $i - star2(\*FOO, \*BAR)\n";
+ }); $i++;
+
+# test scalarref prototype
+sub sreftest (\$$) {
+ print "not " unless ref $_[0];
+ print "ok $_[1] - sreftest\n";
+}
+{
+ no strict 'vars';
+ sreftest my $sref, $i++;
+ sreftest($helem{$i}, $i++);
+ sreftest $aelem[0], $i++;
+}
+
+# test prototypes when they are evaled and there is a syntax error
+# Byacc generates the string "syntax error". Bison gives the
+# string "parse error".
+#
+for my $p ( "", qw{ () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@) } ) {
+ no warnings 'prototype';
+ my $eval = "sub evaled_subroutine $p { &void *; }";
+ eval $eval;
+ print "# eval[$eval]\nnot " unless $@ && $@ =~ /(parse|syntax) error/i;
+ print "ok ", $i++, "\n";
+}
+
+# Not $$;$;$
+print "not " unless prototype "CORE::substr" eq '$$;$$';
+print "ok ", $i++, "\n";
+
+# recv takes a scalar reference for its second argument
+print "not " unless prototype "CORE::recv" eq '*\\$$$';
+print "ok ", $i++, "\n";
+
+{
+ my $myvar;
+ my @myarray;
+ my %myhash;
+ sub mysub { print "not calling mysub I hope\n" }
+ local *myglob;
+
+ sub myref (\[$@%&*]) { print "# $_[0]\n"; return "$_[0]" }
+
+ print "not " unless myref($myvar) =~ /^SCALAR\(/;
+ print "ok ", $i++, "\n";
+ print "not " unless myref(@myarray) =~ /^ARRAY\(/;
+ print "ok ", $i++, "\n";
+ print "not " unless myref(%myhash) =~ /^HASH\(/;
+ print "ok ", $i++, "\n";
+ print "not " unless myref(&mysub) =~ /^CODE\(/;
+ print "ok ", $i++, "\n";
+ print "not " unless myref(*myglob) =~ /^GLOB\(/;
+ print "ok ", $i++, "\n";
+
+ eval q/sub multi1 (\[%@]) { 1 } multi1 $myvar;/;
+ print "not " unless $@ =~ /Type of arg 1 to main::multi1 must be one of/;
+ print "ok ", $i++, "\n";
+ eval q/sub multi2 (\[$*&]) { 1 } multi2 @myarray;/;
+ print "not " unless $@ =~ /Type of arg 1 to main::multi2 must be one of/;
+ print "ok ", $i++, "\n";
+ eval q/sub multi3 (\[$@]) { 1 } multi3 %myhash;/;
+ print "not " unless $@ =~ /Type of arg 1 to main::multi3 must be one of/;
+ print "ok ", $i++, "\n";
+ eval q/sub multi4 ($\[%]) { 1 } multi4 1, &mysub;/;
+ print "not " unless $@ =~ /Type of arg 2 to main::multi4 must be one of/;
+ print "ok ", $i++, "\n";
+ eval q/sub multi5 (\[$@]$) { 1 } multi5 *myglob;/;
+ print "not " unless $@ =~ /Type of arg 1 to main::multi5 must be one of/
+ && $@ =~ /Not enough arguments/;
+ print "ok ", $i++, "\n";
+}
+
+# check that obviously bad prototypes are getting warnings
+{
+ use warnings 'syntax';
+ my $warn = "";
+ local $SIG{__WARN__} = sub { $warn .= join("",@_) };
+
+ eval 'sub badproto (@bar) { 1; }';
+ print "not " unless $warn =~ /Illegal character in prototype for main::badproto : \@bar/;
+ print "ok ", $i++, "\n";
+
+ eval 'sub badproto2 (bar) { 1; }';
+ print "not " unless $warn =~ /Illegal character in prototype for main::badproto2 : bar/;
+ print "ok ", $i++, "\n";
+
+ eval 'sub badproto3 (&$bar$@) { 1; }';
+ print "not " unless $warn =~ /Illegal character in prototype for main::badproto3 : &\$bar\$\@/;
+ print "ok ", $i++, "\n";
+
+ eval 'sub badproto4 (@ $b ar) { 1; }';
+ print "not " unless $warn =~ /Illegal character in prototype for main::badproto4 : \@\$bar/;
+ print "ok ", $i++, "\n";
+}
+
+# make sure whitespace in prototypes works
+eval "sub good (\$\t\$\n\$) { 1; }";
+print "not " if $@;
+print "ok ", $i++, "\n";