AutoSplit.t (was Re: Untested libraries update)
[p5sagit/p5-mst-13.2.git] / t / op / inccode.t
CommitLineData
f8973f08 1#!./perl -wT
e5d18500 2
3# Tests for the coderef-in-@INC feature
4
5BEGIN {
f8973f08 6 chdir 't' if -d 't';
7 @INC = '../lib';
e5d18500 8}
f8973f08 9
47de4e93 10use File::Spec;
6ece0f6b 11use Test::More tests => 39;
47de4e93 12
22e2837f 13my @tempfiles = ();
14
47de4e93 15sub 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 26END { 1 while unlink @tempfiles }
27
e5d18500 28sub 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
38push @INC, \&fooinc;
39
f8973f08 40ok( !eval { require Bar; 1 }, 'Trying non-magic package' );
41
42ok( eval { require Foo; 1 }, 'require() magic via code ref' );
43ok( exists $INC{'Foo.pm'}, ' %INC sees it' );
6ece0f6b 44is( ref $INC{'Foo.pm'}, 'CODE', ' key is a coderef in %INC' );
45is( $INC{'Foo.pm'}, \&fooinc, ' key is correct in %INC' );
f8973f08 46
47ok( eval "use Foo1; 1;", 'use()' );
48ok( exists $INC{'Foo1.pm'}, ' %INC sees it' );
6ece0f6b 49is( ref $INC{'Foo1.pm'}, 'CODE', ' key is a coderef in %INC' );
50is( $INC{'Foo1.pm'}, \&fooinc, ' key is correct in %INC' );
f8973f08 51
52ok( eval { do 'Foo2.pl'; 1 }, 'do()' );
53ok( exists $INC{'Foo2.pl'}, ' %INC sees it' );
6ece0f6b 54is( ref $INC{'Foo2.pl'}, 'CODE', ' key is a coderef in %INC' );
55is( $INC{'Foo2.pl'}, \&fooinc, ' key is correct in %INC' );
e5d18500 56
57pop @INC;
58
f8973f08 59
e5d18500 60sub 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 70my $arrayref = [ \&fooinc2, 'Bar' ];
71push @INC, $arrayref;
e5d18500 72
f8973f08 73ok( eval { require Foo; 1; }, 'Originally loaded packages preserved' );
74ok( !eval { require Foo3; 1; }, 'Original magic INC purged' );
75
76ok( eval { require Bar; 1 }, 'require() magic via array ref' );
77ok( exists $INC{'Bar.pm'}, ' %INC sees it' );
6ece0f6b 78is( ref $INC{'Bar.pm'}, 'ARRAY', ' key is an arrayref in %INC' );
79is( $INC{'Bar.pm'}, $arrayref, ' key is correct in %INC' );
f8973f08 80
81ok( eval "use Bar1; 1;", 'use()' );
82ok( exists $INC{'Bar1.pm'}, ' %INC sees it' );
6ece0f6b 83is( ref $INC{'Bar1.pm'}, 'ARRAY', ' key is an arrayref in %INC' );
84is( $INC{'Bar1.pm'}, $arrayref, ' key is correct in %INC' );
f8973f08 85
86ok( eval { do 'Bar2.pl'; 1 }, 'do()' );
87ok( exists $INC{'Bar2.pl'}, ' %INC sees it' );
6ece0f6b 88is( ref $INC{'Bar2.pl'}, 'ARRAY', ' key is an arrayref in %INC' );
89is( $INC{'Bar2.pl'}, $arrayref, ' key is correct in %INC' );
e5d18500 90
91pop @INC;
92
93sub 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 103my $href = bless( {}, 'FooLoader' );
104push @INC, $href;
e5d18500 105
f8973f08 106ok( eval { require Quux; 1 }, 'require() magic via hash object' );
107ok( exists $INC{'Quux.pm'}, ' %INC sees it' );
6ece0f6b 108is( ref $INC{'Quux.pm'}, 'FooLoader',
109 ' key is an object in %INC' );
110is( $INC{'Quux.pm'}, $href, ' key is correct in %INC' );
e5d18500 111
112pop @INC;
113
47de4e93 114my $aref = bless( [], 'FooLoader' );
115push @INC, $aref;
e5d18500 116
f8973f08 117ok( eval { require Quux1; 1 }, 'require() magic via array object' );
118ok( exists $INC{'Quux1.pm'}, ' %INC sees it' );
6ece0f6b 119is( ref $INC{'Quux1.pm'}, 'FooLoader',
120 ' key is an object in %INC' );
121is( $INC{'Quux1.pm'}, $aref, ' key is correct in %INC' );
e5d18500 122
123pop @INC;
124
47de4e93 125my $sref = bless( \(my $x = 1), 'FooLoader' );
126push @INC, $sref;
e5d18500 127
f8973f08 128ok( eval { require Quux2; 1 }, 'require() magic via scalar object' );
129ok( exists $INC{'Quux2.pm'}, ' %INC sees it' );
6ece0f6b 130is( ref $INC{'Quux2.pm'}, 'FooLoader',
131 ' key is an object in %INC' );
132is( $INC{'Quux2.pm'}, $sref, ' key is correct in %INC' );
f8973f08 133
134pop @INC;