/
objecthash.t
90 lines (79 loc) 路 2.83 KB
/
objecthash.t
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
use v6;
use Test;
plan 34;
{
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';
} #4
{
my %h{Int};
%h{2} = 3;
is %h{1 + 1}, 3, 'value-type semantics';
dies-ok { %h{'foo'} }, 'non-conformant type dies';
} #2
# 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';
dies-ok { %h.push: 0.5 => 2 },
'Hash.push fails when the resulting array conflicts with the type check';
lives-ok { %h.push: 0.9 => 3 }, 'Hash.push without array creation is OK';
dies-ok { %h.push: 1 => 3 }, 'Hash.push key type check failure';
dies-ok { %h.push: 1.1 => 0.2 }, 'Hash.push value type check failure';
} #18
{
my %h{Any};
%h = 1, 2;
ok %h.keys[0] === 1, 'list assignment + object hashes';
} #1
{
my %h{Mu};
#?rakudo todo 'oh noes, it dies'
lives-ok { %h{Mu} = 2 }, "using Mu as a key (1)"; # TODO: remove 'lives-ok' when this no longer dies
#?rakudo skip 'oh noes, it dies'
is %h{Mu}, 2, 'using Mu as a key (2)';
%h{Any} = 3;
is %h{Any}, 3, 'using Any as a key';
#?rakudo skip 'oh noes, it dies'
is %h{ Mu, Any }.join(","), "2,3", 'check slice access on Mu';
#?rakudo todo 'oh noes, it dies'
is %h{*}.join(","), "2,3", 'check whatever access with Mu as key';
} #6
# RT #118037
{
my %h{Any};
%h{Any}=1;
ok %h{Any}:exists, '.exists returns True on a %h{Any} in a TypedHash';
}
# RT #125249
{
lives-ok { my %h = my %x{Date}; },
'declaring empty object hash on rhs of assignment to hash does not die with "Cannot look up attributes in a type object"';
}
# RT #125352
{
is (my %h).list.perl, '()', 'empty hash listifies to "()"';
is (my %h{Any}).list.perl, '()', 'empty object hash listifies to "()"';
}
#vim: ft=perl6