6 use Test::More tests => (2 * 24 + 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 watch { %b = %h } { }, 'assign to';
28 is_deeply \%b, \%n, 'hash: assign to correctly';
30 $s = watch { \%h } { }, 'reference';
32 my @b = watch { @h{qw<bar qux>} }
33 +{ (fetch => 2) x VMG_UVAR }, 'slice';
34 is_deeply \@b, [ @n{qw<bar qux>} ], 'hash: slice correctly';
38 watch { exists $h{bar} } +{ (exists => 1) x VMG_UVAR },'exists in void context';
41 $s = watch { exists $h{bar} } +{ (exists => 1) x VMG_UVAR },
42 "exists in scalar context ($_)";
43 ok $s, "hash: exists correctly ($_)";
46 watch { %h = () } { clear => 1 }, 'empty in list context';
48 watch { $h{a} = -1; %h = (b => $h{a}) }
49 +{ (fetch => 1, store => 2, copy => 2) x VMG_UVAR, clear => 1 },
50 'empty and set in void context';
52 watch { %h = (a => 1, d => 3) }
53 +{ (store => 2, copy => 2) x VMG_UVAR, clear => 1 },
54 'assign from list in void context';
56 @b = watch { %h = (a => 1, d => 3) }
57 +{ (exists => 2, store => 2, copy => 2) x VMG_UVAR, clear => 1 },
58 'assign from list in void context';
60 watch { %h = map { $_ => 1 } qw<a b d>; }
61 +{ (store => 3, copy => 3) x VMG_UVAR, clear => 1 },
62 'assign from map in void context';
64 watch { $h{d} = 2 } +{ (store => 1) x VMG_UVAR },
67 watch { $h{c} = 3 } +{ (store => 1, copy => 1) x VMG_UVAR },
70 $s = watch { %h } { }, 'buckets';
72 @b = watch { keys %h } { }, 'keys';
73 is_deeply [ sort @b ], [ qw<a b c d> ], 'hash: keys correctly';
75 @b = watch { values %h } { }, 'values';
76 is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly';
78 watch { while (my ($k, $v) = each %h) { } } { }, 'each';
82 watch { cast %b, $wiz } { }, 'cast 2';
83 } { free => 1 }, 'scope end';
85 watch { undef %h } { clear => 1 }, 'undef';
87 watch { dispell %h, $wiz } { }, 'dispell';
96 unless (eval { require B::Deparse; 1 }) {
101 $SKIP .= ' required to test uvar/clear interaction fix';
102 skip $SKIP => 2 * 5 + 5;
105 my $bd = B::Deparse->new;
107 my %h = (a => 13, b => 15);
108 watch { cast %h, $wiz } { }, 'cast clear/uvar';
110 my $code = sub { my $x = $h{$_[0]}; ++$x; $x };
111 my $before = $bd->coderef2text($code);
114 watch { $res = $code->('a') } { fetch => 1 }, 'fixed fetch "a"';
115 is $res, 14, 'uvar: fixed fetch "a" returned the right thing';
117 my $after = $bd->coderef2text($code);
118 is $before, $after, 'uvar: fixed fetch deparse correctly';
120 watch { $res = $code->('b') } { fetch => 1 }, 'fixed fetch "b"';
121 is $res, 16, 'uvar: fixed fetch "b" returned the right thing';
123 $after = $bd->coderef2text($code);
124 is $before, $after, 'uvar: fixed fetch deparse correctly';
126 watch { %h = () } { clear => 1 }, 'fixed clear';
128 watch { dispell %h, $wiz } { }, 'dispell clear/uvar';
131 ok(!(B::svref_2object(\%h)->FLAGS & B::SVs_RMG()), '%h no longer has the RMG flag set');