This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
untodo the no-longer-failing todo test for rgs' patch
[perl5.git] / lib / subs.t
CommitLineData
8ebc5c01 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
8ebc5c01 6 $ENV{PERL5LIB} = '../lib';
0642d82a 7 require './test.pl';
8ebc5c01 8}
9
10$| = 1;
11undef $/;
12my @prgs = split "\n########\n", <DATA>;
13print "1..", scalar @prgs, "\n";
14
44a8e56a 15my $Is_VMS = $^O eq 'VMS';
68dc0745 16my $Is_MSWin32 = $^O eq 'MSWin32';
2986a63f 17my $Is_NetWare = $^O eq 'NetWare';
8ebc5c01 18my $i = 0 ;
8ebc5c01 19
20for (@prgs){
21 my $switch = "";
22 my @temps = () ;
23 if (s/^\s*-\w+//){
24 $switch = $&;
25 }
26 my($prog,$expected) = split(/\nEXPECT\n/, $_);
27 if ( $prog =~ /--FILE--/) {
28 my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
29 shift @files ;
30 die "Internal error test $i didn't split into pairs, got " .
31 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
32 if @files % 2 ;
33 while (@files > 2) {
34 my $filename = shift @files ;
35 my $code = shift @files ;
36 push @temps, $filename ;
37 open F, ">$filename" or die "Cannot open $filename: $!\n" ;
38 print F $code ;
39 close F ;
40 }
41 shift @files ;
42 $prog = shift @files ;
43 }
f89542f7 44 my $tmpfile = tempfile();
44a8e56a 45 open TEST, ">$tmpfile";
46 print TEST $prog,"\n";
8ebc5c01 47 close TEST;
44a8e56a 48 my $results = $Is_VMS ?
be708cc0 49 `./perl $switch $tmpfile 2>&1` :
68dc0745 50 $Is_MSWin32 ?
be708cc0 51 `.\\perl -I../lib $switch $tmpfile 2>&1` :
2986a63f 52 $Is_NetWare ?
be708cc0 53 `perl -I../lib $switch $tmpfile 2>&1` :
648cac19 54 `./perl $switch $tmpfile 2>&1`;
8ebc5c01 55 my $status = $?;
8ebc5c01 56 $results =~ s/\n+$//;
44a8e56a 57 # allow expected output to be written as if $prog is on STDIN
0642d82a 58 $results =~ s/tmp\d+[A-Z][A-Z]?/-/g;
44a8e56a 59 $results =~ s/\n%[A-Z]+-[SIWEF]-.*$// if $Is_VMS; # clip off DCL status msg
f0ec1f9a 60# bison says 'parse error' instead of 'syntax error',
d91e2bdb 61# various yaccs may or may not capitalize 'syntax'.
2a8ee232 62 $results =~ s/^(syntax|parse) error/syntax error/mig;
8ebc5c01 63 $expected =~ s/\n+$//;
64 my $prefix = ($results =~ s/^PREFIX\n//) ;
65 if ( $results =~ s/^SKIPPED\n//) {
66 print "$results\n" ;
67 }
68 elsif (($prefix and $results !~ /^\Q$expected/) or
69 (!$prefix and $results ne $expected)){
70 print STDERR "PROG: $switch\n$prog\n";
71 print STDERR "EXPECTED:\n$expected\n";
72 print STDERR "GOT:\n$results\n";
73 print "not ";
74 }
75 print "ok ", ++$i, "\n";
76 foreach (@temps)
77 { unlink $_ if $_ }
78}
79
80__END__
81
82# Error - not predeclaring a sub
83Fred 1,2 ;
84sub Fred {}
85EXPECT
86Number found where operator expected at - line 3, near "Fred 1"
87 (Do you need to predeclare Fred?)
88syntax error at - line 3, near "Fred 1"
89Execution of - aborted due to compilation errors.
90########
91
92# Error - not predeclaring a sub in time
93Fred 1,2 ;
94use subs qw( Fred ) ;
95sub Fred {}
96EXPECT
97Number found where operator expected at - line 3, near "Fred 1"
98 (Do you need to predeclare Fred?)
99syntax error at - line 3, near "Fred 1"
68dc0745 100BEGIN not safe after errors--compilation aborted at - line 4.
8ebc5c01 101########
102
103# AOK
104use subs qw( Fred) ;
105Fred 1,2 ;
106sub Fred { print $_[0] + $_[1], "\n" }
107EXPECT
1083
109########
110
111# override a built-in function
112use subs qw( open ) ;
113open 1,2 ;
114sub open { print $_[0] + $_[1], "\n" }
115EXPECT
1163
117########
118
55a105fd
NC
119# override a built-in function, call after definition
120use subs qw( open ) ;
121sub open { print $_[0] + $_[1], "\n" }
122open 1,2 ;
123EXPECT
1243
125########
126
127# override a built-in function, call with ()
128use subs qw( open ) ;
129open (1,2) ;
130sub open { print $_[0] + $_[1], "\n" }
131EXPECT
1323
133########
134
135# override a built-in function, call with () after definition
136use subs qw( open ) ;
137sub open { print $_[0] + $_[1], "\n" }
138open (1,2) ;
139EXPECT
1403
141########
142
8ebc5c01 143--FILE-- abc
144Fred 1,2 ;
1451;
146--FILE--
147use subs qw( Fred ) ;
148require "./abc" ;
149sub Fred { print $_[0] + $_[1], "\n" }
150EXPECT
1513
152########
153
154# check that it isn't affected by block scope
155{
156 use subs qw( Fred ) ;
157}
158Fred 1, 2;
159sub Fred { print $_[0] + $_[1], "\n" }
160EXPECT
1613