X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F25-copy.t;h=394449541b3ab123f7ff015b0bf16834ab77c3a1;hb=93df7812b9a0da8cdfa57a107eb2f8f4b4744b49;hp=83e604a2485b5603cfd0f01870c0c97a36d1c2c0;hpb=a86e3e47a167afadf7de1231d6401a1139330ad0;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/25-copy.t b/t/25-copy.t index 83e604a..3944495 100644 --- a/t/25-copy.t +++ b/t/25-copy.t @@ -5,81 +5,78 @@ use warnings; use Test::More; -use Variable::Magic qw/wizard cast MGf_COPY/; +use lib 't/lib'; +use VPIT::TestHelpers; -if (MGf_COPY) { - plan tests => 1 + 8 + 14; -} else { - plan skip_all => 'No copy magic for this perl' if !MGf_COPY; -} +use Variable::Magic qw; + +plan tests => 2 + ((2 * 5 + 3) + (2 * 2 + 1)) + (2 * 9 + 6) + 1; -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 $@; + load_or_skip('Tie::Array', undef, undef, (2 * 5 + 3) + (2 * 2 + 1)); 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 $@; + load_or_skip('Tie::Hash', undef, undef, 2 * 9 + 6); 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 ], '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'; }