Data-Hopen
view release on metacpan or search on metacpan
t/010-scope-hash.t view on Meta::CPAN
#!perl
# t/010-scope-hash.t: test Data::Hopen::Scope::Hash
use rlib 'lib';
use HopenTest 'Data::Hopen::Scope::Hash';
use Test::Fatal;
use Data::Hopen::Scope;
sub makeset {
my $set = Set::Scalar->new;
$set->insert(@_);
return $set;
}
# --- creation, put, and get -----------------------------------------
my $s = $DUT->new();
isa_ok($s, "$DUT");
$s->put(foo => 42);
cmp_ok($s->find('foo'), '==', 42, 'Retrieving works');
cmp_ok($s->find('foo', 0), '==', 42, 'Retrieving from set 0 works');
cmp_ok($s->find('foo', "0"), '==', 42, 'Retrieving from set "0" works');
cmp_ok($s->find('foo', FIRST_ONLY), '==', 42, 'Retrieving from set FIRST_ONLY works');
is_deeply($s->find('foo', '*'), {'0' => 42}, 'Retrieving from set "*" works');
ok($s->names->is_equal(makeset('foo')), 'names works with a non-nested scope');
ok($s->names(0)->is_equal(makeset('foo')), 'names(0) works with a non-nested scope');
my $t = $DUT->new()->put(bar => 1337);
$t->outer($s);
ok($t->names->is_equal(makeset(qw(foo bar))), 'names works with a nested scope');
ok($t->names(1)->is_equal(makeset(qw(foo bar))), 'names(1) works with a nested scope');
ok($t->names(0)->is_equal(makeset(qw(bar))), 'names(0) works with a nested scope');
cmp_ok($s->find('foo'), '==', 42, 'Retrieving from a parent (outer) scope works');
my $u = $DUT->new()->put(quux => 128);
$u->outer($t);
ok($u->names->is_equal(makeset(qw(foo bar quux))), 'names works with a doubly-nested scope');
ok($u->names(2)->is_equal(makeset(qw(foo bar quux))), 'names(2) works with a doubly-nested scope');
ok($u->names(1)->is_equal(makeset(qw(bar quux))), 'names(1) works with a doubly-nested scope');
ok($u->names(0)->is_equal(makeset(qw(quux))), 'names(0) works with a doubly-nested scope');
cmp_ok($s->find('foo'), '==', 42, 'Retrieving from a grandparent scope works');
ok(!defined exception { $s->put; }, 'empty put() allowed');
# Error cases
like( exception { Data::Hopen::Scope::Hash::put() }, qr/Need an instance/, 'dies without instance');
like( exception { $s->put('oops') }, qr/odd number/, 'dies with odd number of params');
# --- merge ----------------------------------------------------------
# Default merge strategy
sub defstrat {
my $strategy = shift;
my $sname = $strategy // 'undef';
$s = $DUT->new(); # merge into empty
$s->merge_strategy($strategy) if defined $strategy;
$s->merge(foo=>2);
is_deeply($s->as_hashref, {foo=>2}, "$sname Merge into empty works");
cmp_ok($s->find('foo'), '==', 2, "$sname Merge into empty works (find)");
$s = $DUT->new(); # put then merge scalars
$s->merge_strategy($strategy) if defined $strategy;
( run in 0.922 second using v1.01-cache-2.11-cpan-54e63673c56 )