3 # Tests for the coderef-in-@INC feature
11 use Test::More tests => 30;
16 my $f = "DummyModule0000";
19 open my $fh, ">$f" or die "Can't create $f: $!";
20 print $fh "package ".substr($_[0],0,-3)."; 1;";
22 open $fh, $f or die "Can't open $f: $!";
26 END { 1 while unlink @tempfiles }
30 $str =~ /(0x[0-9a-f]+)/i;
35 my ($self, $filename) = @_;
36 if (substr($filename,0,3) eq 'Foo') {
37 return get_temp_fh($filename);
46 ok( !eval { require Bar; 1 }, 'Trying non-magic package' );
48 ok( eval { require Foo; 1 }, 'require() magic via code ref' );
49 ok( exists $INC{'Foo.pm'}, ' %INC sees it' );
50 is( get_addr($INC{'Foo.pm'}), get_addr(\&fooinc),
51 ' key is correct in %INC' );
53 ok( eval "use Foo1; 1;", 'use()' );
54 ok( exists $INC{'Foo1.pm'}, ' %INC sees it' );
55 is( get_addr($INC{'Foo1.pm'}), get_addr(\&fooinc),
56 ' key is correct in %INC' );
58 ok( eval { do 'Foo2.pl'; 1 }, 'do()' );
59 ok( exists $INC{'Foo2.pl'}, ' %INC sees it' );
60 is( get_addr($INC{'Foo2.pl'}), get_addr(\&fooinc),
61 ' key is correct in %INC' );
67 my ($self, $filename) = @_;
68 if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
69 return get_temp_fh($filename);
76 my $arrayref = [ \&fooinc2, 'Bar' ];
79 ok( eval { require Foo; 1; }, 'Originally loaded packages preserved' );
80 ok( !eval { require Foo3; 1; }, 'Original magic INC purged' );
82 ok( eval { require Bar; 1 }, 'require() magic via array ref' );
83 ok( exists $INC{'Bar.pm'}, ' %INC sees it' );
84 is( get_addr($INC{'Bar.pm'}), get_addr($arrayref),
85 ' key is correct in %INC' );
87 ok( eval "use Bar1; 1;", 'use()' );
88 ok( exists $INC{'Bar1.pm'}, ' %INC sees it' );
89 is( get_addr($INC{'Bar1.pm'}), get_addr($arrayref),
90 ' key is correct in %INC' );
92 ok( eval { do 'Bar2.pl'; 1 }, 'do()' );
93 ok( exists $INC{'Bar2.pl'}, ' %INC sees it' );
94 is( get_addr($INC{'Bar2.pl'}), get_addr($arrayref),
95 ' key is correct in %INC' );
100 my ($self, $filename) = @_;
101 if (substr($filename,0,4) eq 'Quux') {
102 return get_temp_fh($filename);
109 my $href = bless( {}, 'FooLoader' );
112 ok( eval { require Quux; 1 }, 'require() magic via hash object' );
113 ok( exists $INC{'Quux.pm'}, ' %INC sees it' );
114 is( get_addr($INC{'Quux.pm'}), get_addr($href),
115 ' key is correct in %INC' );
119 my $aref = bless( [], 'FooLoader' );
122 ok( eval { require Quux1; 1 }, 'require() magic via array object' );
123 ok( exists $INC{'Quux1.pm'}, ' %INC sees it' );
124 is( get_addr($INC{'Quux1.pm'}), get_addr($aref),
125 ' key is correct in %INC' );
129 my $sref = bless( \(my $x = 1), 'FooLoader' );
132 ok( eval { require Quux2; 1 }, 'require() magic via scalar object' );
133 ok( exists $INC{'Quux2.pm'}, ' %INC sees it' );
134 is( get_addr($INC{'Quux2.pm'}), get_addr($sref),
135 ' key is correct in %INC' );