This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [PATCH] newer tests for the coderef-in-@INC !
[perl5.git] / t / op / inccode.t
CommitLineData
f8973f08 1#!./perl -wT
e5d18500
AMS
2
3# Tests for the coderef-in-@INC feature
4
5BEGIN {
f8973f08
MS
6 chdir 't' if -d 't';
7 @INC = '../lib';
e5d18500 8}
f8973f08 9
47de4e93
RGS
10use File::Spec;
11use File::Temp qw/tempfile/;
12use Test::More tests => 30;
13
14sub get_temp_fh {
15 my ($fh,$f) = tempfile("DummyModuleXXXX", DIR => File::Spec->curdir,
16 UNLINK => 1);
17 print $fh "package ".substr($_[0],0,-3)."; 1;";
18 close $fh;
19 open $fh, $f or die "Can't open $f: $!";
20 return $fh;
21}
f8973f08 22
47de4e93
RGS
23sub get_addr {
24 my $str = shift;
25 $str =~ /(0x[0-9a-f]+)/i;
26 return $1;
f8973f08 27}
e5d18500
AMS
28
29sub fooinc {
30 my ($self, $filename) = @_;
31 if (substr($filename,0,3) eq 'Foo') {
47de4e93 32 return get_temp_fh($filename);
e5d18500
AMS
33 }
34 else {
f8973f08 35 return undef;
e5d18500
AMS
36 }
37}
38
39push @INC, \&fooinc;
40
f8973f08
MS
41ok( !eval { require Bar; 1 }, 'Trying non-magic package' );
42
43ok( eval { require Foo; 1 }, 'require() magic via code ref' );
44ok( exists $INC{'Foo.pm'}, ' %INC sees it' );
47de4e93
RGS
45is( get_addr($INC{'Foo.pm'}), get_addr(\&fooinc),
46 ' key is correct in %INC' );
f8973f08
MS
47
48ok( eval "use Foo1; 1;", 'use()' );
49ok( exists $INC{'Foo1.pm'}, ' %INC sees it' );
47de4e93
RGS
50is( get_addr($INC{'Foo1.pm'}), get_addr(\&fooinc),
51 ' key is correct in %INC' );
f8973f08
MS
52
53ok( eval { do 'Foo2.pl'; 1 }, 'do()' );
54ok( exists $INC{'Foo2.pl'}, ' %INC sees it' );
47de4e93
RGS
55is( get_addr($INC{'Foo2.pl'}), get_addr(\&fooinc),
56 ' key is correct in %INC' );
e5d18500
AMS
57
58pop @INC;
59
f8973f08 60
e5d18500
AMS
61sub fooinc2 {
62 my ($self, $filename) = @_;
63 if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
47de4e93 64 return get_temp_fh($filename);
e5d18500
AMS
65 }
66 else {
f8973f08 67 return undef;
e5d18500
AMS
68 }
69}
70
47de4e93
RGS
71my $arrayref = [ \&fooinc2, 'Bar' ];
72push @INC, $arrayref;
e5d18500 73
f8973f08
MS
74ok( eval { require Foo; 1; }, 'Originally loaded packages preserved' );
75ok( !eval { require Foo3; 1; }, 'Original magic INC purged' );
76
77ok( eval { require Bar; 1 }, 'require() magic via array ref' );
78ok( exists $INC{'Bar.pm'}, ' %INC sees it' );
47de4e93
RGS
79is( get_addr($INC{'Bar.pm'}), get_addr($arrayref),
80 ' key is correct in %INC' );
f8973f08
MS
81
82ok( eval "use Bar1; 1;", 'use()' );
83ok( exists $INC{'Bar1.pm'}, ' %INC sees it' );
47de4e93
RGS
84is( get_addr($INC{'Bar1.pm'}), get_addr($arrayref),
85 ' key is correct in %INC' );
f8973f08
MS
86
87ok( eval { do 'Bar2.pl'; 1 }, 'do()' );
88ok( exists $INC{'Bar2.pl'}, ' %INC sees it' );
47de4e93
RGS
89is( get_addr($INC{'Bar2.pl'}), get_addr($arrayref),
90 ' key is correct in %INC' );
e5d18500
AMS
91
92pop @INC;
93
94sub FooLoader::INC {
95 my ($self, $filename) = @_;
96 if (substr($filename,0,4) eq 'Quux') {
47de4e93 97 return get_temp_fh($filename);
e5d18500
AMS
98 }
99 else {
f8973f08 100 return undef;
e5d18500
AMS
101 }
102}
103
47de4e93
RGS
104my $href = bless( {}, 'FooLoader' );
105push @INC, $href;
e5d18500 106
f8973f08
MS
107ok( eval { require Quux; 1 }, 'require() magic via hash object' );
108ok( exists $INC{'Quux.pm'}, ' %INC sees it' );
47de4e93
RGS
109is( get_addr($INC{'Quux.pm'}), get_addr($href),
110 ' key is correct in %INC' );
e5d18500
AMS
111
112pop @INC;
113
47de4e93
RGS
114my $aref = bless( [], 'FooLoader' );
115push @INC, $aref;
e5d18500 116
f8973f08
MS
117ok( eval { require Quux1; 1 }, 'require() magic via array object' );
118ok( exists $INC{'Quux1.pm'}, ' %INC sees it' );
47de4e93
RGS
119is( get_addr($INC{'Quux1.pm'}), get_addr($aref),
120 ' key is correct in %INC' );
e5d18500
AMS
121
122pop @INC;
123
47de4e93
RGS
124my $sref = bless( \(my $x = 1), 'FooLoader' );
125push @INC, $sref;
e5d18500 126
f8973f08
MS
127ok( eval { require Quux2; 1 }, 'require() magic via scalar object' );
128ok( exists $INC{'Quux2.pm'}, ' %INC sees it' );
47de4e93
RGS
129is( get_addr($INC{'Quux2.pm'}), get_addr($sref),
130 ' key is correct in %INC' );
f8973f08
MS
131
132pop @INC;