8 # symbolic references used later
9 use strict qw( vars subs );
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');
16 use Test::More tests => 106;
18 # must happen at compile time for DB:: package variable localizations to work
26 local $DB::sub = sub {
27 $callflag += shift || 1;
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');
39 ok( !defined($DB::ret), '... should respect void context' );
40 $DB::sub = '::DESTROY';
41 ok( !defined($DB::ret), '... should return undef for DESTROY()' );
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' );
51 # change packages to mess with caller()
53 ::ok( ! defined DB::DB(), 'DB::DB() should skip skippable packages' );
56 is( $DB::filename, $0, '... should set $DB::filename' );
57 is( $DB::lineno, __LINE__ - 4, '... should set $DB::lineno' );
65 no warnings 'uninitialized';
67 # assigning a number to $! seems to produce an error message, when read
68 local ($@, $,, $/, $\, $^W, $!) = (1 .. 5);
70 is( "$@$!$,$/$\$^W", "1\n0", 'DB::save() should reset punctuation vars' );
77 ok( $DB::signal, 'DB::catch() should set $DB::signal' );
78 # add clients and test to see if they are awakened
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');
85 # test DB::next() and DB::step()
89 is( $DB::single, 2, 'DB->next() should set $DB::single to 2' );
91 is( $DB::single, 1, 'DB->step() should set $DB::single to 1' );
98 local $DB::single = 1;
99 my $fdb = FakeDB->new();
101 is( $fdb->{tbreak}, 2, 'DB::cont() should set tbreak in object' );
102 is( $DB::single, 0, '... should set $DB::single to 0' );
109 local $DB::single = 1;
111 is( $DB::single, 0, 'DB::ret() should set $DB::single to 0' );
114 # test DB::backtrace()
116 local (@DB::args, $DB::signal);
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' );
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');
135 is( scalar @ret, 1, '... should end loop if $DB::signal is true' );
137 # does not check 'require' or @DB::args mangling
141 return (eval "one($_[0])")[-1];
143 sub one { DB->backtrace(@_) }
145 sub three { two(@_) }
147 # test DB::trace_toggle
149 local $DB::trace = 0;
151 ok( $DB::trace, 'DB::trace_toggle() should toggle $DB::trace' );
153 ok( !$DB::trace, '... should toggle $DB::trace (back)' );
160 is( $subs, 0, 'DB::subs() should return keys of %DB::subs' );
161 %DB::sub = ( foo => 'foo:23-45' , bar => 'ba:r:7-890' );
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' );
170 # test DB::filesubs()
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');
187 my $dbf = () = DB::files();
188 my $main = () = grep ( m!^_<!, keys %main:: );
189 is( $dbf, $main, 'DB::files() should pick up filenames from %main::' );
194 local @DB::dbline = ( 'foo' );
195 is( DB->lines->[0], 'foo', 'DB::lines() should return ref to @DB::dbline' );
198 # test DB::loadfile()
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;
207 my $db = DB->loadfile($file);
208 like( $db, qr!$file\z!, '... should find loaded file from partial name');
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' );
217 # test DB::lineevents()
221 local $DB::filename = 'baz';
222 local *baz = *{ "main::_<baz" };
224 @baz = map { dualvar(1, $_) } qw( one two three four five );
230 my %ret = DB->lineevents();
231 is( scalar keys %ret, 3, 'DB::lineevents() should pick up defined lines' );
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');
237 # test DB::set_break()
239 local ($DB::lineno, *DB::dbline, $DB::package);
248 *DB::dbline = [ $dualfalse, $dualtrue, $dualfalse, $dualfalse, $dualtrue ];
251 'main::foo' => 'foo:1-4',
254 DB->set_break(1, 'foo');
255 is( $DB::dbline{1}, "foo\0", 'DB::set_break() should set break condition' );
258 DB->set_break(undef, 'bar');
259 is( $DB::dbline{1}, "bar\0",
260 '... should use $DB::lineno without specified line' );
263 is( $DB::dbline{4}, "1\0abc", '... should use default condition if needed');
266 'main::foo' => 'foo:1-4',
268 DB->set_break('foo', 'baz');
269 is( $DB::dbline{4}, "baz\0abc",
270 '... should use _find_subline() to resolve subname' );
272 my $db = FakeDB->new();
273 DB::set_break($db, 2);
274 like( $db->{output}, qr/2 not break/, '... should respect @DB::dbline' );
276 DB::set_break($db, 'nonfoo');
277 like( $db->{output}, qr/not found/, '... should warn on unfound sub' );
280 # test DB::set_tbreak()
282 local ($DB::lineno, *DB::dbline, $DB::package);
283 *DB::dbline = [ $dualfalse, $dualtrue, $dualfalse, $dualfalse, $dualtrue ];
286 is( $DB::dbline{1}, ';9', 'DB::set_tbreak() should set tbreak condition' );
289 'main::foo' => 'foo:1-4',
291 DB->set_tbreak('foo', 'baz');
292 is( $DB::dbline{4}, ';9',
293 '... should use _find_subline() to resolve subname' );
295 my $db = FakeDB->new();
296 DB::set_tbreak($db, 2);
297 like( $db->{output}, qr/2 not break/, '... should respect @DB::dbline' );
299 DB::set_break($db, 'nonfoo');
300 like( $db->{output}, qr/not found/, '... should warn on unfound sub' );
303 # test DB::_find_subline()
306 local *{ "::_<foo" } = \@foo;
310 'TEST::foo' => 'foo:10-15',
311 'main::foo' => 'foo:11-12',
312 'bar::bar' => 'foo:10-16',
315 $foo[11] = $dualtrue;
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::' );
323 $DB::package = 'bar';
324 is( DB::_find_subline('bar'), 11,
325 '... should resolve unqualified name with $DB::package, if defined' );
327 $foo[11] = $dualfalse;
329 is( DB::_find_subline('TEST::foo'), 15,
330 '... should increment past lines with no events' );
332 ok( ! defined DB::_find_subline('sirnotappearinginthisfilm'),
333 '... should not find nonexistant sub' );
336 # test DB::clr_breaks()
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' );
353 local *{ "::_<foo" } = [ 0, 0, 0, 1 ];
357 'main::foo' => 'foo:1-3',
360 %DB::dbline = %lines;
361 DB->clr_breaks('foo');
363 is( $DB::dbline{3}, "\0\0\0abc",
364 '... should find lines via _find_subline()' );
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');
371 @DB::dbline = (1 .. 4);
372 %DB::dbline = (%lines, 5 => "\0" );
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' );
385 # test DB::set_action()
393 *DB::dbline = [ $dualfalse, $dualfalse, $dualtrue, $dualtrue ];
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' );
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');
406 DB::set_action($db, 1);
407 like( $db->{output}, qr/1 not action/,
408 '... should warn if line cannot be actionivated' );
411 # test DB::clr_actions()
421 %DB::dbline = %lines;
422 *DB::dbline = [ ($dualtrue) x 4 ];
424 DB->clr_actions(1 .. 4);
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' );
431 local *{ "::_<foo" } = [ 0, 0, 0, 1 ];
435 'main::foo' => 'foo:1-3',
438 %DB::dbline = %lines;
439 DB->clr_actions('foo');
441 is( $DB::dbline{3}, "123", '... should find lines via _find_subline()' );
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');
448 @DB::dbline = (1 .. 4);
449 %DB::dbline = (%lines, 5 => "\0" );
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' );
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' );
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' );
474 # test DB::evalcode()
475 ok( ! defined DB::evalcode('foo'),
476 'DB::evalcode() should return undef for undef value' );
478 DB::evalcode('foo', 'bar');
479 is( DB::evalcode('foo'), 'bar', '... should return value when set' );
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);
485 DB::_outputall(1, 2, 3);
486 is( $FakeDB::output, '123123123',
487 'DB::_outputall() should call output(@_) on all clients' );
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" );
494 # DB::skippkg() uses lexical
495 # DB::ready() uses lexical
499 use vars qw( $output );
506 my ($self, $val) = @_;
507 $self->{tbreak} = $val;
513 $self->{output} = join('', @_);
515 $output .= join('', @_);