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