X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F25-copy.t;h=8548db03b6c6d0a42983b73e633f525faf2037fd;hb=refs%2Ftags%2Fv0.08;hp=58cf8e91db6df1ce407174a6a2ebe20528ba8937;hpb=183e73e0590b46550dfa6fdd4b6cf3280c1a5877;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/25-copy.t b/t/25-copy.t index 58cf8e9..8548db0 100644 --- a/t/25-copy.t +++ b/t/25-copy.t @@ -7,57 +7,79 @@ use Test::More; use Variable::Magic qw/wizard cast MGf_COPY/; -if (!MGf_COPY) { - plan skip_all => "this perl doesn't handle copy magic"; +if (MGf_COPY) { + plan tests => 1 + 8 + 14; } else { - plan tests => 16; + plan skip_all => 'No copy magic for this perl' if !MGf_COPY; } my $c = 0; -my $wiz = wizard copy => sub { ++$c }; +my $wiz = wizard 'copy' => sub { ++$c }; ok($c == 0, 'copy : create wizard'); -use Tie::Array; +SKIP: { + eval "use Tie::Array"; + skip 'Tie::Array required to test copy magic on arrays', 8 if $@; -tie my @a, 'Tie::StdArray'; -cast @a, $wiz; -ok($c == 0, 'copy (array) : cast'); + tie my @a, 'Tie::StdArray'; + @a = (1 .. 10); -my $n = time; -$a[0] = $n; -ok($c == 1, 'copy (array) : store element'); + my $res = cast @a, $wiz; + ok($res, 'copy : cast on array succeeded'); + ok($c == 0, 'copy : cast on array didn\'t triggered the callback'); -my $e = exists $a[0]; -ok($c == 2, 'copy (array) : exists element'); -ok($e, 'copy (array) : exists element, really'); + $a[3] = 13; + ok($c == 1, 'copy : callback triggers on array store'); -my $b = $a[0]; -ok($c == 3, 'copy (array) : fetch element'); -ok($b == $n, 'copy (array) : fetch element correctly'); + my $s = $a[3]; + ok($c == 2, 'copy : callback triggers on array fetch'); + ok($s == 13, 'copy : array fetch is correct'); -use Tie::Hash; + $s = exists $a[3]; + ok($c == 3, 'copy : callback triggers on array exists'); + ok($s, 'copy : array exists is correct'); -$c = 0; + undef @a; + ok($c == 3, 'copy : callback doesn\'t trigger on array undef'); +} + +SKIP: { + eval "use Tie::Has"; + skip 'Tie::Hash required to test copy magic on hashes', 14 if $@; + + 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'); + ok($c == 0, 'copy : cast on hash didn\'t triggered the callback'); -tie my %h, 'Tie::StdHash'; -cast %h, $wiz; -ok($c == 0, 'copy (hash) : cast'); + $h{b} = 7; + ok($c == 1, 'copy : callback triggers on hash store'); -my ($k, $v) = (time, int rand time); -$h{$k} = $v; -ok($c == 1, 'copy (hash) : store element'); + my $s = $h{c}; + ok($c == 2, 'copy : callback triggers on hash fetch'); + ok($s == 3, 'copy : hash fetch is correct'); -$e = exists $h{$k}; -ok($c == 2, 'copy (hash) : exists element'); -ok($e, 'copy (hash) : exists element, really'); + $s = exists $h{a}; + ok($c == 3, 'copy : callback triggers on hash exists'); + ok($s, 'copy : hash exists is correct'); -my $w = $h{$k}; -ok($c == 3, 'copy (hash) : fetch element'); -ok($w == $v, 'copy (hash) : fetch element correctly'); + $s = delete $h{b}; + ok($c == 4, 'copy : callback triggers on hash delete'); + ok($s == 7, 'copy : hash delete is correct'); -my ($K, $V) = each %h; -ok($c == 4, 'copy (hash) : iterate'); -ok($k == $K && $v == $V, 'copy (hash) : iterate correctly'); + my ($k, $v) = each %h; + ok($c == 5, 'copy : callback triggers on hash each'); -delete $h{$k}; -ok($c == 5, 'copy (hash) : delete'); + my @k = keys %h; + ok($c == 5, 'copy : callback doesn\'t trigger on hash keys'); + + my @v = values %h; + ok(@v == 2, 'copy : two values in the hash'); + ok($c == 7, 'copy : callback triggers on hash values'); + + undef %h; + ok($c == 7, 'copy : callback doesn\'t trigger on hash undef'); +}