6 use Test::More tests => (2 * 22 + 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 @b = watch { %h = (a => 1, d => 3) }
48 +{ (exists => 2, store => 2, copy => 2) x VMG_UVAR, clear => 1 },
49 'assign from list in void context';
51 watch { %h = map { $_ => 1 } qw<a b d>; }
52 +{ (store => 3, copy => 3) x VMG_UVAR, clear => 1 },
53 'assign from map in void context';
55 watch { $h{d} = 2 } +{ (store => 1) x VMG_UVAR },
58 watch { $h{c} = 3 } +{ (store => 1, copy => 1) x VMG_UVAR },
61 $s = watch { %h } { }, 'buckets';
63 @b = watch { keys %h } { }, 'keys';
64 is_deeply [ sort @b ], [ qw<a b c d> ], 'hash: keys correctly';
66 @b = watch { values %h } { }, 'values';
67 is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly';
69 watch { while (my ($k, $v) = each %h) { } } { }, 'each';
73 watch { cast %b, $wiz } { }, 'cast 2';
74 } { free => 1 }, 'scope end';
76 watch { undef %h } { clear => 1 }, 'undef';
78 watch { dispell %h, $wiz } { }, 'dispell';
87 unless (eval { require B::Deparse; 1 }) {
92 $SKIP .= ' required to test uvar/clear interaction fix';
93 skip $SKIP => 2 * 5 + 5;
96 my $bd = B::Deparse->new;
98 my %h = (a => 13, b => 15);
99 watch { cast %h, $wiz } { }, 'cast clear/uvar';
101 my $code = sub { my $x = $h{$_[0]}; ++$x; $x };
102 my $before = $bd->coderef2text($code);
105 watch { $res = $code->('a') } { fetch => 1 }, 'fixed fetch "a"';
106 is $res, 14, 'uvar: fixed fetch "a" returned the right thing';
108 my $after = $bd->coderef2text($code);
109 is $before, $after, 'uvar: fixed fetch deparse correctly';
111 watch { $res = $code->('b') } { fetch => 1 }, 'fixed fetch "b"';
112 is $res, 16, 'uvar: fixed fetch "b" returned the right thing';
114 $after = $bd->coderef2text($code);
115 is $before, $after, 'uvar: fixed fetch deparse correctly';
117 watch { %h = () } { clear => 1 }, 'fixed clear';
119 watch { dispell %h, $wiz } { }, 'dispell clear/uvar';
122 ok(!(B::svref_2object(\%h)->FLAGS & B::SVs_RMG()), '%h no longer has the RMG flag set');