Implement Hash/Array ~~ Regex (with tests)
[p5sagit/p5-mst-13.2.git] / t / run / switches.t
CommitLineData
8a73d5dd 1#!./perl -w
2
feafb1eb 3# Tests for the command-line switches:
fed0ca7f 4# -0, -c, -l, -s, -m, -M, -V, -v, -h, -i, -E and all unknown
48eaf804 5# Some switches have their own tests, see MANIFEST.
8a73d5dd 6
7BEGIN {
8 chdir 't' if -d 't';
9 @INC = '../lib';
10}
11
768fd157 12BEGIN { require "./test.pl"; }
8a73d5dd 13
d3133c89 14plan(tests => 70);
8a73d5dd 15
a09a0aa2 16use 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 23my $r;
24my @tmpfiles = ();
25END { 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);
34is( $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);
41is( $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);
48is( $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);
55is( $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);
62is( $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);
69is( $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);
75is( $r, "(\066)[\066]", '$/ set at compile-time' );
76
8a73d5dd 77# Tests for -c
78
2d90ac95 79my $filename = tempfile();
8a73d5dd 80SKIP: {
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';
85BEGIN { print "block 1\n"; }
86CHECK { print "block 2\n"; }
87INIT { print "block 3\n"; }
88 print "block 4\n";
89END { print "block 5\n"; }
90SWTEST
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 );
8a73d5dd 108}
109
110# Tests for -l
111
112$r = runperl(
113 switches => [ sprintf("-l%o", ord 'x') ],
114 prog => 'print for qw/foo bar/'
115);
116is( $r, 'fooxbarx', '-l with octal number' );
117
118# Tests for -s
119
120$r = runperl(
121 switches => [ '-s' ],
122 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
123 args => [ '--', '-abc=2', '-def', ],
124);
125is( $r, '21-', '-s switch parsing' );
126
2d90ac95 127$filename = tempfile();
8a73d5dd 128SKIP: {
129 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
130 print $f <<'SWTEST';
59e235cb 131#!perl -s
132BEGIN { print $x,$y; exit }
133SWTEST
134 close $f or die "Could not close: $!";
135 $r = runperl(
136 progfile => $filename,
137 args => [ '-x=foo -y' ],
138 );
139 is( $r, 'foo1', '-s on the shebang line' );
59e235cb 140}
141
142# Bug ID 20011106.084
2d90ac95 143$filename = tempfile();
59e235cb 144SKIP: {
145 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
146 print $f <<'SWTEST';
f0b2cf55 147#!perl -sn
148BEGIN { print $x; exit }
8a73d5dd 149SWTEST
d1e4d418 150 close $f or die "Could not close: $!";
8a73d5dd 151 $r = runperl(
8a73d5dd 152 progfile => $filename,
153 args => [ '-x=foo' ],
154 );
59e235cb 155 is( $r, 'foo', '-sn on the shebang line' );
8a73d5dd 156}
157
158# Tests for -m and -M
159
2d90ac95 160my $package = tempfile();
161$filename = "$package.pm";
8a73d5dd 162SKIP: {
163 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
2d90ac95 164 print $f <<"SWTESTPM";
165package $package;
166sub import { print map "<\$_>", \@_ }
8a73d5dd 1671;
168SWTESTPM
d1e4d418 169 close $f or die "Could not close: $!";
8a73d5dd 170 $r = runperl(
2d90ac95 171 switches => [ "-M$package" ],
8a73d5dd 172 prog => '1',
173 );
2d90ac95 174 is( $r, "<$package>", '-M' );
8a73d5dd 175 $r = runperl(
2d90ac95 176 switches => [ "-M$package=foo" ],
8a73d5dd 177 prog => '1',
178 );
2d90ac95 179 is( $r, "<$package><foo>", '-M with import parameter' );
8a73d5dd 180 $r = runperl(
2d90ac95 181 switches => [ "-m$package" ],
8a73d5dd 182 prog => '1',
183 );
b734d6c9 184
185 {
186 local $TODO = ''; # this one works on VMS
187 is( $r, '', '-m' );
188 }
8a73d5dd 189 $r = runperl(
2d90ac95 190 switches => [ "-m$package=foo,bar" ],
8a73d5dd 191 prog => '1',
192 );
2d90ac95 193 is( $r, "<$package><foo><bar>", '-m with import parameters' );
8a73d5dd 194 push @tmpfiles, $filename;
0544e6df 195
196 is( runperl( switches => [ '-MTie::Hash' ], stderr => 1, prog => 1 ),
197 '', "-MFoo::Bar allowed" );
198
2d90ac95 199 like( runperl( switches => [ "-M:$package" ], stderr => 1,
0544e6df 200 prog => 'die "oops"' ),
201 qr/Invalid module name [\w:]+ with -M option\b/,
202 "-M:Foo not allowed" );
203
204 like( runperl( switches => [ '-mA:B:C' ], stderr => 1,
205 prog => 'die "oops"' ),
206 qr/Invalid module name [\w:]+ with -m option\b/,
207 "-mFoo:Bar not allowed" );
208
209 like( runperl( switches => [ '-m-A:B:C' ], stderr => 1,
210 prog => 'die "oops"' ),
211 qr/Invalid module name [\w:]+ with -m option\b/,
212 "-m-Foo:Bar not allowed" );
213
214 like( runperl( switches => [ '-m-' ], stderr => 1,
215 prog => 'die "oops"' ),
216 qr/Module name required with -m option\b/,
217 "-m- not allowed" );
218
219 like( runperl( switches => [ '-M-=' ], stderr => 1,
220 prog => 'die "oops"' ),
221 qr/Module name required with -M option\b/,
222 "-M- not allowed" );
8a73d5dd 223}
e54d2dfa 224
225# Tests for -V
226
227{
228 local $TODO = ''; # these ones should work on VMS
229
230 # basic perl -V should generate significant output.
ceda46a1 231 # we don't test actual format too much since it could change
e54d2dfa 232 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
233 '-V generates 20+ lines' );
234
ceda46a1 235 like( runperl( switches => ['-V'] ),
236 qr/\ASummary of my perl5 .*configuration:/,
237 '-V looks okay' );
238
e54d2dfa 239 # lookup a known config var
240 chomp( $r=runperl( switches => ['-V:osname'] ) );
241 is( $r, "osname='$^O';", 'perl -V:osname');
242
243 # lookup a nonexistent var
244 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
245 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
246 'perl -V:unknown var');
247
248 # regexp lookup
249 # platforms that don't like this quoting can either skip this test
250 # or fix test.pl _quote_args
251 $r = runperl( switches => ['"-V:i\D+size"'] );
252 # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
253 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
254
255 # make sure each line we got matches the re
256 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
257}
ceda46a1 258
259# Tests for -v
260
261{
262 local $TODO = ''; # these ones should work on VMS
46807d8e 263 # there are definitely known build configs where this test will fail
264 # DG/UX comes to mind. Maybe we should remove these special cases?
ceda46a1 265 my $v = sprintf "%vd", $^V;
ceda46a1 266 like( runperl( switches => ['-v'] ),
46807d8e 267 qr/This is perl, v$v(?:[-\w]+| \([^)]+\))? built for \Q$Config{archname}\E.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s,
ceda46a1 268 '-v looks okay' );
269
270}
b8e3af44 271
272# Tests for -h
273
274{
275 local $TODO = ''; # these ones should work on VMS
276
277 like( runperl( switches => ['-h'] ),
a09a0aa2 278 qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
b8e3af44 279 '-h looks okay' );
280
281}
282
fed0ca7f 283# Tests for switches which do not exist
b8e3af44 284
96889982 285foreach my $switch (split //, "ABbGgHJjKkLNOoPQqRrYyZz123456789_")
b8e3af44 286{
287 local $TODO = ''; # these ones should work on VMS
288
fed0ca7f 289 like( runperl( switches => ["-$switch"], stderr => 1,
290 prog => 'die "oops"' ),
291 qr/\QUnrecognized switch: -$switch (-h will show valid options)./,
292 "-$switch correctly unknown" );
b8e3af44 293
294}
feafb1eb 295
296# Tests for -i
297
298{
299 local $TODO = ''; # these ones should work on VMS
300
301 sub do_i_unlink { 1 while unlink("file", "file.bak") }
302
303 open(FILE, ">file") or die "$0: Failed to create 'file': $!";
304 print FILE <<__EOF__;
305foo yada dada
306bada foo bing
307king kong foo
308__EOF__
309 close FILE;
310
311 END { do_i_unlink() }
312
313 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
314
315 open(FILE, "file") or die "$0: Failed to open 'file': $!";
316 chomp(my @file = <FILE>);
317 close FILE;
318
319 open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
320 chomp(my @bak = <BAK>);
321 close BAK;
322
323 is(join(":", @file),
324 "bar yada dada:bada bar bing:king kong bar",
325 "-i new file");
326 is(join(":", @bak),
327 "foo yada dada:bada foo bing:king kong foo",
328 "-i backup file");
329}
bc9b29db 330
331# Tests for -E
332
333$r = runperl(
334 switches => [ '-E', '"say q(Hello, world!)"']
335);
336is( $r, "Hello, world!\n", "-E say" );
337
338
339$r = runperl(
bc9b29db 340 switches => [ '-E', '"undef ~~ undef and say q(Hello, world!)"']
341);
342is( $r, "Hello, world!\n", "-E ~~" );
343
344$r = runperl(
345 switches => [ '-E', '"given(undef) {when(undef) { say q(Hello, world!)"}}']
346);
347is( $r, "Hello, world!\n", "-E given" );
9f639728 348
349$r = runperl(
f21873d1 350 switches => [ '-nE', q("} END { say q/affe/") ],
9f639728 351 stdin => 'zomtek',
352);
353is( $r, "affe\n", '-E works outside of the block created by -n' );
d3133c89 354
355# RT #30660
356
357$filename = tempfile();
358SKIP: {
359 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
360 print $f <<'SWTEST';
361#!perl -w -iok
362print "$^I\n";
363SWTEST
364 close $f or die "Could not close: $!";
365 $r = runperl(
366 progfile => $filename,
367 );
368 like( $r, qr/ok/, 'Spaces on the #! line (#30660)' );
369}