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