Move the test for require 5.11.0 not loading strictures to require.t from use.t
[p5sagit/p5-mst-13.2.git] / t / comp / require.t
CommitLineData
f46d017c 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '.';
6 push @INC, '../lib';
f46d017c 7}
8
9e7c6af3 9sub do_require {
10 %INC = ();
11 write_file('bleah.pm',@_);
12 eval { require "bleah.pm" };
13 my @a; # magic guard for scope violations (must be first lexical in file)
14}
15
f46d017c 16# don't make this lexical
17$i = 1;
d2f5bb60 18
b0927e10 19my @fjles_to_delete = qw (bleah.pm bleah.do bleah.flg urkkk.pm urkkk.pmc
20krunch.pm krunch.pmc whap.pm whap.pmc);
21
22
d2f5bb60 23my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
8a220075 24my $Is_UTF8 = (${^OPEN} || "") =~ /:utf8/;
91937335 25my $total_tests = 48;
76b0784a 26if ($Is_EBCDIC || $Is_UTF8) { $total_tests -= 3; }
d2f5bb60 27print "1..$total_tests\n";
f46d017c 28
4a9ae47a 29sub write_file {
30 my $f = shift;
31 open(REQ,">$f") or die "Can't write '$f': $!";
9f82a649 32 binmode REQ;
4a9ae47a 33 print REQ @_;
d1e4d418 34 close REQ or die "Could not close $f: $!";
4a9ae47a 35}
36
9f3d182e 37eval {require 5.005};
38print "# $@\nnot " if $@;
39print "ok ",$i++,"\n";
40
41eval { require 5.005 };
42print "# $@\nnot " if $@;
43print "ok ",$i++,"\n";
44
45eval { require 5.005; };
46print "# $@\nnot " if $@;
47print "ok ",$i++,"\n";
48
49eval {
50 require 5.005
51};
52print "# $@\nnot " if $@;
53print "ok ",$i++,"\n";
54
a7cb1f99 55# new style version numbers
56
57eval { require v5.5.630; };
58print "# $@\nnot " if $@;
59print "ok ",$i++,"\n";
60
dd629d5b 61eval { require 10.0.2; };
a7cb1f99 62print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.2 required/;
63print "ok ",$i++,"\n";
64
44dcb63b 65my $ver = 5.005_63;
a7cb1f99 66eval { require $ver; };
67print "# $@\nnot " if $@;
68print "ok ",$i++,"\n";
69
44dcb63b 70# check inaccurate fp
dbe7b177 71$ver = 10.2;
72eval { require $ver; };
9137345a 73print "# $@\nnot " unless $@ =~ /^Perl v10\.200.0 required/;
dbe7b177 74print "ok ",$i++,"\n";
44dcb63b 75
76$ver = 10.000_02;
a7cb1f99 77eval { require $ver; };
44dcb63b 78print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.20 required/;
a7cb1f99 79print "ok ",$i++,"\n";
80
dd629d5b 81print "not " unless 5.5.1 gt v5.5;
a7cb1f99 82print "ok ",$i++,"\n";
83
a7cb1f99 84{
a7cb1f99 85 print "not " unless v5.5.640 eq "\x{5}\x{5}\x{280}";
86 print "ok ",$i++,"\n";
87
88 print "not " unless v7.15 eq "\x{7}\x{f}";
89 print "ok ",$i++,"\n";
90
91 print "not "
92 unless v1.20.300.4000.50000.600000 eq "\x{1}\x{14}\x{12c}\x{fa0}\x{c350}\x{927c0}";
93 print "ok ",$i++,"\n";
94}
95
91937335 96# "use 5.11.0" (and higher) loads strictures.
97# check that this doesn't happen with require
98eval 'require 5.11.0; ${"foo"} = "bar";';
99print "# $@\nnot " if $@;
100print "ok ",$i++,"\n";
101
4a9ae47a 102# interaction with pod (see the eof)
103write_file('bleah.pm', "print 'ok $i\n'; 1;\n");
104require "bleah.pm";
105$i++;
106
f46d017c 107# run-time failure in require
108do_require "0;\n";
109print "# $@\nnot " unless $@ =~ /did not return a true/;
110print "ok ",$i++,"\n";
111
4d8b06f1 112print "not " if exists $INC{'bleah.pm'};
113print "ok ",$i++,"\n";
114
115my $flag_file = 'bleah.flg';
116# run-time error in require
117for my $expected_compile (1,0) {
118 write_file($flag_file, 1);
119 print "not " unless -e $flag_file;
120 print "ok ",$i++,"\n";
121 write_file('bleah.pm', "unlink '$flag_file' or die; \$a=0; \$b=1/\$a; 1;\n");
122 print "# $@\nnot " if eval { require 'bleah.pm' };
123 print "ok ",$i++,"\n";
124 print "not " unless -e $flag_file xor $expected_compile;
125 print "ok ",$i++,"\n";
27bcc0a7 126 print "not " unless exists $INC{'bleah.pm'};
127 print "ok ",$i++,"\n";
4d8b06f1 128}
129
f46d017c 130# compile-time failure in require
131do_require "1)\n";
f0ec1f9a 132# bison says 'parse error' instead of 'syntax error',
d91e2bdb 133# various yaccs may or may not capitalize 'syntax'.
f0ec1f9a 134print "# $@\nnot " unless $@ =~ /(syntax|parse) error/mi;
f46d017c 135print "ok ",$i++,"\n";
136
27bcc0a7 137# previous failure cached in %INC
138print "not " unless exists $INC{'bleah.pm'};
139print "ok ",$i++,"\n";
4d8b06f1 140write_file($flag_file, 1);
141write_file('bleah.pm', "unlink '$flag_file'; 1");
142print "# $@\nnot " if eval { require 'bleah.pm' };
143print "ok ",$i++,"\n";
144print "# $@\nnot " unless $@ =~ /Compilation failed/i;
145print "ok ",$i++,"\n";
146print "not " unless -e $flag_file;
147print "ok ",$i++,"\n";
27bcc0a7 148print "not " unless exists $INC{'bleah.pm'};
4d8b06f1 149print "ok ",$i++,"\n";
150
f46d017c 151# successful require
152do_require "1";
153print "# $@\nnot " if $@;
154print "ok ",$i++,"\n";
155
faa7e5bb 156# do FILE shouldn't see any outside lexicals
157my $x = "ok $i\n";
158write_file("bleah.do", <<EOT);
159\$x = "not ok $i\\n";
160EOT
e81465be 161do "bleah.do" or die $@;
faa7e5bb 162dofile();
e81465be 163sub dofile { do "bleah.do" or die $@; };
faa7e5bb 164print $x;
faa7e5bb 165
a89be09a 166# Test that scalar context is forced for require
167
168write_file('bleah.pm', <<'**BLEAH**'
169print "not " if !defined wantarray || wantarray ne '';
0c58d367 170print "ok $i - require() context\n";
a89be09a 1711;
172**BLEAH**
173);
174 delete $INC{"bleah.pm"}; ++$::i;
175$foo = eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
176@foo = eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
177 eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
0c58d367 178 eval q{$_=$_+2;require bleah}; delete $INC{"bleah.pm"}; ++$::i;
a89be09a 179$foo = eval {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
180@foo = eval {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
181 eval {require bleah};
182
2d6f15ea 183# Test for fix of RT #24404 : "require $scalar" may load a directory
184my $r = "threads";
185eval { require $r };
186$i++;
6b845e56 187if($@ =~ /Can't locate threads in \@INC/) {
2d6f15ea 188 print "ok $i\n";
189} else {
190 print "not ok $i\n";
191}
192
57f3ff92 193
194write_file('bleah.pm', qq(die "This is an expected error";\n));
195delete $INC{"bleah.pm"}; ++$::i;
196eval { CORE::require bleah; };
197if ($@ =~ /^This is an expected error/) {
198 print "ok $i\n";
199} else {
200 print "not ok $i\n";
201}
202
b0927e10 203sub write_file_not_thing {
204 my ($file, $thing, $test) = @_;
205 write_file($file, <<"EOT");
206 print "not ok $test\n";
207 die "The $thing file should not be loaded";
208EOT
209}
210
211{
212 # Right. We really really need Config here.
213 require Config;
214 die "Failed to load Config for some reason"
215 unless $Config::Config{version};
216 my $ccflags = $Config::Config{ccflags};
217 die "Failed to get ccflags for some reason" unless defined $ccflags;
218
219 my $simple = ++$i;
220 my $pmc_older = ++$i;
221 my $pmc_dies = ++$i;
222 if ($ccflags =~ /(?:^|\s)-DPERL_DISABLE_PMC\b/) {
223 print "# .pmc files are ignored, so test that\n";
224 write_file_not_thing('krunch.pmc', '.pmc', $pmc_older);
225 write_file('urkkk.pm', qq(print "ok $simple\n"));
226 write_file('whap.pmc', qq(die "This is not an expected error"));
227
228 print "# Sleeping for 2 seconds before creating some more files\n";
229 sleep 2;
230
231 write_file('krunch.pm', qq(print "ok $pmc_older\n"));
232 write_file_not_thing('urkkk.pmc', '.pmc', $simple);
233 write_file('whap.pm', qq(die "This is an expected error"));
234 } else {
235 print "# .pmc files should be loaded, so test that\n";
236 write_file('krunch.pmc', qq(print "ok $pmc_older\n";));
237 write_file_not_thing('urkkk.pm', '.pm', $simple);
238 write_file('whap.pmc', qq(die "This is an expected error"));
239
240 print "# Sleeping for 2 seconds before creating some more files\n";
241 sleep 2;
242
243 write_file_not_thing('krunch.pm', '.pm', $pmc_older);
244 write_file('urkkk.pmc', qq(print "ok $simple\n";));
245 write_file_not_thing('whap.pm', '.pm', $pmc_dies);
246 }
247 require urkkk;
248 require krunch;
249 eval {CORE::require whap; 1} and die;
250
251 if ($@ =~ /^This is an expected error/) {
252 print "ok $pmc_dies\n";
253 } else {
254 print "not ok $pmc_dies\n";
255 }
256}
257
d20ea72b 258##########################################
259# What follows are UTF-8 specific tests. #
260# Add generic tests before this point. #
261##########################################
2d6f15ea 262
8a220075 263# UTF-encoded things - skipped on EBCDIC machines and on UTF-8 input
d2f5bb60 264
8a220075 265if ($Is_EBCDIC || $Is_UTF8) { exit; }
d2f5bb60 266
b6357110 267my %templates = (
268 utf8 => 'C0U',
269 utf16be => 'n',
270 utf16le => 'v',
271 );
272
273sub bytes_to_utf {
274 my ($enc, $content, $do_bom) = @_;
275 my $template = $templates{$enc};
276 die "Unsupported encoding $enc" unless $template;
277 return pack "$template*", ($do_bom ? 0xFEFF : ()), unpack "C*", $content;
278}
dea0fc0b 279
b6357110 280foreach (sort keys %templates) {
91229223 281 $i++; do_require(bytes_to_utf($_, qq(print "ok $i # $_\\n"; 1;\n), 1));
dea0fc0b 282}
283
4d8b06f1 284END {
b0927e10 285 foreach my $file (@fjles_to_delete) {
286 1 while unlink $file;
287 }
4d8b06f1 288}
4a9ae47a 289
290# ***interaction with pod (don't put any thing after here)***
291
292=pod