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