]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - t/22-len.t
In t/22-len.t, localize the wizard for strings to the SKIP block
[perl/modules/Variable-Magic.git] / t / 22-len.t
index 3b8039efc1f926af430c4c5da0eda14235d5aa19..d4f59d15fd9cad882626e783b5fa0d9d1001b689 100644 (file)
@@ -3,9 +3,12 @@
 use strict;
 use warnings;
 
-use Test::More tests => 33;
+use Test::More tests => 39 + (2 * 2 + 1);
 
-use Variable::Magic qw/wizard cast VMG_COMPAT_SCALAR_LENGTH_NOLEN/;
+use Variable::Magic qw<wizard cast dispell VMG_COMPAT_SCALAR_LENGTH_NOLEN>;
+
+use lib 't/lib';
+use Variable::Magic::TestValue;
 
 my $c = 0;
 
@@ -14,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<a b c>;
 
 $c = 0;
 cast @a, $wiz;
@@ -67,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;
 
@@ -124,3 +127,43 @@ SKIP: {
  is $d, 5,  'len: get utf8 scalar length have correct default length';
  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);
+
+ my $wv = init_value @val, 'len', 'len';
+
+ value { $val[-1] = 8 } [ 4, 5, 6 ];
+
+ dispell @val, $wv;
+ is_deeply \@val, [ 4, 5, 8 ], 'len: after value';
+}