This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
correct detection of arg absence 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 => 9;
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(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
56 is($dumpstr{noindent}, $dumpstr{indent_no_arg},
57     "absence of Indent is same as Indent()");
58 isnt($dumpstr{noindent}, $dumpstr{indent_0},
59     "absence of Indent is different from Indent(0)");
60 isnt($dumpstr{indent_0}, $dumpstr{indent_1},
61     "Indent(0) is different from Indent(1)");
62 cmp_ok(length($dumpstr{indent_0}), '<=', length($dumpstr{indent_1}),
63     "Indent(0) is more compact than Indent(1)");
64 is($dumpstr{noindent}, $dumpstr{indent_2},
65     "absence of Indent is same as Indent(2), i.e., 2 is default");
66 cmp_ok(length($dumpstr{indent_1}), '<=', length($dumpstr{indent_2}),
67     "Indent(1) is more compact than Indent(2)");
68
69 my $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
88 isnt($dumpstr{ar_indent_2}, $dumpstr{ar_indent_3},
89     "On arrays, Indent(2) is different from Indent(3)");
90 like($dumpstr{ar_indent_3},
91     qr/\#0.+'foo'.+\#1.+42/s,
92     "Indent(3) annotates array elements with their indices"
93 );
94 {
95     no if $] < 5.011, warnings => 'deprecated';
96     is(scalar(split("\n" => $dumpstr{ar_indent_2})) + 2,
97         scalar(split("\n" => $dumpstr{ar_indent_3})),
98         "Indent(3) runs 2 lines longer than Indent(2)");
99 }
100
101 __END__
102 is($dumpstr{noindent}, $dumpstr{indent_0},
103     "absence of Indent is same as Indent(0)");
104 isnt($dumpstr{noindent}, $dumpstr{indent_1},
105     "absence of Indent is different from Indent(1)");
106 print STDERR $dumpstr{indent_0};
107 print STDERR $dumpstr{ar_indent_3};