1N/A#!./perl -Tw
1N/A
1N/ABEGIN {
1N/A chdir 't' if -d 't';
1N/A @INC = '../lib';
1N/A}
1N/A
1N/A# symbolic references used later
1N/Ause strict qw( vars subs );
1N/A
1N/A# @DB::dbline values have both integer and string components (Benjamin Goldberg)
1N/Ause Scalar::Util qw( dualvar );
1N/Amy $dualfalse = dualvar(0, 'false');
1N/Amy $dualtrue = dualvar(1, 'true');
1N/A
1N/Ause Test::More tests => 106;
1N/A
1N/A# must happen at compile time for DB:: package variable localizations to work
1N/ABEGIN {
1N/A use_ok( 'DB' );
1N/A}
1N/A
1N/A# test DB::sub()
1N/A{
1N/A my $callflag = 0;
1N/A local $DB::sub = sub {
1N/A $callflag += shift || 1;
1N/A my @vals = (1, 4, 9);
1N/A return @vals;
1N/A };
1N/A my $ret = DB::sub;
1N/A is( $ret, 3, 'DB::sub() should handle scalar context' );
1N/A is( $callflag, 1, '... should call $DB::sub contents' );
1N/A $ret = join(' ', DB::sub(2));
1N/A is( $ret, '1 4 9', '... should handle scalar context' );
1N/A is( $callflag, 3, '... should pass along arguments to the sub' );
1N/A ok( defined($DB::ret),'$DB::ret should be defined after successful return');
1N/A DB::sub;
1N/A ok( !defined($DB::ret), '... should respect void context' );
1N/A $DB::sub = '::DESTROY';
1N/A ok( !defined($DB::ret), '... should return undef for DESTROY()' );
1N/A}
1N/A
1N/A# test DB::DB()
1N/A{
1N/A ok( ! defined DB::DB(),
1N/A 'DB::DB() should return undef if $DB::ready is false');
1N/A is( DB::catch(), 1, 'DB::catch() should work' );
1N/A is( DB->skippkg('foo'), 1, 'DB->skippkg() should push args' );
1N/A
1N/A # change packages to mess with caller()
1N/A package foo;
1N/A ::ok( ! defined DB::DB(), 'DB::DB() should skip skippable packages' );
1N/A
1N/A package main;
1N/A is( $DB::filename, $0, '... should set $DB::filename' );
1N/A is( $DB::lineno, __LINE__ - 4, '... should set $DB::lineno' );
1N/A
1N/A DB::DB();
1N/A # stops at line 94
1N/A}
1N/A
1N/A# test DB::save()
1N/A{
1N/A no warnings 'uninitialized';
1N/A
1N/A # assigning a number to $! seems to produce an error message, when read
1N/A local ($@, $,, $/, $\, $^W, $!) = (1 .. 5);
1N/A DB::save();
1N/A is( "$@$!$,$/$\$^W", "1\n0", 'DB::save() should reset punctuation vars' );
1N/A}
1N/A
1N/A# test DB::catch()
1N/A{
1N/A local $DB::signal;
1N/A DB::catch();
1N/A ok( $DB::signal, 'DB::catch() should set $DB::signal' );
1N/A # add clients and test to see if they are awakened
1N/A}
1N/A
1N/A# test DB::_clientname()
1N/Ais( DB::_clientname('foo=A(1)'), 'foo',
1N/A 'DB::_clientname should return refname');
1N/Acmp_ok( DB::_clientname('bar'), 'eq', '',
1N/A 'DB::_clientname should not return non refname');
1N/A
1N/A# test DB::next() and DB::step()
1N/A{
1N/A local $DB::single;
1N/A DB->next();
1N/A is( $DB::single, 2, 'DB->next() should set $DB::single to 2' );
1N/A DB->step();
1N/A is( $DB::single, 1, 'DB->step() should set $DB::single to 1' );
1N/A}
1N/A
1N/A# test DB::cont()
1N/A{
1N/A # cannot test @stack
1N/A
1N/A local $DB::single = 1;
1N/A my $fdb = FakeDB->new();
1N/A DB::cont($fdb, 2);
1N/A is( $fdb->{tbreak}, 2, 'DB::cont() should set tbreak in object' );
1N/A is( $DB::single, 0, '... should set $DB::single to 0' );
1N/A}
1N/A
1N/A# test DB::ret()
1N/A{
1N/A # cannot test @stack
1N/A
1N/A local $DB::single = 1;
1N/A DB::ret();
1N/A is( $DB::single, 0, 'DB::ret() should set $DB::single to 0' );
1N/A}
1N/A
1N/A# test DB::backtrace()
1N/A{
1N/A local (@DB::args, $DB::signal);
1N/A
1N/A my $line = __LINE__ + 1;
1N/A my @ret = eval { DB->backtrace() };
1N/A like( $ret[0], qr/file.+\Q$0\E/, 'DB::backtrace() should report current file');
1N/A like( $ret[0], qr/line $line/, '... should report calling line number' );
1N/A like( $ret[0], qr/eval {...}/, '... should catch eval BLOCK' );
1N/A
1N/A @ret = eval "one(2)";
1N/A is( scalar @ret, 1, '... should report from provided stack frame number' );
1N/A like( $ret[0], qr/\@ = &eval \'one.+?2\)\'/, #'
1N/A '... should find eval STRING construct');
1N/A $ret[0] = check_context(1);
1N/A like( $ret[0], qr/\$ = &main::check_context/,
1N/A '... should respect context of calling construct');
1N/A
1N/A $DB::signal = 1;
1N/A @DB::args = (1, 7);
1N/A @ret = three(1);
1N/A is( scalar @ret, 1, '... should end loop if $DB::signal is true' );
1N/A
1N/A # does not check 'require' or @DB::args mangling
1N/A}
1N/A
1N/Asub check_context {
1N/A return (eval "one($_[0])")[-1];
1N/A}
1N/Asub one { DB->backtrace(@_) }
1N/Asub two { one(@_) }
1N/Asub three { two(@_) }
1N/A
1N/A# test DB::trace_toggle
1N/A{
1N/A local $DB::trace = 0;
1N/A DB->trace_toggle;
1N/A ok( $DB::trace, 'DB::trace_toggle() should toggle $DB::trace' );
1N/A DB->trace_toggle;
1N/A ok( !$DB::trace, '... should toggle $DB::trace (back)' );
1N/A}
1N/A
1N/A# test DB::subs()
1N/A{
1N/A local %DB::sub;
1N/A my $subs = DB->subs;
1N/A is( $subs, 0, 'DB::subs() should return keys of %DB::subs' );
1N/A %DB::sub = ( foo => 'foo:23-45' , bar => 'ba:r:7-890' );
1N/A $subs = DB->subs;
1N/A is( $subs, 2, '... same song, different key' );
1N/A my @subs = DB->subs( 'foo', 'boo', 'bar' );
1N/A is( scalar @subs, 2, '... should report only for requested subs' );
1N/A my @expected = ( [ 'foo', 23, 45 ], [ 'ba:r', 7, 890 ] );
1N/A ok( eq_array( \@subs, \@expected ), '... find file, start, end for subs' );
1N/A}
1N/A
1N/A# test DB::filesubs()
1N/A{
1N/A local ($DB::filename, %DB::sub);
1N/A $DB::filename = 'baz';
1N/A %DB::sub = map { $_ => $_ } qw( bazbar bazboo boobar booboo boobaz );
1N/A my @ret = DB->filesubs();
1N/A is( scalar @ret, 2, 'DB::filesubs() should use $DB::filename with no args');
1N/A @ret = grep { /^baz/ } @ret;
1N/A is( scalar @ret, 2, '... should pick up subs in proper file' );
1N/A @ret = DB->filesubs('boo');
1N/A is( scalar @ret, 3, '... should use argument to find subs' );
1N/A @ret = grep { /^boo/ } @ret;
1N/A is( scalar @ret, 3, '... should pick up subs in proper file with argument');
1N/A}
1N/A
1N/A# test DB::files()
1N/A{
1N/A my $dbf = () = DB::files();
1N/A my $main = () = grep ( m!^_<!, keys %main:: );
1N/A is( $dbf, $main, 'DB::files() should pick up filenames from %main::' );
1N/A}
1N/A
1N/A# test DB::lines()
1N/A{
1N/A local @DB::dbline = ( 'foo' );
1N/A is( DB->lines->[0], 'foo', 'DB::lines() should return ref to @DB::dbline' );
1N/A}
1N/A
1N/A# test DB::loadfile()
1N/ASKIP: {
1N/A local (*DB::dbline, $DB::filename);
1N/A ok( ! defined DB->loadfile('notafile'),
1N/A 'DB::loadfile() should not find unloaded file' );
1N/A my $file = (grep { m|^_<.+\.pm| } keys %main:: )[0];
1N/A skip('cannot find loaded file', 3) unless $file;
1N/A $file =~ s/^_<..//;
1N/A
1N/A my $db = DB->loadfile($file);
1N/A like( $db, qr!$file\z!, '... should find loaded file from partial name');
1N/A
1N/A is( *DB::dbline, *{ "_<$db" } ,
1N/A '... should set *DB::dbline to associated glob');
1N/A is( $DB::filename, $db, '... should set $DB::filename to file name' );
1N/A
1N/A # test clients
1N/A}
1N/A
1N/A# test DB::lineevents()
1N/A{
1N/A use vars qw( *baz );
1N/A
1N/A local $DB::filename = 'baz';
1N/A local *baz = *{ "main::_<baz" };
1N/A
1N/A @baz = map { dualvar(1, $_) } qw( one two three four five );
1N/A %baz = (
1N/A 1 => "foo\0bar",
1N/A 3 => "boo\0far",
1N/A 4 => "fazbaz",
1N/A );
1N/A my %ret = DB->lineevents();
1N/A is( scalar keys %ret, 3, 'DB::lineevents() should pick up defined lines' );
1N/A
1N/A # array access in DB::lineevents() starts at element 1, not 0
1N/A is( join(' ', @{ $ret{1} }), 'two foo bar', '... should stash data in hash');
1N/A}
1N/A
1N/A# test DB::set_break()
1N/A{
1N/A local ($DB::lineno, *DB::dbline, $DB::package);
1N/A
1N/A %DB::dbline = (
1N/A 1 => "\0",
1N/A 2 => undef,
1N/A 3 => "123\0\0\0abc",
1N/A 4 => "\0abc",
1N/A );
1N/A
1N/A *DB::dbline = [ $dualfalse, $dualtrue, $dualfalse, $dualfalse, $dualtrue ];
1N/A
1N/A local %DB::sub = (
1N/A 'main::foo' => 'foo:1-4',
1N/A );
1N/A
1N/A DB->set_break(1, 'foo');
1N/A is( $DB::dbline{1}, "foo\0", 'DB::set_break() should set break condition' );
1N/A
1N/A $DB::lineno = 1;
1N/A DB->set_break(undef, 'bar');
1N/A is( $DB::dbline{1}, "bar\0",
1N/A '... should use $DB::lineno without specified line' );
1N/A
1N/A DB->set_break(4);
1N/A is( $DB::dbline{4}, "1\0abc", '... should use default condition if needed');
1N/A
1N/A local %DB::sub = (
1N/A 'main::foo' => 'foo:1-4',
1N/A );
1N/A DB->set_break('foo', 'baz');
1N/A is( $DB::dbline{4}, "baz\0abc",
1N/A '... should use _find_subline() to resolve subname' );
1N/A
1N/A my $db = FakeDB->new();
1N/A DB::set_break($db, 2);
1N/A like( $db->{output}, qr/2 not break/, '... should respect @DB::dbline' );
1N/A
1N/A DB::set_break($db, 'nonfoo');
1N/A like( $db->{output}, qr/not found/, '... should warn on unfound sub' );
1N/A}
1N/A
1N/A# test DB::set_tbreak()
1N/A{
1N/A local ($DB::lineno, *DB::dbline, $DB::package);
1N/A *DB::dbline = [ $dualfalse, $dualtrue, $dualfalse, $dualfalse, $dualtrue ];
1N/A
1N/A DB->set_tbreak(1);
1N/A is( $DB::dbline{1}, ';9', 'DB::set_tbreak() should set tbreak condition' );
1N/A
1N/A local %DB::sub = (
1N/A 'main::foo' => 'foo:1-4',
1N/A );
1N/A DB->set_tbreak('foo', 'baz');
1N/A is( $DB::dbline{4}, ';9',
1N/A '... should use _find_subline() to resolve subname' );
1N/A
1N/A my $db = FakeDB->new();
1N/A DB::set_tbreak($db, 2);
1N/A like( $db->{output}, qr/2 not break/, '... should respect @DB::dbline' );
1N/A
1N/A DB::set_break($db, 'nonfoo');
1N/A like( $db->{output}, qr/not found/, '... should warn on unfound sub' );
1N/A}
1N/A
1N/A# test DB::_find_subline()
1N/A{
1N/A my @foo;
1N/A local *{ "::_<foo" } = \@foo;
1N/A
1N/A local $DB::package;
1N/A local %DB::sub = (
1N/A 'TEST::foo' => 'foo:10-15',
1N/A 'main::foo' => 'foo:11-12',
1N/A 'bar::bar' => 'foo:10-16',
1N/A );
1N/A
1N/A $foo[11] = $dualtrue;
1N/A
1N/A is( DB::_find_subline('TEST::foo'), 11,
1N/A 'DB::_find_subline() should find fully qualified sub' );
1N/A is( DB::_find_subline("TEST'foo"), 11, '... should handle old package sep');
1N/A is( DB::_find_subline('foo'), 11,
1N/A '... should resolve unqualified package name to main::' );
1N/A
1N/A $DB::package = 'bar';
1N/A is( DB::_find_subline('bar'), 11,
1N/A '... should resolve unqualified name with $DB::package, if defined' );
1N/A
1N/A $foo[11] = $dualfalse;
1N/A
1N/A is( DB::_find_subline('TEST::foo'), 15,
1N/A '... should increment past lines with no events' );
1N/A
1N/A ok( ! defined DB::_find_subline('sirnotappearinginthisfilm'),
1N/A '... should not find nonexistant sub' );
1N/A}
1N/A
1N/A# test DB::clr_breaks()
1N/A{
1N/A local *DB::dbline;
1N/A my %lines = (
1N/A 1 => "\0",
1N/A 2 => undef,
1N/A 3 => "123\0\0\0abc",
1N/A 4 => "\0\0\0abc",
1N/A );
1N/A
1N/A %DB::dbline = %lines;
1N/A DB->clr_breaks(1 .. 4);
1N/A is( scalar keys %DB::dbline, 3, 'DB::clr_breaks() should clear breaks' );
1N/A ok( ! exists($DB::dbline{1}), '... should delete empty actions' );
1N/A is( $DB::dbline{3}, "\0\0\0abc", '... should remove break, leaving action');
1N/A is( $DB::dbline{4}, "\0\0\0abc", '... should not remove set actions' );
1N/A
1N/A local *{ "::_<foo" } = [ 0, 0, 0, 1 ];
1N/A
1N/A local $DB::package;
1N/A local %DB::sub = (
1N/A 'main::foo' => 'foo:1-3',
1N/A );
1N/A
1N/A %DB::dbline = %lines;
1N/A DB->clr_breaks('foo');
1N/A
1N/A is( $DB::dbline{3}, "\0\0\0abc",
1N/A '... should find lines via _find_subline()' );
1N/A
1N/A my $db = FakeDB->new();
1N/A DB::clr_breaks($db, 'abadsubname');
1N/A is( $db->{output}, "Subroutine not found.\n",
1N/A '... should output warning if sub cannot be found');
1N/A
1N/A @DB::dbline = (1 .. 4);
1N/A %DB::dbline = (%lines, 5 => "\0" );
1N/A
1N/A DB::clr_breaks();
1N/A
1N/A is( scalar keys %DB::dbline, 4,
1N/A 'Relying on @DB::dbline in DB::clr_breaks() should clear breaks' );
1N/A ok( ! exists($DB::dbline{1}), '... should delete empty actions' );
1N/A is( $DB::dbline{3}, "\0\0\0abc", '... should remove break, leaving action');
1N/A is( $DB::dbline{4}, "\0\0\0abc", '... should not remove set actions' );
1N/A ok( exists($DB::dbline{5}),
1N/A '... should only go to last index of @DB::dbline' );
1N/A}
1N/A
1N/A# test DB::set_action()
1N/A{
1N/A local *DB::dbline;
1N/A
1N/A %DB::dbline = (
1N/A 2 => "\0abc",
1N/A );
1N/A
1N/A *DB::dbline = [ $dualfalse, $dualfalse, $dualtrue, $dualtrue ];
1N/A
1N/A DB->set_action(2, 'def');
1N/A is( $DB::dbline{2}, "\0def",
1N/A 'DB::set_action() should replace existing action' );
1N/A DB->set_action(3, '');
1N/A is( $DB::dbline{3}, "\0", '... should set new action' );
1N/A
1N/A my $db = FakeDB->new();
1N/A DB::set_action($db, 'abadsubname');
1N/A is( $db->{output}, "Subroutine not found.\n",
1N/A '... should output warning if sub cannot be found');
1N/A
1N/A DB::set_action($db, 1);
1N/A like( $db->{output}, qr/1 not action/,
1N/A '... should warn if line cannot be actionivated' );
1N/A}
1N/A
1N/A# test DB::clr_actions()
1N/A{
1N/A local *DB::dbline;
1N/A my %lines = (
1N/A 1 => "\0",
1N/A 2 => undef,
1N/A 3 => "123\0abc",
1N/A 4 => "abc\0",
1N/A );
1N/A
1N/A %DB::dbline = %lines;
1N/A *DB::dbline = [ ($dualtrue) x 4 ];
1N/A
1N/A DB->clr_actions(1 .. 4);
1N/A
1N/A is( scalar keys %DB::dbline, 2, 'DB::clr_actions() should clear actions' );
1N/A ok( ! exists($DB::dbline{1}), '... should delete empty actions' );
1N/A is( $DB::dbline{3}, "123", '... should remove action, leaving break');
1N/A is( $DB::dbline{4}, "abc\0", '... should not remove set breaks' );
1N/A
1N/A local *{ "::_<foo" } = [ 0, 0, 0, 1 ];
1N/A
1N/A local $DB::package;
1N/A local %DB::sub = (
1N/A 'main::foo' => 'foo:1-3',
1N/A );
1N/A
1N/A %DB::dbline = %lines;
1N/A DB->clr_actions('foo');
1N/A
1N/A is( $DB::dbline{3}, "123", '... should find lines via _find_subline()' );
1N/A
1N/A my $db = FakeDB->new();
1N/A DB::clr_actions($db, 'abadsubname');
1N/A is( $db->{output}, "Subroutine not found.\n",
1N/A '... should output warning if sub cannot be found');
1N/A
1N/A @DB::dbline = (1 .. 4);
1N/A %DB::dbline = (%lines, 5 => "\0" );
1N/A
1N/A DB::clr_actions();
1N/A
1N/A is( scalar keys %DB::dbline, 4,
1N/A 'Relying on @DB::dbline in DB::clr_actions() should clear actions' );
1N/A ok( ! exists($DB::dbline{1}), '... should delete empty actions' );
1N/A is( $DB::dbline{3}, "123", '... should remove action, leaving break');
1N/A is( $DB::dbline{4}, "abc\0", '... should not remove set breaks' );
1N/A ok( exists($DB::dbline{5}),
1N/A '... should only go to last index of @DB::dbline' );
1N/A}
1N/A
1N/A# test DB::prestop()
1N/Aok( ! defined DB::prestop('test'),
1N/A 'DB::prestop() should return undef for undef value' );
1N/ADB::prestop('test', 897);
1N/Ais( DB::prestop('test'), 897, '... should return value when set' );
1N/A
1N/A# test DB::poststop(), not exactly parallel
1N/Aok( ! defined DB::poststop('tset'),
1N/A 'DB::prestop() should return undef for undef value' );
1N/ADB::poststop('tset', 987);
1N/Ais( DB::poststop('tset'), 987, '... should return value when set' );
1N/A
1N/A# test DB::evalcode()
1N/Aok( ! defined DB::evalcode('foo'),
1N/A 'DB::evalcode() should return undef for undef value' );
1N/A
1N/ADB::evalcode('foo', 'bar');
1N/Ais( DB::evalcode('foo'), 'bar', '... should return value when set' );
1N/A
1N/A# test DB::_outputall(), must create fake clients first
1N/Aok( DB::register( FakeDB->new() ), 'DB::register() should work' );
1N/ADB::register( FakeDB->new() ) for ( 1 .. 2);
1N/A
1N/ADB::_outputall(1, 2, 3);
1N/Ais( $FakeDB::output, '123123123',
1N/A 'DB::_outputall() should call output(@_) on all clients' );
1N/A
1N/A# test virtual methods
1N/Afor my $method (qw( cprestop cpoststop awaken init stop idle cleanup output )) {
1N/A ok( defined &{ "DB::$method" }, "DB::$method() should be defined" );
1N/A}
1N/A
1N/A# DB::skippkg() uses lexical
1N/A# DB::ready() uses lexical
1N/A
1N/Apackage FakeDB;
1N/A
1N/Ause vars qw( $output );
1N/A
1N/Asub new {
1N/A bless({}, $_[0]);
1N/A}
1N/A
1N/Asub set_tbreak {
1N/A my ($self, $val) = @_;
1N/A $self->{tbreak} = $val;
1N/A}
1N/A
1N/Asub output {
1N/A my $self = shift;
1N/A if (ref $self) {
1N/A $self->{output} = join('', @_);
1N/A } else {
1N/A $output .= join('', @_);
1N/A }
1N/A}