xref: /openbsd/gnu/usr.bin/perl/dist/Data-Dumper/t/pair.t (revision 771fbea0)
1#!./perl -w
2#
3# test for $Data::Dumper::Pair AKA Data::Dumper->new([ ... ])->Pair('...')
4#
5
6BEGIN {
7    if ($ENV{PERL_CORE}){
8        require Config; import Config;
9        no warnings 'once';
10        if ($Config{'extensions'} !~ /\bData\/Dumper\b/) {
11            print "1..0 # Skip: Data::Dumper was not built\n";
12            exit 0;
13        }
14    }
15}
16
17use strict;
18our ($want_colon, $want_comma);
19use Test::More tests => 9;
20
21no warnings qw(once);
22
23require_ok 'Data::Dumper';
24
25my $HASH = { alpha => 'beta', gamma => 'vlissides' };
26my $WANT = q({'alpha' => 'beta','gamma' => 'vlissides'});
27
28$Data::Dumper::Useperl = 1;
29$Data::Dumper::Indent = 0;
30$Data::Dumper::Terse = 1;
31$Data::Dumper::Sortkeys = 1;
32
33$want_colon = $want_comma = $WANT;
34$want_colon =~ s/=>/:/g;
35$want_comma =~ s/ => /,/g;
36
37####################### XS Tests #####################
38
39SKIP: {
40    skip 'XS extension not loaded', 3 unless (defined &Data::Dumper::Dumpxs);
41    is (Data::Dumper::DumperX($HASH), $WANT,
42	'XS: Default hash key/value separator: " => "');
43    local $Data::Dumper::Pair = ' : ';
44    is (Data::Dumper::DumperX($HASH), $want_colon, 'XS: $Data::Dumper::Pair = " : "');
45    my $dd = Data::Dumper->new([ $HASH ])->Pair(',');
46    is ($dd->Dumpxs(), $want_comma,
47	'XS: Data::Dumper->new([ $HASH ])->Pair(",")->Dumpxs()');
48};
49
50###################### Perl Tests ####################
51
52{
53    is ($Data::Dumper::Pair, ' => ', 'Perl: $Data::Dumper::Pair eq " => "');
54    is (Data::Dumper::Dumper($HASH), $WANT,
55	'Perl: Default hash key/value separator: " => "');
56    local $Data::Dumper::Pair = ' : ';
57    is (Data::Dumper::Dumper($HASH), $want_colon, 'Perl: $Data::Dumper::Pair = " : "');
58    my $dd = Data::Dumper->new([ $HASH ])->Pair(',');
59    is ($dd->Pair(), ',',
60	'Perl: Data::Dumper->new([ $HASH ])->Pair(",")->Pair() eq ","');
61    is ($dd->Dump(), $want_comma, 'Perl: Data::Dumper->new([ $HASH ])->Pair(",")->Dump()');
62}
63