Test tweak for #9886.
[p5sagit/p5-mst-13.2.git] / t / lib / selfstubber.t
CommitLineData
7f4f6daf 1#!./perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8use strict;
9use Devel::SelfStubber;
10
ef61f41f 11my $runperl = "$^X \"-I../lib\"";
7f4f6daf 12
78bb12ca 13$| = 1;
14
33235a50 15print "1..12\n";
7f4f6daf 16
17my @cleanup;
18
19END {
20 foreach my $file (reverse @cleanup) {
21 unlink $file or warn "unlink $file failed: $!" while -f $file;
22 rmdir $file or warn "rmdir $file failed: $!" if -d $file;
23 }
24}
25
26my $inlib = "SSI-$$";
27mkdir $inlib, 0777 or die $!;
28push @cleanup, $inlib;
29
30while (<DATA>) {
31 if (/^\#{16,}\s+(.*)/) {
32 my $file = "$inlib/$1";
33 push @cleanup, $file;
34 open FH, ">$file" or die $!;
35 } else {
36 print FH;
37 }
38}
39close FH;
40
41{
42 my $file = "A-$$";
43 push @cleanup, $file;
44 open FH, ">$file" or die $!;
45 select FH;
46 Devel::SelfStubber->stub('Child', $inlib);
47 select STDOUT;
48 print "ok 1\n";
49 close FH or die $!;
50
51 open FH, $file or die $!;
52 my @A = <FH>;
53
54 if (@A == 1 && $A[0] =~ /^\s*sub\s+Child::foo\s*;\s*$/) {
55 print "ok 2\n";
56 } else {
57 print "not ok 2\n";
58 print "# $_" foreach (@A);
59 }
60}
61
62{
63 my $file = "B-$$";
64 push @cleanup, $file;
65 open FH, ">$file" or die $!;
66 select FH;
67 Devel::SelfStubber->stub('Proto', $inlib);
68 select STDOUT;
69 print "ok 3\n"; # Checking that we did not die horribly.
70 close FH or die $!;
71
72 open FH, $file or die $!;
73 my @B = <FH>;
74
75 if (@B == 1 && $B[0] =~ /^\s*sub\s+Proto::bar\s*\(\$\$\);\s*$/) {
76 print "ok 4\n";
77 } else {
78 print "not ok 4\n";
79 print "# $_" foreach (@B);
80 }
81
82 close FH or die $!;
83}
84
33235a50 85{
86 my $file = "C-$$";
87 push @cleanup, $file;
88 open FH, ">$file" or die $!;
89 select FH;
90 Devel::SelfStubber->stub('Attribs', $inlib);
91 select STDOUT;
92 print "ok 5\n"; # Checking that we did not die horribly.
93 close FH or die $!;
94
95 open FH, $file or die $!;
96 my @C = <FH>;
97
98 if (@C == 2 && $C[0] =~ /^\s*sub\s+Attribs::baz\s+:\s*locked\s*;\s*$/
99 && $C[1] =~ /^\s*sub\s+Attribs::lv\s+:\s*lvalue\s*:\s*method\s*;\s*$/) {
100 print "ok 6\n";
101 } else {
102 print "not ok 6\n";
103 print "# $_" foreach (@C);
104 }
105
106 close FH or die $!;
107}
108
7f4f6daf 109# "wrong" and "right" may change if SelfLoader is changed.
110my %wrong = ( Parent => 'Parent', Child => 'Parent' );
111my %right = ( Parent => 'Parent', Child => 'Child' );
cbd00933 112if ($^O eq 'VMS') {
113 # extra line feeds for MBX IPC
114 %wrong = ( Parent => "Parent\n", Child => "Parent\n" );
115 %right = ( Parent => "Parent\n", Child => "Child\n" );
116}
7f4f6daf 117my @module = qw(Parent Child)
118;
119sub fail {
120 my ($left, $right) = @_;
121 while (my ($key, $val) = each %$left) {
122 # warn "$key $val $$right{$key}";
123 return 1
124 unless $val eq $$right{$key};
125 }
126 return;
127}
128
129sub faildump {
130 my ($expect, $got) = @_;
131 foreach (sort keys %$expect) {
132 print "# $_ expect '$$expect{$_}' got '$$got{$_}'\n";
133 }
134}
135
136# Now test that the module tree behaves "wrongly" as expected
137
138foreach my $module (@module) {
139 my $file = "$module--$$";
140 push @cleanup, $file;
141 open FH, ">$file" or die $!;
142 print FH "use $module;
143print ${module}->foo;
144";
145 close FH or die $!;
146}
147
148{
149 my %output;
150 foreach my $module (@module) {
ef61f41f 151 print "# $runperl \"-I$inlib\" $module--$$\n";
152 ($output{$module} = `$runperl "-I$inlib" $module--$$`)
7f4f6daf 153 =~ s/\'s foo//;
154 }
155
156 if (&fail (\%wrong, \%output)) {
33235a50 157 print "not ok 7\n", &faildump (\%wrong, \%output);
7f4f6daf 158 } else {
33235a50 159 print "ok 7\n";
7f4f6daf 160 }
161}
162
163my $lib="SSO-$$";
164mkdir $lib, 0777 or die $!;
165push @cleanup, $lib;
166$Devel::SelfStubber::JUST_STUBS=0;
167
168undef $/;
33235a50 169foreach my $module (@module, 'Data', 'End') {
7f4f6daf 170 my $file = "$lib/$module.pm";
171 open FH, "$inlib/$module.pm" or die $!;
172 my $contents = <FH>;
173 close FH or die $!;
174 push @cleanup, $file;
175 open FH, ">$file" or die $!;
176 select FH;
177 if ($contents =~ /__DATA__/) {
178 # This will die for any module with no __DATA__
179 Devel::SelfStubber->stub($module, $inlib);
180 } else {
181 print $contents;
182 }
183 select STDOUT;
184 close FH or die $!;
185}
33235a50 186print "ok 8\n";
7f4f6daf 187
188{
189 my %output;
190 foreach my $module (@module) {
ef61f41f 191 print "# $runperl \"-I$lib\" $module--$$\n";
192 ($output{$module} = `$runperl "-I$lib" $module--$$`)
7f4f6daf 193 =~ s/\'s foo//;
194 }
195
196 if (&fail (\%right, \%output)) {
33235a50 197 print "not ok 9\n", &faildump (\%right, \%output);
7f4f6daf 198 } else {
33235a50 199 print "ok 9\n";
7f4f6daf 200 }
201}
202
33235a50 203# Check that the DATA handle stays open
204system "$runperl -w \"-I$lib\" -MData -e Data::ok";
205
206# Possibly a pointless test as this doesn't really verify that it's been
207# stubbed.
208system "$runperl -w \"-I$lib\" -MEnd -e End::lime";
209
210# But check that the documentation after the __END__ survived.
211open FH, "$lib/End.pm" or die $!;
212$_ = <FH>;
213close FH or die $!;
214
215if (/Did the documentation here survive\?/) {
216 print "ok 12\n";
217} else {
218 print "not ok 12 # information after an __END__ token seems to be lost\n";
219}
220
7f4f6daf 221__DATA__
222################ Parent.pm
223package Parent;
224
225sub foo {
226 return __PACKAGE__;
227}
2281;
229__END__
230################ Child.pm
231package Child;
232require Parent;
233@ISA = 'Parent';
234use SelfLoader;
235
2361;
237__DATA__
238sub foo {
239 return __PACKAGE__;
240}
241__END__
242################ Proto.pm
243package Proto;
244use SelfLoader;
245
2461;
247__DATA__
248sub bar ($$) {
249}
33235a50 250################ Attribs.pm
251package Attribs;
252use SelfLoader;
253
2541;
255__DATA__
256sub baz : locked {
257}
258sub lv : lvalue : method {
259 my $a;
260 \$a;
261}
262################ Data.pm
263package Data;
264use SelfLoader;
265
2661;
267__DATA__
268sub ok {
269 print <DATA>;
270}
271__END__ DATA
272ok 10
273################ End.pm
274package End;
275use SelfLoader;
276
2771;
278__DATA__
279sub lime {
280 print "ok 11\n";
281}
282__END__
283Did the documentation here survive?