X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F25-copy.t;h=46e324141653d2298c32b85ad5a3b4ce948e379e;hb=aae88f4cf6f2f32553f8cc29cb8f0792cb5cd910;hp=80b3bc95c5212ab1c3c2cd99d2e8cfbcaa5ab418;hpb=18206f59cd87293a083e13340285b1d0c5ad0632;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/25-copy.t b/t/25-copy.t index 80b3bc9..46e3241 100644 --- a/t/25-copy.t +++ b/t/25-copy.t @@ -5,74 +5,98 @@ use warnings; use Test::More; -use Variable::Magic qw/cast MGf_COPY/; +use lib 't/lib'; +use VPIT::TestHelpers; -if (MGf_COPY) { - plan tests => 2 + (2 * 5 + 3) + (2 * 9 + 6) + 1; -} else { - plan skip_all => 'No copy magic for this perl'; -} +use Variable::Magic qw; + +plan tests => 2 + ((2 * 5 + 3) + (2 * 2 + 1)) + (2 * 9 + 6) + 3 + 1; use lib 't/lib'; use Variable::Magic::TestWatcher; +use Variable::Magic::TestValue; -my $wiz = init 'copy', 'copy'; +my $wiz = init_watcher 'copy', 'copy'; SKIP: { - eval "use Tie::Array"; - skip 'Tie::Array required to test copy magic on arrays', 2 * 5 + 3 if $@; - diag "Using Tie::Array $Tie::Array::VERSION" if defined $Tie::Array::VERSION; + load_or_skip('Tie::Array', undef, undef, (2 * 5 + 3) + (2 * 2 + 1)); tie my @a, 'Tie::StdArray'; @a = (1 .. 10); - my $res = check { cast @a, $wiz } { }, 'cast on tied array'; + my $res = watch { cast @a, $wiz } { }, 'cast on tied array'; ok $res, 'copy: cast on tied array succeeded'; - check { $a[3] = 13 } { copy => 1 }, 'tied array store'; + watch { $a[3] = 13 } { copy => 1 }, 'tied array store'; - my $s; - check { $s = $a[3] } { copy => 1 }, 'tied array fetch'; + my $s = watch { $a[3] } { copy => 1 }, 'tied array fetch'; is $s, 13, 'copy: tied array fetch correctly'; - check { $s = exists $a[3] } { copy => 1 }, 'tied array exists'; + $s = watch { exists $a[3] } { copy => 1 }, 'tied array exists'; ok $s, 'copy: tied array exists correctly'; - check { undef @a } { }, 'tied array undef'; + watch { undef @a } { }, 'tied array undef'; + + { + tie my @val, 'Tie::StdArray'; + @val = (4 .. 6); + + my $wv = init_value @val, 'copy', 'copy'; + + value { $val[3] = 8 } [ 4 .. 6 ]; + + dispell @val, $wv; + is_deeply \@val, [ 4 .. 6, 8 ], 'copy: value after'; + } } SKIP: { - eval "use Tie::Hash"; - skip 'Tie::Hash required to test copy magic on hashes' => 2 * 9 + 6 if $@; - diag "Using Tie::Hash $Tie::Hash::VERSION" if defined $Tie::Hash::VERSION; + load_or_skip('Tie::Hash', undef, undef, 2 * 9 + 6); tie my %h, 'Tie::StdHash'; %h = (a => 1, b => 2, c => 3); - my $res = check { cast %h, $wiz } { }, 'cast on tied hash'; + my $res = watch { cast %h, $wiz } { }, 'cast on tied hash'; ok $res, 'copy: cast on tied hash succeeded'; - check { $h{b} = 7 } { copy => 1 }, 'tied hash store'; + watch { $h{b} = 7 } { copy => 1 }, 'tied hash store'; - my $s; - check { $s = $h{c} } { copy => 1 }, 'tied hash fetch'; + my $s = watch { $h{c} } { copy => 1 }, 'tied hash fetch'; is $s, 3, 'copy: tied hash fetch correctly'; - check { $s = exists $h{a} } { copy => 1 }, 'tied hash exists'; + $s = watch { exists $h{a} } { copy => 1 }, 'tied hash exists'; ok $s, 'copy: tied hash exists correctly'; - check { $s = delete $h{b} } { copy => 1 }, 'tied hash delete'; + $s = watch { delete $h{b} } { copy => 1 }, 'tied hash delete'; is $s, 7, 'copy: tied hash delete correctly'; - check { my ($k, $v) = each %h } { copy => 1 }, 'tied hash each'; + watch { my ($k, $v) = each %h } { copy => 1 }, 'tied hash each'; - my @k; - check { @k = keys %h } { }, 'tied hash keys'; - is_deeply [ sort @k ], [ qw/a c/ ], 'copy: tied hash keys correctly'; + my @k = watch { keys %h } { }, 'tied hash keys'; + is_deeply [ sort @k ], [ qw ], 'copy: tied hash keys correctly'; - my @v; - check { @v = values %h } { copy => 2 }, 'tied hash values'; + my @v = watch { values %h } { copy => 2 }, 'tied hash values'; is_deeply [ sort { $a <=> $b } @v ], [ 1, 3 ], 'copy: tied hash values correctly'; - check { undef %h } { }, 'tied hash undef'; + watch { undef %h } { }, 'tied hash undef'; +} + +SKIP: { + skip 'copy magic not called for cloned prototypes before perl 5.17.0' => 3 + unless VMG_COMPAT_CODE_COPY_CLONE; + my $w = wizard copy => sub { + is ref($_[0]), 'CODE', 'first arg in copy on clone is a code ref'; + is $_[2], undef, 'third arg in copy on clone is undef'; + is ref($_[3]), 'CODE', 'fourth arg in copy on clone is a code ref'; + }; + eval <<'TEST_COPY'; + package X; + sub MODIFY_CODE_ATTRIBUTES { + my ($pkg, $sub) = @_; + &Variable::Magic::cast($sub, $w); + return; + } + my $i; + my $f = sub : Hello { $i }; +TEST_COPY }