Integrate mainline
[p5sagit/p5-mst-13.2.git] / t / op / inccode.t
1 #!./perl -wT
2
3 # Tests for the coderef-in-@INC feature
4
5 BEGIN {
6     chdir 't' if -d 't';
7     @INC = '../lib';
8 }
9
10 use File::Spec;
11 use Test::More tests => 39;
12
13 my @tempfiles = ();
14
15 sub get_temp_fh {
16     my $f = "DummyModule0000";
17     1 while -e ++$f;
18     push @tempfiles, $f;
19     open my $fh, ">$f" or die "Can't create $f: $!";
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 }
25
26 END { 1 while unlink @tempfiles }
27
28 sub fooinc {
29     my ($self, $filename) = @_;
30     if (substr($filename,0,3) eq 'Foo') {
31         return get_temp_fh($filename);
32     }
33     else {
34         return undef;
35     }
36 }
37
38 push @INC, \&fooinc;
39
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' );
44 is( ref $INC{'Foo.pm'}, 'CODE',    '  key is a coderef in %INC' );
45 is( $INC{'Foo.pm'}, \&fooinc,      '  key is correct in %INC' );
46
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' );
51
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' );
56
57 pop @INC;
58
59
60 sub fooinc2 {
61     my ($self, $filename) = @_;
62     if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
63         return get_temp_fh($filename);
64     }
65     else {
66         return undef;
67     }
68 }
69
70 my $arrayref = [ \&fooinc2, 'Bar' ];
71 push @INC, $arrayref;
72
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' );
78 is( ref $INC{'Bar.pm'}, 'ARRAY',  '  key is an arrayref in %INC' );
79 is( $INC{'Bar.pm'}, $arrayref,    '  key is correct in %INC' );
80
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' );
85
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' );
90
91 pop @INC;
92
93 sub FooLoader::INC {
94     my ($self, $filename) = @_;
95     if (substr($filename,0,4) eq 'Quux') {
96         return get_temp_fh($filename);
97     }
98     else {
99         return undef;
100     }
101 }
102
103 my $href = bless( {}, 'FooLoader' );
104 push @INC, $href;
105
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' );
111
112 pop @INC;
113
114 my $aref = bless( [], 'FooLoader' );
115 push @INC, $aref;
116
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' );
122
123 pop @INC;
124
125 my $sref = bless( \(my $x = 1), 'FooLoader' );
126 push @INC, $sref;
127
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' );
133
134 pop @INC;