This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Cannot do perlio into scalars without perlio.
[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';
df14e5ac
JH
6 unless (find PerlIO::Layer 'perlio') { # no perlio, no PerlIO::scalar.
7 print "1..0 # Skip: not perlio\n";
8 exit 0;
9 }
fafebdf5
A
10}
11
12use strict;
13
14$|=1;
96b31d9e
JH
15
16my @prgs;
17{
18 local $/;
19 @prgs = split "########\n", <DATA>;
20 close DATA;
21}
22
23use Test::More;
24
25plan tests => scalar @prgs;
26
fafebdf5
A
27require "dumpvar.pl";
28
f97a9a4b
JH
29sub unctrl { print dumpvar::unctrl($_[0]), "\n" }
30sub uniescape { print dumpvar::uniescape($_[0]), "\n" }
31sub stringify { print dumpvar::stringify($_[0]), "\n" }
32
33package Foo;
34
35sub new { my $class = shift; bless [ @_ ], $class }
36
37package Bar;
38
39sub new { my $class = shift; bless [ @_ ], $class }
40
41use overload '""' => sub { "Bar<@{$_[0]}>" };
42
43package main;
44
45my $foo = Foo->new(1..5);
46my $bar = Bar->new(1..5);
47
96b31d9e
JH
48for (@prgs) {
49 my($prog, $expected) = split(/\nEXPECT\n?/, $_);
f97a9a4b
JH
50 # TODO: dumpvar::stringify() is controlled by a pile of package
51 # dumpvar variables: $printUndef, $unctrl, $quoteHighBit, $bareStringify,
52 # and so forth. We need to test with various settings of those.
1d8663a3 53 my $out = tie *STDOUT, 'TieOut';
fafebdf5 54 eval $prog;
96b31d9e 55 my $ERR = $@;
1d8663a3 56 untie $out;
96b31d9e
JH
57 if ($ERR) {
58 ok(0, "$prog - $ERR");
59 } else {
f97a9a4b 60 if ($expected =~ m:^/:) {
1d8663a3 61 like($$out, $expected, $prog);
f97a9a4b 62 } else {
1d8663a3 63 is($$out, $expected, $prog);
f97a9a4b 64 }
96b31d9e 65 }
fafebdf5
A
66}
67
1d8663a3
JH
68package TieOut;
69
70sub TIEHANDLE {
71 bless( \(my $self), $_[0] );
72}
73
74sub PRINT {
75 my $self = shift;
76 $$self .= join('', @_);
77}
78
79sub read {
80 my $self = shift;
81 substr( $$self, 0, length($$self), '' );
82}
83
fafebdf5 84__END__
f97a9a4b
JH
85unctrl("A");
86EXPECT
87A
88########
89unctrl("\cA");
90EXPECT
91^A
92########
93uniescape("A");
94EXPECT
95A
96########
97uniescape("\x{100}");
98EXPECT
99\x{0100}
100########
101stringify(undef);
102EXPECT
103undef
104########
105stringify("foo");
106EXPECT
107'foo'
108########
109stringify("\cA");
110EXPECT
111"\cA"
112########
113stringify(*a);
114EXPECT
115*main::a
116########
117stringify(\undef);
118EXPECT
119/^'SCALAR\(0x[0-9a-f]+\)'$/i
120########
121stringify([]);
122EXPECT
123/^'ARRAY\(0x[0-9a-f]+\)'$/i
124########
125stringify({});
126EXPECT
127/^'HASH\(0x[0-9a-f]+\)'$/i
128########
129stringify(sub{});
130EXPECT
131/^'CODE\(0x[0-9a-f]+\)'$/i
132########
133stringify(\*a);
134EXPECT
135/^'GLOB\(0x[0-9a-f]+\)'$/i
136########
137stringify($foo);
138EXPECT
139/^'Foo=ARRAY\(0x[0-9a-f]+\)'$/i
140########
141stringify($bar);
142EXPECT
143/^'Bar=ARRAY\(0x[0-9a-f]+\)'$/i
144########
145dumpValue(undef);
146EXPECT
147undef
148########
fafebdf5
A
149dumpValue(1);
150EXPECT
1511
152########
f97a9a4b
JH
153dumpValue("\cA");
154EXPECT
155"\cA"
156########
157dumpValue("\x{100}");
158EXPECT
159'\x{0100}'
160########
fafebdf5
A
161dumpValue("1\n2\n3");
162EXPECT
163'1
1642
1653'
166########
167dumpValue([1..3],1);
168EXPECT
1690 1
1701 2
1712 3
172########
173dumpValue({1..4},1);
174EXPECT
1751 => 2
1763 => 4
177########
f97a9a4b
JH
178dumpValue($foo,1);
179EXPECT
1800 1
1811 2
1822 3
1833 4
1844 5
185########
186dumpValue($bar,1);
187EXPECT
1880 1
1891 2
1902 3
1913 4
1924 5
193########