From: Vincent Pit Date: Sat, 24 Apr 2010 21:46:47 +0000 (+0200) Subject: Complete coverage of the len callback X-Git-Tag: v0.42~3 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FVariable-Magic.git;a=commitdiff_plain;h=4488b5731d76409b5901b194129e3475c2fb086c Complete coverage of the len callback --- diff --git a/t/22-len.t b/t/22-len.t index 7025e7b..01a06dd 100644 --- a/t/22-len.t +++ b/t/22-len.t @@ -3,7 +3,7 @@ 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/; @@ -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);