Re: [PATCH] perl -V:
[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 => 19);
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 # Tests for -c
68
69 my $filename = 'swctest.tmp';
70 SKIP: {
71     local $TODO = '';   # this one works on VMS
72
73     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
74     print $f <<'SWTEST';
75 BEGIN { print "block 1\n"; }
76 CHECK { print "block 2\n"; }
77 INIT  { print "block 3\n"; }
78         print "block 4\n";
79 END   { print "block 5\n"; }
80 SWTEST
81     close $f or die "Could not close: $!";
82     $r = runperl(
83         switches        => [ '-c' ],
84         progfile        => $filename,
85         stderr          => 1,
86     );
87     # Because of the stderr redirection, we can't tell reliably the order
88     # in which the output is given
89     ok(
90         $r =~ /$filename syntax OK/
91         && $r =~ /\bblock 1\b/
92         && $r =~ /\bblock 2\b/
93         && $r !~ /\bblock 3\b/
94         && $r !~ /\bblock 4\b/
95         && $r !~ /\bblock 5\b/,
96         '-c'
97     );
98     push @tmpfiles, $filename;
99 }
100
101 # Tests for -l
102
103 $r = runperl(
104     switches    => [ sprintf("-l%o", ord 'x') ],
105     prog        => 'print for qw/foo bar/'
106 );
107 is( $r, 'fooxbarx', '-l with octal number' );
108
109 # Tests for -s
110
111 $r = runperl(
112     switches    => [ '-s' ],
113     prog        => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
114     args        => [ '--', '-abc=2', '-def', ],
115 );
116 is( $r, '21-', '-s switch parsing' );
117
118 # Bug ID 20011106.084
119 $filename = 'swstest.tmp';
120 SKIP: {
121     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
122     print $f <<'SWTEST';
123 #!perl -s
124 print $x
125 SWTEST
126     close $f or die "Could not close: $!";
127     $r = runperl(
128         switches    => [ '-s' ],
129         progfile    => $filename,
130         args        => [ '-x=foo' ],
131     );
132     is( $r, 'foo', '-s on the shebang line' );
133     push @tmpfiles, $filename;
134 }
135
136 # Tests for -m and -M
137
138 $filename = 'swtest.pm';
139 SKIP: {
140     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
141     print $f <<'SWTESTPM';
142 package swtest;
143 sub import { print map "<$_>", @_ }
144 1;
145 SWTESTPM
146     close $f or die "Could not close: $!";
147     $r = runperl(
148         switches    => [ '-Mswtest' ],
149         prog        => '1',
150     );
151     is( $r, '<swtest>', '-M' );
152     $r = runperl(
153         switches    => [ '-Mswtest=foo' ],
154         prog        => '1',
155     );
156     is( $r, '<swtest><foo>', '-M with import parameter' );
157     $r = runperl(
158         switches    => [ '-mswtest' ],
159         prog        => '1',
160     );
161
162     {
163         local $TODO = '';  # this one works on VMS
164         is( $r, '', '-m' );
165     }
166     $r = runperl(
167         switches    => [ '-mswtest=foo,bar' ],
168         prog        => '1',
169     );
170     is( $r, '<swtest><foo><bar>', '-m with import parameters' );
171     push @tmpfiles, $filename;
172 }
173
174 # Tests for -V
175
176 {
177     local $TODO = '';   # these ones should work on VMS
178
179     # basic perl -V should generate significant output.
180     # we don't test actual format since it could change
181     like( runperl( switches => ['-V'] ), qr/(\n.*){20}/,
182           '-V generates 20+ lines' );
183
184     # lookup a known config var
185     chomp( $r=runperl( switches => ['-V:osname'] ) );
186     is( $r, "osname='$^O';", 'perl -V:osname');
187
188     # lookup a nonexistent var
189     chomp( $r=runperl( switches => ['-V:this_var_makes_switches_test_fail'] ) );
190     is( $r, "this_var_makes_switches_test_fail='UNKNOWN';",
191         'perl -V:unknown var');
192
193     # regexp lookup
194     # platforms that don't like this quoting can either skip this test
195     # or fix test.pl _quote_args
196     $r = runperl( switches => ['"-V:i\D+size"'] );
197     # should be unlike( $r, qr/^$|not found|UNKNOWN/ );
198     like( $r, qr/^(?!.*(not found|UNKNOWN))./, 'perl -V:re got a result' );
199
200     # make sure each line we got matches the re
201     ok( !( grep !/^i\D+size=/, split /^/, $r ), '-V:re correct' );
202 }