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