8 use Variable::Magic qw/wizard cast dispell VMG_UVAR/;
11 plan tests => 2 * 15 + 12 + 14 + (4 * 2 * 2 + 1 + 1) + 1;
13 plan skip_all => 'No nice uvar magic for this perl';
17 use Variable::Magic::TestWatcher;
18 use Variable::Magic::TestValue;
20 my $wiz = init_watcher [ qw/fetch store exists delete/ ], 'uvar';
22 my %h = (a => 1, b => 2, c => 3);
24 my $res = watch { cast %h, $wiz } { }, 'cast';
25 ok $res, 'uvar: cast succeeded';
27 my $x = watch { $h{a} } { fetch => 1 }, 'fetch directly';
28 is $x, 1, 'uvar: fetch directly correctly';
30 $x = watch { "$h{b}" } { fetch => 1 }, 'fetch by interpolation';
31 is $x, 2, 'uvar: fetch by interpolation correctly';
33 watch { $h{c} = 4 } { store => 1 }, 'store directly';
35 $x = watch { $h{c} = 5 } { store => 1 }, 'fetch and store';
36 is $x, 5, 'uvar: fetch and store correctly';
38 $x = watch { exists $h{c} } { exists => 1 }, 'exists';
39 ok $x, 'uvar: exists correctly';
41 $x = watch { delete $h{c} } { delete => 1 }, 'delete existing key';
42 is $x, 5, 'uvar: delete existing key correctly';
44 $x = watch { delete $h{z} } { delete => 1 }, 'delete non-existing key';
45 ok !defined $x, 'uvar: delete non-existing key correctly';
47 my $wiz2 = wizard get => sub { 0 };
50 $x = watch { $h{a} } { fetch => 1 }, 'fetch directly with also non uvar magic';
51 is $x, 1, 'uvar: fetch directly with also non uvar magic correctly';
55 skip 'Tie::Hash required to test uvar magic on tied hashes' => 2 * 5 + 4 if $@;
56 diag "Using Tie::Hash $Tie::Hash::VERSION" if defined $Tie::Hash::VERSION;
58 tie my %h, 'Tie::StdHash';
59 %h = (x => 7, y => 8);
61 $res = watch { cast %h, $wiz } { }, 'cast on tied hash';
62 ok $res, 'uvar: cast on tied hash succeeded';
64 $x = watch { $h{x} } { fetch => 1 }, 'fetch on tied hash';
65 is $x, 7, 'uvar: fetch on tied hash succeeded';
67 watch { $h{x} = 9 } { store => 1 }, 'store on tied hash';
69 $x = watch { exists $h{x} } { exists => 1 }, 'exists on tied hash';
70 ok $x, 'uvar: exists on tied hash succeeded';
72 $x = watch { delete $h{x} } { delete => 1 }, 'delete on tied hash';
73 is $x, 9, 'uvar: delete on tied hash succeeded';
76 $wiz2 = wizard fetch => sub { 0 };
77 my %h2 = (a => 37, b => 2, c => 3);
81 local $SIG{__WARN__} = sub { die };
84 is $@, '', 'uvar: fetch with incomplete magic doesn\'t croak';
85 is $x, 37, 'uvar: fetch with incomplete magic correctly';
88 local $SIG{__WARN__} = sub { die };
91 is $@, '', 'uvar: store with incomplete magic doesn\'t croak';
92 is $h2{a}, 73, 'uvar: store with incomplete magic correctly';
94 my $wiz3 = wizard store => sub { ++$_[2]; 0 }, copy_key => 1;
100 eval { $h3{$key} = 3 + $i };
101 is $@, '', "uvar: change key in store doesn't croak ($i)";
102 is $key, 'a', "uvar: change key didn't clobber \$key ($i)";
103 is_deeply \%h3, { a => 3, b => 3 + $i },
104 "uvar: change key in store correcty ($i)";
108 eval { $h3{b} = 5 + $i };
109 is $@, '', "uvar: change readonly key in store doesn't croak ($i)";
110 is_deeply \%h3, { a => 3, b => 5, c => 5 + $i },
111 "uvar: change readonly key in store correcty ($i)";
115 my %val = (apple => 1);
117 init_value %val, 'fetch', 'uvar';
119 value { my $x = $val{apple} } { apple => 1 }, 'value store';
123 my %val = (apple => 1);
125 my $wv = init_value %val, 'store', 'uvar';
127 value { $val{apple} = 2 } { apple => 1 }, 'value store';
130 is_deeply \%val, { apple => 2 }, 'uvar: value after store';
134 my %val = (apple => 1);
136 init_value %val, 'exists', 'uvar';
138 value { my $x = exists $val{apple} } { apple => 1 }, 'value exists';
142 my %val = (apple => 1, banana => 2);
144 my $wv = init_value %val, 'delete', 'uvar';
146 value { delete $val{apple} } { apple => 1, banana => 2 }, 'value delete';
149 is_deeply \%val, { banana => 2 }, 'uvar: value after delete';