This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
OS/2 File::* modules
[perl5.git] / lib / DB.t
CommitLineData
4eba29c1 1#!./perl -w
c95f170b 2
3BEGIN {
acc79e62
JH
4 chdir 't' if -d 't';
5 @INC = '../lib';
c95f170b 6}
7
4eba29c1 8# symbolic references used later
9use strict qw( vars subs );
10
11# @DB::dbline values have both integer and string components (Benjamin Goldberg)
12use Scalar::Util qw( dualvar );
13my $dualfalse = dualvar(0, 'false');
14my $dualtrue = dualvar(1, 'true');
15
c95f170b 16use Test::More tests => 106;
17
18# must happen at compile time for DB:: package variable localizations to work
19BEGIN {
acc79e62 20 use_ok( 'DB' );
c95f170b 21}
22
23# test DB::sub()
24{
acc79e62
JH
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()' );
c95f170b 42}
43
44# test DB::DB()
45{
acc79e62
JH
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' );
c95f170b 50
acc79e62
JH
51 # change packages to mess with caller()
52 package foo;
53 ::ok( ! defined DB::DB(), 'DB::DB() should skip skippable packages' );
c95f170b 54
acc79e62
JH
55 package main;
56 is( $DB::filename, $0, '... should set $DB::filename' );
57 is( $DB::lineno, __LINE__ - 4, '... should set $DB::lineno' );
c95f170b 58
acc79e62
JH
59 DB::DB();
60 # stops at line 94
c95f170b 61}
62
63# test DB::save()
64{
eca89c5a
CB
65 no warnings 'uninitialized';
66
acc79e62
JH
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' );
c95f170b 71}
72
73# test DB::catch()
74{
acc79e62
JH
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
c95f170b 79}
80
81# test DB::_clientname()
82is( DB::_clientname('foo=A(1)'), 'foo','DB::_clientname should return refname');
83is( DB::_clientname('bar'), '','DB::_clientname should not return non refname');
84
85# test DB::next() and DB::step()
86{
acc79e62
JH
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' );
c95f170b 92}
93
94# test DB::cont()
95{
acc79e62 96 # cannot test @stack
c95f170b 97
acc79e62
JH
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' );
c95f170b 103}
104
105# test DB::ret()
106{
acc79e62 107 # cannot test @stack
c95f170b 108
acc79e62
JH
109 local $DB::single = 1;
110 DB::ret();
111 is( $DB::single, 0, 'DB::ret() should set $DB::single to 0' );
c95f170b 112}
113
114# test DB::backtrace()
115{
acc79e62
JH
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
c95f170b 138}
139
140sub check_context {
acc79e62 141 return (eval "one($_[0])")[-1];
c95f170b 142}
143sub one { DB->backtrace(@_) }
144sub two { one(@_) }
145sub three { two(@_) }
146
147# test DB::trace_toggle
148{
acc79e62
JH
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)' );
c95f170b 154}
155
156# test DB::subs()
157{
acc79e62
JH
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' );
c95f170b 168}
169
170# test DB::filesubs()
171{
acc79e62
JH
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');
c95f170b 183}
184
185# test DB::files()
186{
acc79e62
JH
187 my $dbf = () = DB::files();
188 my $main = () = grep ( m!^_<!, keys %main:: );
189 is( $dbf, $main, 'DB::files() should pick up filenames from %main::' );
c95f170b 190}
191
192# test DB::lines()
193{
acc79e62
JH
194 local @DB::dbline = ( 'foo' );
195 is( DB->lines->[0], 'foo', 'DB::lines() should return ref to @DB::dbline' );
c95f170b 196}
197
198# test DB::loadfile()
199SKIP: {
acc79e62
JH
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/^_<..//;
c95f170b 206
acc79e62
JH
207 my $db = DB->loadfile($file);
208 like( $db, qr!$file\z!, '... should find loaded file from partial name');
4eba29c1 209
acc79e62
JH
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' );
c95f170b 213
acc79e62 214 # test clients
c95f170b 215}
216
217# test DB::lineevents()
218{
acc79e62
JH
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');
c95f170b 235}
236
237# test DB::set_break()
238{
acc79e62
JH
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' );
c95f170b 278}
279
280# test DB::set_tbreak()
281{
acc79e62
JH
282 local ($DB::lineno, *DB::dbline, $DB::package);
283 *DB::dbline = [ $dualfalse, $dualtrue, $dualfalse, $dualfalse, $dualtrue ];
c95f170b 284
acc79e62
JH
285 DB->set_tbreak(1);
286 is( $DB::dbline{1}, ';9', 'DB::set_tbreak() should set tbreak condition' );
c95f170b 287
acc79e62
JH
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' );
c95f170b 294
acc79e62
JH
295 my $db = FakeDB->new();
296 DB::set_tbreak($db, 2);
297 like( $db->{output}, qr/2 not break/, '... should respect @DB::dbline' );
c95f170b 298
acc79e62
JH
299 DB::set_break($db, 'nonfoo');
300 like( $db->{output}, qr/not found/, '... should warn on unfound sub' );
c95f170b 301}
302
303# test DB::_find_subline()
304{
acc79e62
JH
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' );
c95f170b 334}
335
336# test DB::clr_breaks()
337{
acc79e62
JH
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' );
c95f170b 383}
384
385# test DB::set_action()
386{
acc79e62 387 local *DB::dbline;
c95f170b 388
acc79e62
JH
389 %DB::dbline = (
390 2 => "\0abc",
391 );
c95f170b 392
acc79e62 393 *DB::dbline = [ $dualfalse, $dualfalse, $dualtrue, $dualtrue ];
c95f170b 394
acc79e62
JH
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' );
c95f170b 400
acc79e62
JH
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');
c95f170b 405
acc79e62
JH
406 DB::set_action($db, 1);
407 like( $db->{output}, qr/1 not action/,
408 '... should warn if line cannot be actionivated' );
c95f170b 409}
410
411# test DB::clr_actions()
412{
acc79e62
JH
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' );
c95f170b 460}
461
462# test DB::prestop()
463ok( ! defined DB::prestop('test'),
acc79e62 464 'DB::prestop() should return undef for undef value' );
c95f170b 465DB::prestop('test', 897);
466is( DB::prestop('test'), 897, '... should return value when set' );
467
468# test DB::poststop(), not exactly parallel
469ok( ! defined DB::poststop('tset'),
acc79e62 470 'DB::prestop() should return undef for undef value' );
c95f170b 471DB::poststop('tset', 987);
472is( DB::poststop('tset'), 987, '... should return value when set' );
473
474# test DB::evalcode()
475ok( ! defined DB::evalcode('foo'),
acc79e62 476 'DB::evalcode() should return undef for undef value' );
c95f170b 477
478DB::evalcode('foo', 'bar');
479is( DB::evalcode('foo'), 'bar', '... should return value when set' );
480
481# test DB::_outputall(), must create fake clients first
482ok( DB::register( FakeDB->new() ), 'DB::register() should work' );
483DB::register( FakeDB->new() ) for ( 1 .. 2);
484
485DB::_outputall(1, 2, 3);
486is( $FakeDB::output, '123123123',
acc79e62 487 'DB::_outputall() should call output(@_) on all clients' );
c95f170b 488
489# test virtual methods
490for my $method (qw( cprestop cpoststop awaken init stop idle cleanup output )) {
acc79e62 491 ok( defined &{ "DB::$method" }, "DB::$method() should be defined" );
c95f170b 492}
493
494# DB::skippkg() uses lexical
495# DB::ready() uses lexical
496
497package FakeDB;
498
499use vars qw( $output );
500
501sub new {
acc79e62 502 bless({}, $_[0]);
c95f170b 503}
504
505sub set_tbreak {
acc79e62
JH
506 my ($self, $val) = @_;
507 $self->{tbreak} = $val;
c95f170b 508}
509
510sub output {
acc79e62
JH
511 my $self = shift;
512 if (ref $self) {
513 $self->{output} = join('', @_);
514 } else {
515 $output .= join('', @_);
516 }
c95f170b 517}