This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Convert use.t to an inlined is/isnt/like implementation, to give better
[perl5.git] / t / comp / use.t
CommitLineData
8ebc5c01
PP
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
8ebc5c01
PP
6}
7
468aa647 8print "1..31\n";
8ebc5c01 9
6a4a49dd
NC
10# Can't require test.pl, as we're testing the use/require mechanism here.
11
12my $test = 1;
13
14sub _ok {
15 my ($type, $got, $expected, $name) = @_;
16
17 my @caller = caller(2);
18 if ($name) {
19 $name = " $name";
20 }
21 $name .= " at $caller[1] line $caller[2]";
22
23 my $result;
24 if ($type eq 'is') {
25 $result = $got eq $expected;
26 } elsif ($type eq 'isnt') {
27 $result = $got ne $expected;
28 } elsif ($type eq 'like') {
29 $result = $got =~ $expected;
30 } else {
31 die "Unexpected type '$type'$name";
32 }
33 if ($result) {
34 print "ok $test\n";
35 } else {
36 print "not ok $test\n";
37 print "# Failed test $name\n";
38 print "# Got '$got'\n";
39 if ($type eq 'is') {
40 print "# Expected '$expected'\n";
41 } elsif ($type eq 'isnt') {
42 print "# Expected not '$expected'\n";
43 } elsif ($type eq 'like') {
44 print "# Expected $expected\n";
45 }
46 }
47 $test = $test + 1;
48 $result;
9f3d182e 49}
8ebc5c01 50
6a4a49dd
NC
51sub like ($$;$) {
52 _ok ('like', @_);
53}
54sub is ($$;$) {
55 _ok ('is', @_);
56}
57sub isnt ($$;$) {
58 _ok ('isnt', @_);
8ebc5c01 59}
6a4a49dd
NC
60
61eval "use 5.000"; # implicit semicolon
62is ($@, '');
63
64eval "use 5.000;";
65is ($@, '');
8ebc5c01 66
468aa647 67eval "use 6.000;";
6a4a49dd 68like ($@, qr/Perl v6\.0\.0 required--this is only \Q$^V\E, stopped/);
468aa647
RGS
69
70eval "no 6.000;";
6a4a49dd 71is ($@, '');
468aa647
RGS
72
73eval "no 5.000;";
6a4a49dd 74like ($@, qr/Perls since v5\.0\.0 too modern--this is \Q$^V\E, stopped/);
468aa647 75
6d2c9499 76eval sprintf "use %.6f;", $];
6a4a49dd 77is ($@, '');
8ebc5c01
PP
78
79
6d2c9499 80eval sprintf "use %.6f;", $] - 0.000001;
6a4a49dd 81is ($@, '');
8ebc5c01 82
6d2c9499 83eval sprintf("use %.6f;", $] + 1);
6a4a49dd 84like ($@, qr/Perl v6.\d+.\d+ required--this is only \Q$^V\E, stopped/);
8ebc5c01 85
6d2c9499 86eval sprintf "use %.6f;", $] + 0.00001;
6a4a49dd 87like ($@, qr/Perl v5.\d+.\d+ required--this is only \Q$^V\E, stopped/);
8ebc5c01 88
18b09519 89{ use lib } # check that subparse saves pending tokens
8ebc5c01
PP
90
91local $lib::VERSION = 1.0;
92
93eval "use lib 0.9";
6a4a49dd 94is ($@, '');
8ebc5c01
PP
95
96eval "use lib 1.0";
6a4a49dd 97is ($@, '');
8ebc5c01
PP
98
99eval "use lib 1.01";
6a4a49dd 100isnt ($@, '');
8ebc5c01
PP
101
102
103eval "use lib 0.9 qw(fred)";
6a4a49dd 104is ($@, '');
8ebc5c01 105
6a4a49dd
NC
106if ($^O eq 'MacOS') {
107 is($INC[0], ":fred:");
108} else {
109 is($INC[0], "fred");
110}
8ebc5c01
PP
111
112eval "use lib 1.0 qw(joe)";
6a4a49dd
NC
113is ($@, '');
114
115
116if ($^O eq 'MacOS') {
117 is($INC[0], ":joe:");
118} else {
119 is($INC[0], "joe");
8ebc5c01 120}
8ebc5c01 121
8ebc5c01
PP
122
123eval "use lib 1.01 qw(freda)";
6a4a49dd 124isnt($@, '');
8ebc5c01 125
6a4a49dd
NC
126if ($^O eq 'MacOS') {
127 isnt($INC[0], ":freda:");
128} else {
129 isnt($INC[0], "freda");
130}
1571675a
GS
131
132{
133 local $lib::VERSION = 35.36;
134 eval "use lib v33.55";
6a4a49dd 135 is ($@, '');
1571675a
GS
136
137 eval "use lib v100.105";
6a4a49dd 138 like ($@, qr/lib version 100.105 \(v100\.105\.0\) required--this is only version 35.360 \(v35\.360\.0\)/);
1571675a
GS
139
140 eval "use lib 33.55";
6a4a49dd 141 is ($@, '');
1571675a
GS
142
143 eval "use lib 100.105";
6a4a49dd 144 like ($@, qr/lib version 100.105 \(v100\.105\.0\) required--this is only version 35.360 \(v35\.360\.0\)/);
1571675a
GS
145
146 local $lib::VERSION = '35.36';
147 eval "use lib v33.55";
6a4a49dd 148 like ($@, '');
1571675a
GS
149
150 eval "use lib v100.105";
6a4a49dd 151 like ($@, qr/lib version 100.105 \(v100\.105\.0\) required--this is only version 35.360 \(v35\.360\.0\)/);
1571675a
GS
152
153 eval "use lib 33.55";
6a4a49dd 154 is ($@, '');
1571675a
GS
155
156 eval "use lib 100.105";
6a4a49dd 157 like ($@, qr/lib version 100.105 \(v100\.105\.0\) required--this is only version 35.360 \(v35\.360\.0\)/);
1571675a
GS
158
159 local $lib::VERSION = v35.36;
160 eval "use lib v33.55";
6a4a49dd 161 is ($@, '');
1571675a
GS
162
163 eval "use lib v100.105";
6a4a49dd 164 like ($@, qr/lib version 100.105 \(v100\.105\.0\) required--this is only version 35.036000 \(v35\.36\.0\)/);
1571675a
GS
165
166 eval "use lib 33.55";
6a4a49dd 167 is ($@, '');
1571675a
GS
168
169 eval "use lib 100.105";
6a4a49dd 170 like ($@, qr/lib version 100.105 \(v100\.105\.0\) required--this is only version 35.036000 \(v35\.36\.0\)/);
1571675a 171}
8312d5ee
PM
172
173
174{
175 # Regression test for patch 14937:
176 # Check that a .pm file with no package or VERSION doesn't core.
177 open F, ">xxx.pm" or die "Cannot open xxx.pm: $!\n";
178 print F "1;\n";
179 close F;
180 eval "use lib '.'; use xxx 3;";
6a4a49dd 181 like ($@, qr/^xxx defines neither package nor VERSION--version check failed at/);
8312d5ee
PM
182 unlink 'xxx.pm';
183}