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';
35 check { $s = $a[3] } { copy => 1 }, 'tied array fetch';
36 is $s, 13, 'copy: tied array fetch correctly';
38 check { $s = exists $a[3] } { copy => 1 }, 'tied array exists';
39 ok $s, 'copy: tied array exists correctly';
41 check { undef @a } { }, 'tied array undef';
46 skip 'Tie::Hash required to test copy magic on hashes' => 2 * 9 + 6 if $@;
47 diag "Using Tie::Hash $Tie::Hash::VERSION" if defined $Tie::Hash::VERSION;
49 tie my %h, 'Tie::StdHash';
50 %h = (a => 1, b => 2, c => 3);
52 my $res = check { cast %h, $wiz } { }, 'cast on tied hash';
53 ok $res, 'copy: cast on tied hash succeeded';
55 check { $h{b} = 7 } { copy => 1 }, 'tied hash store';
58 check { $s = $h{c} } { copy => 1 }, 'tied hash fetch';
59 is $s, 3, 'copy: tied hash fetch correctly';
61 check { $s = exists $h{a} } { copy => 1 }, 'tied hash exists';
62 ok $s, 'copy: tied hash exists correctly';
64 check { $s = delete $h{b} } { copy => 1 }, 'tied hash delete';
65 is $s, 7, 'copy: tied hash delete correctly';
67 check { my ($k, $v) = each %h } { copy => 1 }, 'tied hash each';
70 check { @k = keys %h } { }, 'tied hash keys';
71 is_deeply [ sort @k ], [ qw/a c/ ], 'copy: tied hash keys correctly';
74 check { @v = values %h } { copy => 2 }, 'tied hash values';
75 is_deeply [ sort { $a <=> $b } @v ], [ 1, 3 ], 'copy: tied hash values correctly';
77 check { undef %h } { }, 'tied hash undef';