6 use Test::More tests => (2 * 21 + 7) + (2 * 5 + 5) + 1;
8 use Variable::Magic qw<cast dispell VMG_UVAR>;
11 use Variable::Magic::TestWatcher;
13 my $wiz = init_watcher
14 [ qw<get set len clear free copy dup local fetch store exists delete> ],
17 my %n = map { $_ => int rand 1000 } qw<foo bar baz qux>;
20 watch { cast %h, $wiz } { }, 'cast';
22 my $s = watch { $h{foo} } +{ (fetch => 1) x VMG_UVAR },
24 is $s, $n{foo}, 'hash: assign element to correctly';
27 $s = watch { exists $h{foo} } +{ (exists => 1) x VMG_UVAR }, "exists ($_)";
28 ok $s, "hash: exists correctly ($_)";
32 watch { %b = %h } { }, 'assign to';
33 is_deeply \%b, \%n, 'hash: assign to correctly';
35 $s = watch { \%h } { }, 'reference';
37 my @b = watch { @h{qw<bar qux>} }
38 +{ (fetch => 2) x VMG_UVAR }, 'slice';
39 is_deeply \@b, [ @n{qw<bar qux>} ], 'hash: slice correctly';
41 watch { %h = () } { clear => 1 }, 'empty in list context';
43 watch { %h = (a => 1, d => 3); () }
44 +{ (store => 2, copy => 2) x VMG_UVAR, clear => 1 },
45 'assign from list in void context';
47 watch { %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 watch { $h{d} = 2; () } +{ (store => 1) x VMG_UVAR },
54 watch { $h{c} = 3; () } +{ (store => 1, copy => 1) x VMG_UVAR },
57 $s = watch { %h } { }, 'buckets';
59 @b = watch { keys %h } { }, 'keys';
60 is_deeply [ sort @b ], [ qw<a b c d> ], 'hash: keys correctly';
62 @b = watch { values %h } { }, 'values';
63 is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly';
65 watch { while (my ($k, $v) = each %h) { } } { }, 'each';
69 watch { cast %b, $wiz } { }, 'cast 2';
70 } { free => 1 }, 'scope end';
72 watch { undef %h } { clear => 1 }, 'undef';
74 watch { dispell %h, $wiz } { }, 'dispell';
83 unless (eval { require B::Deparse; 1 }) {
88 $SKIP .= ' required to test uvar/clear interaction fix';
89 skip $SKIP => 2 * 5 + 5;
92 my $bd = B::Deparse->new;
94 my %h = (a => 13, b => 15);
95 watch { cast %h, $wiz } { }, 'cast clear/uvar';
97 my $code = sub { my $x = $h{$_[0]}; ++$x; $x };
98 my $before = $bd->coderef2text($code);
101 watch { $res = $code->('a') } { fetch => 1 }, 'fixed fetch "a"';
102 is $res, 14, 'uvar: fixed fetch "a" returned the right thing';
104 my $after = $bd->coderef2text($code);
105 is $before, $after, 'uvar: fixed fetch deparse correctly';
107 watch { $res = $code->('b') } { fetch => 1 }, 'fixed fetch "b"';
108 is $res, 16, 'uvar: fixed fetch "b" returned the right thing';
110 $after = $bd->coderef2text($code);
111 is $before, $after, 'uvar: fixed fetch deparse correctly';
113 watch { %h = () } { clear => 1 }, 'fixed clear';
115 watch { dispell %h, $wiz } { }, 'dispell clear/uvar';
118 ok(!(B::svref_2object(\%h)->FLAGS & B::SVs_RMG()), '%h no longer has the RMG flag set');