6 use Test::More tests => (2 * 27 + 9) + (2 * 5 + 5) + 1;
8 use Variable::Magic qw<
11 VMG_COMPAT_HASH_DELETE_NOUVAR_VOID
15 use Variable::Magic::TestWatcher;
17 my $wiz = init_watcher
18 [ qw<get set len clear free copy dup local fetch store exists delete> ],
21 my %n = map { $_ => int rand 1000 } qw<foo bar baz qux>;
24 watch { cast %h, $wiz } { }, 'cast';
26 my $s = watch { $h{foo} } +{ (fetch => 1) x VMG_UVAR },
28 is $s, $n{foo}, 'hash: assign element to correctly';
31 watch { %b = %h } { }, 'assign to';
32 is_deeply \%b, \%n, 'hash: assign to correctly';
34 $s = watch { \%h } { }, 'reference';
36 my @b = watch { @h{qw<bar qux>} }
37 +{ (fetch => 2) x VMG_UVAR }, 'slice';
38 is_deeply \@b, [ @n{qw<bar qux>} ], 'hash: slice correctly';
42 watch { exists $h{bar} } +{ (exists => 1) x VMG_UVAR },'exists in void context';
45 $s = watch { exists $h{bar} } +{ (exists => 1) x VMG_UVAR },
46 "exists in scalar context ($_)";
47 ok $s, "hash: exists correctly ($_)";
52 watch { delete $h{bar} } +{
53 ((delete => 1) x !VMG_COMPAT_HASH_DELETE_NOUVAR_VOID, copy => 1) x VMG_UVAR
54 }, 'delete in void context';
57 $s = watch { delete $h{baz} } +{ (delete => 1, copy => 1) x VMG_UVAR },
58 "delete in scalar context ($_)";
59 my $exp = $_ == 1 ? $n{baz} : undef;
60 is $s, $exp, "hash: delete correctly ($_)";
65 watch { %h = () } { clear => 1 }, 'empty in list context';
67 watch { $h{a} = -1; %h = (b => $h{a}) }
68 +{ (fetch => 1, store => 2, copy => 2) x VMG_UVAR, clear => 1 },
69 'empty and set in void context';
71 watch { %h = (a => 1, d => 3) }
72 +{ (store => 2, copy => 2) x VMG_UVAR, clear => 1 },
73 'assign from list in void context';
75 @b = watch { %h = (a => 1, d => 3) }
76 +{ (exists => 2, store => 2, copy => 2) x VMG_UVAR, clear => 1 },
77 'assign from list in void context';
79 watch { %h = map { $_ => 1 } qw<a b d>; }
80 +{ (store => 3, copy => 3) x VMG_UVAR, clear => 1 },
81 'assign from map in void context';
83 watch { $h{d} = 2 } +{ (store => 1) x VMG_UVAR },
86 watch { $h{c} = 3 } +{ (store => 1, copy => 1) x VMG_UVAR },
89 $s = watch { %h } { }, 'buckets';
91 @b = watch { keys %h } { }, 'keys';
92 is_deeply [ sort @b ], [ qw<a b c d> ], 'hash: keys correctly';
94 @b = watch { values %h } { }, 'values';
95 is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly';
97 watch { while (my ($k, $v) = each %h) { } } { }, 'each';
101 watch { cast %b, $wiz } { }, 'cast 2';
102 } { free => 1 }, 'scope end';
104 watch { undef %h } { clear => 1 }, 'undef';
106 watch { dispell %h, $wiz } { }, 'dispell';
112 $SKIP = 'uvar magic';
115 unless (eval { require B::Deparse; 1 }) {
116 $SKIP = 'B::Deparse';
120 $SKIP .= ' required to test uvar/clear interaction fix';
121 skip $SKIP => 2 * 5 + 5;
124 my $bd = B::Deparse->new;
126 my %h = (a => 13, b => 15);
127 watch { cast %h, $wiz } { }, 'cast clear/uvar';
129 my $code = sub { my $x = $h{$_[0]}; ++$x; $x };
130 my $before = $bd->coderef2text($code);
133 watch { $res = $code->('a') } { fetch => 1 }, 'fixed fetch "a"';
134 is $res, 14, 'uvar: fixed fetch "a" returned the right thing';
136 my $after = $bd->coderef2text($code);
137 is $before, $after, 'uvar: fixed fetch deparse correctly';
139 watch { $res = $code->('b') } { fetch => 1 }, 'fixed fetch "b"';
140 is $res, 16, 'uvar: fixed fetch "b" returned the right thing';
142 $after = $bd->coderef2text($code);
143 is $before, $after, 'uvar: fixed fetch deparse correctly';
145 watch { %h = () } { clear => 1 }, 'fixed clear';
147 watch { dispell %h, $wiz } { }, 'dispell clear/uvar';
150 ok(!(B::svref_2object(\%h)->FLAGS & B::SVs_RMG()), '%h no longer has the RMG flag set');