6 use Test::More tests => 2 * 20 + 6 + 1;
8 use Variable::Magic qw/cast dispell MGf_COPY VMG_UVAR/;
11 use Variable::Magic::TestWatcher;
14 [ qw/get set len free copy dup local fetch store exists delete/ ], # clear
17 my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/;
20 check { cast %h, $wiz } { }, 'cast';
22 my $s = check { $h{foo} } +{ (fetch => 1) x VMG_UVAR },
23 # (copy => 1) x MGf_COPY # if clear magic
25 is $s, $n{foo}, 'hash: assign element to correctly';
27 $s = check { exists $h{foo} } +{ (exists => 1) x VMG_UVAR }, 'exists';
28 ok $s, 'hash: exists correctly';
31 check { %b = %h } { }, 'assign to';
32 is_deeply \%b, \%n, 'hash: assign to correctly';
34 $s = check { \%h } { }, 'reference';
36 my @b = check { @h{qw/bar qux/} }
37 +{ (fetch => 2) x VMG_UVAR }, 'slice';
38 # (copy => 2) x MGf_COPY # if clear magic
39 is_deeply \@b, [ @n{qw/bar qux/} ], 'hash: slice correctly';
41 check { %h = () } { }, 'empty in list context'; # clear => 1
43 check { %h = (a => 1, d => 3); () }
44 +{ (store => 2, copy => 2) x VMG_UVAR }, # clear => 1
45 'assign from list in void context';
47 check { %h = map { $_ => 1 } qw/a b d/; }
48 +{ (exists => 3, store => 3, copy => 3) x VMG_UVAR }, # clear =>1
49 'assign from map in list context';
51 check { $h{d} = 2; () } +{ (store => 1) x VMG_UVAR },
54 check { $h{c} = 3; () } +{ (store => 1, copy => 1) x VMG_UVAR },
57 $s = check { %h } { }, 'buckets';
59 @b = check { keys %h } { }, 'keys';
60 is_deeply [ sort @b ], [ qw/a b c d/ ], 'hash: keys correctly';
62 @b = check { values %h } { }, 'values';
63 is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly';
65 check { while (my ($k, $v) = each %h) { } } { }, 'each';
69 check { cast %b, $wiz } { }, 'cast 2';
70 } { free => 1 }, 'scope end';
72 check { undef %h } { }, 'undef'; # clear => 1
74 check { dispell %h, $wiz } { }, 'dispell';