7 print "1..0 # skip: EBCDIC\n";
12 use vars qw( $foo @bar %baz );
14 use Test::More tests => 88;
16 use_ok( 'Dumpvalue' );
19 ok( $d = Dumpvalue->new(), 'create a new Dumpvalue object' );
21 $d->set( globPrint => 1, dumpReused => 1 );
22 is( $d->{globPrint}, 1, 'set an option correctly' );
23 is( $d->get('globPrint'), 1, 'get an option correctly' );
24 is( $d->get('globPrint', 'dumpReused'), qw( 1 1 ), 'get multiple options' );
26 # check to see if unctrl works
27 is( ref( Dumpvalue::unctrl(*FOO) ), 'GLOB', 'unctrl should not modify GLOB' );
28 is( Dumpvalue::unctrl('donotchange'), 'donotchange', "unctrl shouldn't modify");
29 like( Dumpvalue::unctrl("bo\007nd"), qr/bo\^.nd/, 'unctrl should escape' );
31 # check to see if stringify works
32 is( $d->stringify(), 'undef', 'stringify handles undef okay' );
34 # the default is 1, but we want two single quotes
36 is( $d->stringify(), "''", 'stringify skips undef when asked nicely' );
38 is( $d->stringify(*FOO), *FOO . "", 'stringify stringifies globs alright' );
40 # check for double-quotes if there's an unprintable character
42 like( $d->stringify("hi\005"), qr/^"hi/, 'added double-quotes when necessary' );
44 # if no unprintable character, escape ticks or backslashes
45 is( $d->stringify('hi'), "'hi'", 'used single-quotes when appropriate' );
48 $d->{unctrl} = 'unctrl';
49 like( $d->stringify('double and whack:\ "'), qr!\\ \"!, 'escaped with unctrl' );
50 like( $d->stringify("a\005"), qr/^"a\^/, 'escaped ASCII value in unctrl' );
51 like( $d->stringify("b\205"), qr!^'b.'$!, 'no high-bit escape value in unctrl');
53 $d->{quoteHighBit} = 1;
54 like( $d->stringify("b\205"), qr!^'b\\205!, 'high-bit now escaped in unctrl');
57 $d->{unctrl} = 'quote';
58 is( $d->stringify('5@ $1'), "'5\@ \$1'", 'quoted $ and @ fine' );
59 is( $d->stringify("5@\033\$1"), '"5\@\e\$1"', 'quoted $ and @ and \033 fine' );
60 like( $d->stringify("\037"), qr/^"\\c/, 'escaped ASCII value okay' );
62 # add ticks, if necessary
63 is( $d->stringify("no ticks", 1), 'no ticks', 'avoid ticks if asked' );
65 my $out = tie *OUT, 'TieOut';
68 # test DumpElem, it does its magic with veryCompact set
69 $d->{veryCompact} = 1;
70 $d->DumpElem([1, 2, 3]);
71 is( $out->read, "0..2 1 2 3\n", 'DumpElem worked on array ref');
72 $d->DumpElem({ one => 1, two => 2 });
73 is( $out->read, "'one' => 1, 'two' => 2\n", 'DumpElem worked on hash ref' );
75 is( $out->read, "'hi'\n", 'DumpElem worked on simple scalar' );
76 $d->{veryCompact} = 0;
78 like( $out->read, qr/ARRAY/, 'DumpElem okay with reference and no veryCompact');
80 # should compact simple arrays just fine
81 $d->{veryCompact} = 1;
82 $d->DumpElem([1, 2, 3]);
83 is( $out->read, "0..2 1 2 3\n", 'dumped array fine' );
85 $d->DumpElem([1, 2, 3]);
86 is( $out->read, "0..2 1 2 ...\n", 'dumped limited array fine' );
88 # should compact simple hashes just fine
89 $d->DumpElem({ a => 1, b => 2, c => 3 });
90 is( $out->read, "'a' => 1, 'b' => 2, 'c' => 3\n", 'dumped hash fine' );
92 $d->DumpElem({ a => 1, b => 2, c => 3 });
93 is( $out->read, "'a' => 1, 'b' => 2 ...\n", 'dumped limited hash fine' );
95 # should just stringify what it is
96 $d->{veryCompact} = 0;
98 like( $out->read, qr/ARRAY.+empty array/s, 'stringified empty array ref' );
100 like( $out->read, qr/HASH.+empty hash/s, 'stringified empty hash ref' );
102 is( $out->read, "1\n", 'stringified simple scalar' );
105 $DB::signal = $d->{stopDbSignal} = 1;
106 is( $d->unwrap(), undef, 'unwrap returns if DB signal is set' );
110 $d->{dumpReused} = 0;
112 is( $out->read, "-> 7\n", 'unwrap worked on scalar' );
114 is( $out->read, "-> REUSED_ADDRESS\n", 'unwrap worked on scalar' );
115 $d->unwrap({ one => 1 });
117 # leaving this at zero may cause some subsequent tests to fail
118 # if they reuse an address creating an anonymous variable
119 $d->{dumpReused} = 1;
120 is( $out->read, "'one' => 1\n", 'unwrap worked on hash' );
121 $d->unwrap([ 2, 3 ]);
122 is( $out->read, "0 2\n1 3\n", 'unwrap worked on array' );
124 is( $out->read, '', 'unwrap ignored glob on first try');
126 is( $out->read, "*DUMPED_GLOB*\n", 'unwrap worked on glob');
127 $d->unwrap(qr/foo(.+)/);
128 is( $out->read, "-> qr/(?-xism:foo(.+))/\n", 'unwrap worked on Regexp' );
129 $d->unwrap( sub {} );
130 like( $out->read, qr/^-> &CODE/, 'unwrap worked on sub ref' );
133 # test to see if first arg 'eq' second
134 ok( Dumpvalue::matchvar(1, 1), 'matchvar matched numbers fine' );
135 ok( Dumpvalue::matchvar('hi', 'hi'), 'matchvar matched strings fine' );
136 ok( !Dumpvalue::matchvar('hello', 1), 'matchvar caught failed match fine' );
138 # test compactDump, which doesn't do much
139 is( $d->compactDump(3), 3, 'set compactDump to 3' );
140 is( $d->compactDump(1), 479, 'compactDump reset to 6*80-1 when less than 2' );
142 # test veryCompact, which does slightly more, setting compactDump sometimes
143 $d->{compactDump} = 0;
144 is( $d->veryCompact(1), 1, 'set veryCompact successfully' );
145 ok( $d->compactDump(), 'and it set compactDump as well' );
148 $d->set_unctrl('impossible value');
149 like( $out->read, qr/^Unknown value/, 'set_unctrl caught bad value' );
150 is( $d->set_unctrl('quote'), 'quote', 'set quote fine' );
151 is( $d->set_unctrl(), 'quote', 'retrieved quote fine' );
155 is( $d->{tick}, '"', 'set_quote set tick right' );
156 is( $d->{unctrl}, 'quote', 'set unctrl right too' );
157 $d->set_quote('auto');
158 is( $d->{tick}, 'auto', 'set_quote set auto right' );
159 $d->set_quote('foo');
160 is( $d->{tick}, "'", 'default value set to " correctly' );
163 # should do nothing if debugger signal flag is raised
164 $d->{stopDbSignal} = $DB::signal = 1;
165 is( $d->dumpglob(*DB::signal), undef, 'returned early with DB signal set' );
168 # test dumping "normal" variables, this is a nasty glob trick
170 $d->dumpglob( '', 2, 'foo', local *foo = \$foo );
171 is( $out->read, " \$foo = 1\n", 'dumped glob for $foo correctly' );
174 # the key name is a little different here
175 $d->dumpglob( '', 0, 'boo', *bar );
176 is( $out->read, "\@boo = (\n 0..1 1 2\n)\n", 'dumped glob for @bar fine' );
178 %baz = ( one => 1, two => 2 );
179 $d->dumpglob( '', 0, 'baz', *baz );
180 is( $out->read, "\%baz = (\n 'one' => 1, 'two' => 2\n)\n",
181 'dumped glob for %baz fine' );
184 skip( "Couldn't open $0 for reading", 1 ) unless open(FILE, $0);
185 my $fileno = fileno(FILE);
186 $d->dumpglob( '', 0, 'FILE', *FILE );
187 is( $out->read, "FileHandle(FILE) => fileno($fileno)\n",
188 'dumped filehandle from glob fine' );
191 $d->dumpglob( '', 0, 'read', *TieOut::read );
192 is( $out->read, '', 'no sub dumped without $all set' );
193 $d->dumpglob( '', 0, 'read', \&TieOut::read, 1 );
194 is( $out->read, "&read in ???\n", 'sub dumped when requested' );
196 # see if it dumps DB-like values correctly
197 $d->{dumpDBFiles} = 1;
198 $d->dumpglob( '', 0, '_<foo', *foo );
199 is( $out->read, "\$_<foo = 1\n", 'dumped glob for $_<foo correctly (DB)' );
203 skip( 'no Devel::Peek', 1 ) unless use_ok( 'Devel::Peek' );
204 is( $d->CvGV_name(\&TieOut::read), 'TieOut::read', 'CvGV_name found sub' );
208 $d->dumpsub( '', 'TieOut::read' );
209 like( $out->read, qr/&TieOut::read in/, 'dumpsub found sub fine' );
212 is( $d->findsubs(), undef, 'findsubs returns nothing without %DB::sub' );
213 $DB::sub{'TieOut::read'} = 'TieOut';
214 is( $d->findsubs( \&TieOut::read ), 'TieOut::read', 'findsubs reported sub' );
216 # now that it's capable of finding the package...
217 $d->dumpsub( '', 'TieOut::read' );
218 is( $out->read, "&TieOut::read in TieOut\n", 'dumpsub found sub fine again' );
220 # this should print just a usage message
222 $d->dumpvars( 'Fake', 'veryfake' );
223 like( $out->read, qr/^String space:/, 'printed usage message fine' );
224 delete $d->{usageOnly};
226 # this should report @INC and %INC
227 $d->dumpvars( 'main', 'INC' );
228 like( $out->read, qr/\@INC =/, 'dumped variables from a package' );
230 # this should report nothing
232 $d->dumpvars( 'main', 'INC' );
233 is( $out->read, '', 'no dump when $DB::signal is set' );
236 is( $d->scalarUsage('12345'), 5, 'scalarUsage reports length correctly' );
237 is( $d->arrayUsage( [1, 2, 3], 'a' ), 3, 'arrayUsage reports correct lengths' );
238 is( $out->read, "\@a = 3 items (data: 3 bytes)\n", 'arrayUsage message okay' );
239 is( $d->hashUsage({ one => 1 }, 'b'), 4, 'hashUsage reports correct lengths' );
240 is( $out->read, "\%b = 1 item (keys: 3; values: 1; total: 4 bytes)\n",
241 'hashUsage message okay' );
242 is( $d->hashUsage({ one => [ 1, 2, 3 ]}, 'c'), 6, 'complex hash okay' );
243 is( $out->read, "\%c = 1 item (keys: 3; values: 3; total: 6 bytes)\n",
244 'hashUsage complex message okay' );
248 %foo = ( three => '123' );
249 is( $d->globUsage(\*foo, 'foo'), 14, 'globUsage reports length correctly' );
250 like( $out->read, qr/\@foo =.+\%foo =/s, 'globValue message okay' );
252 # and now, the real show
253 $d->dumpValue(undef);
254 is( $out->read, "undef\n", 'dumpValue caught undef value okay' );
256 is( $out->read, "'one'\n", 'dumpValue worked' );
258 is( $out->read, "'two'\n", 'dumpValue worked on array' );
259 $d->dumpValue(\$foo);
260 is( $out->read, "-> 'one'\n", 'dumpValue worked on scalar ref' );
262 # dumpValues (the rest of these should be caught by unwrap)
263 $d->dumpValues(undef);
264 is( $out->read, "undef\n", 'dumpValues caught undef value fine' );
265 $d->dumpValues(\@foo);
266 is( $out->read, "0 0..0 'two'\n", 'dumpValues worked on array ref' );
267 $d->dumpValues('one', 'two');
268 is( $out->read, "0..1 'one' 'two'\n", 'dumpValues worked on multiple values' );
272 use overload '"' => sub { "overloaded!" };
276 bless(\( my $ref), $class);
281 $$self .= join('', @_);
286 return substr($$self, 0, length($$self), '');