summaryrefslogtreecommitdiff
path: root/t/comp/use.t
blob: e66c4a39b59aca2399782034d6bc8691cd614211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!./perl

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    $INC{"feature.pm"} = 1; # so we don't attempt to load feature.pm
}

print "1..59\n";

# Can't require test.pl, as we're testing the use/require mechanism here.

my $test = 1;

sub _ok {
    my ($type, $got, $expected, $name) = @_;

    my $result;
    if ($type eq 'is') {
	$result = $got eq $expected;
    } elsif ($type eq 'isnt') {
	$result = $got ne $expected;
    } elsif ($type eq 'like') {
	$result = $got =~ $expected;
    } else {
	die "Unexpected type '$type'$name";
    }
    if ($result) {
	if ($name) {
	    print "ok $test - $name\n";
	} else {
	    print "ok $test\n";
	}
    } else {
	if ($name) {
	    print "not ok $test - $name\n";
	} else {
	    print "not ok $test\n";
	}
	my @caller = caller(2);
	print "# Failed test at $caller[1] line $caller[2]\n";
	print "# Got      '$got'\n";
	if ($type eq 'is') {
	    print "# Expected '$expected'\n";
	} elsif ($type eq 'isnt') {
	    print "# Expected not '$expected'\n";
	} elsif ($type eq 'like') {
	    print "# Expected $expected\n";
	}
    }
    $test = $test + 1;
    $result;
}

sub like ($$;$) {
    _ok ('like', @_);
}
sub is ($$;$) {
    _ok ('is', @_);
}
sub isnt ($$;$) {
    _ok ('isnt', @_);
}

eval "use 5.000";	# implicit semicolon
is ($@, '');

eval "use 5.000;";
is ($@, '');

eval "use 6.000;";
like ($@, qr/Perl v6\.0\.0 required--this is only \Q$^V\E, stopped/);

eval "no 6.000;";
is ($@, '');

eval "no 5.000;";
like ($@, qr/Perls since v5\.0\.0 too modern--this is \Q$^V\E, stopped/);

eval sprintf "use %.6f;", $];
is ($@, '');


eval sprintf "use %.6f;", $] - 0.000001;
is ($@, '');

eval sprintf("use %.6f;", $] + 1);
like ($@, qr/Perl v6.\d+.\d+ required--this is only \Q$^V\E, stopped/);

eval sprintf "use %.6f;", $] + 0.00001;
like ($@, qr/Perl v5.\d+.\d+ required--this is only \Q$^V\E, stopped/);

{ use lib }	# check that subparse saves pending tokens

local $lib::VERSION = 1.0;

eval "use lib 0.9";
is ($@, '');

eval "use lib 1.0";
is ($@, '');

eval "use lib 1.01";
isnt ($@, '');


eval "use lib 0.9 qw(fred)";
is ($@, '');

if ($^O eq 'MacOS') {
    is($INC[0], ":fred:");
} else {
    is($INC[0], "fred");
}

eval "use lib 1.0 qw(joe)";
is ($@, '');


if ($^O eq 'MacOS') {
    is($INC[0], ":joe:");
} else {
    is($INC[0], "joe");
}


eval "use lib 1.01 qw(freda)";
isnt($@, '');

if ($^O eq 'MacOS') {
    isnt($INC[0], ":freda:");
} else {
    isnt($INC[0], "freda");
}

{
    local $lib::VERSION = 35.36;
    eval "use lib v33.55";
    is ($@, '');

    eval "use lib v100.105";
    like ($@, qr/lib version v100.105.0 required--this is only version v35\.360\.0/);

    eval "use lib 33.55";
    is ($@, '');

    eval "use lib 100.105";
    like ($@, qr/lib version 100.105 required--this is only version 35.36/);

    local $lib::VERSION = '35.36';
    eval "use lib v33.55";
    like ($@, '');

    eval "use lib v100.105";
    like ($@, qr/lib version v100.105.0 required--this is only version v35\.360\.0/);

    eval "use lib 33.55";
    is ($@, '');

    eval "use lib 100.105";
    like ($@, qr/lib version 100.105 required--this is only version 35.36/);

    local $lib::VERSION = v35.36;
    eval "use lib v33.55";
    is ($@, '');

    eval "use lib v100.105";
    like ($@, qr/lib version v100.105.0 required--this is only version v35\.36\.0/);

    eval "use lib 33.55";
    is ($@, '');

    eval "use lib 100.105";
    like ($@, qr/lib version 100.105 required--this is only version v35.36/);
}


{
    # Regression test for patch 14937: 
    #   Check that a .pm file with no package or VERSION doesn't core.
    open F, ">xxx.pm" or die "Cannot open xxx.pm: $!\n";
    print F "1;\n";
    close F;
    eval "use lib '.'; use xxx 3;";
    like ($@, qr/^xxx defines neither package nor VERSION--version check failed at/);
    unlink 'xxx.pm';
}

my @ver = split /\./, sprintf "%vd", $^V;

foreach my $index (-3..+3) {
    foreach my $v (0, 1) {
	my @parts = @ver;
	if ($index) {
	    if ($index < 0) {
		# Jiggle one of the parts down
		--$parts[-$index - 1];
	    } else {
		# Jiggle one of the parts up
		++$parts[$index - 1];
	    }
	}
	my $v_version = sprintf "v%d.%d.%d", @parts;
	my $version;
	if ($v) {
	    $version = $v_version;
	} else {
	    $version = $parts[0] + $parts[1] / 1000 + $parts[2] / 1000000;
	}

	eval "use $version";
	if ($index > 0) {
	    # The future
	    like ($@,
		  qr/Perl $v_version required--this is only \Q$^V\E, stopped/,
		  "use $version");
	} else {
	    # The present or past
	    is ($@, '', "use $version");
	}

	eval "no $version";
	if ($index <= 0) {
	    # The present or past
	    like ($@,
		  qr/Perls since $v_version too modern--this is \Q$^V\E, stopped/,
		  "no $version");
	} else {
	    # future
	    is ($@, '', "no $version");
	}
    }
}