Commit | Line | Data |
f8973f08 |
1 | #!./perl -wT |
e5d18500 |
2 | |
3 | # Tests for the coderef-in-@INC feature |
4 | |
5 | BEGIN { |
f8973f08 |
6 | chdir 't' if -d 't'; |
7 | @INC = '../lib'; |
e5d18500 |
8 | } |
f8973f08 |
9 | |
47de4e93 |
10 | use File::Spec; |
6ece0f6b |
11 | use Test::More tests => 39; |
47de4e93 |
12 | |
22e2837f |
13 | my @tempfiles = (); |
14 | |
47de4e93 |
15 | sub get_temp_fh { |
22e2837f |
16 | my $f = "DummyModule0000"; |
17 | 1 while -e ++$f; |
18 | push @tempfiles, $f; |
19 | open my $fh, ">$f" or die "Can't create $f: $!"; |
47de4e93 |
20 | print $fh "package ".substr($_[0],0,-3)."; 1;"; |
21 | close $fh; |
22 | open $fh, $f or die "Can't open $f: $!"; |
23 | return $fh; |
24 | } |
f8973f08 |
25 | |
22e2837f |
26 | END { 1 while unlink @tempfiles } |
27 | |
e5d18500 |
28 | sub fooinc { |
29 | my ($self, $filename) = @_; |
30 | if (substr($filename,0,3) eq 'Foo') { |
47de4e93 |
31 | return get_temp_fh($filename); |
e5d18500 |
32 | } |
33 | else { |
f8973f08 |
34 | return undef; |
e5d18500 |
35 | } |
36 | } |
37 | |
38 | push @INC, \&fooinc; |
39 | |
f8973f08 |
40 | ok( !eval { require Bar; 1 }, 'Trying non-magic package' ); |
41 | |
42 | ok( eval { require Foo; 1 }, 'require() magic via code ref' ); |
43 | ok( exists $INC{'Foo.pm'}, ' %INC sees it' ); |
6ece0f6b |
44 | is( ref $INC{'Foo.pm'}, 'CODE', ' key is a coderef in %INC' ); |
45 | is( $INC{'Foo.pm'}, \&fooinc, ' key is correct in %INC' ); |
f8973f08 |
46 | |
47 | ok( eval "use Foo1; 1;", 'use()' ); |
48 | ok( exists $INC{'Foo1.pm'}, ' %INC sees it' ); |
6ece0f6b |
49 | is( ref $INC{'Foo1.pm'}, 'CODE', ' key is a coderef in %INC' ); |
50 | is( $INC{'Foo1.pm'}, \&fooinc, ' key is correct in %INC' ); |
f8973f08 |
51 | |
52 | ok( eval { do 'Foo2.pl'; 1 }, 'do()' ); |
53 | ok( exists $INC{'Foo2.pl'}, ' %INC sees it' ); |
6ece0f6b |
54 | is( ref $INC{'Foo2.pl'}, 'CODE', ' key is a coderef in %INC' ); |
55 | is( $INC{'Foo2.pl'}, \&fooinc, ' key is correct in %INC' ); |
e5d18500 |
56 | |
57 | pop @INC; |
58 | |
f8973f08 |
59 | |
e5d18500 |
60 | sub fooinc2 { |
61 | my ($self, $filename) = @_; |
62 | if (substr($filename, 0, length($self->[1])) eq $self->[1]) { |
47de4e93 |
63 | return get_temp_fh($filename); |
e5d18500 |
64 | } |
65 | else { |
f8973f08 |
66 | return undef; |
e5d18500 |
67 | } |
68 | } |
69 | |
47de4e93 |
70 | my $arrayref = [ \&fooinc2, 'Bar' ]; |
71 | push @INC, $arrayref; |
e5d18500 |
72 | |
f8973f08 |
73 | ok( eval { require Foo; 1; }, 'Originally loaded packages preserved' ); |
74 | ok( !eval { require Foo3; 1; }, 'Original magic INC purged' ); |
75 | |
76 | ok( eval { require Bar; 1 }, 'require() magic via array ref' ); |
77 | ok( exists $INC{'Bar.pm'}, ' %INC sees it' ); |
6ece0f6b |
78 | is( ref $INC{'Bar.pm'}, 'ARRAY', ' key is an arrayref in %INC' ); |
79 | is( $INC{'Bar.pm'}, $arrayref, ' key is correct in %INC' ); |
f8973f08 |
80 | |
81 | ok( eval "use Bar1; 1;", 'use()' ); |
82 | ok( exists $INC{'Bar1.pm'}, ' %INC sees it' ); |
6ece0f6b |
83 | is( ref $INC{'Bar1.pm'}, 'ARRAY', ' key is an arrayref in %INC' ); |
84 | is( $INC{'Bar1.pm'}, $arrayref, ' key is correct in %INC' ); |
f8973f08 |
85 | |
86 | ok( eval { do 'Bar2.pl'; 1 }, 'do()' ); |
87 | ok( exists $INC{'Bar2.pl'}, ' %INC sees it' ); |
6ece0f6b |
88 | is( ref $INC{'Bar2.pl'}, 'ARRAY', ' key is an arrayref in %INC' ); |
89 | is( $INC{'Bar2.pl'}, $arrayref, ' key is correct in %INC' ); |
e5d18500 |
90 | |
91 | pop @INC; |
92 | |
93 | sub FooLoader::INC { |
94 | my ($self, $filename) = @_; |
95 | if (substr($filename,0,4) eq 'Quux') { |
47de4e93 |
96 | return get_temp_fh($filename); |
e5d18500 |
97 | } |
98 | else { |
f8973f08 |
99 | return undef; |
e5d18500 |
100 | } |
101 | } |
102 | |
47de4e93 |
103 | my $href = bless( {}, 'FooLoader' ); |
104 | push @INC, $href; |
e5d18500 |
105 | |
f8973f08 |
106 | ok( eval { require Quux; 1 }, 'require() magic via hash object' ); |
107 | ok( exists $INC{'Quux.pm'}, ' %INC sees it' ); |
6ece0f6b |
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' ); |
e5d18500 |
111 | |
112 | pop @INC; |
113 | |
47de4e93 |
114 | my $aref = bless( [], 'FooLoader' ); |
115 | push @INC, $aref; |
e5d18500 |
116 | |
f8973f08 |
117 | ok( eval { require Quux1; 1 }, 'require() magic via array object' ); |
118 | ok( exists $INC{'Quux1.pm'}, ' %INC sees it' ); |
6ece0f6b |
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' ); |
e5d18500 |
122 | |
123 | pop @INC; |
124 | |
47de4e93 |
125 | my $sref = bless( \(my $x = 1), 'FooLoader' ); |
126 | push @INC, $sref; |
e5d18500 |
127 | |
f8973f08 |
128 | ok( eval { require Quux2; 1 }, 'require() magic via scalar object' ); |
129 | ok( exists $INC{'Quux2.pm'}, ' %INC sees it' ); |
6ece0f6b |
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' ); |
f8973f08 |
133 | |
134 | pop @INC; |