This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
don't recurse infinitely in Data::Dumper
[perl5.git] / dist / Data-Dumper / t / indent.t
1 #!./perl -w
2 # t/indent.t - Test Indent()
3 BEGIN {
4     if ($ENV{PERL_CORE}){
5         require Config; import Config;
6         no warnings 'once';
7         if ($Config{'extensions'} !~ /\bData\/Dumper\b/) {
8             print "1..0 # Skip: Data::Dumper was not built\n";
9             exit 0;
10         }
11     }
12 }
13
14 use strict;
15
16 use Data::Dumper;
17 use Test::More tests => 10;
18 use lib qw( ./t/lib );
19 use Testing qw( _dumptostr );
20
21
22 my $hash = { foo => 42 };
23
24 my (%dumpstr);
25 my $dumper;
26
27 $dumper = Data::Dumper->new([$hash]);
28 $dumpstr{noindent} = _dumptostr($dumper);
29 # $VAR1 = {
30 #           'foo' => 42
31 #         };
32
33 $dumper = Data::Dumper->new([$hash]);
34 $dumper->Indent();
35 $dumpstr{indent_no_arg} = _dumptostr($dumper);
36
37 $dumper = Data::Dumper->new([$hash]);
38 $dumper->Indent(undef);
39 $dumpstr{indent_undef} = _dumptostr($dumper);
40
41 $dumper = Data::Dumper->new([$hash]);
42 $dumper->Indent(0);
43 $dumpstr{indent_0} = _dumptostr($dumper);
44 # $VAR1 = {'foo' => 42}; # no newline
45
46 $dumper = Data::Dumper->new([$hash]);
47 $dumper->Indent(1);
48 $dumpstr{indent_1} = _dumptostr($dumper);
49 # $VAR1 = {
50 #   'foo' => 42
51 # };
52
53 $dumper = Data::Dumper->new([$hash]);
54 $dumper->Indent(2);
55 $dumpstr{indent_2} = _dumptostr($dumper);
56 # $VAR1 = {
57 #           'foo' => 42
58 #         };
59
60 is($dumpstr{noindent}, $dumpstr{indent_no_arg},
61     "absence of Indent is same as Indent()");
62 is($dumpstr{noindent}, $dumpstr{indent_undef},
63     "absence of Indent is same as Indent(undef)");
64 isnt($dumpstr{noindent}, $dumpstr{indent_0},
65     "absence of Indent is different from Indent(0)");
66 isnt($dumpstr{indent_0}, $dumpstr{indent_1},
67     "Indent(0) is different from Indent(1)");
68 cmp_ok(length($dumpstr{indent_0}), '<=', length($dumpstr{indent_1}),
69     "Indent(0) is more compact than Indent(1)");
70 is($dumpstr{noindent}, $dumpstr{indent_2},
71     "absence of Indent is same as Indent(2), i.e., 2 is default");
72 cmp_ok(length($dumpstr{indent_1}), '<=', length($dumpstr{indent_2}),
73     "Indent(1) is more compact than Indent(2)");
74
75 my $array = [ qw| foo 42 | ];
76 $dumper = Data::Dumper->new([$array]);
77 $dumper->Indent(2);
78 $dumpstr{ar_indent_2} = _dumptostr($dumper);
79 # $VAR1 = [
80 #           'foo',
81 #           '42'
82 #         ];
83
84 $dumper = Data::Dumper->new([$array]);
85 $dumper->Indent(3);
86 $dumpstr{ar_indent_3} = _dumptostr($dumper);
87 # $VAR1 = [
88 #           #0
89 #           'foo',
90 #           #1
91 #           '42'
92 #         ];
93
94 isnt($dumpstr{ar_indent_2}, $dumpstr{ar_indent_3},
95     "On arrays, Indent(2) is different from Indent(3)");
96 like($dumpstr{ar_indent_3},
97     qr/\#0.+'foo'.+\#1.+42/s,
98     "Indent(3) annotates array elements with their indices"
99 );
100 {
101     no if $] < 5.011, warnings => 'deprecated';
102     is(scalar(split("\n" => $dumpstr{ar_indent_2})) + 2,
103         scalar(split("\n" => $dumpstr{ar_indent_3})),
104         "Indent(3) runs 2 lines longer than Indent(2)");
105 }
106
107 __END__
108 is($dumpstr{noindent}, $dumpstr{indent_0},
109     "absence of Indent is same as Indent(0)");
110 isnt($dumpstr{noindent}, $dumpstr{indent_1},
111     "absence of Indent is different from Indent(1)");
112 print STDERR $dumpstr{indent_0};
113 print STDERR $dumpstr{ar_indent_3};