This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate mainline
[perl5.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 => 14);
13
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
18 my $r;
19 my @tmpfiles = ();
20 END { 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 );
29 is( $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 );
36 is( $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 );
43 is( $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 );
50 is( $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 );
57 is( $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 );
64 is( $r, 'abc-def--ghi-jkl-mno--pq-/', '-0777 (slurp mode)' );
65
66 # Tests for -c
67
68 my $filename = 'swctest.tmp';
69 SKIP: {
70     local $TODO = '';   # this one works on VMS
71
72     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
73     print $f <<'SWTEST';
74 BEGIN { print "block 1\n"; }
75 CHECK { print "block 2\n"; }
76 INIT  { print "block 3\n"; }
77         print "block 4\n";
78 END   { print "block 5\n"; }
79 SWTEST
80     close $f or die "Could not close: $!";
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 );
106 is( $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 );
115 is( $r, '21-', '-s switch parsing' );
116
117 # Bug ID 20011106.084
118 $filename = 'swstest.tmp';
119 SKIP: {
120     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!" );
121     print $f <<'SWTEST';
122 #!perl -s
123 print $x
124 SWTEST
125     close $f or die "Could not close: $!";
126     $r = runperl(
127         switches    => [ '-s' ],
128         progfile    => $filename,
129         args        => [ '-x=foo' ],
130     );
131     is( $r, 'foo', '-s on the shebang line' );
132     push @tmpfiles, $filename;
133 }
134
135 # Tests for -m and -M
136
137 $filename = 'swtest.pm';
138 SKIP: {
139     open my $f, ">$filename" or skip( "Can't write temp file $filename: $!",4 );
140     print $f <<'SWTESTPM';
141 package swtest;
142 sub import { print map "<$_>", @_ }
143 1;
144 SWTESTPM
145     close $f or die "Could not close: $!";
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     );
160
161     {
162         local $TODO = '';  # this one works on VMS
163         is( $r, '', '-m' );
164     }
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 }