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