This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new tests for command-line switches
[perl5.git] / t / run / switches.t
CommitLineData
8a73d5dd
RGS
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
12plan(tests => 15);
13
14my $r;
15my @tmpfiles = ();
16END { unlink @tmpfiles }
17
18# Tests for -0
19
20$r = runperl(
21 switches => [ '-0', ],
22 stdin => 'foo\0bar\0baz\0',
23 prog => 'print qq(<$_>) while <>',
24);
25is( $r, "<foo\0><bar\0><baz\0>", "-0" );
26
27$r = runperl(
28 switches => [ '-l', '-0', '-p' ],
29 stdin => 'foo\0bar\0baz\0',
30 prog => '1',
31);
32is( $r, "foo\nbar\nbaz\n", "-0 after a -l" );
33
34$r = runperl(
35 switches => [ '-0', '-l', '-p' ],
36 stdin => 'foo\0bar\0baz\0',
37 prog => '1',
38);
39is( $r, "foo\0bar\0baz\0", "-0 before a -l" );
40
41$r = runperl(
42 switches => [ sprintf("-0%o", ord 'x') ],
43 stdin => 'fooxbarxbazx',
44 prog => 'print qq(<$_>) while <>',
45);
46is( $r, "<foox><barx><bazx>", "-0 with octal number" );
47
48$r = runperl(
49 switches => [ '-00', '-p' ],
50 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
51 prog => 's/\n/-/g;$_.=q(/)',
52);
53is( $r, 'abc-def--/ghi-jkl-mno--/pq-/', '-00 (paragraph mode)' );
54
55$r = runperl(
56 switches => [ '-0777', '-p' ],
57 stdin => 'abc\ndef\n\nghi\njkl\nmno\n\npq\n',
58 prog => 's/\n/-/g;$_.=q(/)',
59);
60is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
61
62# Tests for -c
63
64my $filename = 'swctest.tmp';
65SKIP: {
66 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
67 print $f <<'SWTEST';
68BEGIN { print "block 1\n"; }
69CHECK { print "block 2\n"; }
70INIT { print "block 3\n"; }
71 print "block 4\n";
72END { print "block 5\n"; }
73SWTEST
74 close $f;
75 $r = runperl(
76 switches => [ '-c' ],
77 progfile => $filename,
78 stderr => 1,
79 );
80 # Because of the stderr redirection, we can't tell reliably the order
81 # in which the output is given
82 ok(
83 $r =~ /$filename syntax OK/
84 && $r =~ /\bblock 1\b/
85 && $r =~ /\bblock 2\b/
86 && $r !~ /\bblock 3\b/
87 && $r !~ /\bblock 4\b/
88 && $r !~ /\bblock 5\b/,
89 '-c'
90 );
91 push @tmpfiles, $filename;
92}
93
94# Tests for -l
95
96$r = runperl(
97 switches => [ sprintf("-l%o", ord 'x') ],
98 prog => 'print for qw/foo bar/'
99);
100is( $r, 'fooxbarx', '-l with octal number' );
101
102# Tests for -s
103
104$r = runperl(
105 switches => [ '-s' ],
106 prog => 'for (qw/abc def ghi/) {print defined $$_ ? $$_ : q(-)}',
107 args => [ '--', '-abc=2', '-def', ],
108);
109is( $r, '21-', '-s switch parsing' );
110
111# Bug ID 20011106.084
112$filename = 'swstest.tmp';
113SKIP: {
114 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
115 print $f <<'SWTEST';
116#!perl -s
117print $x
118SWTEST
119 close $f;
120 $r = runperl(
121 switches => [ '-s' ],
122 progfile => $filename,
123 args => [ '-x=foo' ],
124 );
125 is( $r, 'foo', '-s on the #! line' );
126 push @tmpfiles, $filename;
127}
128
129# Tests for -m and -M
130
131$filename = 'swtest.pm';
132SKIP: {
133 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
134 print $f <<'SWTESTPM';
135package swtest;
136sub import { print map "<$_>", @_ }
1371;
138SWTESTPM
139 close $f;
140 $r = runperl(
141 switches => [ '-Mswtest' ],
142 prog => '1',
143 );
144 is( $r, '<swtest>', '-M' );
145 $r = runperl(
146 switches => [ '-Mswtest=foo' ],
147 prog => '1',
148 );
149 is( $r, '<swtest><foo>', '-M with import parameter' );
150 $r = runperl(
151 switches => [ '-mswtest' ],
152 prog => '1',
153 );
154 is( $r, '', '-m' );
155 $r = runperl(
156 switches => [ '-mswtest=foo,bar' ],
157 prog => '1',
158 );
159 is( $r, '<swtest><foo><bar>', '-m with import parameters' );
160 push @tmpfiles, $filename;
161}
162
163# Tests for -x
164
165$filename = 'swxtest.tmp';
166SKIP: {
167 open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
168 print $f <<'SWTEST';
169print 1;
170#!perl
171print 2;
172__END__
173print 3;
174SWTEST
175 close $f;
176 $r = runperl(
177 switches => [ '-x' ],
178 progfile => $filename,
179 );
180 is( $r, '2', '-x' );
181 push @tmpfiles, $filename;
182}