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