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