1#!./perl -w
2# t/indent.t - Test Indent()
3BEGIN {
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
14use strict;
15
16use Data::Dumper;
17use Test::More tests => 9;
18use lib qw( ./t/lib );
19use Testing qw( _dumptostr );
20
21
22my $hash = { foo => 42 };
23
24my (%dumpstr);
25my $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(0);
39$dumpstr{indent_0} = _dumptostr($dumper);
40# $VAR1 = {'foo' => 42}; # no newline
41
42$dumper = Data::Dumper->new([$hash]);
43$dumper->Indent(1);
44$dumpstr{indent_1} = _dumptostr($dumper);
45# $VAR1 = {
46#   'foo' => 42
47# };
48
49$dumper = Data::Dumper->new([$hash]);
50$dumper->Indent(2);
51$dumpstr{indent_2} = _dumptostr($dumper);
52# $VAR1 = {
53#           'foo' => 42
54#         };
55
56is($dumpstr{noindent}, $dumpstr{indent_no_arg},
57    "absence of Indent is same as Indent()");
58isnt($dumpstr{noindent}, $dumpstr{indent_0},
59    "absence of Indent is different from Indent(0)");
60isnt($dumpstr{indent_0}, $dumpstr{indent_1},
61    "Indent(0) is different from Indent(1)");
62cmp_ok(length($dumpstr{indent_0}), '<=', length($dumpstr{indent_1}),
63    "Indent(0) is more compact than Indent(1)");
64is($dumpstr{noindent}, $dumpstr{indent_2},
65    "absence of Indent is same as Indent(2), i.e., 2 is default");
66cmp_ok(length($dumpstr{indent_1}), '<=', length($dumpstr{indent_2}),
67    "Indent(1) is more compact than Indent(2)");
68
69my $array = [ qw| foo 42 | ];
70$dumper = Data::Dumper->new([$array]);
71$dumper->Indent(2);
72$dumpstr{ar_indent_2} = _dumptostr($dumper);
73# $VAR1 = [
74#           'foo',
75#           '42'
76#         ];
77
78$dumper = Data::Dumper->new([$array]);
79$dumper->Indent(3);
80$dumpstr{ar_indent_3} = _dumptostr($dumper);
81# $VAR1 = [
82#           #0
83#           'foo',
84#           #1
85#           '42'
86#         ];
87
88isnt($dumpstr{ar_indent_2}, $dumpstr{ar_indent_3},
89    "On arrays, Indent(2) is different from Indent(3)");
90like($dumpstr{ar_indent_3},
91    qr/\#0.+'foo'.+\#1.+42/s,
92    "Indent(3) annotates array elements with their indices"
93);
94sub count_newlines { scalar $_[0] =~ tr/\n// }
95{
96    no if $] < 5.011, warnings => 'deprecated';
97    is(count_newlines($dumpstr{ar_indent_2}) + 2,
98        count_newlines($dumpstr{ar_indent_3}),
99        "Indent(3) runs 2 lines longer than Indent(2)");
100}
101
102__END__
103is($dumpstr{noindent}, $dumpstr{indent_0},
104    "absence of Indent is same as Indent(0)");
105isnt($dumpstr{noindent}, $dumpstr{indent_1},
106    "absence of Indent is different from Indent(1)");
107print STDERR $dumpstr{indent_0};
108print STDERR $dumpstr{ar_indent_3};
109