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.
12 # XXX known to leak scalars
13 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
27 my $p = prototype(shift);
29 my $what = defined $c ? '(' . $p . ')' : 'no prototype';
31 print '# Testing ',$what,"\n";
34 if((defined($p) && defined($c) && $p ne $c)
35 || (defined($p) != defined($c)));
36 printf "ok %d\n",$i++;
47 testing \&no_proto, undef;
50 print "# \@_ = (",join(",",@_),")\n";
54 print "not " unless 0 == no_proto();
55 printf "ok %d\n",$i++;
57 print "not " unless 1 == no_proto(5);
58 printf "ok %d\n",$i++;
60 print "not " unless 4 == &no_proto;
61 printf "ok %d\n",$i++;
63 print "not " unless 1 == no_proto +6;
64 printf "ok %d\n",$i++;
66 print "not " unless 4 == no_proto(@_);
67 printf "ok %d\n",$i++;
74 testing \&no_args, '';
77 print "# \@_ = (",join(",",@_),")\n";
81 print "not " unless 0 == no_args();
82 printf "ok %d\n",$i++;
84 print "not " unless 0 == no_args;
85 printf "ok %d\n",$i++;
87 print "not " unless 5 == no_args +5;
88 printf "ok %d\n",$i++;
90 print "not " unless 4 == &no_args;
91 printf "ok %d\n",$i++;
93 print "not " unless 2 == &no_args(1,2);
94 printf "ok %d\n",$i++;
97 print "not " unless $@;
98 printf "ok %d\n",$i++;
104 testing \&one_args, '$';
107 print "# \@_ = (",join(",",@_),")\n";
111 print "not " unless 1 == one_args(1);
112 printf "ok %d\n",$i++;
114 print "not " unless 1 == one_args +5;
115 printf "ok %d\n",$i++;
117 print "not " unless 4 == &one_args;
118 printf "ok %d\n",$i++;
120 print "not " unless 2 == &one_args(1,2);
121 printf "ok %d\n",$i++;
123 eval "one_args(1,2)";
124 print "not " unless $@;
125 printf "ok %d\n",$i++;
128 print "not " unless $@;
129 printf "ok %d\n",$i++;
132 print "# \@_ = (",join(",",@_),")\n";
133 print "not " unless @_ == 1 && $_[0] == 4;
134 printf "ok %d\n",$i++;
143 testing \&over_one_args, '$@';
145 sub over_one_args ($@) {
146 print "# \@_ = (",join(",",@_),")\n";
150 print "not " unless 1 == over_one_args(1);
151 printf "ok %d\n",$i++;
153 print "not " unless 2 == over_one_args(1,2);
154 printf "ok %d\n",$i++;
156 print "not " unless 1 == over_one_args +5;
157 printf "ok %d\n",$i++;
159 print "not " unless 4 == &over_one_args;
160 printf "ok %d\n",$i++;
162 print "not " unless 2 == &over_one_args(1,2);
163 printf "ok %d\n",$i++;
165 print "not " unless 5 == &over_one_args(1,@_);
166 printf "ok %d\n",$i++;
168 eval "over_one_args()";
169 print "not " unless $@;
170 printf "ok %d\n",$i++;
172 sub over_one_a_args ($@) {
173 print "# \@_ = (",join(",",@_),")\n";
174 print "not " unless @_ >= 1 && $_[0] == 4;
175 printf "ok %d\n",$i++;
179 over_one_a_args(@_,1);
180 over_one_a_args(@_,1,2);
181 over_one_a_args(@_,@_);
187 testing \&scalar_and_hash, '$%';
189 sub scalar_and_hash ($%) {
190 print "# \@_ = (",join(",",@_),")\n";
194 print "not " unless 1 == scalar_and_hash(1);
195 printf "ok %d\n",$i++;
197 print "not " unless 3 == scalar_and_hash(1,2,3);
198 printf "ok %d\n",$i++;
200 print "not " unless 1 == scalar_and_hash +5;
201 printf "ok %d\n",$i++;
203 print "not " unless 4 == &scalar_and_hash;
204 printf "ok %d\n",$i++;
206 print "not " unless 2 == &scalar_and_hash(1,2);
207 printf "ok %d\n",$i++;
209 print "not " unless 5 == &scalar_and_hash(1,@_);
210 printf "ok %d\n",$i++;
212 eval "scalar_and_hash()";
213 print "not " unless $@;
214 printf "ok %d\n",$i++;
216 sub scalar_and_hash_a ($@) {
217 print "# \@_ = (",join(",",@_),")\n";
218 print "not " unless @_ >= 1 && $_[0] == 4;
219 printf "ok %d\n",$i++;
222 scalar_and_hash_a(@_);
223 scalar_and_hash_a(@_,1);
224 scalar_and_hash_a(@_,1,2);
225 scalar_and_hash_a(@_,@_);
231 testing \&one_or_two, '$;$';
233 sub one_or_two ($;$) {
234 print "# \@_ = (",join(",",@_),")\n";
238 print "not " unless 1 == one_or_two(1);
239 printf "ok %d\n",$i++;
241 print "not " unless 2 == one_or_two(1,3);
242 printf "ok %d\n",$i++;
244 print "not " unless 1 == one_or_two +5;
245 printf "ok %d\n",$i++;
247 print "not " unless 4 == &one_or_two;
248 printf "ok %d\n",$i++;
250 print "not " unless 3 == &one_or_two(1,2,3);
251 printf "ok %d\n",$i++;
253 print "not " unless 5 == &one_or_two(1,@_);
254 printf "ok %d\n",$i++;
257 print "not " unless $@;
258 printf "ok %d\n",$i++;
260 eval "one_or_two(1,2,3)";
261 print "not " unless $@;
262 printf "ok %d\n",$i++;
264 sub one_or_two_a ($;$) {
265 print "# \@_ = (",join(",",@_),")\n";
266 print "not " unless @_ >= 1 && $_[0] == 4;
267 printf "ok %d\n",$i++;
278 testing \&a_sub, '&';
281 print "# \@_ = (",join(",",@_),")\n";
285 sub tmp_sub_1 { printf "ok %d\n",$i++ }
287 a_sub { printf "ok %d\n",$i++ };
290 @array = ( \&tmp_sub_1 );
292 print "not " unless $@;
293 printf "ok %d\n",$i++;
299 testing \&a_subx, '\&';
302 print "# \@_ = (",join(",",@_),")\n";
306 sub tmp_sub_2 { printf "ok %d\n",$i++ }
309 @array = ( \&tmp_sub_2 );
310 eval 'a_subx @array';
311 print "not " unless $@;
312 printf "ok %d\n",$i++;
318 testing \&sub_aref, '&\@';
321 print "# \@_ = (",join(",",@_),")\n";
322 my($sub,$array) = @_;
323 print "not " unless @_ == 2 && @{$array} == 4;
324 print map { &{$sub}($_) } @{$array}
327 @array = (qw(O K)," ", $i++);
328 sub_aref { lc shift } @array;
335 testing \&sub_array, '&@';
338 print "# \@_ = (",join(",",@_),")\n";
339 print "not " unless @_ == 5;
341 print map { &{$sub}($_) } @_
344 @array = (qw(O K)," ", $i++);
345 sub_array { lc shift } @array;
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 # correctly note too-short parameter lists that don't end with '$',
417 # a possible regression.
421 print "not " unless $@ =~ /^Not enough/;
422 print "ok ", $i++, "\n";
426 print "not " unless $@ =~ /^Not enough/;
427 print "ok ", $i++, "\n";
430 *X::foo3 = sub {'ok'};
431 print "# $@not " unless eval {X->foo3} eq 'ok';
432 print "ok ", $i++, "\n";
435 *X::foo4 = sub ($) {'ok'};
436 print "not " unless X->foo4 eq 'ok';
437 print "ok ", $i++, "\n";
439 # test if the (*) prototype allows barewords, constants, scalar expressions,
440 # globs and globrefs (just as CORE::open() does), all under stricture
441 sub star (*&) { &{$_[1]} }
442 sub star2 (**&) { &{$_[2]} }
444 sub Bar::BAZ { "quuz" }
446 star FOO, sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
447 star(FOO, sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
448 star "FOO", sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
449 star("FOO", sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
450 star $star, sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
451 star($star, sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
452 star *FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }; $i++;
453 star(*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }); $i++;
454 star \*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }; $i++;
455 star(\*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }); $i++;
456 star2 FOO, BAR, sub { print "ok $i\n"
457 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }; $i++;
458 star2(Bar::BAZ, FOO, sub { print "ok $i\n"
459 if $_[0] eq 'Bar::BAZ' and $_[1] eq 'FOO' }); $i++;
460 star2 BAR(), FOO, sub { print "ok $i\n"
461 if $_[0] eq 'quux' and $_[1] eq 'FOO' }; $i++;
462 star2(FOO, BAR(), sub { print "ok $i\n"
463 if $_[0] eq 'FOO' and $_[1] eq 'quux' }); $i++;
464 star2 "FOO", "BAR", sub { print "ok $i\n"
465 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }; $i++;
466 star2("FOO", "BAR", sub { print "ok $i\n"
467 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }); $i++;
468 star2 $star, $star, sub { print "ok $i\n"
469 if $_[0] eq 'FOO' and $_[1] eq 'FOO' }; $i++;
470 star2($star, $star, sub { print "ok $i\n"
471 if $_[0] eq 'FOO' and $_[1] eq 'FOO' }); $i++;
472 star2 *FOO, *BAR, sub { print "ok $i\n"
473 if $_[0] eq \*FOO and $_[1] eq \*BAR }; $i++;
474 star2(*FOO, *BAR, sub { print "ok $i\n"
475 if $_[0] eq \*FOO and $_[1] eq \*BAR }); $i++;
476 star2 \*FOO, \*BAR, sub { no strict 'refs'; print "ok $i\n"
477 if $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'} }; $i++;
478 star2(\*FOO, \*BAR, sub { no strict 'refs'; print "ok $i\n"
479 if $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'} }); $i++;
481 # test scalarref prototype
483 print "ok $_[1]\n" if ref $_[0];
487 sreftest my $sref, $i++;
488 sreftest($helem{$i}, $i++);
489 sreftest $aelem[0], $i++;
492 # test prototypes when they are evaled and there is a syntax error
494 for my $p ( "", qw{ () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@) } ) {
495 no warnings 'redefine';
496 my $eval = "sub evaled_subroutine $p { &void *; }";
498 print "# eval[$eval]\nnot " unless $@ && $@ =~ /syntax error/;
499 print "ok ", $i++, "\n";