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