8 use Variable::Magic qw/wizard cast dispell VMG_UVAR/;
11 plan tests => 2 * 9 + 7 + 4 + 1;
13 plan skip_all => 'No nice uvar magic for this perl';
17 use Variable::Magic::TestWatcher;
19 my $wiz = init [ qw/fetch store exists delete/ ], 'uvar';
21 my %h = (a => 1, b => 2, c => 3);
23 my $res = check { cast %h, $wiz } { }, 'cast';
24 ok $res, 'uvar: cast succeeded';
28 check { $x = $h{a} } { fetch => 1 }, 'fetch directly';
29 is $x, 1, 'uvar: fetch directly correctly';
31 check { $x = "$h{b}" } { fetch => 1 }, 'fetch by interpolation';
32 is $x, 2, 'uvar: fetch by interpolation correctly';
34 check { $h{c} = 4 } { store => 1 }, 'store directly';
36 check { $x = $h{c} = 5 } { store => 1 }, 'fetch and store';
37 is $x, 5, 'uvar: fetch and store correctly';
39 check { $x = exists $h{c} } { exists => 1 }, 'exists';
40 ok $x, 'uvar: exists correctly';
42 check { $x = delete $h{c} } { delete => 1 }, 'delete existing key';
43 is $x, 5, 'uvar: delete existing key correctly';
45 check { $x = delete $h{z} } { delete => 1 }, 'delete non-existing key';
46 ok !defined $x, 'uvar: delete non-existing key correctly';
48 my $wiz2 = wizard 'fetch' => sub { 0 };
49 my %h2 = (a => 37, b => 2, c => 3);
53 local $SIG{__WARN__} = sub { die };
56 is $@, '', 'uvar: fetch with incomplete magic';
57 is $x, 37, 'uvar: fetch with incomplete magic correctly';
60 local $SIG{__WARN__} = sub { die };
63 is $@, '', 'uvar: store with incomplete magic';
64 is $h2{a}, 73, 'uvar: store with incomplete magic correctly';