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
|
use strict;
use warnings;
use Test::More tests => 12;
use Data::Printer::Config;
is_deeply(
Data::Printer::Config::_merge_options(undef, { foo => 42, bar => 27 }),
{ foo => 42, bar => 27 },
'merge undef and hash'
);
is_deeply(
Data::Printer::Config::_merge_options(undef,[ foo => 42, bar => 27 ]),
[ 'foo', 42, 'bar',27 ],
'merge undef and array'
);
is_deeply(
Data::Printer::Config::_merge_options({}, { foo => 42, bar => 27 }),
{ foo => 42, bar => 27 },
'merge hash and hash'
);
is_deeply(
Data::Printer::Config::_merge_options([],[ foo => 42, bar => 27 ]),
[ 'foo', 42, 'bar',27 ],
'merge array and array'
);
is_deeply(
Data::Printer::Config::_merge_options([], { foo => 42, bar => 27 }),
{ foo => 42, bar => 27 },
'merge array and hash'
);
is_deeply(
Data::Printer::Config::_merge_options({},[ foo => 42, bar => 27 ]),
[ 'foo', 42, 'bar',27 ],
'merge hash and array'
);
is_deeply(
Data::Printer::Config::_merge_options(
{ foo => 42, bar => 27 },
{ foo => 666 },
),
{ foo => 666, bar => 27 },
'merge two hashes'
);
is_deeply(
Data::Printer::Config::_merge_options(
{ foo => { bar => 42, baz => 27 } },
{ foo => { bar => 666 } },
),
{ foo => { bar => 666, baz => 27 } },
'merge two hashes with recursion'
);
my $old = { x => [1], foo => { bar => 42, baz => { a => 1, b => 2 } } };
my $new = { x => [9,8], bar => 10, foo => { meep => 1, baz => { b => 4, c => q(a) } } };
my $merged = Data::Printer::Config::_merge_options($old, $new);
is_deeply(
$merged,
{ x => [9,8], bar => 10, foo => { bar => 42, meep => 1, baz => { a => 1, b => 4, c => q(a) } } },
'merge two deep hash variables'
);
$merged->{foo}{baz} = undef; # <-- are we really a new value or a ref? let's check!
is_deeply(
$old,
{ x => [1], foo => { bar => 42, baz => { a => 1, b => 2 } } },
'old variable was not changed'
);
is_deeply(
$new,
{ x => [9,8], bar => 10, foo => { meep => 1, baz => { b => 4, c => q(a) } } },
'new variable was not changed'
);
is_deeply(
Data::Printer::Config::_merge_options(
{ foo => 1, bar => 2, baz => { meep => 666, moop => [444], bla => [3,2,1] } },
{ foo => 42, baz => { otherkey => 42, moop => [44,3] } }
),
{
foo => 42,
bar => 2,
baz => {
meep => 666,
moop => [44,3],
otherkey => 42,
bla => [3,2,1]
}
},
'merged complex data structures'
);
|