X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F22-len.t;h=3b8039efc1f926af430c4c5da0eda14235d5aa19;hb=ed45fb4404201c2e17ffa5c26a2320ceeb132e61;hp=8ea332c5fc8603ff3405370699604bcd62aba468;hpb=91aec4cfae75e61ff8eeb79448501a8739b0d240;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/22-len.t b/t/22-len.t index 8ea332c..3b8039e 100644 --- a/t/22-len.t +++ b/t/22-len.t @@ -3,48 +3,124 @@ use strict; use warnings; -use Test::More tests => 11; +use Test::More tests => 33; 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 }; -is($c, 0, 'len : create wizard'); + +my $n = 1 + int rand 1000; +my $d; +my $wiz = wizard len => sub { $d = $_[2]; ++$c; return $n }; +is $c, 0, 'len: wizard() doesn\'t trigger magic'; my @a = qw/a b c/; +$c = 0; cast @a, $wiz; -is($c, 0, 'len : cast on array'); +is $c, 0, 'len: cast on array doesn\'t trigger magic'; +$c = 0; +$d = undef; my $b = scalar @a; -is($c, 1, 'len : get array length'); -is($b, $n, 'len : get array length correctly'); +is $c, 1, 'len: get array length triggers magic correctly'; +is $d, 3, 'len: get array length have correct default length'; +is $b, $n, 'len: get array length correctly'; +$c = 0; +$d = undef; $b = $#a; -is($c, 2, 'len : get last array index'); -is($b, $n - 1, 'len : get last array index correctly'); +is $c, 1, 'len: get last array index triggers magic correctly'; +is $d, 3, 'len: get last array index have correct default length'; +is $b, $n - 1, 'len: get last array index correctly'; $n = 0; + +$c = 0; +$d = undef; $b = scalar @a; -is($c, 3, 'len : get array length 0'); -is($b, 0, 'len : get array length 0 correctly'); +is $c, 1, 'len: get array length 0 triggers magic correctly'; +is $d, 3, 'len: get array length 0 have correct default length'; +is $b, 0, 'len: get array length 0 correctly'; + +$n = undef; +@a = (); +cast @a, $wiz; $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 }; +$d = undef; +$b = scalar @a; +is $c, 1, 'len: get empty array length triggers magic correctly'; +is $d, 0, 'len: get empty array length have correct default length'; +is $b, 0, 'len: get empty array length correctly'; -my $x = int rand 1000; +$c = 0; +$d = undef; +$b = $#a; +is $c, 1, 'len: get last empty array index triggers magic correctly'; +is $d, 0, 'len: get last empty array index have correct default length'; +is $b, -1, 'len: get last empty array index correctly'; SKIP: { - skip 'length() no longer calls mg_len magic', 3 if VMG_COMPAT_SCALAR_LENGTH_NOLEN; + skip 'length() no longer calls mg_len magic' => 16 if VMG_COMPAT_SCALAR_LENGTH_NOLEN; + + $c = 0; + $n = 1 + int rand 1000; + # length magic on scalars needs also get magic to be triggered. + $wiz = wizard get => sub { return 'anything' }, + len => sub { $d = $_[2]; ++$c; return $n }; + + my $x = 6789; + $c = 0; cast $x, $wiz; - is($c, 0, 'len : cast on scalar'); + is $c, 0, 'len: cast on scalar doesn\'t trigger magic'; + + $c = 0; + $d = undef; + $b = length $x; + is $c, 1, 'len: get scalar length triggers magic correctly'; + is $d, 4, 'len: get scalar length have correct default length'; + is $b, $n, 'len: get scalar length correctly'; + + $n = 0; + + $c = 0; + $d = undef; + $b = length $x; + is $c, 1, 'len: get scalar length 0 triggers magic correctly'; + is $d, 4, 'len: get scalar length 0 have correct default length'; + is $b, $n, 'len: get scalar length 0 correctly'; + + $n = undef; + $x = ''; + cast $x, $wiz; + + $c = 0; + $d = undef; + $b = length $x; + is $c, 1, 'len: get empty scalar length triggers magic correctly'; + is $d, 0, 'len: get empty scalar length have correct default length'; + is $b, 0, 'len: get empty scalar length correctly'; + + $x = "\x{20AB}ongs"; + cast $x, $wiz; + + { + use bytes; + + $c = 0; + $d = undef; + $b = length $x; + is $c, 1, 'len: get utf8 scalar length in bytes triggers magic correctly'; + is $d, 7, 'len: get utf8 scalar length in bytes have correct default length'; + is $b, $d, 'len: get utf8 scalar length in bytes correctly'; + } + $c = 0; + $d = undef; $b = length $x; - is($c, 1, 'len : get scalar length'); - is($b, $n - 1, 'len : get scalar length correctly'); + is $c, 1, 'len: get utf8 scalar length triggers magic correctly'; + is $d, 5, 'len: get utf8 scalar length have correct default length'; + is $b, $d, 'len: get utf8 scalar length correctly'; }