8 use Variable::Magic qw/wizard cast dispell VMG_UVAR/;
11 plan tests => 2 * 10 + 8 + 14 + 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';
26 my $x = check { $h{a} } { fetch => 1 }, 'fetch directly';
27 is $x, 1, 'uvar: fetch directly correctly';
29 $x = check { "$h{b}" } { fetch => 1 }, 'fetch by interpolation';
30 is $x, 2, 'uvar: fetch by interpolation correctly';
32 check { $h{c} = 4 } { store => 1 }, 'store directly';
34 $x = check { $h{c} = 5 } { store => 1 }, 'fetch and store';
35 is $x, 5, 'uvar: fetch and store correctly';
37 $x = check { exists $h{c} } { exists => 1 }, 'exists';
38 ok $x, 'uvar: exists correctly';
40 $x = check { delete $h{c} } { delete => 1 }, 'delete existing key';
41 is $x, 5, 'uvar: delete existing key correctly';
43 $x = check { delete $h{z} } { delete => 1 }, 'delete non-existing key';
44 ok !defined $x, 'uvar: delete non-existing key correctly';
46 my $wiz2 = wizard get => sub { 0 };
49 $x = check { $h{a} } { fetch => 1 }, 'fetch directly with also non uvar magic';
50 is $x, 1, 'uvar: fetch directly with also non uvar magic correctly';
52 $wiz2 = wizard fetch => sub { 0 };
53 my %h2 = (a => 37, b => 2, c => 3);
57 local $SIG{__WARN__} = sub { die };
60 is $@, '', 'uvar: fetch with incomplete magic doesn\'t croak';
61 is $x, 37, 'uvar: fetch with incomplete magic correctly';
64 local $SIG{__WARN__} = sub { die };
67 is $@, '', 'uvar: store with incomplete magic doesn\'t croak';
68 is $h2{a}, 73, 'uvar: store with incomplete magic correctly';
70 my $wiz3 = wizard store => sub { ++$_[2]; 0 }, copy_key => 1;
76 eval { $h3{$key} = 3 + $i };
77 is $@, '', "uvar: change key in store doesn't croak ($i)";
78 is $key, 'a', "uvar: change key didn't clobber \$key ($i)";
79 is_deeply \%h3, { a => 3, b => 3 + $i },
80 "uvar: change key in store correcty ($i)";
84 eval { $h3{b} = 5 + $i };
85 is $@, '', "uvar: change readonly key in store croaks ($i)";
86 is_deeply \%h3, { a => 3, b => 5, c => 5 + $i },
87 "uvar: change readonly key in store correcty ($i)";