X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F22-len.t;h=d4f59d15fd9cad882626e783b5fa0d9d1001b689;hb=99a180a4963343a5ce0a7336e6ea54e47630d099;hp=7025e7bb1ffce743f0d70f5a83aef9a72dec3cac;hpb=b9df7824d4619174e28f9b1b5856c4a228d9cc7b;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/22-len.t b/t/22-len.t index 7025e7b..d4f59d1 100644 --- a/t/22-len.t +++ b/t/22-len.t @@ -3,9 +3,9 @@ use strict; use warnings; -use Test::More tests => 33 + (2 * 2 + 1); +use Test::More tests => 39 + (2 * 2 + 1); -use Variable::Magic qw/wizard cast dispell VMG_COMPAT_SCALAR_LENGTH_NOLEN/; +use Variable::Magic qw; use lib 't/lib'; use Variable::Magic::TestValue; @@ -17,7 +17,7 @@ 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/; +my @a = qw; $c = 0; cast @a, $wiz; @@ -70,8 +70,8 @@ SKIP: { $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 $wiz = wizard get => sub { return 'anything' }, + len => sub { $d = $_[2]; ++$c; return $n }; my $x = 6789; @@ -128,6 +128,35 @@ SKIP: { is $b, $d, 'len: get utf8 scalar length correctly'; } +{ + our $c; + # length magic on scalars needs also get magic to be triggered. + my $wiz = wizard get => sub { 0 }, + len => sub { $d = $_[2]; ++$c; return $_[2] }; + + { + my $x = "banana"; + cast $x, $wiz; + + local $c = 0; + pos($x) = 2; + is $c, 1, 'len: pos scalar triggers magic correctly'; + is $d, 6, 'len: pos scalar have correct default length'; + is $x, 'banana', 'len: pos scalar works correctly' + } + + { + my $x = "hl\x{20AB}gh"; # Force utf8 on string + cast $x, $wiz; + + local $c = 0; + substr($x, 2, 1) = 'a'; + is $c, 1, 'len: substr utf8 scalar triggers magic correctly'; + is $d, 5, 'len: substr utf8 scalar have correct default length'; + is $x, 'hlagh', 'len: substr utf8 scalar correctly'; + } +} + { my @val = (4 .. 6);