1N/A#!./perl
1N/A
1N/ABEGIN {
1N/A chdir 't' if -d 't';
1N/A @INC = '../lib';
1N/A if (ord('A') == 193) {
1N/A print "1..0 # skip: EBCDIC\n";
1N/A exit 0;
1N/A }
1N/A}
1N/A
1N/Ause vars qw( $foo @bar %baz );
1N/A
1N/Ause Test::More tests => 88;
1N/A
1N/Ause_ok( 'Dumpvalue' );
1N/A
1N/Amy $d;
1N/Aok( $d = Dumpvalue->new(), 'create a new Dumpvalue object' );
1N/A
1N/A$d->set( globPrint => 1, dumpReused => 1 );
1N/Ais( $d->{globPrint}, 1, 'set an option correctly' );
1N/Ais( $d->get('globPrint'), 1, 'get an option correctly' );
1N/Ais( $d->get('globPrint', 'dumpReused'), qw( 1 1 ), 'get multiple options' );
1N/A
1N/A# check to see if unctrl works
1N/Ais( ref( Dumpvalue::unctrl(*FOO) ), 'GLOB', 'unctrl should not modify GLOB' );
1N/Ais( Dumpvalue::unctrl('donotchange'), 'donotchange', "unctrl shouldn't modify");
1N/Alike( Dumpvalue::unctrl("bo\007nd"), qr/bo\^.nd/, 'unctrl should escape' );
1N/A
1N/A# check to see if stringify works
1N/Ais( $d->stringify(), 'undef', 'stringify handles undef okay' );
1N/A
1N/A# the default is 1, but we want two single quotes
1N/A$d->{printUndef} = 0;
1N/Ais( $d->stringify(), "''", 'stringify skips undef when asked nicely' );
1N/A
1N/Ais( $d->stringify(*FOO), *FOO . "", 'stringify stringifies globs alright' );
1N/A
1N/A# check for double-quotes if there's an unprintable character
1N/A$d->{tick} = 'auto';
1N/Alike( $d->stringify("hi\005"), qr/^"hi/, 'added double-quotes when necessary' );
1N/A
1N/A# if no unprintable character, escape ticks or backslashes
1N/Ais( $d->stringify('hi'), "'hi'", 'used single-quotes when appropriate' );
1N/A
1N/A# if 'unctrl' is set
1N/A$d->{unctrl} = 'unctrl';
1N/Alike( $d->stringify('double and whack:\ "'), qr!\\ \"!, 'escaped with unctrl' );
1N/Alike( $d->stringify("a\005"), qr/^"a\^/, 'escaped ASCII value in unctrl' );
1N/Alike( $d->stringify("b\205"), qr!^'b.'$!, 'no high-bit escape value in unctrl');
1N/A
1N/A$d->{quoteHighBit} = 1;
1N/Alike( $d->stringify("b\205"), qr!^'b\\205!, 'high-bit now escaped in unctrl');
1N/A
1N/A# if 'quote' is set
1N/A$d->{unctrl} = 'quote';
1N/Ais( $d->stringify('5@ $1'), "'5\@ \$1'", 'quoted $ and @ fine' );
1N/Ais( $d->stringify("5@\033\$1"), '"5\@\e\$1"', 'quoted $ and @ and \033 fine' );
1N/Alike( $d->stringify("\037"), qr/^"\\c/, 'escaped ASCII value okay' );
1N/A
1N/A# add ticks, if necessary
1N/Ais( $d->stringify("no ticks", 1), 'no ticks', 'avoid ticks if asked' );
1N/A
1N/Amy $out = tie *OUT, 'TieOut';
1N/Aselect(OUT);
1N/A
1N/A# test DumpElem, it does its magic with veryCompact set
1N/A$d->{veryCompact} = 1;
1N/A$d->DumpElem([1, 2, 3]);
1N/Ais( $out->read, "0..2 1 2 3\n", 'DumpElem worked on array ref');
1N/A$d->DumpElem({ one => 1, two => 2 });
1N/Ais( $out->read, "'one' => 1, 'two' => 2\n", 'DumpElem worked on hash ref' );
1N/A$d->DumpElem('hi');
1N/Ais( $out->read, "'hi'\n", 'DumpElem worked on simple scalar' );
1N/A$d->{veryCompact} = 0;
1N/A$d->DumpElem([]);
1N/Alike( $out->read, qr/ARRAY/, 'DumpElem okay with reference and no veryCompact');
1N/A
1N/A# should compact simple arrays just fine
1N/A$d->{veryCompact} = 1;
1N/A$d->DumpElem([1, 2, 3]);
1N/Ais( $out->read, "0..2 1 2 3\n", 'dumped array fine' );
1N/A$d->{arrayDepth} = 2;
1N/A$d->DumpElem([1, 2, 3]);
1N/Ais( $out->read, "0..2 1 2 ...\n", 'dumped limited array fine' );
1N/A
1N/A# should compact simple hashes just fine
1N/A$d->DumpElem({ a => 1, b => 2, c => 3 });
1N/Ais( $out->read, "'a' => 1, 'b' => 2, 'c' => 3\n", 'dumped hash fine' );
1N/A$d->{hashDepth} = 2;
1N/A$d->DumpElem({ a => 1, b => 2, c => 3 });
1N/Ais( $out->read, "'a' => 1, 'b' => 2 ...\n", 'dumped limited hash fine' );
1N/A
1N/A# should just stringify what it is
1N/A$d->{veryCompact} = 0;
1N/A$d->DumpElem([]);
1N/Alike( $out->read, qr/ARRAY.+empty array/s, 'stringified empty array ref' );
1N/A$d->DumpElem({});
1N/Alike( $out->read, qr/HASH.+empty hash/s, 'stringified empty hash ref' );
1N/A$d->DumpElem(1);
1N/Ais( $out->read, "1\n", 'stringified simple scalar' );
1N/A
1N/A# test unwrap
1N/A$DB::signal = $d->{stopDbSignal} = 1;
1N/Ais( $d->unwrap(), undef, 'unwrap returns if DB signal is set' );
1N/Aundef $DB::signal;
1N/A
1N/Amy $foo = 7;
1N/A$d->{dumpReused} = 0;
1N/A$d->unwrap(\$foo);
1N/Ais( $out->read, "-> 7\n", 'unwrap worked on scalar' );
1N/A$d->unwrap(\$foo);
1N/Ais( $out->read, "-> REUSED_ADDRESS\n", 'unwrap worked on scalar' );
1N/A$d->unwrap({ one => 1 });
1N/A
1N/A# leaving this at zero may cause some subsequent tests to fail
1N/A# if they reuse an address creating an anonymous variable
1N/A$d->{dumpReused} = 1;
1N/Ais( $out->read, "'one' => 1\n", 'unwrap worked on hash' );
1N/A$d->unwrap([ 2, 3 ]);
1N/Ais( $out->read, "0 2\n1 3\n", 'unwrap worked on array' );
1N/A$d->unwrap(*FOO);
1N/Ais( $out->read, '', 'unwrap ignored glob on first try');
1N/A$d->unwrap(*FOO);
1N/Ais( $out->read, "*DUMPED_GLOB*\n", 'unwrap worked on glob');
1N/A$d->unwrap(qr/foo(.+)/);
1N/Ais( $out->read, "-> qr/(?-xism:foo(.+))/\n", 'unwrap worked on Regexp' );
1N/A$d->unwrap( sub {} );
1N/Alike( $out->read, qr/^-> &CODE/, 'unwrap worked on sub ref' );
1N/A
1N/A# test matchvar
1N/A# test to see if first arg 'eq' second
1N/Aok( Dumpvalue::matchvar(1, 1), 'matchvar matched numbers fine' );
1N/Aok( Dumpvalue::matchvar('hi', 'hi'), 'matchvar matched strings fine' );
1N/Aok( !Dumpvalue::matchvar('hello', 1), 'matchvar caught failed match fine' );
1N/A
1N/A# test compactDump, which doesn't do much
1N/Ais( $d->compactDump(3), 3, 'set compactDump to 3' );
1N/Ais( $d->compactDump(1), 479, 'compactDump reset to 6*80-1 when less than 2' );
1N/A
1N/A# test veryCompact, which does slightly more, setting compactDump sometimes
1N/A$d->{compactDump} = 0;
1N/Ais( $d->veryCompact(1), 1, 'set veryCompact successfully' );
1N/Aok( $d->compactDump(), 'and it set compactDump as well' );
1N/A
1N/A# test set_unctrl
1N/A$d->set_unctrl('impossible value');
1N/Alike( $out->read, qr/^Unknown value/, 'set_unctrl caught bad value' );
1N/Ais( $d->set_unctrl('quote'), 'quote', 'set quote fine' );
1N/Ais( $d->set_unctrl(), 'quote', 'retrieved quote fine' );
1N/A
1N/A# test set_quote
1N/A$d->set_quote('"');
1N/Ais( $d->{tick}, '"', 'set_quote set tick right' );
1N/Ais( $d->{unctrl}, 'quote', 'set unctrl right too' );
1N/A$d->set_quote('auto');
1N/Ais( $d->{tick}, 'auto', 'set_quote set auto right' );
1N/A$d->set_quote('foo');
1N/Ais( $d->{tick}, "'", 'default value set to " correctly' );
1N/A
1N/A# test dumpglob
1N/A# should do nothing if debugger signal flag is raised
1N/A$d->{stopDbSignal} = $DB::signal = 1;
1N/Ais( $d->dumpglob(*DB::signal), undef, 'returned early with DB signal set' );
1N/Aundef $DB::signal;
1N/A
1N/A# test dumping "normal" variables, this is a nasty glob trick
1N/A$foo = 1;
1N/A$d->dumpglob( '', 2, 'foo', local *foo = \$foo );
1N/Ais( $out->read, " \$foo = 1\n", 'dumped glob for $foo correctly' );
1N/A@bar = (1, 2);
1N/A
1N/A# the key name is a little different here
1N/A$d->dumpglob( '', 0, 'boo', *bar );
1N/Ais( $out->read, "\@boo = (\n 0..1 1 2\n)\n", 'dumped glob for @bar fine' );
1N/A
1N/A%baz = ( one => 1, two => 2 );
1N/A$d->dumpglob( '', 0, 'baz', *baz );
1N/Ais( $out->read, "\%baz = (\n 'one' => 1, 'two' => 2\n)\n",
1N/A 'dumped glob for %baz fine' );
1N/A
1N/ASKIP: {
1N/A skip( "Couldn't open $0 for reading", 1 ) unless open(FILE, $0);
1N/A my $fileno = fileno(FILE);
1N/A $d->dumpglob( '', 0, 'FILE', *FILE );
1N/A is( $out->read, "FileHandle(FILE) => fileno($fileno)\n",
1N/A 'dumped filehandle from glob fine' );
1N/A}
1N/A
1N/A$d->dumpglob( '', 0, 'read', *TieOut::read );
1N/Ais( $out->read, '', 'no sub dumped without $all set' );
1N/A$d->dumpglob( '', 0, 'read', \&TieOut::read, 1 );
1N/Ais( $out->read, "&read in ???\n", 'sub dumped when requested' );
1N/A
1N/A# see if it dumps DB-like values correctly
1N/A$d->{dumpDBFiles} = 1;
1N/A$d->dumpglob( '', 0, '_<foo', *foo );
1N/Ais( $out->read, "\$_<foo = 1\n", 'dumped glob for $_<foo correctly (DB)' );
1N/A
1N/A# test CvGV name
1N/ASKIP: {
1N/A skip( 'no Devel::Peek', 1 ) unless use_ok( 'Devel::Peek' );
1N/A is( $d->CvGV_name(\&TieOut::read), 'TieOut::read', 'CvGV_name found sub' );
1N/A}
1N/A
1N/A# test dumpsub
1N/A$d->dumpsub( '', 'TieOut::read' );
1N/Alike( $out->read, qr/&TieOut::read in/, 'dumpsub found sub fine' );
1N/A
1N/A# test findsubs
1N/Ais( $d->findsubs(), undef, 'findsubs returns nothing without %DB::sub' );
1N/A$DB::sub{'TieOut::read'} = 'TieOut';
1N/Ais( $d->findsubs( \&TieOut::read ), 'TieOut::read', 'findsubs reported sub' );
1N/A
1N/A# now that it's capable of finding the package...
1N/A$d->dumpsub( '', 'TieOut::read' );
1N/Ais( $out->read, "&TieOut::read in TieOut\n", 'dumpsub found sub fine again' );
1N/A
1N/A# this should print just a usage message
1N/A$d->{usageOnly} = 1;
1N/A$d->dumpvars( 'Fake', 'veryfake' );
1N/Alike( $out->read, qr/^String space:/, 'printed usage message fine' );
1N/Adelete $d->{usageOnly};
1N/A
1N/A# this should report @INC and %INC
1N/A$d->dumpvars( 'main', 'INC' );
1N/Alike( $out->read, qr/\@INC =/, 'dumped variables from a package' );
1N/A
1N/A# this should report nothing
1N/A$DB::signal = 1;
1N/A$d->dumpvars( 'main', 'INC' );
1N/Ais( $out->read, '', 'no dump when $DB::signal is set' );
1N/Aundef $DB::signal;
1N/A
1N/Ais( $d->scalarUsage('12345'), 5, 'scalarUsage reports length correctly' );
1N/Ais( $d->arrayUsage( [1, 2, 3], 'a' ), 3, 'arrayUsage reports correct lengths' );
1N/Ais( $out->read, "\@a = 3 items (data: 3 bytes)\n", 'arrayUsage message okay' );
1N/Ais( $d->hashUsage({ one => 1 }, 'b'), 4, 'hashUsage reports correct lengths' );
1N/Ais( $out->read, "\%b = 1 item (keys: 3; values: 1; total: 4 bytes)\n",
1N/A 'hashUsage message okay' );
1N/Ais( $d->hashUsage({ one => [ 1, 2, 3 ]}, 'c'), 6, 'complex hash okay' );
1N/Ais( $out->read, "\%c = 1 item (keys: 3; values: 3; total: 6 bytes)\n",
1N/A 'hashUsage complex message okay' );
1N/A
1N/A$foo = 'one';
1N/A@foo = ('two');
1N/A%foo = ( three => '123' );
1N/Ais( $d->globUsage(\*foo, 'foo'), 14, 'globUsage reports length correctly' );
1N/Alike( $out->read, qr/\@foo =.+\%foo =/s, 'globValue message okay' );
1N/A
1N/A# and now, the real show
1N/A$d->dumpValue(undef);
1N/Ais( $out->read, "undef\n", 'dumpValue caught undef value okay' );
1N/A$d->dumpValue($foo);
1N/Ais( $out->read, "'one'\n", 'dumpValue worked' );
1N/A$d->dumpValue(@foo);
1N/Ais( $out->read, "'two'\n", 'dumpValue worked on array' );
1N/A$d->dumpValue(\$foo);
1N/Ais( $out->read, "-> 'one'\n", 'dumpValue worked on scalar ref' );
1N/A
1N/A# dumpValues (the rest of these should be caught by unwrap)
1N/A$d->dumpValues(undef);
1N/Ais( $out->read, "undef\n", 'dumpValues caught undef value fine' );
1N/A$d->dumpValues(\@foo);
1N/Ais( $out->read, "0 0..0 'two'\n", 'dumpValues worked on array ref' );
1N/A$d->dumpValues('one', 'two');
1N/Ais( $out->read, "0..1 'one' 'two'\n", 'dumpValues worked on multiple values' );
1N/A
1N/A
1N/Apackage TieOut;
1N/Ause overload '"' => sub { "overloaded!" };
1N/A
1N/Asub TIEHANDLE {
1N/A my $class = shift;
1N/A bless(\( my $ref), $class);
1N/A}
1N/A
1N/Asub PRINT {
1N/A my $self = shift;
1N/A $$self .= join('', @_);
1N/A}
1N/A
1N/Asub read {
1N/A my $self = shift;
1N/A return substr($$self, 0, length($$self), '');
1N/A}