3 # Tests for the coderef-in-@INC feature
11 use Test::More tests => 39;
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 }
29 my ($self, $filename) = @_;
30 if (substr($filename,0,3) eq 'Foo') {
31 return get_temp_fh($filename);
40 ok( !eval { require Bar; 1 }, 'Trying non-magic package' );
42 ok( eval { require Foo; 1 }, 'require() magic via code ref' );
43 ok( exists $INC{'Foo.pm'}, ' %INC sees it' );
44 is( ref $INC{'Foo.pm'}, 'CODE', ' key is a coderef in %INC' );
45 is( $INC{'Foo.pm'}, \&fooinc, ' key is correct in %INC' );
47 ok( eval "use Foo1; 1;", 'use()' );
48 ok( exists $INC{'Foo1.pm'}, ' %INC sees it' );
49 is( ref $INC{'Foo1.pm'}, 'CODE', ' key is a coderef in %INC' );
50 is( $INC{'Foo1.pm'}, \&fooinc, ' key is correct in %INC' );
52 ok( eval { do 'Foo2.pl'; 1 }, 'do()' );
53 ok( exists $INC{'Foo2.pl'}, ' %INC sees it' );
54 is( ref $INC{'Foo2.pl'}, 'CODE', ' key is a coderef in %INC' );
55 is( $INC{'Foo2.pl'}, \&fooinc, ' key is correct in %INC' );
61 my ($self, $filename) = @_;
62 if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
63 return get_temp_fh($filename);
70 my $arrayref = [ \&fooinc2, 'Bar' ];
73 ok( eval { require Foo; 1; }, 'Originally loaded packages preserved' );
74 ok( !eval { require Foo3; 1; }, 'Original magic INC purged' );
76 ok( eval { require Bar; 1 }, 'require() magic via array ref' );
77 ok( exists $INC{'Bar.pm'}, ' %INC sees it' );
78 is( ref $INC{'Bar.pm'}, 'ARRAY', ' key is an arrayref in %INC' );
79 is( $INC{'Bar.pm'}, $arrayref, ' key is correct in %INC' );
81 ok( eval "use Bar1; 1;", 'use()' );
82 ok( exists $INC{'Bar1.pm'}, ' %INC sees it' );
83 is( ref $INC{'Bar1.pm'}, 'ARRAY', ' key is an arrayref in %INC' );
84 is( $INC{'Bar1.pm'}, $arrayref, ' key is correct in %INC' );
86 ok( eval { do 'Bar2.pl'; 1 }, 'do()' );
87 ok( exists $INC{'Bar2.pl'}, ' %INC sees it' );
88 is( ref $INC{'Bar2.pl'}, 'ARRAY', ' key is an arrayref in %INC' );
89 is( $INC{'Bar2.pl'}, $arrayref, ' key is correct in %INC' );
94 my ($self, $filename) = @_;
95 if (substr($filename,0,4) eq 'Quux') {
96 return get_temp_fh($filename);
103 my $href = bless( {}, 'FooLoader' );
106 ok( eval { require Quux; 1 }, 'require() magic via hash object' );
107 ok( exists $INC{'Quux.pm'}, ' %INC sees it' );
108 is( ref $INC{'Quux.pm'}, 'FooLoader',
109 ' key is an object in %INC' );
110 is( $INC{'Quux.pm'}, $href, ' key is correct in %INC' );
114 my $aref = bless( [], 'FooLoader' );
117 ok( eval { require Quux1; 1 }, 'require() magic via array object' );
118 ok( exists $INC{'Quux1.pm'}, ' %INC sees it' );
119 is( ref $INC{'Quux1.pm'}, 'FooLoader',
120 ' key is an object in %INC' );
121 is( $INC{'Quux1.pm'}, $aref, ' key is correct in %INC' );
125 my $sref = bless( \(my $x = 1), 'FooLoader' );
128 ok( eval { require Quux2; 1 }, 'require() magic via scalar object' );
129 ok( exists $INC{'Quux2.pm'}, ' %INC sees it' );
130 is( ref $INC{'Quux2.pm'}, 'FooLoader',
131 ' key is an object in %INC' );
132 is( $INC{'Quux2.pm'}, $sref, ' key is correct in %INC' );