8 use Variable::Magic qw/wizard cast MGf_COPY/;
11 plan tests => 1 + 8 + 14;
13 plan skip_all => 'No copy magic for this perl' if !MGf_COPY;
17 my $wiz = wizard 'copy' => sub { ++$c };
18 is($c, 0, 'copy : create wizard');
21 eval "use Tie::Array";
22 skip 'Tie::Array required to test copy magic on arrays', 8 if $@;
24 tie my @a, 'Tie::StdArray';
27 my $res = cast @a, $wiz;
28 ok($res, 'copy : cast on array succeeded');
29 is($c, 0, 'copy : cast on array didn\'t triggered the callback');
32 is($c, 1, 'copy : callback triggers on array store');
35 is($c, 2, 'copy : callback triggers on array fetch');
36 is($s, 13, 'copy : array fetch is correct');
39 is($c, 3, 'copy : callback triggers on array exists');
40 ok($s, 'copy : array exists is correct');
43 is($c, 3, 'copy : callback doesn\'t trigger on array undef');
48 skip 'Tie::Hash required to test copy magic on hashes', 14 if $@;
50 tie my %h, 'Tie::StdHash';
51 %h = (a => 1, b => 2, c => 3);
54 my $res = cast %h, $wiz;
55 ok($res, 'copy : cast on hash succeeded');
56 is($c, 0, 'copy : cast on hash didn\'t triggered the callback');
59 is($c, 1, 'copy : callback triggers on hash store');
62 is($c, 2, 'copy : callback triggers on hash fetch');
63 is($s, 3, 'copy : hash fetch is correct');
66 is($c, 3, 'copy : callback triggers on hash exists');
67 ok($s, 'copy : hash exists is correct');
70 is($c, 4, 'copy : callback triggers on hash delete');
71 is($s, 7, 'copy : hash delete is correct');
73 my ($k, $v) = each %h;
74 is($c, 5, 'copy : callback triggers on hash each');
77 is($c, 5, 'copy : callback doesn\'t trigger on hash keys');
80 is(scalar @v, 2, 'copy : two values in the hash');
81 is($c, 7, 'copy : callback triggers on hash values');
84 is($c, 7, 'copy : callback doesn\'t trigger on hash undef');