This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Math::BigInt 1.44 from Tels and
[perl5.git] / lib / Dumpvalue.t
1 #!./perl
2
3 BEGIN {
4         chdir 't' if -d 't';
5         @INC = '../lib';
6 }
7
8 use vars qw( $foo @bar %baz );
9
10 use Test::More tests => 88;
11
12 use_ok( 'Dumpvalue' );
13
14 my $d;
15 ok( $d = Dumpvalue->new(), 'create a new Dumpvalue object' );
16
17 $d->set( globPrint => 1, dumpReused => 1 );
18 is( $d->{globPrint}, 1, 'set an option correctly' );
19 is( $d->get('globPrint'), 1, 'get an option correctly' );
20 is( $d->get('globPrint', 'dumpReused'), qw( 1 1 ), 'get multiple options' );
21
22 # check to see if unctrl works
23 is( ref( Dumpvalue::unctrl(*FOO) ), 'GLOB', 'unctrl should not modify GLOB' );
24 is( Dumpvalue::unctrl('donotchange'), 'donotchange', "unctrl shouldn't modify");
25 like( Dumpvalue::unctrl("bo\007nd"), qr/bo\^.nd/, 'unctrl should escape' );
26
27 # check to see if stringify works
28 is( $d->stringify(), 'undef', 'stringify handles undef okay' );
29
30 # the default is 1, but we want two single quotes
31 $d->{printUndef} = 0;
32 is( $d->stringify(), "''", 'stringify skips undef when asked nicely' );
33
34 is( $d->stringify(*FOO), *FOO . "", 'stringify stringifies globs alright' );
35
36 # check for double-quotes if there's an unprintable character
37 $d->{tick} = 'auto';
38 like( $d->stringify("hi\005"), qr/^"hi/, 'added double-quotes when necessary' );
39
40 # if no unprintable character, escape ticks or backslashes
41 is( $d->stringify('hi'), "'hi'", 'used single-quotes when appropriate' );
42
43 # if 'unctrl' is set
44 $d->{unctrl} = 'unctrl';
45 like( $d->stringify('double and whack:\ "'), qr!\\ \"!, 'escaped with unctrl' );
46 like( $d->stringify("a\005"), qr/^"a\^/, 'escaped ASCII value in unctrl' );
47 like( $d->stringify("b\205"), qr!^'b.'$!, 'no high-bit escape value in unctrl');
48
49 $d->{quoteHighBit} = 1;
50 like( $d->stringify("b\205"), qr!^'b\\205!, 'high-bit now escaped in unctrl');
51
52 # if 'quote' is set
53 $d->{unctrl} = 'quote';
54 is( $d->stringify('5@ $1'), "'5\@ \$1'", 'quoted $ and @ fine' );
55 is( $d->stringify("5@\033\$1"), '"5\@\e\$1"', 'quoted $ and @ and \033 fine' );
56 like( $d->stringify("\037"), qr/^"\\c/, 'escaped ASCII value okay' );
57
58 # add ticks, if necessary
59 is( $d->stringify("no ticks", 1), 'no ticks', 'avoid ticks if asked' );
60
61 my $out = tie *OUT, 'TieOut';
62 select(OUT);
63
64 # test DumpElem, it does its magic with veryCompact set
65 $d->{veryCompact} = 1;
66 $d->DumpElem([1, 2, 3]);
67 is( $out->read, "0..2  1 2 3\n", 'DumpElem worked on array ref');
68 $d->DumpElem({ one => 1, two => 2 });
69 is( $out->read, "'one' => 1, 'two' => 2\n", 'DumpElem worked on hash ref' );
70 $d->DumpElem('hi');
71 is( $out->read, "'hi'\n", 'DumpElem worked on simple scalar' );
72 $d->{veryCompact} = 0;
73 $d->DumpElem([]);
74 like( $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]);
79 is( $out->read, "0..2  1 2 3\n", 'dumped array fine' );
80 $d->{arrayDepth} = 2;
81 $d->DumpElem([1, 2, 3]);
82 is( $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 });
86 is( $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 });
89 is( $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([]);
94 like( $out->read, qr/ARRAY.+empty array/s, 'stringified empty array ref' );
95 $d->DumpElem({});
96 like( $out->read, qr/HASH.+empty hash/s, 'stringified empty hash ref' );
97 $d->DumpElem(1);
98 is( $out->read, "1\n", 'stringified simple scalar' );
99
100 # test unwrap
101 $DB::signal = $d->{stopDbSignal} = 1;
102 is( $d->unwrap(), undef, 'unwrap returns if DB signal is set' );
103 undef $DB::signal;
104
105 my $foo = 7;
106 $d->{dumpReused} = 0;
107 $d->unwrap(\$foo);
108 is( $out->read, "-> 7\n", 'unwrap worked on scalar' );
109 $d->unwrap(\$foo);
110 is( $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;
116 is( $out->read, "'one' => 1\n", 'unwrap worked on hash' );
117 $d->unwrap([ 2, 3 ]);
118 is( $out->read, "0  2\n1  3\n", 'unwrap worked on array' );
119 $d->unwrap(*FOO);
120 is( $out->read, '', 'unwrap ignored glob on first try');
121 $d->unwrap(*FOO);
122 is( $out->read, "*DUMPED_GLOB*\n", 'unwrap worked on glob');
123 $d->unwrap(qr/foo(.+)/);
124 is( $out->read, "-> qr/(?-xism:foo(.+))/\n", 'unwrap worked on Regexp' );
125 $d->unwrap( sub {} );
126 like( $out->read, qr/^-> &CODE/, 'unwrap worked on sub ref' );
127
128 # test matchvar
129 # test to see if first arg 'eq' second
130 ok( Dumpvalue::matchvar(1, 1), 'matchvar matched numbers fine' );
131 ok( Dumpvalue::matchvar('hi', 'hi'), 'matchvar matched strings fine' );
132 ok( !Dumpvalue::matchvar('hello', 1), 'matchvar caught failed match fine' );
133
134 # test compactDump, which doesn't do much
135 is( $d->compactDump(3), 3, 'set compactDump to 3' );
136 is( $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;
140 is( $d->veryCompact(1), 1, 'set veryCompact successfully' );
141 ok( $d->compactDump(), 'and it set compactDump as well' );
142
143 # test set_unctrl
144 $d->set_unctrl('impossible value');
145 like( $out->read, qr/^Unknown value/, 'set_unctrl caught bad value' );
146 is( $d->set_unctrl('quote'), 'quote', 'set quote fine' );
147 is( $d->set_unctrl(), 'quote', 'retrieved quote fine' );
148
149 # test set_quote
150 $d->set_quote('"');
151 is( $d->{tick}, '"', 'set_quote set tick right' );
152 is( $d->{unctrl}, 'quote', 'set unctrl right too' );
153 $d->set_quote('auto');
154 is( $d->{tick}, 'auto', 'set_quote set auto right' );
155 $d->set_quote('foo');
156 is( $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;
161 is( $d->dumpglob(*DB::signal), undef, 'returned early with DB signal set' );
162 undef $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 );
167 is( $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 );
172 is( $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 );
176 is( $out->read, "\%baz = (\n   'one' => 1, 'two' => 2\n)\n",
177         'dumped glob for %baz fine' );
178
179 SKIP: {
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 );
188 is( $out->read, '', 'no sub dumped without $all set' );
189 $d->dumpglob( '', 0, 'read', \&TieOut::read, 1 );
190 is( $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 );
195 is( $out->read, "\$_<foo = 1\n", 'dumped glob for $_<foo correctly (DB)' );
196
197 # test CvGV name
198 SKIP: {
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' );
205 like( $out->read, qr/&TieOut::read in/, 'dumpsub found sub fine' );
206
207 # test findsubs
208 is( $d->findsubs(), undef, 'findsubs returns nothing without %DB::sub' );
209 $DB::sub{'TieOut::read'} = 'TieOut';
210 is( $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' );
214 is( $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' );
219 like( $out->read, qr/^String space:/, 'printed usage message fine' );
220 delete $d->{usageOnly};
221
222 # this should report @INC and %INC
223 $d->dumpvars( 'main', 'INC' );
224 like( $out->read, qr/\@INC =/, 'dumped variables from a package' );
225
226 # this should report nothing
227 $DB::signal = 1;
228 $d->dumpvars( 'main', 'INC' );
229 is( $out->read, '', 'no dump when $DB::signal is set' );
230 undef $DB::signal;
231
232 is( $d->scalarUsage('12345'), 5, 'scalarUsage reports length correctly' );
233 is( $d->arrayUsage( [1, 2, 3], 'a' ), 3, 'arrayUsage reports correct lengths' );
234 is( $out->read, "\@a = 3 items (data: 3 bytes)\n", 'arrayUsage message okay' );
235 is( $d->hashUsage({ one => 1 }, 'b'), 4, 'hashUsage reports correct lengths' );
236 is( $out->read, "\%b = 1 item (keys: 3; values: 1; total: 4 bytes)\n",
237         'hashUsage message okay' );
238 is( $d->hashUsage({ one => [ 1, 2, 3 ]}, 'c'), 6, 'complex hash okay' );
239 is( $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' );
245 is( $d->globUsage(\*foo, 'foo'), 14, 'globUsage reports length correctly' );
246 like( $out->read, qr/\@foo =.+\%foo =/s, 'globValue message okay' );
247
248 # and now, the real show
249 $d->dumpValue(undef);
250 is( $out->read, "undef\n", 'dumpValue caught undef value okay' );
251 $d->dumpValue($foo);
252 is( $out->read, "'one'\n", 'dumpValue worked' );
253 $d->dumpValue(@foo);
254 is( $out->read, "'two'\n", 'dumpValue worked on array' );
255 $d->dumpValue(\$foo);
256 is( $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);
260 is( $out->read, "undef\n", 'dumpValues caught undef value fine' );
261 $d->dumpValues(\@foo);
262 is( $out->read, "0  0..0  'two'\n", 'dumpValues worked on array ref' );
263 $d->dumpValues('one', 'two');
264 is( $out->read, "0..1  'one' 'two'\n", 'dumpValues worked on multiple values' );
265
266
267 package TieOut;
268 use overload '"' => sub { "overloaded!" };
269
270 sub TIEHANDLE {
271         my $class = shift;
272         bless(\( my $ref), $class);
273 }
274
275 sub PRINT {
276         my $self = shift;
277         $$self .= join('', @_);
278 }
279
280 sub read {
281         my $self = shift;
282         return substr($$self, 0, length($$self), '');
283 }