[perl #32717] BeOS specific Updates
[p5sagit/p5-mst-13.2.git] / t / run / switches.t
CommitLineData
8a73d5dd 1#!./perl -w
2
feafb1eb 3# Tests for the command-line switches:
4# -0, -c, -l, -s, -m, -M, -V, -v, -h, -z, -i
48eaf804 5# Some switches have their own tests, see MANIFEST.
8a73d5dd 6
7BEGIN {
8 chdir 't' if -d 't';
9 @INC = '../lib';
10}
11
12require "./test.pl";
13
feafb1eb 14plan(tests => 26);
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
79my $filename = 'swctest.tmp';
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 );
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);
117is( $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);
126is( $r, '21-', '-s switch parsing' );
127
128# Bug ID 20011106.084
129$filename = 'swstest.tmp';
130SKIP: {
131 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
132 print $f <<'SWTEST';
f0b2cf55 133#!perl -sn
134BEGIN { print $x; exit }
8a73d5dd 135SWTEST
d1e4d418 136 close $f or die "Could not close: $!";
8a73d5dd 137 $r = runperl(
8a73d5dd 138 progfile => $filename,
139 args => [ '-x=foo' ],
140 );
b734d6c9 141 is( $r, 'foo', '-s on the shebang line' );
8a73d5dd 142 push @tmpfiles, $filename;
143}
144
145# Tests for -m and -M
146
147$filename = 'swtest.pm';
148SKIP: {
149 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
150 print $f <<'SWTESTPM';
151package swtest;
152sub import { print map "<$_>", @_ }
1531;
154SWTESTPM
d1e4d418 155 close $f or die "Could not close: $!";
8a73d5dd 156 $r = runperl(
157 switches => [ '-Mswtest' ],
158 prog => '1',
159 );
160 is( $r, '<swtest>', '-M' );
161 $r = runperl(
162 switches => [ '-Mswtest=foo' ],
163 prog => '1',
164 );
165 is( $r, '<swtest><foo>', '-M with import parameter' );
166 $r = runperl(
167 switches => [ '-mswtest' ],
168 prog => '1',
169 );
b734d6c9 170
171 {
172 local $TODO = ''; # this one works on VMS
173 is( $r, '', '-m' );
174 }
8a73d5dd 175 $r = runperl(
176 switches => [ '-mswtest=foo,bar' ],
177 prog => '1',
178 );
179 is( $r, '<swtest><foo><bar>', '-m with import parameters' );
180 push @tmpfiles, $filename;
181}
e54d2dfa 182
183# Tests for -V
184
185{
186 local $TODO = ''; # these ones should work on VMS
187
188 # basic perl -V should generate significant output.
ceda46a1 189 # we don't test actual format too much since it could change
e54d2dfa 190 like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
191 '-V generates 20+ lines' );
192
ceda46a1 193 like( runperl( switches => ['-V'] ),
194 qr/\ASummary of my perl5 .*configuration:/,
195 '-V looks okay' );
196
e54d2dfa 197 # lookup a known config var
198 chomp( $r=runperl( switches => ['-V:osname'] ) );
199 is( $r, "osname='$^O';", 'perl -V:osname');
200
201 # lookup a nonexistent var
202 chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
203 is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
204 'perl -V:unknown var');
205
206 # regexp lookup
207 # platforms that don't like this quoting can either skip this test
208 # or fix test.pl _quote_args
209 $r = runperl( switches => ['"-V:i\D+size"'] );
210 # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
211 like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
212
213 # make sure each line we got matches the re
214 ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
215}
ceda46a1 216
217# Tests for -v
218
219{
220 local $TODO = ''; # these ones should work on VMS
221
ceda46a1 222 my $v = sprintf "%vd", $^V;
ceda46a1 223 like( runperl( switches => ['-v'] ),
224 qr/This is perl, v$v built for $Config{archname}.+Copyright.+Larry Wall.+Artistic License.+GNU General Public License/s,
225 '-v looks okay' );
226
227}
b8e3af44 228
229# Tests for -h
230
231{
232 local $TODO = ''; # these ones should work on VMS
233
234 like( runperl( switches => ['-h'] ),
a09a0aa2 235 qr/Usage: .+(?i:perl(?:$Config{_exe})?).+switches.+programfile.+arguments/,
b8e3af44 236 '-h looks okay' );
237
238}
239
240# Tests for -z (which does not exist)
241
242{
243 local $TODO = ''; # these ones should work on VMS
244
245 like( runperl( switches => ['-z'], stderr => 1 ),
246 qr/\QUnrecognized switch: -z (-h will show valid options)./,
247 '-z correctly unknown' );
248
249}
feafb1eb 250
251# Tests for -i
252
253{
254 local $TODO = ''; # these ones should work on VMS
255
256 sub do_i_unlink { 1 while unlink("file", "file.bak") }
257
258 open(FILE, ">file") or die "$0: Failed to create 'file': $!";
259 print FILE <<__EOF__;
260foo yada dada
261bada foo bing
262king kong foo
263__EOF__
264 close FILE;
265
266 END { do_i_unlink() }
267
268 runperl( switches => ['-pi.bak'], prog => 's/foo/bar/', args => ['file'] );
269
270 open(FILE, "file") or die "$0: Failed to open 'file': $!";
271 chomp(my @file = <FILE>);
272 close FILE;
273
274 open(BAK, "file.bak") or die "$0: Failed to open 'file': $!";
275 chomp(my @bak = <BAK>);
276 close BAK;
277
278 is(join(":", @file),
279 "bar yada dada:bada bar bing:king kong bar",
280 "-i new file");
281 is(join(":", @bak),
282 "foo yada dada:bada foo bing:king kong foo",
283 "-i backup file");
284}