summaryrefslogtreecommitdiff
path: root/dist/Data-Dumper/t/names.t
blob: 782f1cb768c5f53e5d6fc43ab80ddfce927e8399 (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
#!./perl -w

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 Carp;
use Data::Dumper;
use Test::More tests => 15;
use lib qw( ./t/lib );
use Testing qw( _dumptostr );

my ($a, $b, $obj);
my (@names);
my (@newnames, $objagain, %newnames);
my $dumpstr;
$a = 'alpha';
$b = 'beta';

$obj = Data::Dumper->new([$a,$b], [qw(a b)]);
@names = $obj->Names;
is_deeply(\@names, [qw(a b)], "Names() returned expected list");

@newnames = ( qw| gamma delta | );
$objagain = $obj->Names(\@newnames);
is($objagain, $obj, "Names returned same object");
is_deeply($objagain->{names}, \@newnames,
    "Able to use Names() to set names to be dumped");

$obj = Data::Dumper->new([$a,$b], [qw(a b)]);
%newnames = ( gamma => 'delta', epsilon => 'zeta' );
eval { @names = $obj->Names(\%newnames); };
like($@, qr/Argument to Names, if provided, must be array ref/,
    "Got expected error message: bad argument to Names()");

$obj = Data::Dumper->new([$a,$b], [qw(a b)]);
@newnames = ( qw| gamma delta epsilon | );
$objagain = $obj->Names(\@newnames);
is($objagain, $obj, "Names returned same object");
is_deeply($objagain->{names}, \@newnames,
    "Able to use Names() to set names to be dumped");
$dumpstr = _dumptostr($obj);
like($dumpstr, qr/gamma/s, "Got first name expected");
like($dumpstr, qr/delta/s, "Got first name expected");
unlike($dumpstr, qr/epsilon/s, "Did not get name which was not expected");

$obj = Data::Dumper->new([$a,$b], [qw(a b)]);
@newnames = ( qw| gamma | );
$objagain = $obj->Names(\@newnames);
is($objagain, $obj, "Names returned same object");
is_deeply($objagain->{names}, \@newnames,
    "Able to use Names() to set names to be dumped");
$dumpstr = _dumptostr($obj);
like($dumpstr, qr/gamma/s, "Got name expected");
unlike($dumpstr, qr/delta/s, "Did not get name which was not expected");
unlike($dumpstr, qr/epsilon/s, "Did not get name which was not expected");
like($dumpstr, qr/\$VAR2/s, "Got default name");