6 use Config qw/%Config/;
10 use Variable::Magic qw/wizard cast dispell VMG_UVAR/;
13 plan tests => 2 * 9 + 7 + 12 + 1;
15 plan skip_all => 'No nice uvar magic for this perl';
19 use Variable::Magic::TestWatcher;
21 my $wiz = init [ qw/fetch store exists delete/ ], 'uvar';
23 my %h = (a => 1, b => 2, c => 3);
25 my $res = check { cast %h, $wiz } { }, 'cast';
26 ok $res, 'uvar: cast succeeded';
30 check { $x = $h{a} } { fetch => 1 }, 'fetch directly';
31 is $x, 1, 'uvar: fetch directly correctly';
33 check { $x = "$h{b}" } { fetch => 1 }, 'fetch by interpolation';
34 is $x, 2, 'uvar: fetch by interpolation correctly';
36 check { $h{c} = 4 } { store => 1 }, 'store directly';
38 check { $x = $h{c} = 5 } { store => 1 }, 'fetch and store';
39 is $x, 5, 'uvar: fetch and store correctly';
41 check { $x = exists $h{c} } { exists => 1 }, 'exists';
42 ok $x, 'uvar: exists correctly';
44 check { $x = delete $h{c} } { delete => 1 }, 'delete existing key';
45 is $x, 5, 'uvar: delete existing key correctly';
47 check { $x = delete $h{z} } { delete => 1 }, 'delete non-existing key';
48 ok !defined $x, 'uvar: delete non-existing key correctly';
50 my $wiz2 = wizard fetch => sub { 0 };
51 my %h2 = (a => 37, b => 2, c => 3);
55 local $SIG{__WARN__} = sub { die };
58 is $@, '', 'uvar: fetch with incomplete magic doesn\'t croak';
59 is $x, 37, 'uvar: fetch with incomplete magic correctly';
62 local $SIG{__WARN__} = sub { die };
65 is $@, '', 'uvar: store with incomplete magic doesn\'t croak';
66 is $h2{a}, 73, 'uvar: store with incomplete magic correctly';
68 my $wiz3 = wizard store => sub { ++$_[2]; 0 };
73 eval { my $key = 'a'; $h3{$key} = 3 + $i };
74 is $@, '', "uvar: change key in store doesn't croak ($i)";
75 is_deeply \%h3, { a => 3, b => 3 + $i },
76 "uvar: change key in store correcty ($i)";
79 my $ro_bare_hk = $] >= 5.010 && $Config{useithreads};
80 diag 'This perl has readonly bare hash keys' if $ro_bare_hk;
83 eval { $h3{b} = 5 + $i };
85 like $@, qr/Modification\s+of\s+a\s+read-only\s+value/,
86 "uvar: change readonly key in store croaks ($i)";
87 is_deeply \%h3, { a => 3, b => 5 },
88 "uvar: change readonly key in store correcty ($i)";
90 is $@, '', "uvar: change readonly key in store croaks ($i)";
91 is_deeply \%h3, { a => 3, b => 5, c => 6, (d => 7) x ($i >= 2) },
92 "uvar: change readonly key in store correcty ($i)";