]> git.vpit.fr Git - perl/modules/subs-auto.git/blob - t/10-base.t
Test -X _
[perl/modules/subs-auto.git] / t / 10-base.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 67;
7
8 my %_re = (
9  bareword => sub { qr/^Bareword\s+['"]?\s*$_[0]\s*['"]?\s+not\s+allowed\s+while\s+["']?\s*strict\s+subs\s*['"]?\s+in\s+use\s+at\s+$_[1]\s+line\s+$_[2]/ },
10  undefined => sub { qr/^Undefined\s+subroutine\s+\&$_[0]\s+called\s+at\s+$_[1]\s+line\s+$_[2]/ },
11 );
12
13 sub _got_test {
14  my $sub  = shift;
15  my $line = shift;
16  my %args = @_;
17  my $msg  = delete $args{msg};
18  $msg     = join ' ', $args{name}, $sub, 'line', $line unless $msg;
19  my $file = $args{eval} ? '\\(eval\\s+\\d+\\)' : quotemeta $0;
20  my $re   = $_re{$args{name}}->($sub, $file, $line);
21  if ($args{todo}) {
22   TODO: {
23    local $TODO = $args{todo};
24    like($@, $re, $msg);
25   }
26  } else {
27   like($@, $re, $msg);
28  }
29 }
30
31 sub _got_bareword { _got_test(@_, name => 'bareword'); }
32
33 sub _got_undefined {
34  my $sub = shift;
35  $sub = 'main::' . $sub if $sub !~ /::/;
36  _got_test($sub, @_, name => 'undefined');
37 }
38
39 sub _got_ok { is($@, '', $_[0]); }
40
41 my $warn;
42
43 my $bar;
44 sub bar { $bar = 1 }
45
46 eval "yay 11, 13"; # Defined on the other side of the scope
47 _got_ok('compiling to yay(11,13)');
48 our @yay;
49 is_deeply(\@yay, [ 11, 13 ], 'yay really was executed');
50
51 eval "flip"; # Not called in sub::auto zone, not declared, not defined
52 _got_bareword('flip', 1, eval => 1);
53
54 eval "flop"; # Not called in sub::auto zone, declared, not defined
55 _got_undefined('flop', 1, eval => 1);
56
57 my $qux;
58 eval "qux"; # Called in sub::auto zone, not declared, not defined
59 _got_bareword('qux', 1, eval => 1);
60
61 my $blech;
62 eval "blech"; # Called in sub::auto zone, declared, not defined
63 _got_undefined('blech', 1, eval => 1);
64
65 my $wut;
66 eval "wut"; # Called in sub::auto zone, declared, defined
67 _got_ok('compiling to wut()');
68
69 # === Starting from here ======================================================
70 use subs::auto;
71
72 eval { onlycalledonce 1, 2 };
73 _got_undefined('onlycalledonce', __LINE__-1);
74
75 eval { Test::More->import() };
76 _got_ok('don\'t touch class names');
77
78 my $strict;
79 sub strict { $strict = 1; undef }
80 eval { strict->import };
81 is($strict, 1, 'the strict subroutine was called');
82
83 my %h = (
84  a => 5,
85  b => 7,
86 );
87
88 my $foo;
89 our @foo;
90
91 my $y = eval { foo 1, 2, \%h };
92 _got_ok('compiling to foo(1,2,\\\%h)');
93 is($foo, 15, 'foo really was executed');
94
95 eval { wut 13, "what" };
96 _got_ok('compiling to wut(13,"what")');
97 is($wut, 17, 'wut really was executed');
98
99 eval { qux };
100 _got_undefined('qux', __LINE__-1);
101
102 {
103  no strict 'refs';
104  is(*{'::feh'}{CODE}, undef, 'feh isn\'t defined');
105  is(*{'::feh'}{CODE}, undef, 'feh isn\'t defined, really');
106  isnt(*{'::yay'}{CODE}, undef, 'yay is defined');
107  isnt(*{'::foo'}{CODE}, undef, 'foo is defined');
108  is(*{'::flip'}{CODE}, undef, 'flip isn\'t defined');
109  isnt(*{'::flop'}{CODE}, undef, 'flip is defined');
110  is(*{'::qux'}{CODE}, undef, 'qux isn\'t defined');
111  isnt(*{'::blech'}{CODE}, undef, 'blech is defined');
112  isnt(*{'::wut'}{CODE}, undef, 'wut is defined');
113 }
114
115 eval { no warnings; no strict; qux };
116 _got_undefined('qux', __LINE__-1);
117
118 eval { no warnings; no strict; blech };
119 _got_undefined('blech', __LINE__-1);
120
121 sub foo {
122  if ($_[2]) {
123   my %h = %{$_[2]};
124   $foo = $_[0] + $_[1] + (($h{a} || 0 == 5) ? 4 : 0)
125                        + (($h{b} || 0 == 7) ? 8 : 0);
126   undef;
127  } else {
128   $foo = '::foo'; # for symbol table tests later
129  }
130 }
131
132 eval { foo 3, 4, { } };
133 _got_ok('compiling to foo(3,4,{})');
134 is($foo, 7, 'foo really was executed');
135
136 $warn = undef;
137 eval {
138  local $SIG{__WARN__} = sub { $warn = $_[0] =~ /Subroutine\s+\S+redefined/ }; 
139  local *qux = sub { $qux = $_[0] };
140  qux 5;
141 };
142 _got_ok('compiling to qux(5)');
143 is($qux, 5, 'qux really was executed');
144 is($warn, undef, 'no redefine warning');
145
146 $warn = undef;
147 eval {
148  local $SIG{__WARN__} = sub { $warn = $_[0] =~ /Subroutine\s+\S+redefined/ };
149  local *blech = sub { $blech = $_[0] };
150  blech 7;
151 };
152 _got_ok('compiling to blech(7)');
153 is($blech, 7, 'blech really was executed');
154 is($warn, undef, 'no redefine warning');
155
156 eval { qux };
157 _got_undefined('qux', __LINE__-1);
158
159 eval { blech };
160 _got_undefined('blech', __LINE__-1);
161
162 # === Up to there =============================================================
163 no subs::auto;
164
165 my $b;
166 my $cb = eval {
167  sub {
168   $b = do {
169    no strict;
170    no warnings 'reserved';
171    blech;
172   }  
173  }
174 };
175 _got_ok('compiling to bareword');
176 $cb->();
177 is($b, 'blech', 'bareword ok');
178
179 eval { foo 13, 1, { } };
180 _got_ok('compiling to foo(13,1,{})');
181 is($foo, 14, 'foo really was executed');
182
183 $warn = undef;
184 {
185  local $SIG{__WARN__} = sub { $warn = $_[0] =~ /Subroutine\s+\S+redefined/; diag $_[0] };
186  local *qux = sub { $qux = 2 * $_[0] };
187  qux(3);
188 }
189 _got_ok('compiling to qux(3)');
190 is($qux, 6, 'new qux really was executed');
191 is($warn, undef, 'no redefine warning');
192
193 $warn = undef;
194 {
195  local $SIG{__WARN__} = sub { $warn = $_[0] =~ /Subroutine\s+\S+redefined/ };
196  local *blech = sub { $blech = 2 * $_[0] };
197  blech(9);
198 }
199 _got_ok('compiling to blech(9)');
200 is($blech, 18, 'new blech really was executed');
201 is($warn, undef, 'no redefine warning');
202
203 eval "qux";
204 _got_bareword('qux', 1, eval => 1);
205
206 eval "blech";
207 _got_undefined('blech', 1, eval => 1);
208
209 {
210  no strict qw/refs subs/;
211  is(*{::feh}{CODE}, undef, 'feh isn\'t defined');
212  is(*{::feh}{CODE}, undef, 'feh isn\'t defined, really');
213  isnt(*{::yay}{CODE}, undef, 'yay is defined');
214  isnt(*{::foo}{CODE}, undef, 'foo is defined'); # calls foo
215  is($foo, '::foo', 'foo was called');
216  is(*{::flip}{CODE}, undef, 'flip isn\'t defined');
217  isnt(*{::flop}{CODE}, undef, 'flip is defined');
218  is(*{::qux}{CODE}, undef, 'qux isn\'t defined');
219  isnt(*{::blech}{CODE}, undef, 'blech is defined');
220  isnt(*{::wut}{CODE}, undef, 'wut is defined');
221 }
222
223 sub blech;
224 eval { blech };
225 _got_undefined('blech', __LINE__-1);
226
227 sub flop;
228
229 bar();
230 is($bar, 1, 'bar ok');
231
232 sub wut { $wut = ($_[0] || 0) + length($_[1] || ''); '::wut' }
233
234 sub yay { @yay = @_; '::yay' }
235
236 # === Restarting from there ===================================================
237 use subs::auto;
238
239 eval "no subs::auto; meh";
240 _got_bareword("meh", 1, eval => 1);
241 # eval "use subs::auto; meh";
242 # _got_undefined('meh', 1, eval => 1, todo => 'Fails because of some bug in perl or Variable::Magic');
243 # eval "meh";
244 # _got_undefined('meh', 1, eval => 1, todo => 'Fails because of some bug in perl or Variable::Magic');
245
246 my $buf = '';
247 {
248  no subs::auto;
249  open DONGS, '>', \$buf or die "open-in-memory: $!";
250 }
251 print DONGS "hlagh\n";
252 is($buf, "hlagh\n", 'filehandles should\'t be touched');
253 close DONGS;
254
255 seek DATA, 0, 1;
256 my @fruits = <DATA>;
257 chomp @fruits;
258 is_deeply(\@fruits, [ qw/apple pear banana/ ], 'DATA filehandle ok');
259
260 eval { foo 7, 9, { } };
261 _got_ok('compiling to foo(7,9,{})');
262 is($foo, 16, 'foo really was executed');
263
264 eval { blech };
265 _got_undefined('blech', __LINE__-1);
266
267 ok(-f $0 && -r _, '-X _');
268
269 __DATA__
270 apple
271 pear
272 banana