File: 010-hashes.t

package info (click to toggle)
libdata-printer-perl 1.002001-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 748 kB
  • sloc: perl: 4,364; makefile: 7; sh: 1
file content (187 lines) | stat: -rw-r--r-- 4,137 bytes parent folder | download | duplicates (3)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use strict;
use warnings;
use Test::More tests => 16;
use Data::Printer::Object;

my $ddp = Data::Printer::Object->new( colored => 0 );
my %hash = ();
is($ddp->parse(\%hash), '{}', 'empty hash');
undef %hash;
$ddp = Data::Printer::Object->new( colored => 0 );
is($ddp->parse(\%hash), '{}', 'undefined hash');

# the "%hash = 1" code below is wrong and issues
# an "odd number of elements in hash assignment"
# warning message. But since it's just a warning
# (meaning the code will still run even under strictness)
# we make sure to test everything will be alright.
{
    no warnings 'misc';
    %hash = 1;
}
$ddp = Data::Printer::Object->new( colored => 0 );
is( $ddp->parse(\%hash),
'{
    1   undef
}', 'evil hash of doom');

%hash = ( foo => 33, bar => 99 );
$ddp = Data::Printer::Object->new( colored => 0 );
is( $ddp->parse(\%hash),
'{
    bar   99,
    foo   33
}', 'simple hash');

$ddp = Data::Printer::Object->new( colored => 0, hash_separator => ': ' );
is( $ddp->parse(\%hash),
'{
    bar: 99,
    foo: 33
}', 'simple hash with custom separator');

$ddp = Data::Printer::Object->new( colored => 0 );
my $scalar = 4.2;
$hash{$scalar} = \$scalar;
$hash{hash} = { 1 => 2, 3 => { 4 => 5 }, 10 => 11 };
$hash{something} = [ 3 .. 5 ];
$hash{zelda} = 'moo';

is( $ddp->parse(\%hash),
'{
    4.2         \\ 4.2,
    bar         99,
    foo         33,
    hash        {
        1    2,
        3    {
            4   5
        },
        10   11
    },
    something   [
        [0] 3,
        [1] 4,
        [2] 5
    ],
    zelda       "moo"
}', 'nested hash');

$ddp = Data::Printer::Object->new( colored => 0, align_hash => 0 );
is( $ddp->parse(\%hash),
'{
    4.2   \\ 4.2,
    bar   99,
    foo   33,
    hash   {
        1   2,
        3   {
            4   5
        },
        10   11
    },
    something   [
        [0] 3,
        [1] 4,
        [2] 5
    ],
    zelda   "moo"
}', 'nested hash, unaligned');

$ddp = Data::Printer::Object->new( colored => 0 );
my $hash_ref = { c => 3 };
%hash = ( a => 1, b => \$hash_ref, d => 4 );
is( $ddp->parse(\%hash),
'{
    a   1,
    b   \\ {
        c   3
    },
    d   4
}', 'reference of a hash reference');

$ddp = Data::Printer::Object->new( colored => 0 );
is( $ddp->parse(\\$hash_ref),
'\\ {
    c   3
}', 'simple ref to hash ref' );

%hash = ( 'undef' => undef, foo => { 'meep' => undef }, zed => 26 );
$ddp = Data::Printer::Object->new( colored => 0 );
is( $ddp->parse(\%hash),
'{
    foo     {
        meep   undef
    },
    undef   undef,
    zed     26
}', 'hash with undefs' );

$ddp = Data::Printer::Object->new( colored => 0, hash_max => 6 );
my $i = 10;
%hash = map { $_ => $i++ } split //, 'abcdefghijklmnopqrstuvwxyz';
is($ddp->parse(\%hash),
'{
    a   10,
    b   11,
    c   12,
    d   13,
    e   14,
    f   15,
    (...skipping 20 keys...)
}', 'hash_max reached');

$ddp = Data::Printer::Object->new( colored => 0, hash_max => 6, hash_preserve => 'begin' );
is($ddp->parse(\%hash),
'{
    a   10,
    b   11,
    c   12,
    d   13,
    e   14,
    f   15,
    (...skipping 20 keys...)
}', 'hash_max reached, preserve begin is the default');

$ddp = Data::Printer::Object->new( colored => 0, hash_max => 6, hash_preserve => 'end' );
is($ddp->parse(\%hash),
'{
    (...skipping 20 keys...)
    u   30,
    v   31,
    w   32,
    x   33,
    y   34,
    z   35
}', 'hash_max reached, preserving end');

$ddp = Data::Printer::Object->new( colored => 0, hash_max => 6, hash_preserve => 'middle' );
is($ddp->parse(\%hash),
'{
    (...skipping 9 keys...)
    j   19,
    k   20,
    l   21,
    m   22,
    n   23,
    o   24,
    (...skipping 11 keys...)
}', 'hash_max reached, preserving middle');

$ddp = Data::Printer::Object->new( colored => 0, hash_max => 6, hash_preserve => 'extremes' );
is($ddp->parse(\%hash),
'{
    a   10,
    b   11,
    c   12,
    (...skipping 20 keys...)
    x   33,
    y   34,
    z   35
}', 'hash_max reached, preserving extremes');

$ddp = Data::Printer::Object->new( colored => 0, hash_max => 6, hash_preserve => 'none' );
is($ddp->parse(\%hash),
'{
    (...skipping 26 keys...)
}', 'hash_max reached, preserving none');