Add tests for -h and (unknown) -z.
[p5sagit/p5-mst-13.2.git] / t / run / switches.t
1 #!./perl -w
2
3 # Tests for the command-line switches -0, -c, -l, -s, -m, -M, -V, -v, -h, -z
4 # Some switches have their own tests, see MANIFEST.
5
6 BEGIN {
7     chdir 't' if -d 't';
8     @INC = '../lib';
9 }
10
11 require "./test.pl";
12
13 plan(tests => 24);
14
15 # due to a bug in VMS's piping which makes it impossible for runperl()
16 # to emulate echo -n (ie. stdin always winds up with a newline), these 
17 # tests almost totally fail.
18 $TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS';
19
20 my $r;
21 my @tmpfiles = ();
22 END { 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 );
31 is( $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 );
38 is( $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 );
45 is( $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 );
52 is( $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 );
59 is( $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 );
66 is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
67
68 $r = runperl(
69     switches    => [ '-066' ],
70     prog        => 'BEGIN { print qq{($/)} } print qq{[$/]}',
71 );
72 is( $r, "(\066)[\066]", '$/ set at compile-time' );
73
74 # Tests for -c
75
76 my $filename = 'swctest.tmp';
77 SKIP: {
78     local $TODO = '';   # this one works on VMS
79
80     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
81     print $f <<'SWTEST';
82 BEGIN { print "block 1\n"; }
83 CHECK { print "block 2\n"; }
84 INIT  { print "block 3\n"; }
85         print "block 4\n";
86 END   { print "block 5\n"; }
87 SWTEST
88     close $f or die "Could not close: $!";
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 );
114 is( $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 );
123 is( $r, '21-', '-s switch parsing' );
124
125 # Bug ID 20011106.084
126 $filename = 'swstest.tmp';
127 SKIP: {
128     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
129     print $f <<'SWTEST';
130 #!perl -sn
131 BEGIN { print $x; exit }
132 SWTEST
133     close $f or die "Could not close: $!";
134     $r = runperl(
135         progfile    => $filename,
136         args        => [ '-x=foo' ],
137     );
138     is( $r, 'foo', '-s on the shebang line' );
139     push @tmpfiles, $filename;
140 }
141
142 # Tests for -m and -M
143
144 $filename = 'swtest.pm';
145 SKIP: {
146     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
147     print $f <<'SWTESTPM';
148 package swtest;
149 sub import { print map "<$_>", @_ }
150 1;
151 SWTESTPM
152     close $f or die "Could not close: $!";
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     );
167
168     {
169         local $TODO = '';  # this one works on VMS
170         is( $r, '', '-m' );
171     }
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 }
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.
186     # we don't test actual format too much since it could change
187     like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
188           '-V generates 20+ lines' );
189
190     like( runperl( switches => ['-V'] ),
191           qr/\ASummary of my perl5 .*configuration:/,
192           '-V looks okay' );
193
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 }
213
214 # Tests for -v
215
216 {
217     local $TODO = '';   # these ones should work on VMS
218
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 }
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 }