summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/perl/dist/Data-Dumper/t/misc.t
blob: 54a89e6dbcc7ec88835f116bb1e6fb2acf3db913 (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
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
188
189
190
191
192
193
194
195
#!./perl -w
# t/misc.t - Test various functionality

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 => 18;
use lib qw( ./t/lib );
use Testing qw( _dumptostr );

my ($a, $b, @c, %d);
$a = 'alpha';
$b = 'beta';
@c = ( qw| gamma delta epsilon | );
%d = ( zeta => 'eta', theta => 'iota' );

note("Argument validation for new()");
{
    local $@ = '';
    eval { my $obj = Data::Dumper->new(undef); };
    like($@,
        qr/^Usage:\s+PACKAGE->new\(ARRAYREF,\s*\[ARRAYREF\]\)/,
        "Got error message: new() needs defined argument"
    );
}

{
    local $@ = '';
    eval { my $obj = Data::Dumper->new( { $a => $b } ); };
    like($@,
        qr/^Usage:\s+PACKAGE->new\(ARRAYREF,\s*\[ARRAYREF\]\)/,
        "Got error message: new() needs array reference"
    );
}

{
    note("\$Data::Dumper::Useperl, Useqq, Deparse");
    my ($obj, %dumpstr);

    local $Data::Dumper::Useperl = 1;
    $obj = Data::Dumper->new( [ \@c, \%d ] );
    $dumpstr{useperl} = [ $obj->Values ];
    local $Data::Dumper::Useperl = 0;

    local $Data::Dumper::Useqq = 1;
    $obj = Data::Dumper->new( [ \@c, \%d ] );
    $dumpstr{useqq} = [ $obj->Values ];
    local $Data::Dumper::Useqq = 0;

    is_deeply($dumpstr{useperl}, $dumpstr{useqq},
        "Useperl and Useqq return same");

    local $Data::Dumper::Deparse = 1;
    $obj = Data::Dumper->new( [ \@c, \%d ] );
    $dumpstr{deparse} = [ $obj->Values ];
    local $Data::Dumper::Deparse = 0;

    is_deeply($dumpstr{useperl}, $dumpstr{deparse},
        "Useperl and Deparse return same");
}

{
    note("\$Data::Dumper::Pad and \$obj->Pad");
    my ($obj, %dumps, $pad);
    $obj = Data::Dumper->new([$a,$b]);
    $dumps{'noprev'} = _dumptostr($obj);

    $obj = Data::Dumper->new([$a,$b]);
    $obj->Pad('');
    $dumps{'emptystring'} = _dumptostr($obj);

    is($dumps{'noprev'}, $dumps{'emptystring'},
        "No setting for \$Data::Dumper::Pad and Pad('') give same result");

    $pad = 'XXX: ';
    local $Data::Dumper::Pad = $pad;
    $obj = Data::Dumper->new([$a,$b]);
    $dumps{'ddp'} = _dumptostr($obj);
    local $Data::Dumper::Pad = '';

    $obj = Data::Dumper->new([$a,$b]);
    $obj->Pad($pad);
    $dumps{'obj'} = _dumptostr($obj);

    is($dumps{'ddp'}, $dumps{'obj'},
        "\$Data::Dumper::Pad and \$obj->Pad() give same result");

    is( (grep {! /^$pad/} (split(/\n/, $dumps{'ddp'}))), 0,
        "Each line of dumped output padded as expected");
}

{
    note("\$Data::Dumper::Varname and \$obj->Varname");
    my ($obj, %dumps, $varname);
    $obj = Data::Dumper->new([$a,$b]);
    $dumps{'noprev'} = _dumptostr($obj);

    $obj = Data::Dumper->new([$a,$b]);
    $obj->Varname('');
    $dumps{'emptystring'} = _dumptostr($obj);

    # Because Varname defaults to '$VAR', providing an empty argument to
    # Varname produces a non-default result.
    isnt($dumps{'noprev'}, $dumps{'emptystring'},
        "No setting for \$Data::Dumper::Varname and Varname('') give different results");

    $varname = 'MIMI';
    local $Data::Dumper::Varname = $varname;
    $obj = Data::Dumper->new([$a,$b]);
    $dumps{'ddv'} = _dumptostr($obj);
    local $Data::Dumper::Varname = undef;

    $obj = Data::Dumper->new([$a,$b]);
    $obj->Varname($varname);
    $dumps{'varname'} = _dumptostr($obj);

    is($dumps{'ddv'}, $dumps{'varname'},
        "Setting for \$Data::Dumper::Varname and Varname() give same result");

    is( (grep { /^\$$varname/ } (split(/\n/, $dumps{'ddv'}))), 2,
        "All lines of dumped output use provided varname");

    is( (grep { /^\$VAR/ } (split(/\n/, $dumps{'ddv'}))), 0,
        "No lines of dumped output use default \$VAR");
}

{
    note("\$Data::Dumper::Useqq and \$obj->Useqq");
    my ($obj, %dumps, $useqq);
    $obj = Data::Dumper->new([$a,$b]);
    $dumps{'noprev'} = _dumptostr($obj);

    $obj = Data::Dumper->new([$a,$b]);
    $obj->Useqq(undef);
    $dumps{'undef'} = _dumptostr($obj);

    $obj = Data::Dumper->new([$a,$b]);
    $obj->Useqq('');
    $dumps{'emptystring'} = _dumptostr($obj);

    $obj = Data::Dumper->new([$a,$b]);
    $obj->Useqq(0);
    $dumps{'zero'} = _dumptostr($obj);

    my $current = $Data::Dumper::Useqq;
    local $Data::Dumper::Useqq = 0;
    $obj = Data::Dumper->new([$a,$b]);
    $dumps{'dduzero'} = _dumptostr($obj);
    local $Data::Dumper::Useqq = $current;

    is($dumps{'noprev'}, $dumps{'undef'},
        "No setting for \$Data::Dumper::Useqq and Useqq(undef) give same result");

    is($dumps{'noprev'}, $dumps{'zero'},
        "No setting for \$Data::Dumper::Useqq and Useqq(0) give same result");

    is($dumps{'noprev'}, $dumps{'emptystring'},
        "No setting for \$Data::Dumper::Useqq and Useqq('') give same result");

    is($dumps{'noprev'}, $dumps{'dduzero'},
        "No setting for \$Data::Dumper::Useqq and Useqq(undef) give same result");

    local $Data::Dumper::Useqq = 1;
    $obj = Data::Dumper->new([$a,$b]);
    $dumps{'ddu'} = _dumptostr($obj);
    local $Data::Dumper::Useqq = $current;

    $obj = Data::Dumper->new([$a,$b]);
    $obj->Useqq(1);
    $dumps{'obj'} = _dumptostr($obj);

    is($dumps{'ddu'}, $dumps{'obj'},
        "\$Data::Dumper::Useqq=1 and Useqq(1) give same result");

    like($dumps{'ddu'},
        qr/"$a".+?"$b"/s,
        "Double-quotes used around values"
    );

    unlike($dumps{'ddu'},
        qr/'$a'.+?'$b'/s,
        "Single-quotes not used around values"
    );
}