1#!./perl
2
3BEGIN {
4	require Config;
5	if (($Config::Config{'extensions'} !~ m!\bList/Util\b!) ){
6	    print "1..0 # Skip -- Perl configured without List::Util module\n";
7	    exit 0;
8	}
9
10	# `make test` in the CPAN version of this module runs us with -w, but
11	# Dumpvalue.pm relies on all sorts of things that can cause warnings. I
12	# don't think that's worth fixing, so we just turn off all warnings
13	# during testing.
14	$^W = 0;
15}
16
17our ( $foo, @bar, %baz );
18
19use lib ("./t/lib");
20use TieOut;
21use Test::More tests => 88;
22
23use_ok( 'Dumpvalue' );
24
25my $d;
26ok( $d = Dumpvalue->new(), 'create a new Dumpvalue object' );
27
28$d->set( globPrint => 1, dumpReused => 1 );
29is( $d->{globPrint}, 1, 'set an option correctly' );
30is( $d->get('globPrint'), 1, 'get an option correctly' );
31is( $d->get('globPrint', 'dumpReused'), qw( 1 1 ), 'get multiple options' );
32
33# check to see if unctrl works
34is( ref( Dumpvalue::unctrl(*FOO) ), 'GLOB', 'unctrl should not modify GLOB' );
35is( Dumpvalue::unctrl('donotchange'), 'donotchange', "unctrl shouldn't modify");
36like( Dumpvalue::unctrl("bo\007nd"), qr/bo\^.nd/, 'unctrl should escape' );
37
38# check to see if stringify works
39is( $d->stringify(), 'undef', 'stringify handles undef okay' );
40
41# the default is 1, but we want two single quotes
42$d->{printUndef} = 0;
43is( $d->stringify(), "''", 'stringify skips undef when asked nicely' );
44
45is( $d->stringify(*FOO), *FOO . "", 'stringify stringifies globs alright' );
46
47# check for double-quotes if there's an unprintable character
48$d->{tick} = 'auto';
49like( $d->stringify("hi\005"), qr/^"hi/, 'added double-quotes when necessary' );
50
51# if no unprintable character, escape ticks or backslashes
52is( $d->stringify('hi'), "'hi'", 'used single-quotes when appropriate' );
53
54# if 'unctrl' is set
55$d->{unctrl} = 'unctrl';
56like( $d->stringify('double and whack:\ "'), qr!\\ \"!, 'escaped with unctrl' );
57like( $d->stringify("a\005"), qr/^"a\^/, 'escaped ASCII value in unctrl' );
58like( $d->stringify("b\xb6"), qr!^'b.'$!, 'no high-bit escape value in unctrl');
59
60$d->{quoteHighBit} = 1;
61like( $d->stringify("b\266"), qr!^'b\\266!, 'high-bit now escaped in unctrl');
62
63# if 'quote' is set
64$d->{unctrl} = 'quote';
65is( $d->stringify('5@ $1'), "'5\@ \$1'", 'quoted $ and @ fine' );
66is( $d->stringify("5@\e\$1"), '"5\@\e\$1"', 'quoted $ and @ and \e fine' );
67like( $d->stringify("\037"), qr/^"\\c/, 'escaped ASCII value okay' );
68
69# add ticks, if necessary
70is( $d->stringify("no ticks", 1), 'no ticks', 'avoid ticks if asked' );
71
72my $out = tie *OUT, 'TieOut';
73select(OUT);
74
75# test DumpElem, it does its magic with veryCompact set
76$d->{veryCompact} = 1;
77$d->DumpElem([1, 2, 3]);
78is( $out->read, "0..2  1 2 3\n", 'DumpElem worked on array ref');
79$d->DumpElem({ one => 1, two => 2 });
80is( $out->read, "'one' => 1, 'two' => 2\n", 'DumpElem worked on hash ref' );
81$d->DumpElem('hi');
82is( $out->read, "'hi'\n", 'DumpElem worked on simple scalar' );
83$d->{veryCompact} = 0;
84$d->DumpElem([]);
85like( $out->read, qr/ARRAY/, 'DumpElem okay with reference and no veryCompact');
86
87# should compact simple arrays just fine
88$d->{veryCompact} = 1;
89$d->DumpElem([1, 2, 3]);
90is( $out->read, "0..2  1 2 3\n", 'dumped array fine' );
91$d->{arrayDepth} = 2;
92$d->DumpElem([1, 2, 3]);
93is( $out->read, "0..2  1 2 ...\n", 'dumped limited array fine' );
94
95# should compact simple hashes just fine
96$d->DumpElem({ a => 1, b => 2, c => 3 });
97is( $out->read, "'a' => 1, 'b' => 2, 'c' => 3\n", 'dumped hash fine' );
98$d->{hashDepth} = 2;
99$d->DumpElem({ a => 1, b => 2, c => 3 });
100is( $out->read, "'a' => 1, 'b' => 2 ...\n", 'dumped limited hash fine' );
101
102# should just stringify what it is
103$d->{veryCompact} = 0;
104$d->DumpElem([]);
105like( $out->read, qr/ARRAY.+empty array/s, 'stringified empty array ref' );
106$d->DumpElem({});
107like( $out->read, qr/HASH.+empty hash/s, 'stringified empty hash ref' );
108$d->DumpElem(1);
109is( $out->read, "1\n", 'stringified simple scalar' );
110
111# test unwrap
112$DB::signal = $d->{stopDbSignal} = 1;
113is( $d->unwrap(), undef, 'unwrap returns if DB signal is set' );
114undef $DB::signal;
115
116my $foo = 7;
117$d->{dumpReused} = 0;
118$d->unwrap(\$foo);
119is( $out->read, "-> 7\n", 'unwrap worked on scalar' );
120$d->unwrap(\$foo);
121is( $out->read, "-> REUSED_ADDRESS\n", 'unwrap worked on scalar' );
122$d->unwrap({ one => 1 });
123
124# leaving this at zero may cause some subsequent tests to fail
125# if they reuse an address creating an anonymous variable
126$d->{dumpReused} = 1;
127is( $out->read, "'one' => 1\n", 'unwrap worked on hash' );
128$d->unwrap([ 2, 3 ]);
129is( $out->read, "0  2\n1  3\n", 'unwrap worked on array' );
130$d->unwrap(*FOO);
131is( $out->read, '', 'unwrap ignored glob on first try');
132$d->unwrap(*FOO);
133is( $out->read, "*DUMPED_GLOB*\n", 'unwrap worked on glob');
134$d->unwrap(qr/foo(.+)/);
135
136my $modifiers = (qr// =~ /\Q(?^/) ? '^' : '-xism';
137is( $out->read, "-> qr/(?${modifiers}:foo(.+))/\n", 'unwrap worked on Regexp' );
138
139$d->unwrap( sub {} );
140like( $out->read, qr/^-> &CODE/, 'unwrap worked on sub ref' );
141
142# test matchvar
143# test to see if first arg 'eq' second
144ok( Dumpvalue::matchvar(1, 1), 'matchvar matched numbers fine' );
145ok( Dumpvalue::matchvar('hi', 'hi'), 'matchvar matched strings fine' );
146ok( !Dumpvalue::matchvar('hello', 1), 'matchvar caught failed match fine' );
147
148# test compactDump, which doesn't do much
149is( $d->compactDump(3), 3, 'set compactDump to 3' );
150is( $d->compactDump(1), 479, 'compactDump reset to 6*80-1 when less than 2' );
151
152# test veryCompact, which does slightly more, setting compactDump sometimes
153$d->{compactDump} = 0;
154is( $d->veryCompact(1), 1, 'set veryCompact successfully' );
155ok( $d->compactDump(), 'and it set compactDump as well' );
156
157# test set_unctrl
158$d->set_unctrl('impossible value');
159like( $out->read, qr/^Unknown value/, 'set_unctrl caught bad value' );
160is( $d->set_unctrl('quote'), 'quote', 'set quote fine' );
161is( $d->set_unctrl(), 'quote', 'retrieved quote fine' );
162
163# test set_quote
164$d->set_quote('"');
165is( $d->{tick}, '"', 'set_quote set tick right' );
166is( $d->{unctrl}, 'quote', 'set unctrl right too' );
167$d->set_quote('auto');
168is( $d->{tick}, 'auto', 'set_quote set auto right' );
169$d->set_quote('foo');
170is( $d->{tick}, "'", 'default value set to " correctly' );
171
172# test dumpglob
173# should do nothing if debugger signal flag is raised
174$d->{stopDbSignal} = $DB::signal = 1;
175is( $d->dumpglob(*DB::signal), undef, 'returned early with DB signal set' );
176undef $DB::signal;
177
178# test dumping "normal" variables, this is a nasty glob trick
179$foo = 1;
180$d->dumpglob( '', 2, 'foo', local *foo = \$foo );
181is( $out->read, "  \$foo = 1\n", 'dumped glob for $foo correctly' );
182@bar = (1, 2);
183
184# the key name is a little different here
185$d->dumpglob( '', 0, 'boo', *bar );
186is( $out->read, "\@boo = (\n   0..1  1 2\n)\n", 'dumped glob for @bar fine' );
187
188%baz = ( one => 1, two => 2 );
189$d->dumpglob( '', 0, 'baz', *baz );
190is( $out->read, "\%baz = (\n   'one' => 1, 'two' => 2\n)\n",
191	'dumped glob for %baz fine' );
192
193SKIP: {
194	skip( "Couldn't open $0 for reading", 1 ) unless open(FILE, '<', $0);
195	my $fileno = fileno(FILE);
196	$d->dumpglob( '', 0, 'FILE', *FILE );
197	is( $out->read, "FileHandle(FILE) => fileno($fileno)\n",
198		'dumped filehandle from glob fine' );
199}
200
201$d->dumpglob( '', 0, 'read', *TieOut::read );
202is( $out->read, '', 'no sub dumped without $all set' );
203$d->dumpglob( '', 0, 'read', \&TieOut::read, 1 );
204is( $out->read, "&read in ???\n", 'sub dumped when requested' );
205
206# see if it dumps DB-like values correctly
207$d->{dumpDBFiles} = 1;
208$d->dumpglob( '', 0, '_<foo', *foo );
209is( $out->read, "\$_<foo = 1\n", 'dumped glob for $_<foo correctly (DB)' );
210
211# test CvGV name
212SKIP: {
213	if (" $Config::Config{'extensions'} " !~ m[ Devel/Peek ]) {
214	    skip( 'no Devel::Peek', 2 );
215	}
216	use_ok( 'Devel::Peek' );
217	is( $d->CvGV_name(\&TieOut::read), 'TieOut::read', 'CvGV_name found sub' );
218}
219
220# test dumpsub
221$d->dumpsub( '', 'TieOut::read' );
222like( $out->read, qr/&TieOut::read in/, 'dumpsub found sub fine' );
223
224# test findsubs
225is( $d->findsubs(), undef, 'findsubs returns nothing without %DB::sub' );
226$DB::sub{'TieOut::read'} = 'TieOut';
227is( $d->findsubs( \&TieOut::read ), 'TieOut::read', 'findsubs reported sub' );
228
229# now that it's capable of finding the package...
230$d->dumpsub( '', 'TieOut::read' );
231is( $out->read, "&TieOut::read in TieOut\n", 'dumpsub found sub fine again' );
232
233# this should print just a usage message
234$d->{usageOnly} = 1;
235$d->dumpvars( 'Fake', 'veryfake' );
236like( $out->read, qr/^String space:/, 'printed usage message fine' );
237delete $d->{usageOnly};
238
239# this should report @INC and %INC
240$d->dumpvars( 'main', 'INC' );
241like( $out->read, qr/\@INC =/, 'dumped variables from a package' );
242
243# this should report nothing
244$DB::signal = 1;
245$d->dumpvars( 'main', 'INC' );
246is( $out->read, '', 'no dump when $DB::signal is set' );
247undef $DB::signal;
248
249is( $d->scalarUsage('12345'), 5, 'scalarUsage reports length correctly' );
250is( $d->arrayUsage( [1, 2, 3], 'a' ), 3, 'arrayUsage reports correct lengths' );
251is( $out->read, "\@a = 3 items (data: 3 bytes)\n", 'arrayUsage message okay' );
252is( $d->hashUsage({ one => 1 }, 'b'), 4, 'hashUsage reports correct lengths' );
253is( $out->read, "\%b = 1 item (keys: 3; values: 1; total: 4 bytes)\n",
254	'hashUsage message okay' );
255is( $d->hashUsage({ one => [ 1, 2, 3 ]}, 'c'), 6, 'complex hash okay' );
256is( $out->read, "\%c = 1 item (keys: 3; values: 3; total: 6 bytes)\n",
257	'hashUsage complex message okay' );
258
259$foo = 'one';
260@foo = ('two');
261%foo = ( three => '123' );
262is( $d->globUsage(\*foo, 'foo'), 14, 'globUsage reports length correctly' );
263like( $out->read, qr/\@foo =.+\%foo =/s, 'globValue message okay' );
264
265# and now, the real show
266$d->dumpValue(undef);
267is( $out->read, "undef\n", 'dumpValue caught undef value okay' );
268$d->dumpValue($foo);
269is( $out->read, "'one'\n", 'dumpValue worked' );
270$d->dumpValue(@foo);
271is( $out->read, "'two'\n", 'dumpValue worked on array' );
272$d->dumpValue(\$foo);
273is( $out->read, "-> 'one'\n", 'dumpValue worked on scalar ref' );
274
275# dumpValues (the rest of these should be caught by unwrap)
276$d->dumpValues(undef);
277is( $out->read, "undef\n", 'dumpValues caught undef value fine' );
278$d->dumpValues(\@foo);
279is( $out->read, "0  0..0  'two'\n", 'dumpValues worked on array ref' );
280$d->dumpValues('one', 'two');
281is( $out->read, "0..1  'one' 'two'\n", 'dumpValues worked on multiple values' );
282
283