6 use Test::More tests => (2 * 21 + 7) + (2 * 5 + 4) + 1;
8 use Variable::Magic qw/cast dispell MGf_COPY 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';
82 eval "use B::Deparse";
83 $SKIP = 'B::Deparse' if $@;
86 $SKIP .= ' required to test uvar/clear interaction fix';
87 skip $SKIP => 2 * 5 + 4;
90 my $bd = B::Deparse->new;
92 my %h = (a => 13, b => 15);
93 watch { cast %h, $wiz } { }, 'cast clear/uvar';
95 my $code = sub { my $x = $h{$_[0]}; ++$x; $x };
96 my $before = $bd->coderef2text($code);
99 watch { $res = $code->('a') } { fetch => 1 }, 'fixed fetch "a"';
100 is $res, 14, 'uvar: fixed fetch "a" returned the right thing';
102 my $after = $bd->coderef2text($code);
103 is $before, $after, 'uvar: fixed fetch deparse correctly';
105 watch { $res = $code->('b') } { fetch => 1 }, 'fixed fetch "b"';
106 is $res, 16, 'uvar: fixed fetch "b" returned the right thing';
108 $after = $bd->coderef2text($code);
109 is $before, $after, 'uvar: fixed fetch deparse correctly';
111 watch { %h = () } { clear => 1 }, 'fixed clear';
113 watch { dispell %h, $wiz } { }, 'dispell clear/uvar';