t/TEST shouldn't use -M options until we've tested that they work.
[p5sagit/p5-mst-13.2.git] / t / comp / use.t
CommitLineData
8ebc5c01 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
2246ee03 6 $INC{"feature.pm"} = 1; # so we don't attempt to load feature.pm
8ebc5c01 7}
8
5cc917d6 9print "1..68\n";
8ebc5c01 10
6a4a49dd 11# Can't require test.pl, as we're testing the use/require mechanism here.
12
13my $test = 1;
14
15sub _ok {
16 my ($type, $got, $expected, $name) = @_;
17
6a4a49dd 18 my $result;
19 if ($type eq 'is') {
20 $result = $got eq $expected;
21 } elsif ($type eq 'isnt') {
22 $result = $got ne $expected;
23 } elsif ($type eq 'like') {
24 $result = $got =~ $expected;
25 } else {
26 die "Unexpected type '$type'$name";
27 }
28 if ($result) {
3cacfbb9 29 if ($name) {
30 print "ok $test - $name\n";
31 } else {
32 print "ok $test\n";
33 }
6a4a49dd 34 } else {
3cacfbb9 35 if ($name) {
36 print "not ok $test - $name\n";
37 } else {
38 print "not ok $test\n";
39 }
40 my @caller = caller(2);
41 print "# Failed test at $caller[1] line $caller[2]\n";
6a4a49dd 42 print "# Got '$got'\n";
43 if ($type eq 'is') {
44 print "# Expected '$expected'\n";
45 } elsif ($type eq 'isnt') {
46 print "# Expected not '$expected'\n";
47 } elsif ($type eq 'like') {
48 print "# Expected $expected\n";
49 }
50 }
51 $test = $test + 1;
52 $result;
9f3d182e 53}
8ebc5c01 54
6a4a49dd 55sub like ($$;$) {
56 _ok ('like', @_);
57}
58sub is ($$;$) {
59 _ok ('is', @_);
60}
61sub isnt ($$;$) {
62 _ok ('isnt', @_);
8ebc5c01 63}
6a4a49dd 64
65eval "use 5.000"; # implicit semicolon
66is ($@, '');
67
68eval "use 5.000;";
69is ($@, '');
8ebc5c01 70
468aa647 71eval "use 6.000;";
6a4a49dd 72like ($@, qr/Perl v6\.0\.0 required--this is only \Q$^V\E, stopped/);
468aa647 73
74eval "no 6.000;";
6a4a49dd 75is ($@, '');
468aa647 76
77eval "no 5.000;";
6a4a49dd 78like ($@, qr/Perls since v5\.0\.0 too modern--this is \Q$^V\E, stopped/);
468aa647 79
d1029faa 80eval "use 5.6;";
81like ($@, qr/Perl v5\.600\.0 required \(did you mean v5\.6\.0\?\)--this is only \Q$^V\E, stopped/);
82
83eval "use 5.8;";
84like ($@, qr/Perl v5\.800\.0 required \(did you mean v5\.8\.0\?\)--this is only \Q$^V\E, stopped/);
85
86eval "use 5.9;";
87like ($@, qr/Perl v5\.900\.0 required \(did you mean v5\.9\.0\?\)--this is only \Q$^V\E, stopped/);
88
89eval "use 5.10;";
90like ($@, qr/Perl v5\.100\.0 required \(did you mean v5\.10\.0\?\)--this is only \Q$^V\E, stopped/);
91
53eb19dd 92eval "use 5.11;";
93like ($@, qr/Perl v5\.110\.0 required \(did you mean v5\.11\.0\?\)--this is only \Q$^V\E, stopped/);
94
6d2c9499 95eval sprintf "use %.6f;", $];
6a4a49dd 96is ($@, '');
8ebc5c01 97
98
6d2c9499 99eval sprintf "use %.6f;", $] - 0.000001;
6a4a49dd 100is ($@, '');
8ebc5c01 101
6d2c9499 102eval sprintf("use %.6f;", $] + 1);
6a4a49dd 103like ($@, qr/Perl v6.\d+.\d+ required--this is only \Q$^V\E, stopped/);
8ebc5c01 104
6d2c9499 105eval sprintf "use %.6f;", $] + 0.00001;
6a4a49dd 106like ($@, qr/Perl v5.\d+.\d+ required--this is only \Q$^V\E, stopped/);
8ebc5c01 107
53eb19dd 108# check that "use 5.11.0" (and higher) loads strictures
109eval 'use 5.11.0; ${"foo"} = "bar";';
110like ($@, qr/Can't use string \("foo"\) as a SCALAR ref while "strict refs" in use/);
5cc917d6 111# but that they can be disabled
112eval 'use 5.11.0; no strict "refs"; ${"foo"} = "bar";';
113is ($@, "");
114# and they are properly scoped
115eval '{use 5.11.0;} ${"foo"} = "bar";';
116is ($@, "");
117# and this doesn't happen with require
118eval 'require 5.11.0; ${"foo"} = "bar";';
119is ($@, "");
53eb19dd 120
18b09519 121{ use lib } # check that subparse saves pending tokens
8ebc5c01 122
123local $lib::VERSION = 1.0;
124
125eval "use lib 0.9";
6a4a49dd 126is ($@, '');
8ebc5c01 127
128eval "use lib 1.0";
6a4a49dd 129is ($@, '');
8ebc5c01 130
131eval "use lib 1.01";
6a4a49dd 132isnt ($@, '');
8ebc5c01 133
134
135eval "use lib 0.9 qw(fred)";
6a4a49dd 136is ($@, '');
8ebc5c01 137
6a4a49dd 138if ($^O eq 'MacOS') {
139 is($INC[0], ":fred:");
140} else {
141 is($INC[0], "fred");
142}
8ebc5c01 143
144eval "use lib 1.0 qw(joe)";
6a4a49dd 145is ($@, '');
146
147
148if ($^O eq 'MacOS') {
149 is($INC[0], ":joe:");
150} else {
151 is($INC[0], "joe");
8ebc5c01 152}
8ebc5c01 153
8ebc5c01 154
155eval "use lib 1.01 qw(freda)";
6a4a49dd 156isnt($@, '');
8ebc5c01 157
6a4a49dd 158if ($^O eq 'MacOS') {
159 isnt($INC[0], ":freda:");
160} else {
161 isnt($INC[0], "freda");
162}
1571675a 163
164{
165 local $lib::VERSION = 35.36;
166 eval "use lib v33.55";
6a4a49dd 167 is ($@, '');
1571675a 168
169 eval "use lib v100.105";
ac0e6a2f 170 like ($@, qr/lib version v100.105.0 required--this is only version v35\.360\.0/);
1571675a 171
172 eval "use lib 33.55";
6a4a49dd 173 is ($@, '');
1571675a 174
175 eval "use lib 100.105";
8cb289bd 176 like ($@, qr/lib version 100.105 required--this is only version 35.36/);
1571675a 177
178 local $lib::VERSION = '35.36';
179 eval "use lib v33.55";
6a4a49dd 180 like ($@, '');
1571675a 181
182 eval "use lib v100.105";
ac0e6a2f 183 like ($@, qr/lib version v100.105.0 required--this is only version v35\.360\.0/);
1571675a 184
185 eval "use lib 33.55";
6a4a49dd 186 is ($@, '');
1571675a 187
188 eval "use lib 100.105";
8cb289bd 189 like ($@, qr/lib version 100.105 required--this is only version 35.36/);
1571675a 190
191 local $lib::VERSION = v35.36;
192 eval "use lib v33.55";
6a4a49dd 193 is ($@, '');
1571675a 194
195 eval "use lib v100.105";
ac0e6a2f 196 like ($@, qr/lib version v100.105.0 required--this is only version v35\.36\.0/);
1571675a 197
198 eval "use lib 33.55";
6a4a49dd 199 is ($@, '');
1571675a 200
201 eval "use lib 100.105";
8cb289bd 202 like ($@, qr/lib version 100.105 required--this is only version v35.36/);
1571675a 203}
8312d5ee 204
205
206{
207 # Regression test for patch 14937:
208 # Check that a .pm file with no package or VERSION doesn't core.
2d90ac95 209 open F, ">xxx$$.pm" or die "Cannot open xxx$$.pm: $!\n";
8312d5ee 210 print F "1;\n";
211 close F;
2d90ac95 212 eval "use lib '.'; use xxx$$ 3;";
213 like ($@, qr/^xxx$$ defines neither package nor VERSION--version check failed at/);
214 unlink "xxx$$.pm";
8312d5ee 215}
3cacfbb9 216
217my @ver = split /\./, sprintf "%vd", $^V;
218
219foreach my $index (-3..+3) {
220 foreach my $v (0, 1) {
221 my @parts = @ver;
222 if ($index) {
223 if ($index < 0) {
224 # Jiggle one of the parts down
225 --$parts[-$index - 1];
8c5f6936 226 if ($parts[-$index - 1] < 0) {
227 # perl's version number ends with '.0'
228 $parts[-$index - 1] = 0;
229 $parts[-$index - 2] -= 2;
230 }
3cacfbb9 231 } else {
232 # Jiggle one of the parts up
233 ++$parts[$index - 1];
234 }
235 }
236 my $v_version = sprintf "v%d.%d.%d", @parts;
237 my $version;
238 if ($v) {
239 $version = $v_version;
240 } else {
241 $version = $parts[0] + $parts[1] / 1000 + $parts[2] / 1000000;
242 }
243
244 eval "use $version";
245 if ($index > 0) {
246 # The future
247 like ($@,
248 qr/Perl $v_version required--this is only \Q$^V\E, stopped/,
249 "use $version");
250 } else {
251 # The present or past
252 is ($@, '', "use $version");
253 }
254
255 eval "no $version";
256 if ($index <= 0) {
257 # The present or past
258 like ($@,
259 qr/Perls since $v_version too modern--this is \Q$^V\E, stopped/,
260 "no $version");
261 } else {
262 # future
263 is ($@, '', "no $version");
264 }
265 }
266}
267