6 use Test::More tests => (2 * 27 + 9) + 2 * (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; require B::Deparse; 1 }) {
116 $SKIP = 'B and B::Deparse';
120 $SKIP .= ' required to test uvar/clear interaction fix';
121 skip $SKIP => 2 * ( 2 * 5 + 5);
124 my $bd = B::Deparse->new;
126 my %h1 = (a => 13, b => 15);
127 my %h2 = (a => 17, b => 19);
130 [ \%h1 => 'first hash' => (14, 16) ],
131 [ \%h2 => 'second hash' => (18, 20) ],
134 for my $test (@tests) {
135 my ($h, $desc, @exp) = @$test;
137 watch { &cast($h, $wiz) } { }, "cast clear/uvar on $desc";
139 my $code = sub { my $x = $h->{$_[0]}; ++$x; $x };
140 my $before = $bd->coderef2text($code);
143 watch { $res = $code->('a') } { fetch => 1 }, "fetch constant 'a' from $desc";
144 is $res, $exp[0], "uvar: fetch constant 'a' from $desc was correct";
146 my $after = $bd->coderef2text($code);
148 "uvar: code deparses correctly after constant fetch from $desc";
151 watch { $res = $code->($key) } { fetch => 1 },"fetch variable 'b' from $desc";
152 is $res, $exp[1], "uvar: fetch variable 'b' from $desc was correct";
154 $after = $bd->coderef2text($code);
156 "uvar: code deparses correctly after variable fetch from $desc";
158 watch { %$h = () } { clear => 1 }, "fixed clear for $desc";
160 watch { &dispell($h, $wiz) } { }, "dispell clear/uvar from $desc";
162 ok(!(B::svref_2object($h)->FLAGS & B::SVs_RMG()),
163 "$desc no longer has the RMG flag set");