2 # t/indent.t - Test Indent()
5 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bData\/Dumper\b/) {
8 print "1..0 # Skip: Data::Dumper was not built\n";
17 use Test::More tests => 10;
18 use lib qw( ./t/lib );
19 use Testing qw( _dumptostr );
22 my $hash = { foo => 42 };
27 $dumper = Data::Dumper->new([$hash]);
28 $dumpstr{noindent} = _dumptostr($dumper);
33 $dumper = Data::Dumper->new([$hash]);
35 $dumpstr{indent_no_arg} = _dumptostr($dumper);
37 $dumper = Data::Dumper->new([$hash]);
38 $dumper->Indent(undef);
39 $dumpstr{indent_undef} = _dumptostr($dumper);
41 $dumper = Data::Dumper->new([$hash]);
43 $dumpstr{indent_0} = _dumptostr($dumper);
44 # $VAR1 = {'foo' => 42}; # no newline
46 $dumper = Data::Dumper->new([$hash]);
48 $dumpstr{indent_1} = _dumptostr($dumper);
53 $dumper = Data::Dumper->new([$hash]);
55 $dumpstr{indent_2} = _dumptostr($dumper);
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)");
75 my $array = [ qw| foo 42 | ];
76 $dumper = Data::Dumper->new([$array]);
78 $dumpstr{ar_indent_2} = _dumptostr($dumper);
84 $dumper = Data::Dumper->new([$array]);
86 $dumpstr{ar_indent_3} = _dumptostr($dumper);
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"
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)");
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};