File: merge-hash.t

package info (click to toggle)
libtext-mediawikiformat-perl 1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 236 kB
  • ctags: 46
  • sloc: perl: 1,848; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,494 bytes parent folder | download
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
#!perl

BEGIN { chdir 't' if -d 't' }

use strict;
use warnings;

use Test::More tests => 9;
use Test::NoWarnings;

use_ok( 'Text::MediawikiFormat' ) or exit;

my $full       = { foo => { bar => 'baz' } };
my $empty      = {};
my $nonempty   = { foo => { a => 'b' } };
my $full_flat  = { a => 'b' };
my $empty_flat = {};
my $zero       = { foo => 0, bar => { baz => 0 } };

$nonempty = Text::MediawikiFormat::_merge_hashes ($full, $nonempty);
is_deeply $nonempty, {foo => {a => 'b', bar => 'baz'}},
	  "merge should work when all keys in from exist in to";
$full->{foo}->{bar} = 'boo';
is_deeply $nonempty, {foo => {a => 'b', bar => 'baz'}},
	  "merge should copy subhashes";

$empty_flat = Text::MediawikiFormat::_merge_hashes ($full_flat, $empty_flat);
is_deeply $empty_flat, $full_flat,
	  '... in flat case when keys exist in from but not in to';

$empty = Text::MediawikiFormat::_merge_hashes ($full, $empty);
is_deeply $empty, $full,
	  '... in non-flat case when keys exist in but not in to';

$empty = {};
$empty = Text::MediawikiFormat::_merge_hashes ($zero, $empty);
is_deeply $empty, $zero, '...and when value is zero but defined';

my $regexer = {a => "regex"};
my $arrayer = {a => ["X", "Y", "Z"]};
my $merged;
$merged = Text::MediawikiFormat::_merge_hashes ($regexer, $arrayer);
is_deeply $merged, {a => "regex"}, "regexes should replace arrays";
$merged = Text::MediawikiFormat::_merge_hashes ($arrayer, $regexer);
is_deeply $merged, {a => ["X", "Y", "Z"]}, "...and vice versa";