Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
initial object hash tests
  • Loading branch information
moritz committed Feb 15, 2012
1 parent b47fb49 commit 1b362ee
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions S09-hashes/objecthash.t
@@ -0,0 +1,44 @@
use v6;
use Test;

plan 20;

{
class A { method Str() { 'foo' } };
my $a = A.new;

my %h{Any};
%h{$a} = 'blubb';
is %h{$a}, 'blubb', 'Any-typed hash access (+)';
nok %h{A.new}, 'and the hash really uses ===-semantics';
dies_ok { %h{Mu.new} = 3 }, 'Any-typed hash does not like Mu keys';
ok %h.keys[0] === $a, 'returned key is correct';
}

{
my %h{Int};
%h{2} = 3;
is %h{1 + 1}, 3, 'value-type semantics';
dies_ok { %h{'foo'} }, 'non-conformant type dies';
}

# combinations of typed and objecthash
{
my Int %h{Rat};
%h{0.5} = 1;
%h{0.3} = 2;
dies_ok { %h{2} = 3 }, 'key type mismatch';
dies_ok { %h{0.5} = 0.2 }, 'value type mismatch';
dies_ok { %h{2} = 0.5 }, 'key and value type mismatch';
is %h.keys.sort.join(','), '0.3,0.5', '.keys';
is ~%h.values.sort, '1 2', '.values';
isa_ok %h.kv.flat[0], Rat, '.kv types (1)';
isa_ok %h.kv.flat[1], Int, '.kv types (2)';
isa_ok %h.pairs[0].key, Rat, '.pairs.key type';
isa_ok %h.pairs[0].value, Int, '.pairs.value type';
is %h.elems, 2, '.elems';
lives_ok { %h{0.2} := 3 }, 'binding to typed objecthash elements';
is %h.elems, 3, 'updated .elems';
dies_ok { %h{ 3 } := 3 }, 'binding key type check failure';
dies_ok { %h{0.2} := 'a' }, 'binding value type check failure';
}

0 comments on commit 1b362ee

Please sign in to comment.