X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F25-copy.t;h=98fbf22d3fcdf378e417ee675e031393dd9b0a1c;hb=a4a7bc01ee056f5f8662ca67bd4aac767e100cb7;hp=83e604a2485b5603cfd0f01870c0c97a36d1c2c0;hpb=a86e3e47a167afadf7de1231d6401a1139330ad0;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/25-copy.t b/t/25-copy.t index 83e604a..98fbf22 100644 --- a/t/25-copy.t +++ b/t/25-copy.t @@ -5,81 +5,84 @@ use warnings; use Test::More; -use Variable::Magic qw/wizard cast MGf_COPY/; +use Variable::Magic qw/cast dispell MGf_COPY/; if (MGf_COPY) { - plan tests => 1 + 8 + 14; + plan tests => 2 + ((2 * 5 + 3) + (2 * 2 + 1)) + (2 * 9 + 6) + 1; } else { - plan skip_all => 'No copy magic for this perl' if !MGf_COPY; + plan skip_all => 'No copy magic for this perl'; } -my $c = 0; -my $wiz = wizard 'copy' => sub { ++$c }; -is($c, 0, 'copy : create wizard'); +use lib 't/lib'; +use Variable::Magic::TestWatcher; +use Variable::Magic::TestValue; + +my $wiz = init_watcher 'copy', 'copy'; SKIP: { eval "use Tie::Array"; - skip 'Tie::Array required to test copy magic on arrays', 8 if $@; + skip 'Tie::Array required to test copy magic on arrays' + => (2 * 5 + 3) + (2 * 2 + 1) if $@; + diag "Using Tie::Array $Tie::Array::VERSION" if defined $Tie::Array::VERSION; tie my @a, 'Tie::StdArray'; @a = (1 .. 10); - my $res = cast @a, $wiz; - ok($res, 'copy : cast on array succeeded'); - is($c, 0, 'copy : cast on array didn\'t triggered the callback'); + my $res = watch { cast @a, $wiz } { }, 'cast on tied array'; + ok $res, 'copy: cast on tied array succeeded'; + + watch { $a[3] = 13 } { copy => 1 }, 'tied array store'; + + my $s = watch { $a[3] } { copy => 1 }, 'tied array fetch'; + is $s, 13, 'copy: tied array fetch correctly'; + + $s = watch { exists $a[3] } { copy => 1 }, 'tied array exists'; + ok $s, 'copy: tied array exists correctly'; + + watch { undef @a } { }, 'tied array undef'; - $a[3] = 13; - is($c, 1, 'copy : callback triggers on array store'); + { + tie my @val, 'Tie::StdArray'; + @val = (4 .. 6); - my $s = $a[3]; - is($c, 2, 'copy : callback triggers on array fetch'); - is($s, 13, 'copy : array fetch is correct'); + my $wv = init_value @val, 'copy', 'copy'; - $s = exists $a[3]; - is($c, 3, 'copy : callback triggers on array exists'); - ok($s, 'copy : array exists is correct'); + value { $val[3] = 8 } [ 4 .. 6 ]; - undef @a; - is($c, 3, 'copy : callback doesn\'t trigger on array undef'); + 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', 14 if $@; + 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; tie my %h, 'Tie::StdHash'; %h = (a => 1, b => 2, c => 3); - $c = 0; - my $res = cast %h, $wiz; - ok($res, 'copy : cast on hash succeeded'); - is($c, 0, 'copy : cast on hash didn\'t triggered the callback'); + my $res = watch { cast %h, $wiz } { }, 'cast on tied hash'; + ok $res, 'copy: cast on tied hash succeeded'; - $h{b} = 7; - is($c, 1, 'copy : callback triggers on hash store'); + watch { $h{b} = 7 } { copy => 1 }, 'tied hash store'; - my $s = $h{c}; - is($c, 2, 'copy : callback triggers on hash fetch'); - is($s, 3, 'copy : hash fetch is correct'); + my $s = watch { $h{c} } { copy => 1 }, 'tied hash fetch'; + is $s, 3, 'copy: tied hash fetch correctly'; - $s = exists $h{a}; - is($c, 3, 'copy : callback triggers on hash exists'); - ok($s, 'copy : hash exists is correct'); + $s = watch { exists $h{a} } { copy => 1 }, 'tied hash exists'; + ok $s, 'copy: tied hash exists correctly'; - $s = delete $h{b}; - is($c, 4, 'copy : callback triggers on hash delete'); - is($s, 7, 'copy : hash delete is correct'); + $s = watch { delete $h{b} } { copy => 1 }, 'tied hash delete'; + is $s, 7, 'copy: tied hash delete correctly'; - my ($k, $v) = each %h; - is($c, 5, 'copy : callback triggers on hash each'); + watch { my ($k, $v) = each %h } { copy => 1 }, 'tied hash each'; - my @k = keys %h; - is($c, 5, 'copy : callback doesn\'t trigger on hash keys'); + my @k = watch { keys %h } { }, 'tied hash keys'; + is_deeply [ sort @k ], [ qw/a c/ ], 'copy: tied hash keys correctly'; - my @v = values %h; - is(scalar @v, 2, 'copy : two values in the hash'); - is($c, 7, 'copy : callback triggers on 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'; - undef %h; - is($c, 7, 'copy : callback doesn\'t trigger on hash undef'); + watch { undef %h } { }, 'tied hash undef'; }