1#!perl -T
2
3use strict;
4use warnings;
5
6use Test::More tests => (2 * 21 + 7) + (2 * 5 + 4) + 1;
7
8use Variable::Magic qw/cast dispell MGf_COPY VMG_UVAR/;
9
10use lib 't/lib';
11use Variable::Magic::TestWatcher;
12
13my $wiz = init_watcher
14        [ qw/get set len clear free copy dup local fetch store exists delete/ ],
15        'hash';
16
17my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/;
18my %h = %n;
19
20watch { cast %h, $wiz } { }, 'cast';
21
22my $s = watch { $h{foo} } +{ (fetch => 1) x VMG_UVAR },
23                       'assign element to';
24is $s, $n{foo}, 'hash: assign element to correctly';
25
26for (1 .. 2) {
27 $s = watch { exists $h{foo} } +{ (exists => 1) x VMG_UVAR }, "exists ($_)";
28 ok $s, "hash: exists correctly ($_)";
29}
30
31my %b;
32watch { %b = %h } { }, 'assign to';
33is_deeply \%b, \%n, 'hash: assign to correctly';
34
35$s = watch { \%h } { }, 'reference';
36
37my @b = watch { @h{qw/bar qux/} }
38                  +{ (fetch => 2) x VMG_UVAR }, 'slice';
39is_deeply \@b, [ @n{qw/bar qux/} ], 'hash: slice correctly';
40
41watch { %h = () } { clear => 1 }, 'empty in list context';
42
43watch { %h = (a => 1, d => 3); () }
44               +{ (store => 2, copy => 2) x VMG_UVAR, clear => 1 },
45               'assign from list in void context';
46
47watch { %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';
50
51watch { $h{d} = 2; () } +{ (store => 1) x VMG_UVAR },
52                    'assign old element';
53
54watch { $h{c} = 3; () } +{ (store => 1, copy => 1) x VMG_UVAR },
55                    'assign new element';
56
57$s = watch { %h } { }, 'buckets';
58
59@b = watch { keys %h } { }, 'keys';
60is_deeply [ sort @b ], [ qw/a b c d/ ], 'hash: keys correctly';
61
62@b = watch { values %h } { }, 'values';
63is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly';
64
65watch { while (my ($k, $v) = each %h) { } } { }, 'each';
66
67watch {
68 my %b = %n;
69 watch { cast %b, $wiz } { }, 'cast 2';
70} { free => 1 }, 'scope end';
71
72watch { undef %h } { clear => 1 }, 'undef';
73
74watch { dispell %h, $wiz } { }, 'dispell';
75
76SKIP: {
77 my $SKIP;
78
79 unless (VMG_UVAR) {
80  $SKIP = 'uvar magic';
81 } else {
82  eval "use B::Deparse";
83  $SKIP = 'B::Deparse' if $@;
84 }
85 if ($SKIP) {
86  $SKIP .= ' required to test uvar/clear interaction fix';
87  skip $SKIP => 2 * 5 + 4;
88 }
89
90 my $bd = B::Deparse->new;
91
92 my %h = (a => 13, b => 15);
93 watch { cast %h, $wiz } { }, 'cast clear/uvar';
94
95 my $code   = sub { my $x = $h{$_[0]}; ++$x; $x };
96 my $before = $bd->coderef2text($code);
97 my $res;
98
99 watch { $res = $code->('a') } { fetch => 1 }, 'fixed fetch "a"';
100 is $res, 14, 'uvar: fixed fetch "a" returned the right thing';
101
102 my $after = $bd->coderef2text($code);
103 is $before, $after, 'uvar: fixed fetch deparse correctly';
104
105 watch { $res = $code->('b') } { fetch => 1 }, 'fixed fetch "b"';
106 is $res, 16, 'uvar: fixed fetch "b" returned the right thing';
107
108 $after = $bd->coderef2text($code);
109 is $before, $after, 'uvar: fixed fetch deparse correctly';
110
111 watch { %h = () } { clear => 1 }, 'fixed clear';
112
113 watch { dispell %h, $wiz } { }, 'dispell clear/uvar';
114}
115