Commit | Line | Data |
8a73d5dd |
1 | #!./perl -w |
2 | |
feafb1eb |
3 | # Tests for the command-line switches: |
bc9b29db |
4 | # -0, -c, -l, -s, -m, -M, -V, -v, -h, -z, -i, -E |
48eaf804 |
5 | # Some switches have their own tests, see MANIFEST. |
8a73d5dd |
6 | |
7 | BEGIN { |
8 | chdir 't' if -d 't'; |
9 | @INC = '../lib'; |
10 | } |
11 | |
12 | require "./test.pl"; |
13 | |
59e235cb |
14 | plan(tests => 31); |
8a73d5dd |
15 | |
a09a0aa2 |
16 | use Config; |
17 | |
b734d6c9 |
18 | # due to a bug in VMS's piping which makes it impossible for runperl() |
e54d2dfa |
19 | # to emulate echo -n (ie. stdin always winds up with a newline), these |
20 | # tests almost totally fail. |
b734d6c9 |
21 | $TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS'; |
22 | |
8a73d5dd |
23 | my $r; |
24 | my @tmpfiles = (); |
25 | END { unlink @tmpfiles } |
26 | |
27 | # Tests for -0 |
28 | |
29 | $r = runperl( |
30 | switches => [ '-0', ], |
31 | stdin => 'foo\0bar\0baz\0', |
32 | prog => 'print qq(<$_>) while <>', |
33 | ); |
34 | is( $r, "<foo\0><bar\0><baz\0>", "-0" ); |
35 | |
36 | $r = runperl( |
37 | switches => [ '-l', '-0', '-p' ], |
38 | stdin => 'foo\0bar\0baz\0', |
39 | prog => '1', |
40 | ); |
41 | is( $r, "foo\nbar\nbaz\n", "-0 after a -l" ); |
42 | |
43 | $r = runperl( |
44 | switches => [ '-0', '-l', '-p' ], |
45 | stdin => 'foo\0bar\0baz\0', |
46 | prog => '1', |
47 | ); |
48 | is( $r, "foo\0bar\0baz\0", "-0 before a -l" ); |
49 | |
50 | $r = runperl( |
51 | switches => [ sprintf("-0%o", ord 'x') ], |
52 | stdin => 'fooxbarxbazx', |
53 | prog => 'print qq(<$_>) while <>', |
54 | ); |
55 | is( $r, "<foox><barx><bazx>", "-0 with octal number" ); |
56 | |
57 | $r = runperl( |
58 | switches => [ '-00', '-p' ], |
59 | stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n', |
60 | prog => 's/\n/-/g;$_.=q(/)', |
61 | ); |
62 | is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' ); |
63 | |
64 | $r = runperl( |
65 | switches => [ '-0777', '-p' ], |
66 | stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n', |
67 | prog => 's/\n/-/g;$_.=q(/)', |
68 | ); |
69 | is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' ); |
70 | |
7f9e821f |
71 | $r = runperl( |
72 | switches => [ '-066' ], |
0fbedf1d |
73 | prog => 'BEGIN { print qq{($/)} } print qq{[$/]}', |
7f9e821f |
74 | ); |
75 | is( $r, "(\066)[\066]", '$/ set at compile-time' ); |
76 | |
8a73d5dd |
77 | # Tests for -c |
78 | |
79 | my $filename = 'swctest.tmp'; |
80 | SKIP: { |
b734d6c9 |
81 | local $TODO = ''; # this one works on VMS |
82 | |
8a73d5dd |
83 | open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" ); |
84 | print $f <<'SWTEST'; |
85 | BEGIN { print "block 1\n"; } |
86 | CHECK { print "block 2\n"; } |
87 | INIT { print "block 3\n"; } |
88 | print "block 4\n"; |
89 | END { print "block 5\n"; } |
90 | SWTEST |
d1e4d418 |
91 | close $f or die "Could not close: $!"; |
8a73d5dd |
92 | $r = runperl( |
93 | switches => [ '-c' ], |
94 | progfile => $filename, |
95 | stderr => 1, |
96 | ); |
97 | # Because of the stderr redirection, we can't tell reliably the order |
98 | # in which the output is given |
99 | ok( |
100 | $r =~ /$filename syntax OK/ |
101 | && $r =~ /\bblock 1\b/ |
102 | && $r =~ /\bblock 2\b/ |
103 | && $r !~ /\bblock 3\b/ |
104 | && $r !~ /\bblock 4\b/ |
105 | && $r !~ /\bblock 5\b/, |
106 | '-c' |
107 | ); |
108 | push @tmpfiles, $filename; |
109 | } |
110 | |
111 | # Tests for -l |
112 | |
113 | $r = runperl( |
114 | switches => [ sprintf("-l%o", ord 'x') ], |
115 | prog => 'print for qw/foo bar/' |
116 | ); |
117 | is( $r, 'fooxbarx', '-l with octal number' ); |
118 | |
119 | # Tests for -s |
120 | |
121 | $r = runperl( |
122 | switches => [ '-s' ], |
123 | prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}', |
124 | args => [ '--', '-abc=2', '-def', ], |
125 | ); |
126 | is( $r, '21-', '-s switch parsing' ); |
127 | |
8a73d5dd |
128 | $filename = 'swstest.tmp'; |
129 | SKIP: { |
130 | open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" ); |
131 | print $f <<'SWTEST'; |
59e235cb |
132 | #!perl -s |
133 | BEGIN { print $x,$y; exit } |
134 | SWTEST |
135 | close $f or die "Could not close: $!"; |
136 | $r = runperl( |
137 | progfile => $filename, |
138 | args => [ '-x=foo -y' ], |
139 | ); |
140 | is( $r, 'foo1', '-s on the shebang line' ); |
141 | push @tmpfiles, $filename; |
142 | } |
143 | |
144 | # Bug ID 20011106.084 |
145 | $filename = 'swsntest.tmp'; |
146 | SKIP: { |
147 | open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" ); |
148 | print $f <<'SWTEST'; |
f0b2cf55 |
149 | #!perl -sn |
150 | BEGIN { print $x; exit } |
8a73d5dd |
151 | SWTEST |
d1e4d418 |
152 | close $f or die "Could not close: $!"; |
8a73d5dd |
153 | $r = runperl( |
8a73d5dd |
154 | progfile => $filename, |
155 | args => [ '-x=foo' ], |
156 | ); |
59e235cb |
157 | is( $r, 'foo', '-sn on the shebang line' ); |
8a73d5dd |
158 | push @tmpfiles, $filename; |
159 | } |
160 | |
161 | # Tests for -m and -M |
162 | |
163 | $filename = 'swtest.pm'; |
164 | SKIP: { |
165 | open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 ); |
166 | print $f <<'SWTESTPM'; |
167 | package swtest; |
168 | sub import { print map "<$_>", @_ } |
169 | 1; |
170 | SWTESTPM |
d1e4d418 |
171 | close $f or die "Could not close: $!"; |
8a73d5dd |
172 | $r = runperl( |
173 | switches => [ '-Mswtest' ], |
174 | prog => '1', |
175 | ); |
176 | is( $r, '<swtest>', '-M' ); |
177 | $r = runperl( |
178 | switches => [ '-Mswtest=foo' ], |
179 | prog => '1', |
180 | ); |
181 | is( $r, '<swtest><foo>', '-M with import parameter' ); |
182 | $r = runperl( |
183 | switches => [ '-mswtest' ], |
184 | prog => '1', |
185 | ); |
b734d6c9 |
186 | |
187 | { |
188 | local $TODO = ''; # this one works on VMS |
189 | is( $r, '', '-m' ); |
190 | } |
8a73d5dd |
191 | $r = runperl( |
192 | switches => [ '-mswtest=foo,bar' ], |
193 | prog => '1', |
194 | ); |
195 | is( $r, '<swtest><foo><bar>', '-m with import parameters' ); |
196 | push @tmpfiles, $filename; |
197 | } |
e54d2dfa |
198 | |
199 | # Tests for -V |
200 | |
201 | { |
202 | local $TODO = ''; # these ones should work on VMS |
203 | |
204 | # basic perl -V should generate significant output. |
ceda46a1 |
205 | # we don't test actual format too much since it could change |
e54d2dfa |
206 | like( runperl( switches => ['-V'] ), qr/(\n.*){20}/, |
207 | '-V generates 20+ lines' ); |
208 | |
ceda46a1 |
209 | like( runperl( switches => ['-V'] ), |
210 | qr/\ASummary of my perl5 .*configuration:/, |
211 | '-V looks okay' ); |
212 | |
e54d2dfa |
213 | # lookup a known config var |
214 | chomp( $r=runperl( switches => ['-V:osname'] ) ); |
215 | is( $r, "osname='$^O';", 'perl -V:osname'); |
216 | |
217 | # lookup a nonexistent var |
218 | chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) ); |
219 | is( $r, "this_var_makes_switches_test_fail='UNKNOWN';", |
220 | 'perl -V:unknown var'); |
221 | |
222 | # regexp lookup |
223 | # platforms that don't like this quoting can either skip this test |
224 | # or fix test.pl _quote_args |
225 | $r = runperl( switches => ['"-V:i\D+size"'] ); |
226 | # should be unlike( $r, qr/^$|not found|UNKNOWN/ ); |
227 | like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' ); |
228 | |
229 | # make sure each line we got matches the re |
230 | ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' ); |
231 | } |
ceda46a1 |
232 | |
233 | # Tests for -v |
234 | |
235 | { |
236 | local $TODO = ''; # these ones should work on VMS |
237 | |
ceda46a1 |
238 | my $v = sprintf "%vd", $^V; |
ceda46a1 |
239 | like( runperl( switches => ['-v'] ), |
3772adb0 |
240 | qr/This is perl, v$v built for $Config{archname}.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s, |
ceda46a1 |
241 | '-v looks okay' ); |
242 | |
243 | } |
b8e3af44 |
244 | |
245 | # Tests for -h |
246 | |
247 | { |
248 | local $TODO = ''; # these ones should work on VMS |
249 | |
250 | like( runperl( switches => ['-h'] ), |
a09a0aa2 |
251 | qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/, |
b8e3af44 |
252 | '-h looks okay' ); |
253 | |
254 | } |
255 | |
256 | # Tests for -z (which does not exist) |
257 | |
258 | { |
259 | local $TODO = ''; # these ones should work on VMS |
260 | |
261 | like( runperl( switches => ['-z'], stderr => 1 ), |
262 | qr/\QUnrecognized switch: -z (-h will show valid options)./, |
263 | '-z correctly unknown' ); |
264 | |
265 | } |
feafb1eb |
266 | |
267 | # Tests for -i |
268 | |
269 | { |
270 | local $TODO = ''; # these ones should work on VMS |
271 | |
272 | sub do_i_unlink { 1 while unlink("file", "file.bak") } |
273 | |
274 | open(FILE, ">file") or die "$0: Failed to create 'file': $!"; |
275 | print FILE <<__EOF__; |
276 | foo yada dada |
277 | bada foo bing |
278 | king kong foo |
279 | __EOF__ |
280 | close FILE; |
281 | |
282 | END { do_i_unlink() } |
283 | |
284 | runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] ); |
285 | |
286 | open(FILE, "file") or die "$0: Failed to open 'file': $!"; |
287 | chomp(my @file = <FILE>); |
288 | close FILE; |
289 | |
290 | open(BAK, "file.bak") or die "$0: Failed to open 'file': $!"; |
291 | chomp(my @bak = <BAK>); |
292 | close BAK; |
293 | |
294 | is(join(":", @file), |
295 | "bar yada dada:bada bar bing:king kong bar", |
296 | "-i new file"); |
297 | is(join(":", @bak), |
298 | "foo yada dada:bada foo bing:king kong foo", |
299 | "-i backup file"); |
300 | } |
bc9b29db |
301 | |
302 | # Tests for -E |
303 | |
304 | $r = runperl( |
305 | switches => [ '-E', '"say q(Hello, world!)"'] |
306 | ); |
307 | is( $r, "Hello, world!\n", "-E say" ); |
308 | |
309 | |
310 | $r = runperl( |
311 | switches => [ '-E', '"undef err say q(Hello, world!)"'] |
312 | ); |
313 | is( $r, "Hello, world!\n", "-E err" ); |
314 | |
315 | $r = runperl( |
316 | switches => [ '-E', '"undef ~~ undef and say q(Hello, world!)"'] |
317 | ); |
318 | is( $r, "Hello, world!\n", "-E ~~" ); |
319 | |
320 | $r = runperl( |
321 | switches => [ '-E', '"given(undef) {when(undef) { say q(Hello, world!)"}}'] |
322 | ); |
323 | is( $r, "Hello, world!\n", "-E given" ); |