Re: use utf8; with bad utf8
[p5sagit/p5-mst-13.2.git] / t / run / switches.t
CommitLineData
8a73d5dd 1#!./perl -w
2
3# Tests for the command-line switches
4
5BEGIN {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8}
9
10require "./test.pl";
11
77818208 12plan(tests => 14);
8a73d5dd 13
b734d6c9 14# due to a bug in VMS's piping which makes it impossible for runperl()
15# to emulate echo -n, these tests almost totally fail.
16$TODO = "runperl() unable to emulate echo -n due to pipe bug" if $^O eq 'VMS';
17
8a73d5dd 18my $r;
19my @tmpfiles = ();
20END { unlink @tmpfiles }
21
22# Tests for -0
23
24$r = runperl(
25 switches => [ '-0', ],
26 stdin => 'foo\0bar\0baz\0',
27 prog => 'print qq(<$_>) while <>',
28);
29is( $r, "<foo\0><bar\0><baz\0>", "-0" );
30
31$r = runperl(
32 switches => [ '-l', '-0', '-p' ],
33 stdin => 'foo\0bar\0baz\0',
34 prog => '1',
35);
36is( $r, "foo\nbar\nbaz\n", "-0 after a -l" );
37
38$r = runperl(
39 switches => [ '-0', '-l', '-p' ],
40 stdin => 'foo\0bar\0baz\0',
41 prog => '1',
42);
43is( $r, "foo\0bar\0baz\0", "-0 before a -l" );
44
45$r = runperl(
46 switches => [ sprintf("-0%o", ord 'x') ],
47 stdin => 'fooxbarxbazx',
48 prog => 'print qq(<$_>) while <>',
49);
50is( $r, "<foox><barx><bazx>", "-0 with octal number" );
51
52$r = runperl(
53 switches => [ '-00', '-p' ],
54 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
55 prog => 's/\n/-/g;$_.=q(/)',
56);
57is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' );
58
59$r = runperl(
60 switches => [ '-0777', '-p' ],
61 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
62 prog => 's/\n/-/g;$_.=q(/)',
63);
64is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
65
66# Tests for -c
67
68my $filename = 'swctest.tmp';
69SKIP: {
b734d6c9 70 local $TODO = ''; # this one works on VMS
71
8a73d5dd 72 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
73 print $f <<'SWTEST';
74BEGIN { print "block 1\n"; }
75CHECK { print "block 2\n"; }
76INIT { print "block 3\n"; }
77 print "block 4\n";
78END { print "block 5\n"; }
79SWTEST
80 close $f;
81 $r = runperl(
82 switches => [ '-c' ],
83 progfile => $filename,
84 stderr => 1,
85 );
86 # Because of the stderr redirection, we can't tell reliably the order
87 # in which the output is given
88 ok(
89 $r =~ /$filename syntax OK/
90 && $r =~ /\bblock 1\b/
91 && $r =~ /\bblock 2\b/
92 && $r !~ /\bblock 3\b/
93 && $r !~ /\bblock 4\b/
94 && $r !~ /\bblock 5\b/,
95 '-c'
96 );
97 push @tmpfiles, $filename;
98}
99
100# Tests for -l
101
102$r = runperl(
103 switches => [ sprintf("-l%o", ord 'x') ],
104 prog => 'print for qw/foo bar/'
105);
106is( $r, 'fooxbarx', '-l with octal number' );
107
108# Tests for -s
109
110$r = runperl(
111 switches => [ '-s' ],
112 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
113 args => [ '--', '-abc=2', '-def', ],
114);
115is( $r, '21-', '-s switch parsing' );
116
117# Bug ID 20011106.084
118$filename = 'swstest.tmp';
119SKIP: {
120 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
121 print $f <<'SWTEST';
122#!perl -s
123print $x
124SWTEST
125 close $f;
126 $r = runperl(
127 switches => [ '-s' ],
128 progfile => $filename,
129 args => [ '-x=foo' ],
130 );
b734d6c9 131 is( $r, 'foo', '-s on the shebang line' );
8a73d5dd 132 push @tmpfiles, $filename;
133}
134
135# Tests for -m and -M
136
137$filename = 'swtest.pm';
138SKIP: {
139 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
140 print $f <<'SWTESTPM';
141package swtest;
142sub import { print map "<$_>", @_ }
1431;
144SWTESTPM
145 close $f;
146 $r = runperl(
147 switches => [ '-Mswtest' ],
148 prog => '1',
149 );
150 is( $r, '<swtest>', '-M' );
151 $r = runperl(
152 switches => [ '-Mswtest=foo' ],
153 prog => '1',
154 );
155 is( $r, '<swtest><foo>', '-M with import parameter' );
156 $r = runperl(
157 switches => [ '-mswtest' ],
158 prog => '1',
159 );
b734d6c9 160
161 {
162 local $TODO = ''; # this one works on VMS
163 is( $r, '', '-m' );
164 }
8a73d5dd 165 $r = runperl(
166 switches => [ '-mswtest=foo,bar' ],
167 prog => '1',
168 );
169 is( $r, '<swtest><foo><bar>', '-m with import parameters' );
170 push @tmpfiles, $filename;
171}