This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
dumpvar.pl broken
[perl5.git] / lib / dumpvar.t
CommitLineData
fafebdf5
A
1#!./perl -- -*- mode: cperl; cperl-indent-level: 4 -*-
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8use strict;
9
10$|=1;
96b31d9e
JH
11
12my @prgs;
13{
14 local $/;
15 @prgs = split "########\n", <DATA>;
16 close DATA;
17}
18
19use Test::More;
20
21plan tests => scalar @prgs;
22
fafebdf5
A
23require "dumpvar.pl";
24
f97a9a4b
JH
25sub unctrl { print dumpvar::unctrl($_[0]), "\n" }
26sub uniescape { print dumpvar::uniescape($_[0]), "\n" }
27sub stringify { print dumpvar::stringify($_[0]), "\n" }
9bcb75ad
JM
28sub dumpvalue {
29 local $\ = '';
30 local $, = '';
31 local $" = ' ';
32 my @params = @_;
33 &main::dumpValue(\@params, -1);
34}
f97a9a4b
JH
35
36package Foo;
37
38sub new { my $class = shift; bless [ @_ ], $class }
39
40package Bar;
41
42sub new { my $class = shift; bless [ @_ ], $class }
43
44use overload '""' => sub { "Bar<@{$_[0]}>" };
45
46package main;
47
48my $foo = Foo->new(1..5);
49my $bar = Bar->new(1..5);
50
96b31d9e
JH
51for (@prgs) {
52 my($prog, $expected) = split(/\nEXPECT\n?/, $_);
f97a9a4b
JH
53 # TODO: dumpvar::stringify() is controlled by a pile of package
54 # dumpvar variables: $printUndef, $unctrl, $quoteHighBit, $bareStringify,
55 # and so forth. We need to test with various settings of those.
1d8663a3 56 my $out = tie *STDOUT, 'TieOut';
fafebdf5 57 eval $prog;
96b31d9e 58 my $ERR = $@;
1d8663a3 59 untie $out;
96b31d9e
JH
60 if ($ERR) {
61 ok(0, "$prog - $ERR");
62 } else {
f97a9a4b 63 if ($expected =~ m:^/:) {
1d8663a3 64 like($$out, $expected, $prog);
f97a9a4b 65 } else {
1d8663a3 66 is($$out, $expected, $prog);
f97a9a4b 67 }
96b31d9e 68 }
fafebdf5
A
69}
70
1d8663a3
JH
71package TieOut;
72
73sub TIEHANDLE {
74 bless( \(my $self), $_[0] );
75}
76
77sub PRINT {
78 my $self = shift;
79 $$self .= join('', @_);
80}
81
82sub read {
83 my $self = shift;
84 substr( $$self, 0, length($$self), '' );
85}
86
fafebdf5 87__END__
f97a9a4b
JH
88unctrl("A");
89EXPECT
90A
91########
92unctrl("\cA");
93EXPECT
94^A
95########
96uniescape("A");
97EXPECT
98A
99########
100uniescape("\x{100}");
101EXPECT
102\x{0100}
103########
104stringify(undef);
105EXPECT
106undef
107########
108stringify("foo");
109EXPECT
110'foo'
111########
112stringify("\cA");
113EXPECT
114"\cA"
115########
116stringify(*a);
117EXPECT
118*main::a
119########
120stringify(\undef);
121EXPECT
122/^'SCALAR\(0x[0-9a-f]+\)'$/i
123########
124stringify([]);
125EXPECT
126/^'ARRAY\(0x[0-9a-f]+\)'$/i
127########
128stringify({});
129EXPECT
130/^'HASH\(0x[0-9a-f]+\)'$/i
131########
132stringify(sub{});
133EXPECT
134/^'CODE\(0x[0-9a-f]+\)'$/i
135########
136stringify(\*a);
137EXPECT
138/^'GLOB\(0x[0-9a-f]+\)'$/i
139########
140stringify($foo);
141EXPECT
142/^'Foo=ARRAY\(0x[0-9a-f]+\)'$/i
143########
144stringify($bar);
145EXPECT
146/^'Bar=ARRAY\(0x[0-9a-f]+\)'$/i
147########
148dumpValue(undef);
149EXPECT
150undef
151########
fafebdf5
A
152dumpValue(1);
153EXPECT
1541
155########
f97a9a4b
JH
156dumpValue("\cA");
157EXPECT
158"\cA"
159########
160dumpValue("\x{100}");
161EXPECT
162'\x{0100}'
163########
fafebdf5
A
164dumpValue("1\n2\n3");
165EXPECT
166'1
1672
1683'
169########
170dumpValue([1..3],1);
171EXPECT
1720 1
1731 2
1742 3
175########
176dumpValue({1..4},1);
177EXPECT
1781 => 2
1793 => 4
180########
f97a9a4b
JH
181dumpValue($foo,1);
182EXPECT
1830 1
1841 2
1852 3
1863 4
1874 5
188########
189dumpValue($bar,1);
190EXPECT
1910 1
1921 2
1932 3
1943 4
1954 5
196########
9bcb75ad
JM
197dumpvalue("a");
198EXPECT
1990 'a'
200########
201dumpvalue("\cA");
202EXPECT
2030 "\cA"
204########
205dumpvalue("\x{100}");
206EXPECT
2070 '\x{0100}'
208########
209dumpvalue(undef);
210EXPECT
2110 undef
212########
213dumpvalue("foo");
214EXPECT
2150 'foo'
216########
217dumpvalue(\undef);
218EXPECT
219/0 SCALAR\(0x[0-9a-f]+\)\n -> undef\n/i
220########
221dumpvalue(\\undef);
222EXPECT
223/0 REF\(0x[0-9a-f]+\)\n -> SCALAR\(0x[0-9a-f]+\)\n -> undef\n/i
224########
225dumpvalue([]);
226EXPECT
227/0 ARRAY\(0x[0-9a-f]+\)\n empty array/i
228########
229dumpvalue({});
230EXPECT
231/0 HASH\(0x[0-9a-f]+\)\n\s+empty hash/i
232########
233dumpvalue(sub{});
234EXPECT
235/0 CODE\(0x[0-9a-f]+\)\n -> &CODE\(0x[0-9a-f]+\) in /i
236########
237dumpvalue(\*a);
238EXPECT
239/0 GLOB\(0x[0-9a-f]+\)\n -> \*main::a\n/i
240########
241dumpvalue($foo);
242EXPECT
243/0 Foo=ARRAY\(0x[0-9a-f]+\)\n 0 1\n 1 2\n 2 3\n 3 4\n 4 5\n/i
244########
245dumpvalue($bar);
246EXPECT
247/0 Bar=ARRAY\(0x[0-9a-f]+\)\n 0 1\n 1 2\n 2 3\n 3 4\n 4 5\n/i
248########
249dumpvalue("1\n2\n3")
250EXPECT
251/0 '1\n2\n3'\n/i
252########
253dumpvalue([1..4]);
254EXPECT
255/0 ARRAY\(0x[0-9a-f]+\)\n 0 1\n 1 2\n 2 3\n 3 4\n/i
256########
257dumpvalue({1..4});
258EXPECT
259/0 HASH\(0x[0-9a-f]+\)\n 1 => 2\n 3 => 4\n/i
260########
261dumpvalue({1=>2,3=>4});
262EXPECT
263/0 HASH\(0x[0-9a-f]+\)\n 1 => 2\n 3 => 4\n/i
264########
265dumpvalue({a=>1,b=>2});
266EXPECT
267/0 HASH\(0x[0-9a-f]+\)\n 'a' => 1\n 'b' => 2\n/i
268########
269dumpvalue([{a=>[1,2,3],b=>{c=>1,d=>2}},{e=>{f=>1,g=>2},h=>[qw(i j k)]}]);
270EXPECT
271/0 ARRAY\(0x[0-9a-f]+\)\n 0 HASH\(0x[0-9a-f]+\)\n 'a' => ARRAY\(0x[0-9a-f]+\)\n 0 1\n 1 2\n 2 3\n 'b' => HASH\(0x[0-9a-f]+\)\n 'c' => 1\n 'd' => 2\n 1 HASH\(0x[0-9a-f]+\)\n 'e' => HASH\(0x[0-9a-f]+\)\n 'f' => 1\n 'g' => 2\n 'h' => ARRAY\(0x[0-9a-f]+\)\n 0 'i'\n 1 'j'\n 2 'k'/i
272########
273dumpvalue({reverse map {$_=>1} sort qw(the quick brown fox)})
274EXPECT
275/0 HASH\(0x[0-9a-f]+\)\n 1 => 'brown'\n/i
276########
277my @x=qw(a b c); dumpvalue(\@x);
278EXPECT
279/0 ARRAY\(0x[0-9a-f]+\)\n 0 'a'\n 1 'b'\n 2 'c'\n/i
280########
281my %x=(a=>1, b=>2); dumpvalue(\%x);
282EXPECT
283/0 HASH\(0x[0-9a-f]+\)\n 'a' => 1\n 'b' => 2\n/i