Commit | Line | Data |
8ebc5c01 |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
5 | @INC = '../lib'; |
6 | $ENV{PERL5LIB} = '../lib'; |
7 | } |
8 | |
9 | $| = 1; |
10 | undef $/; |
11 | my @prgs = split "\n########\n", <DATA>; |
12 | print "1..", scalar @prgs, "\n"; |
13 | |
14 | my $tmpfile = "tmp0000"; |
15 | my $i = 0 ; |
16 | 1 while -f ++$tmpfile; |
17 | END { unlink $tmpfile if $tmpfile; } |
18 | |
19 | for (@prgs){ |
20 | my $switch = ""; |
21 | my @temps = () ; |
22 | if (s/^\s*-\w+//){ |
23 | $switch = $&; |
24 | } |
25 | my($prog,$expected) = split(/\nEXPECT\n/, $_); |
26 | if ( $prog =~ /--FILE--/) { |
27 | my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ; |
28 | shift @files ; |
29 | die "Internal error test $i didn't split into pairs, got " . |
30 | scalar(@files) . "[" . join("%%%%", @files) ."]\n" |
31 | if @files % 2 ; |
32 | while (@files > 2) { |
33 | my $filename = shift @files ; |
34 | my $code = shift @files ; |
35 | push @temps, $filename ; |
36 | open F, ">$filename" or die "Cannot open $filename: $!\n" ; |
37 | print F $code ; |
38 | close F ; |
39 | } |
40 | shift @files ; |
41 | $prog = shift @files ; |
42 | } |
43 | open TEST, "| sh -c './perl $switch' >$tmpfile 2>&1"; |
44 | print TEST $prog, "\n"; |
45 | close TEST; |
46 | my $status = $?; |
47 | my $results = `cat $tmpfile`; |
48 | $results =~ s/\n+$//; |
49 | $expected =~ s/\n+$//; |
50 | my $prefix = ($results =~ s/^PREFIX\n//) ; |
51 | if ( $results =~ s/^SKIPPED\n//) { |
52 | print "$results\n" ; |
53 | } |
54 | elsif (($prefix and $results !~ /^\Q$expected/) or |
55 | (!$prefix and $results ne $expected)){ |
56 | print STDERR "PROG: $switch\n$prog\n"; |
57 | print STDERR "EXPECTED:\n$expected\n"; |
58 | print STDERR "GOT:\n$results\n"; |
59 | print "not "; |
60 | } |
61 | print "ok ", ++$i, "\n"; |
62 | foreach (@temps) |
63 | { unlink $_ if $_ } |
64 | } |
65 | |
66 | __END__ |
67 | |
68 | # Error - not predeclaring a sub |
69 | Fred 1,2 ; |
70 | sub Fred {} |
71 | EXPECT |
72 | Number found where operator expected at - line 3, near "Fred 1" |
73 | (Do you need to predeclare Fred?) |
74 | syntax error at - line 3, near "Fred 1" |
75 | Execution of - aborted due to compilation errors. |
76 | ######## |
77 | |
78 | # Error - not predeclaring a sub in time |
79 | Fred 1,2 ; |
80 | use subs qw( Fred ) ; |
81 | sub Fred {} |
82 | EXPECT |
83 | Number found where operator expected at - line 3, near "Fred 1" |
84 | (Do you need to predeclare Fred?) |
85 | syntax error at - line 3, near "Fred 1" |
86 | Execution of - aborted due to compilation errors. |
87 | ######## |
88 | |
89 | # AOK |
90 | use subs qw( Fred) ; |
91 | Fred 1,2 ; |
92 | sub Fred { print $_[0] + $_[1], "\n" } |
93 | EXPECT |
94 | 3 |
95 | ######## |
96 | |
97 | # override a built-in function |
98 | use subs qw( open ) ; |
99 | open 1,2 ; |
100 | sub open { print $_[0] + $_[1], "\n" } |
101 | EXPECT |
102 | 3 |
103 | ######## |
104 | |
105 | --FILE-- abc |
106 | Fred 1,2 ; |
107 | 1; |
108 | --FILE-- |
109 | use subs qw( Fred ) ; |
110 | require "./abc" ; |
111 | sub Fred { print $_[0] + $_[1], "\n" } |
112 | EXPECT |
113 | 3 |
114 | ######## |
115 | |
116 | # check that it isn't affected by block scope |
117 | { |
118 | use subs qw( Fred ) ; |
119 | } |
120 | Fred 1, 2; |
121 | sub Fred { print $_[0] + $_[1], "\n" } |
122 | EXPECT |
123 | 3 |