Allow several arguments to display().
[p5sagit/p5-mst-13.2.git] / lib / DB.t
1 #!./perl -w
2
3 BEGIN {
4         chdir 't' if -d 't';
5         @INC = '../lib';
6 }
7
8 # symbolic references used later
9 use strict qw( vars subs );
10
11 # @DB::dbline values have both integer and string components (Benjamin Goldberg)
12 use Scalar::Util qw( dualvar );
13 my $dualfalse = dualvar(0, 'false');
14 my $dualtrue = dualvar(1, 'true');
15
16 use Test::More tests => 106;
17
18 # must happen at compile time for DB:: package variable localizations to work
19 BEGIN {
20         use_ok( 'DB' );
21 }
22
23 # test DB::sub()
24 {
25         my $callflag = 0;
26         local $DB::sub = sub {
27                 $callflag += shift || 1;
28                 my @vals = (1, 4, 9);
29                 return @vals;
30         };
31         my $ret = DB::sub;
32         is( $ret, 3, 'DB::sub() should handle scalar context' );
33         is( $callflag, 1, '... should call $DB::sub contents' );
34         $ret = join(' ', DB::sub(2));
35         is( $ret, '1 4 9', '... should handle scalar context' );
36         is( $callflag, 3, '... should pass along arguments to the sub' );
37         ok( defined($DB::ret),'$DB::ret should be defined after successful return');
38         DB::sub;
39         ok( !defined($DB::ret), '... should respect void context' );
40         $DB::sub = '::DESTROY';
41         ok( !defined($DB::ret), '... should return undef for DESTROY()' );
42 }
43
44 # test DB::DB()
45
46         ok( ! defined DB::DB(), 
47                 'DB::DB() should return undef if $DB::ready is false');
48         is( DB::catch(), 1, 'DB::catch() should work' );
49         is( DB->skippkg('foo'), 1, 'DB->skippkg() should push args' );
50
51         # change packages to mess with caller()
52         package foo;
53         ::ok( ! defined DB::DB(), 'DB::DB() should skip skippable packages' );
54
55         package main;
56         is( $DB::filename, $0, '... should set $DB::filename' );
57         is( $DB::lineno, __LINE__ - 4, '... should set $DB::lineno' );
58
59         DB::DB();
60         # stops at line 94
61 }
62
63 # test DB::save()
64 {
65        no warnings 'uninitialized';
66
67         # assigning a number to $! seems to produce an error message, when read
68         local ($@, $,, $/, $\, $^W, $!) = (1 .. 5);
69         DB::save();
70         is( "$@$!$,$/$\$^W", "1\n0", 'DB::save() should reset punctuation vars' );
71 }
72
73 # test DB::catch()
74 {
75         local $DB::signal;
76         DB::catch();
77         ok( $DB::signal, 'DB::catch() should set $DB::signal' );
78         # add clients and test to see if they are awakened
79 }
80
81 # test DB::_clientname()
82 is( DB::_clientname('foo=A(1)'), 'foo','DB::_clientname should return refname');
83 is( DB::_clientname('bar'), '','DB::_clientname should not return non refname');
84
85 # test DB::next() and DB::step()
86 {
87         local $DB::single;
88         DB->next();
89         is( $DB::single, 2, 'DB->next() should set $DB::single to 2' );
90         DB->step();
91         is( $DB::single, 1, 'DB->step() should set $DB::single to 1' );
92 }
93
94 # test DB::cont()
95 {
96         # cannot test @stack
97
98         local $DB::single = 1;
99         my $fdb = FakeDB->new();
100         DB::cont($fdb, 2);
101         is( $fdb->{tbreak}, 2, 'DB::cont() should set tbreak in object' );
102         is( $DB::single, 0, '... should set $DB::single to 0' );
103 }
104
105 # test DB::ret()
106 {
107         # cannot test @stack
108
109         local $DB::single = 1;
110         DB::ret();
111         is( $DB::single, 0, 'DB::ret() should set $DB::single to 0' );
112 }
113
114 # test DB::backtrace()
115 {
116         local (@DB::args, $DB::signal);
117
118         my $line = __LINE__ + 1;
119         my @ret = eval { DB->backtrace() };
120         like( $ret[0], qr/file.+\Q$0\E/, 'DB::backtrace() should report current file');
121         like( $ret[0], qr/line $line/, '... should report calling line number' );
122         like( $ret[0], qr/eval {...}/, '... should catch eval BLOCK' );
123
124         @ret = eval "one(2)";
125         is( scalar @ret, 1, '... should report from provided stack frame number' );
126         like( $ret[0], qr/\@ = &eval \'one.+?2\)\'/, #'
127                 '... should find eval STRING construct');
128         $ret[0] = check_context(1);
129         like( $ret[0], qr/\$ = &main::check_context/, 
130                 '... should respect context of calling construct');
131         
132         $DB::signal = 1;
133         @DB::args = (1, 7);
134         @ret = three(1);
135         is( scalar @ret, 1, '... should end loop if $DB::signal is true' );
136
137         # does not check 'require' or @DB::args mangling
138 }
139
140 sub check_context {
141         return (eval "one($_[0])")[-1];
142 }
143 sub one { DB->backtrace(@_) }
144 sub two { one(@_) }
145 sub three { two(@_) }
146
147 # test DB::trace_toggle
148 {
149         local $DB::trace = 0;
150         DB->trace_toggle;
151         ok( $DB::trace, 'DB::trace_toggle() should toggle $DB::trace' );
152         DB->trace_toggle;
153         ok( !$DB::trace, '... should toggle $DB::trace (back)' );
154 }
155
156 # test DB::subs()
157 {
158         local %DB::sub;
159         my $subs = DB->subs;
160         is( $subs, 0, 'DB::subs() should return keys of %DB::subs' );
161         %DB::sub = ( foo => 'foo:23-45' , bar => 'ba:r:7-890' );
162         $subs = DB->subs;
163         is( $subs, 2, '... same song, different key' );
164         my @subs = DB->subs( 'foo', 'boo', 'bar' );
165         is( scalar @subs, 2, '... should report only for requested subs' );
166         my @expected = ( [ 'foo', 23, 45 ], [ 'ba:r', 7, 890 ] );
167         ok( eq_array( \@subs, \@expected ), '... find file, start, end for subs' );
168 }
169
170 # test DB::filesubs()
171 {
172         local ($DB::filename, %DB::sub);
173         $DB::filename = 'baz';
174         %DB::sub = map { $_ => $_ } qw( bazbar bazboo boobar booboo boobaz );
175         my @ret = DB->filesubs();
176         is( scalar @ret, 2, 'DB::filesubs() should use $DB::filename with no args');
177         @ret = grep { /^baz/ } @ret;    
178         is( scalar @ret, 2, '... should pick up subs in proper file' );
179         @ret = DB->filesubs('boo');
180         is( scalar @ret, 3, '... should use argument to find subs' );
181         @ret = grep { /^boo/ } @ret;    
182         is( scalar @ret, 3, '... should pick up subs in proper file with argument');
183 }
184
185 # test DB::files()
186 {
187         my $dbf = () = DB::files();
188         my $main = () = grep ( m!^_<!, keys %main:: );
189         is( $dbf, $main, 'DB::files() should pick up filenames from %main::' );
190 }
191
192 # test DB::lines()
193 {
194         local @DB::dbline = ( 'foo' );
195         is( DB->lines->[0], 'foo', 'DB::lines() should return ref to @DB::dbline' );
196 }
197
198 # test DB::loadfile()
199 SKIP: {
200         local (*DB::dbline, $DB::filename);
201         ok( ! defined DB->loadfile('notafile'),
202                 'DB::loadfile() should not find unloaded file' );
203         my $file = (grep { m|^_<.+\.pm| } keys %main:: )[0];
204         skip('cannot find loaded file', 3) unless $file;
205         $file =~ s/^_<..//;
206
207         my $db = DB->loadfile($file);
208         like( $db, qr!$file\z!, '... should find loaded file from partial name');
209
210         is( *DB::dbline, *{ "_<$db" } , 
211                 '... should set *DB::dbline to associated glob');
212         is( $DB::filename, $db, '... should set $DB::filename to file name' );
213
214         # test clients
215 }
216
217 # test DB::lineevents()
218 {
219         use vars qw( *baz );
220
221         local $DB::filename = 'baz';
222         local *baz = *{ "main::_<baz" };
223         
224         @baz = map { dualvar(1, $_) } qw( one two three four five );
225         %baz = (
226                 1 => "foo\0bar",
227                 3 => "boo\0far",
228                 4 => "fazbaz",
229         );
230         my %ret = DB->lineevents();
231         is( scalar keys %ret, 3, 'DB::lineevents() should pick up defined lines' );
232
233         # array access in DB::lineevents() starts at element 1, not 0
234         is( join(' ', @{ $ret{1} }), 'two foo bar', '... should stash data in hash');
235 }
236
237 # test DB::set_break()
238 {
239         local ($DB::lineno, *DB::dbline, $DB::package);
240
241         %DB::dbline = (
242                 1 => "\0",
243                 2 => undef,
244                 3 => "123\0\0\0abc",
245                 4 => "\0abc",
246         );
247
248         *DB::dbline = [ $dualfalse, $dualtrue, $dualfalse, $dualfalse, $dualtrue ];
249
250         local %DB::sub = (
251                 'main::foo'     => 'foo:1-4',
252         );
253          
254         DB->set_break(1, 'foo');
255         is( $DB::dbline{1}, "foo\0", 'DB::set_break() should set break condition' );
256
257         $DB::lineno = 1;
258         DB->set_break(undef, 'bar');
259         is( $DB::dbline{1}, "bar\0", 
260                 '... should use $DB::lineno without specified line' );
261
262         DB->set_break(4);
263         is( $DB::dbline{4}, "1\0abc", '... should use default condition if needed');
264
265         local %DB::sub = (
266                 'main::foo'     => 'foo:1-4',
267         );
268         DB->set_break('foo', 'baz');
269         is( $DB::dbline{4}, "baz\0abc", 
270                 '... should use _find_subline() to resolve subname' );
271
272         my $db = FakeDB->new();
273         DB::set_break($db, 2);
274         like( $db->{output}, qr/2 not break/, '... should respect @DB::dbline' );
275
276         DB::set_break($db, 'nonfoo');
277         like( $db->{output}, qr/not found/, '... should warn on unfound sub' );
278 }
279
280 # test DB::set_tbreak()
281 {
282         local ($DB::lineno, *DB::dbline, $DB::package);
283         *DB::dbline = [ $dualfalse, $dualtrue, $dualfalse, $dualfalse, $dualtrue ];
284
285         DB->set_tbreak(1);
286         is( $DB::dbline{1}, ';9', 'DB::set_tbreak() should set tbreak condition' );
287
288         local %DB::sub = (
289                 'main::foo'     => 'foo:1-4',
290         );
291         DB->set_tbreak('foo', 'baz');
292         is( $DB::dbline{4}, ';9', 
293                 '... should use _find_subline() to resolve subname' );
294
295         my $db = FakeDB->new();
296         DB::set_tbreak($db, 2);
297         like( $db->{output}, qr/2 not break/, '... should respect @DB::dbline' );
298
299         DB::set_break($db, 'nonfoo');
300         like( $db->{output}, qr/not found/, '... should warn on unfound sub' );
301 }
302
303 # test DB::_find_subline()
304 {
305         my @foo;
306         local *{ "::_<foo" } = \@foo;
307
308         local $DB::package;
309         local %DB::sub = (
310                 'TEST::foo'     => 'foo:10-15',
311                 'main::foo'     => 'foo:11-12',
312                 'bar::bar'      => 'foo:10-16',
313         );
314
315         $foo[11] = $dualtrue;
316
317         is( DB::_find_subline('TEST::foo'), 11, 
318                 'DB::_find_subline() should find fully qualified sub' );
319         is( DB::_find_subline("TEST'foo"), 11, '... should handle old package sep');
320         is( DB::_find_subline('foo'), 11, 
321                 '... should resolve unqualified package name to main::' );
322
323         $DB::package = 'bar';
324         is( DB::_find_subline('bar'), 11, 
325                 '... should resolve unqualified name with $DB::package, if defined' );
326         
327         $foo[11] = $dualfalse;
328
329         is( DB::_find_subline('TEST::foo'), 15, 
330                 '... should increment past lines with no events' );
331                 
332         ok( ! defined DB::_find_subline('sirnotappearinginthisfilm'),
333                 '... should not find nonexistant sub' );
334 }
335
336 # test DB::clr_breaks()
337 {
338         local *DB::dbline;
339         my %lines = (
340                 1 => "\0",
341                 2 => undef,
342                 3 => "123\0\0\0abc",
343                 4 => "\0\0\0abc",
344         );
345
346         %DB::dbline = %lines;
347         DB->clr_breaks(1 .. 4);
348         is( scalar keys %DB::dbline, 3, 'DB::clr_breaks() should clear breaks' );
349         ok( ! exists($DB::dbline{1}), '... should delete empty actions' );
350         is( $DB::dbline{3}, "\0\0\0abc", '... should remove break, leaving action');
351         is( $DB::dbline{4}, "\0\0\0abc", '... should not remove set actions' );
352
353         local *{ "::_<foo" } = [ 0, 0, 0, 1 ];
354
355         local $DB::package;
356         local %DB::sub = (
357                 'main::foo'     => 'foo:1-3',
358         );
359
360         %DB::dbline = %lines;
361         DB->clr_breaks('foo');
362
363         is( $DB::dbline{3}, "\0\0\0abc", 
364                 '... should find lines via _find_subline()' );
365         
366         my $db = FakeDB->new();
367         DB::clr_breaks($db, 'abadsubname');
368         is( $db->{output}, "Subroutine not found.\n", 
369                 '... should output warning if sub cannot be found');
370
371         @DB::dbline = (1 .. 4);
372         %DB::dbline = (%lines, 5 => "\0" );
373
374         DB::clr_breaks();
375
376         is( scalar keys %DB::dbline, 4, 
377                 'Relying on @DB::dbline in DB::clr_breaks() should clear breaks' );
378         ok( ! exists($DB::dbline{1}), '... should delete empty actions' );
379         is( $DB::dbline{3}, "\0\0\0abc", '... should remove break, leaving action');
380         is( $DB::dbline{4}, "\0\0\0abc", '... should not remove set actions' );
381         ok( exists($DB::dbline{5}), 
382                 '... should only go to last index of @DB::dbline' );
383 }
384
385 # test DB::set_action()
386 {
387         local *DB::dbline;
388
389         %DB::dbline = (
390                 2 => "\0abc",
391         );
392
393         *DB::dbline = [ $dualfalse, $dualfalse, $dualtrue, $dualtrue ];
394
395         DB->set_action(2, 'def');
396         is( $DB::dbline{2}, "\0def", 
397                 'DB::set_action() should replace existing action' );
398         DB->set_action(3, '');
399         is( $DB::dbline{3}, "\0", '... should set new action' );
400
401         my $db = FakeDB->new();
402         DB::set_action($db, 'abadsubname');
403         is( $db->{output}, "Subroutine not found.\n", 
404                 '... should output warning if sub cannot be found');
405
406         DB::set_action($db, 1);
407         like( $db->{output}, qr/1 not action/, 
408                 '... should warn if line cannot be actionivated' );
409 }
410
411 # test DB::clr_actions()
412 {
413         local *DB::dbline;
414         my %lines = (
415                 1 => "\0",
416                 2 => undef,
417                 3 => "123\0abc",
418                 4 => "abc\0",
419         );
420
421         %DB::dbline = %lines;
422         *DB::dbline = [ ($dualtrue) x 4 ];
423
424         DB->clr_actions(1 .. 4);
425
426         is( scalar keys %DB::dbline, 2, 'DB::clr_actions() should clear actions' );
427         ok( ! exists($DB::dbline{1}), '... should delete empty actions' );
428         is( $DB::dbline{3}, "123", '... should remove action, leaving break');
429         is( $DB::dbline{4}, "abc\0", '... should not remove set breaks' );
430
431         local *{ "::_<foo" } = [ 0, 0, 0, 1 ];
432
433         local $DB::package;
434         local %DB::sub = (
435                 'main::foo'     => 'foo:1-3',
436         );
437
438         %DB::dbline = %lines;
439         DB->clr_actions('foo');
440
441         is( $DB::dbline{3}, "123", '... should find lines via _find_subline()' );
442         
443         my $db = FakeDB->new();
444         DB::clr_actions($db, 'abadsubname');
445         is( $db->{output}, "Subroutine not found.\n", 
446                 '... should output warning if sub cannot be found');
447
448         @DB::dbline = (1 .. 4);
449         %DB::dbline = (%lines, 5 => "\0" );
450
451         DB::clr_actions();
452
453         is( scalar keys %DB::dbline, 4, 
454                 'Relying on @DB::dbline in DB::clr_actions() should clear actions' );
455         ok( ! exists($DB::dbline{1}), '... should delete empty actions' );
456         is( $DB::dbline{3}, "123", '... should remove action, leaving break');
457         is( $DB::dbline{4}, "abc\0", '... should not remove set breaks' );
458         ok( exists($DB::dbline{5}), 
459                 '... should only go to last index of @DB::dbline' );
460 }
461
462 # test DB::prestop()
463 ok( ! defined DB::prestop('test'),
464         'DB::prestop() should return undef for undef value' );
465 DB::prestop('test', 897);
466 is( DB::prestop('test'), 897, '... should return value when set' );
467
468 # test DB::poststop(), not exactly parallel
469 ok( ! defined DB::poststop('tset'), 
470         'DB::prestop() should return undef for undef value' );
471 DB::poststop('tset', 987);
472 is( DB::poststop('tset'), 987, '... should return value when set' );
473
474 # test DB::evalcode()
475 ok( ! defined DB::evalcode('foo'),
476         'DB::evalcode() should return undef for undef value' );
477
478 DB::evalcode('foo', 'bar');
479 is( DB::evalcode('foo'), 'bar', '... should return value when set' );
480
481 # test DB::_outputall(), must create fake clients first
482 ok( DB::register( FakeDB->new() ), 'DB::register() should work' );
483 DB::register( FakeDB->new() ) for ( 1 .. 2);
484
485 DB::_outputall(1, 2, 3);
486 is( $FakeDB::output, '123123123', 
487         'DB::_outputall() should call output(@_) on all clients' );
488
489 # test virtual methods
490 for my $method (qw( cprestop cpoststop awaken init stop idle cleanup output )) {
491         ok( defined &{ "DB::$method" }, "DB::$method() should be defined" );
492 }
493
494 # DB::skippkg() uses lexical
495 # DB::ready() uses lexical
496
497 package FakeDB;
498
499 use vars qw( $output );
500
501 sub new {
502         bless({}, $_[0]);
503 }
504
505 sub set_tbreak {
506         my ($self, $val) = @_;
507         $self->{tbreak} = $val;
508 }
509
510 sub output {
511         my $self = shift;
512         if (ref $self) {
513                 $self->{output} = join('', @_);
514         } else {
515                 $output .= join('', @_);
516         }
517 }