X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F22-len.t;h=8ea332c5fc8603ff3405370699604bcd62aba468;hb=91aec4cfae75e61ff8eeb79448501a8739b0d240;hp=c3a95663e0af476f698d59bf278e6ccfafb1f307;hpb=14f66d40970bef63105be046a109c1a32859a8a0;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/22-len.t b/t/22-len.t index c3a9566..8ea332c 100644 --- a/t/22-len.t +++ b/t/22-len.t @@ -3,25 +3,48 @@ use strict; use warnings; -use Test::More tests => 6; +use Test::More tests => 11; -use Variable::Magic qw/wizard cast/; +use Variable::Magic qw/wizard cast VMG_COMPAT_SCALAR_LENGTH_NOLEN/; my $c = 0; my $n = int rand 1000; my $wiz = wizard len => sub { ++$c; return $n }; -ok($c == 0, 'len : create wizard'); +is($c, 0, 'len : create wizard'); my @a = qw/a b c/; cast @a, $wiz; -ok($c == 0, 'len : cast'); +is($c, 0, 'len : cast on array'); my $b = scalar @a; -ok($c == 1, 'len : get length'); -ok($b == $n, 'len : get length correctly'); +is($c, 1, 'len : get array length'); +is($b, $n, 'len : get array length correctly'); + +$b = $#a; +is($c, 2, 'len : get last array index'); +is($b, $n - 1, 'len : get last array index correctly'); $n = 0; $b = scalar @a; -ok($c == 2, 'len : get length 0'); -ok($b == 0, 'len : get length 0 correctly'); +is($c, 3, 'len : get array length 0'); +is($b, 0, 'len : get array length 0 correctly'); + +$c = 0; +$n = int rand 1000; +# length magic on scalars needs also get magic to be triggered. +$wiz = wizard get => sub { return 56478 }, + len => sub { ++$c; return $n }; + +my $x = int rand 1000; + +SKIP: { + skip 'length() no longer calls mg_len magic', 3 if VMG_COMPAT_SCALAR_LENGTH_NOLEN; + + cast $x, $wiz; + is($c, 0, 'len : cast on scalar'); + + $b = length $x; + is($c, 1, 'len : get scalar length'); + is($b, $n - 1, 'len : get scalar length correctly'); +}