summaryrefslogtreecommitdiff
path: root/dist/Data-Dumper/t/indent.t
blob: 2814f0b21539ad8f1e42464c658a5895a662cb92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!./perl -w
# t/indent.t - Test Indent()
BEGIN {
    if ($ENV{PERL_CORE}){
        require Config; import Config;
        no warnings 'once';
        if ($Config{'extensions'} !~ /\bData\/Dumper\b/) {
            print "1..0 # Skip: Data::Dumper was not built\n";
            exit 0;
        }
    }
}

use strict;

use Data::Dumper;
use Test::More tests => 9;
use lib qw( ./t/lib );
use Testing qw( _dumptostr );


my $hash = { foo => 42 };

my (%dumpstr);
my $dumper;

$dumper = Data::Dumper->new([$hash]);
$dumpstr{noindent} = _dumptostr($dumper);
# $VAR1 = {
#           'foo' => 42
#         };

$dumper = Data::Dumper->new([$hash]);
$dumper->Indent();
$dumpstr{indent_no_arg} = _dumptostr($dumper);

$dumper = Data::Dumper->new([$hash]);
$dumper->Indent(0);
$dumpstr{indent_0} = _dumptostr($dumper);
# $VAR1 = {'foo' => 42}; # no newline

$dumper = Data::Dumper->new([$hash]);
$dumper->Indent(1);
$dumpstr{indent_1} = _dumptostr($dumper);
# $VAR1 = {
#   'foo' => 42
# };

$dumper = Data::Dumper->new([$hash]);
$dumper->Indent(2);
$dumpstr{indent_2} = _dumptostr($dumper);
# $VAR1 = {
#           'foo' => 42
#         };

is($dumpstr{noindent}, $dumpstr{indent_no_arg},
    "absence of Indent is same as Indent()");
isnt($dumpstr{noindent}, $dumpstr{indent_0},
    "absence of Indent is different from Indent(0)");
isnt($dumpstr{indent_0}, $dumpstr{indent_1},
    "Indent(0) is different from Indent(1)");
cmp_ok(length($dumpstr{indent_0}), '<=', length($dumpstr{indent_1}),
    "Indent(0) is more compact than Indent(1)");
is($dumpstr{noindent}, $dumpstr{indent_2},
    "absence of Indent is same as Indent(2), i.e., 2 is default");
cmp_ok(length($dumpstr{indent_1}), '<=', length($dumpstr{indent_2}),
    "Indent(1) is more compact than Indent(2)");

my $array = [ qw| foo 42 | ];
$dumper = Data::Dumper->new([$array]);
$dumper->Indent(2);
$dumpstr{ar_indent_2} = _dumptostr($dumper);
# $VAR1 = [
#           'foo',
#           '42'
#         ];

$dumper = Data::Dumper->new([$array]);
$dumper->Indent(3);
$dumpstr{ar_indent_3} = _dumptostr($dumper);
# $VAR1 = [
#           #0
#           'foo',
#           #1
#           '42'
#         ];

isnt($dumpstr{ar_indent_2}, $dumpstr{ar_indent_3},
    "On arrays, Indent(2) is different from Indent(3)");
like($dumpstr{ar_indent_3},
    qr/\#0.+'foo'.+\#1.+42/s,
    "Indent(3) annotates array elements with their indices"
);
{
    no if $] < 5.011, warnings => 'deprecated';
    is(scalar(split("\n" => $dumpstr{ar_indent_2})) + 2,
        scalar(split("\n" => $dumpstr{ar_indent_3})),
        "Indent(3) runs 2 lines longer than Indent(2)");
}

__END__
is($dumpstr{noindent}, $dumpstr{indent_0},
    "absence of Indent is same as Indent(0)");
isnt($dumpstr{noindent}, $dumpstr{indent_1},
    "absence of Indent is different from Indent(1)");
print STDERR $dumpstr{indent_0};
print STDERR $dumpstr{ar_indent_3};