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