8 use Variable::Magic qw/cast MGf_COPY/;
11 plan tests => 2 + (2 * 5 + 3) + (2 * 9 + 6) + 1;
13 plan skip_all => 'No copy magic for this perl';
17 use Variable::Magic::TestWatcher;
19 my $wiz = init 'copy', 'copy';
22 eval "use Tie::Array";
23 skip 'Tie::Array required to test copy magic on arrays', 2 * 5 + 3 if $@;
24 diag "Using Tie::Array $Tie::Array::VERSION" if defined $Tie::Array::VERSION;
26 tie my @a, 'Tie::StdArray';
29 my $res = check { cast @a, $wiz } { }, 'cast on tied array';
30 ok $res, 'copy: cast on tied array succeeded';
32 check { $a[3] = 13 } { copy => 1 }, 'tied array store';
34 my $s = check { $a[3] } { copy => 1 }, 'tied array fetch';
35 is $s, 13, 'copy: tied array fetch correctly';
37 $s = check { exists $a[3] } { copy => 1 }, 'tied array exists';
38 ok $s, 'copy: tied array exists correctly';
40 check { undef @a } { }, 'tied array undef';
45 skip 'Tie::Hash required to test copy magic on hashes' => 2 * 9 + 6 if $@;
46 diag "Using Tie::Hash $Tie::Hash::VERSION" if defined $Tie::Hash::VERSION;
48 tie my %h, 'Tie::StdHash';
49 %h = (a => 1, b => 2, c => 3);
51 my $res = check { cast %h, $wiz } { }, 'cast on tied hash';
52 ok $res, 'copy: cast on tied hash succeeded';
54 check { $h{b} = 7 } { copy => 1 }, 'tied hash store';
56 my $s = check { $h{c} } { copy => 1 }, 'tied hash fetch';
57 is $s, 3, 'copy: tied hash fetch correctly';
59 $s = check { exists $h{a} } { copy => 1 }, 'tied hash exists';
60 ok $s, 'copy: tied hash exists correctly';
62 $s = check { delete $h{b} } { copy => 1 }, 'tied hash delete';
63 is $s, 7, 'copy: tied hash delete correctly';
65 check { my ($k, $v) = each %h } { copy => 1 }, 'tied hash each';
67 my @k = check { keys %h } { }, 'tied hash keys';
68 is_deeply [ sort @k ], [ qw/a c/ ], 'copy: tied hash keys correctly';
70 my @v = check { values %h } { copy => 2 }, 'tied hash values';
71 is_deeply [ sort { $a <=> $b } @v ], [ 1, 3 ], 'copy: tied hash values correctly';
73 check { undef %h } { }, 'tied hash undef';