[ID 20011126.150] t/op/pack patch to fix Useless use of unpack in void context at...
[p5sagit/p5-mst-13.2.git] / t / op / inccode.t
1 #!./perl -w
2
3 # Tests for the coderef-in-@INC feature
4
5 BEGIN {
6     chdir 't' if -d 't';
7     @INC = qw(. ../lib);
8 }
9
10 use File::Spec;
11
12 require "test.pl";
13 plan(tests => 43);
14
15 my @tempfiles = ();
16
17 sub get_temp_fh {
18     my $f = "DummyModule0000";
19     1 while -e ++$f;
20     push @tempfiles, $f;
21     open my $fh, ">$f" or die "Can't create $f: $!";
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 }
27
28 END { 1 while unlink @tempfiles }
29
30 sub fooinc {
31     my ($self, $filename) = @_;
32     if (substr($filename,0,3) eq 'Foo') {
33         return get_temp_fh($filename);
34     }
35     else {
36         return undef;
37     }
38 }
39
40 push @INC, \&fooinc;
41
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' );
46 is( ref $INC{'Foo.pm'}, 'CODE',    '  key is a coderef in %INC' );
47 is( $INC{'Foo.pm'}, \&fooinc,      '  key is correct in %INC' );
48
49 ok( eval "use Foo1; 1;",           'use()' );  
50 ok( exists $INC{'Foo1.pm'},        '  %INC sees it' );
51 is( ref $INC{'Foo1.pm'}, 'CODE',   '  key is a coderef in %INC' );
52 is( $INC{'Foo1.pm'}, \&fooinc,     '  key is correct in %INC' );
53
54 ok( eval { do 'Foo2.pl'; 1 },      'do()' ); 
55 ok( exists $INC{'Foo2.pl'},        '  %INC sees it' );
56 is( ref $INC{'Foo2.pl'}, 'CODE',   '  key is a coderef in %INC' );
57 is( $INC{'Foo2.pl'}, \&fooinc,     '  key is correct in %INC' );
58
59 pop @INC;
60
61
62 sub fooinc2 {
63     my ($self, $filename) = @_;
64     if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
65         return get_temp_fh($filename);
66     }
67     else {
68         return undef;
69     }
70 }
71
72 my $arrayref = [ \&fooinc2, 'Bar' ];
73 push @INC, $arrayref;
74
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' );
80 is( ref $INC{'Bar.pm'}, 'ARRAY',  '  key is an arrayref in %INC' );
81 is( $INC{'Bar.pm'}, $arrayref,    '  key is correct in %INC' );
82
83 ok( eval "use Bar1; 1;",          'use()' );
84 ok( exists $INC{'Bar1.pm'},       '  %INC sees it' );
85 is( ref $INC{'Bar1.pm'}, 'ARRAY', '  key is an arrayref in %INC' );
86 is( $INC{'Bar1.pm'}, $arrayref,   '  key is correct in %INC' );
87
88 ok( eval { do 'Bar2.pl'; 1 },     'do()' );
89 ok( exists $INC{'Bar2.pl'},       '  %INC sees it' );
90 is( ref $INC{'Bar2.pl'}, 'ARRAY', '  key is an arrayref in %INC' );
91 is( $INC{'Bar2.pl'}, $arrayref,   '  key is correct in %INC' );
92
93 pop @INC;
94
95 sub FooLoader::INC {
96     my ($self, $filename) = @_;
97     if (substr($filename,0,4) eq 'Quux') {
98         return get_temp_fh($filename);
99     }
100     else {
101         return undef;
102     }
103 }
104
105 my $href = bless( {}, 'FooLoader' );
106 push @INC, $href;
107
108 ok( eval { require Quux; 1 },      'require() magic via hash object' );
109 ok( exists $INC{'Quux.pm'},        '  %INC sees it' );
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' );
113
114 pop @INC;
115
116 my $aref = bless( [], 'FooLoader' );
117 push @INC, $aref;
118
119 ok( eval { require Quux1; 1 },     'require() magic via array object' );
120 ok( exists $INC{'Quux1.pm'},       '  %INC sees it' );
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' );
124
125 pop @INC;
126
127 my $sref = bless( \(my $x = 1), 'FooLoader' );
128 push @INC, $sref;
129
130 ok( eval { require Quux2; 1 },     'require() magic via scalar object' );
131 ok( exists $INC{'Quux2.pm'},       '  %INC sees it' );
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' );
135
136 pop @INC;
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;