This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to version.pm 0.71, by John Peacock
[perl5.git] / t / comp / use.t
CommitLineData
8ebc5c01
PP
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
PP
7}
8
3cacfbb9 9print "1..59\n";
8ebc5c01 10
6a4a49dd
NC
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
NC
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
NC
29 if ($name) {
30 print "ok $test - $name\n";
31 } else {
32 print "ok $test\n";
33 }
6a4a49dd 34 } else {
3cacfbb9
NC
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
NC
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
NC
55sub like ($$;$) {
56 _ok ('like', @_);
57}
58sub is ($$;$) {
59 _ok ('is', @_);
60}
61sub isnt ($$;$) {
62 _ok ('isnt', @_);
8ebc5c01 63}
6a4a49dd
NC
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
RGS
73
74eval "no 6.000;";
6a4a49dd 75is ($@, '');
468aa647
RGS
76
77eval "no 5.000;";
6a4a49dd 78like ($@, qr/Perls since v5\.0\.0 too modern--this is \Q$^V\E, stopped/);
468aa647 79
6d2c9499 80eval sprintf "use %.6f;", $];
6a4a49dd 81is ($@, '');
8ebc5c01
PP
82
83
6d2c9499 84eval sprintf "use %.6f;", $] - 0.000001;
6a4a49dd 85is ($@, '');
8ebc5c01 86
6d2c9499 87eval sprintf("use %.6f;", $] + 1);
6a4a49dd 88like ($@, qr/Perl v6.\d+.\d+ required--this is only \Q$^V\E, stopped/);
8ebc5c01 89
6d2c9499 90eval sprintf "use %.6f;", $] + 0.00001;
6a4a49dd 91like ($@, qr/Perl v5.\d+.\d+ required--this is only \Q$^V\E, stopped/);
8ebc5c01 92
18b09519 93{ use lib } # check that subparse saves pending tokens
8ebc5c01
PP
94
95local $lib::VERSION = 1.0;
96
97eval "use lib 0.9";
6a4a49dd 98is ($@, '');
8ebc5c01
PP
99
100eval "use lib 1.0";
6a4a49dd 101is ($@, '');
8ebc5c01
PP
102
103eval "use lib 1.01";
6a4a49dd 104isnt ($@, '');
8ebc5c01
PP
105
106
107eval "use lib 0.9 qw(fred)";
6a4a49dd 108is ($@, '');
8ebc5c01 109
6a4a49dd
NC
110if ($^O eq 'MacOS') {
111 is($INC[0], ":fred:");
112} else {
113 is($INC[0], "fred");
114}
8ebc5c01
PP
115
116eval "use lib 1.0 qw(joe)";
6a4a49dd
NC
117is ($@, '');
118
119
120if ($^O eq 'MacOS') {
121 is($INC[0], ":joe:");
122} else {
123 is($INC[0], "joe");
8ebc5c01 124}
8ebc5c01 125
8ebc5c01
PP
126
127eval "use lib 1.01 qw(freda)";
6a4a49dd 128isnt($@, '');
8ebc5c01 129
6a4a49dd
NC
130if ($^O eq 'MacOS') {
131 isnt($INC[0], ":freda:");
132} else {
133 isnt($INC[0], "freda");
134}
1571675a
GS
135
136{
137 local $lib::VERSION = 35.36;
138 eval "use lib v33.55";
6a4a49dd 139 is ($@, '');
1571675a
GS
140
141 eval "use lib v100.105";
ac0e6a2f 142 like ($@, qr/lib version v100.105.0 required--this is only version v35\.360\.0/);
1571675a
GS
143
144 eval "use lib 33.55";
6a4a49dd 145 is ($@, '');
1571675a
GS
146
147 eval "use lib 100.105";
ac0e6a2f 148 like ($@, qr/lib version 100.105 required--this is only version 35.360/);
1571675a
GS
149
150 local $lib::VERSION = '35.36';
151 eval "use lib v33.55";
6a4a49dd 152 like ($@, '');
1571675a
GS
153
154 eval "use lib v100.105";
ac0e6a2f 155 like ($@, qr/lib version v100.105.0 required--this is only version v35\.360\.0/);
1571675a
GS
156
157 eval "use lib 33.55";
6a4a49dd 158 is ($@, '');
1571675a
GS
159
160 eval "use lib 100.105";
ac0e6a2f 161 like ($@, qr/lib version 100.105 required--this is only version 35.360/);
1571675a
GS
162
163 local $lib::VERSION = v35.36;
164 eval "use lib v33.55";
6a4a49dd 165 is ($@, '');
1571675a
GS
166
167 eval "use lib v100.105";
ac0e6a2f 168 like ($@, qr/lib version v100.105.0 required--this is only version v35\.36\.0/);
1571675a
GS
169
170 eval "use lib 33.55";
6a4a49dd 171 is ($@, '');
1571675a
GS
172
173 eval "use lib 100.105";
ac0e6a2f 174 like ($@, qr/lib version 100.105 required--this is only version 35.036000/);
1571675a 175}
8312d5ee
PM
176
177
178{
179 # Regression test for patch 14937:
180 # Check that a .pm file with no package or VERSION doesn't core.
181 open F, ">xxx.pm" or die "Cannot open xxx.pm: $!\n";
182 print F "1;\n";
183 close F;
184 eval "use lib '.'; use xxx 3;";
6a4a49dd 185 like ($@, qr/^xxx defines neither package nor VERSION--version check failed at/);
8312d5ee
PM
186 unlink 'xxx.pm';
187}
3cacfbb9
NC
188
189my @ver = split /\./, sprintf "%vd", $^V;
190
191foreach my $index (-3..+3) {
192 foreach my $v (0, 1) {
193 my @parts = @ver;
194 if ($index) {
195 if ($index < 0) {
196 # Jiggle one of the parts down
197 --$parts[-$index - 1];
198 } else {
199 # Jiggle one of the parts up
200 ++$parts[$index - 1];
201 }
202 }
203 my $v_version = sprintf "v%d.%d.%d", @parts;
204 my $version;
205 if ($v) {
206 $version = $v_version;
207 } else {
208 $version = $parts[0] + $parts[1] / 1000 + $parts[2] / 1000000;
209 }
210
211 eval "use $version";
212 if ($index > 0) {
213 # The future
214 like ($@,
215 qr/Perl $v_version required--this is only \Q$^V\E, stopped/,
216 "use $version");
217 } else {
218 # The present or past
219 is ($@, '', "use $version");
220 }
221
222 eval "no $version";
223 if ($index <= 0) {
224 # The present or past
225 like ($@,
226 qr/Perls since $v_version too modern--this is \Q$^V\E, stopped/,
227 "no $version");
228 } else {
229 # future
230 is ($@, '', "no $version");
231 }
232 }
233}
234