6 use Test::More tests => 39 + (2 * 2 + 1) + (5 + 2 * 3);
8 use Variable::Magic qw<
10 VMG_COMPAT_SCALAR_LENGTH_NOLEN VMG_COMPAT_SCALAR_NOLEN
14 use Variable::Magic::TestValue;
18 my $n = 1 + int rand 1000;
20 my $wiz = wizard len => sub { $d = $_[2]; ++$c; return $n };
21 is $c, 0, 'len: wizard() doesn\'t trigger magic';
27 is $c, 0, 'len: cast on array doesn\'t trigger magic';
32 is $c, 1, 'len: get array length triggers magic correctly';
33 is $d, 3, 'len: get array length have correct default length';
34 is $b, $n, 'len: get array length correctly';
39 is $c, 1, 'len: get last array index triggers magic correctly';
40 is $d, 3, 'len: get last array index have correct default length';
41 is $b, $n - 1, 'len: get last array index correctly';
48 is $c, 1, 'len: get array length 0 triggers magic correctly';
49 is $d, 3, 'len: get array length 0 have correct default length';
50 is $b, 0, 'len: get array length 0 correctly';
59 is $c, 1, 'len: get empty array length triggers magic correctly';
60 is $d, 0, 'len: get empty array length have correct default length';
61 is $b, 0, 'len: get empty array length correctly';
66 is $c, 1, 'len: get last empty array index triggers magic correctly';
67 is $d, 0, 'len: get last empty array index have correct default length';
68 is $b, -1, 'len: get last empty array index correctly';
71 skip 'len magic is no longer called for scalars' => 16 + 6
72 if VMG_COMPAT_SCALAR_NOLEN;
75 skip 'length() no longer calls len magic on plain scalars' => 16
76 if VMG_COMPAT_SCALAR_LENGTH_NOLEN;
79 $n = 1 + int rand 1000;
80 # length magic on scalars needs also get magic to be triggered.
81 my $wiz = wizard get => sub { return 'anything' },
82 len => sub { $d = $_[2]; ++$c; return $n };
88 is $c, 0, 'len: cast on scalar doesn\'t trigger magic';
93 is $c, 1, 'len: get scalar length triggers magic correctly';
94 is $d, 4, 'len: get scalar length have correct default length';
95 is $b, $n, 'len: get scalar length correctly';
102 is $c, 1, 'len: get scalar length 0 triggers magic correctly';
103 is $d, 4, 'len: get scalar length 0 have correct default length';
104 is $b, $n, 'len: get scalar length 0 correctly';
113 is $c, 1, 'len: get empty scalar length triggers magic correctly';
114 is $d, 0, 'len: get empty scalar length have correct default length';
115 is $b, 0, 'len: get empty scalar length correctly';
126 is $c, 1, 'len: get utf8 scalar length in bytes triggers magic correctly';
127 is $d, 7, 'len: get utf8 scalar length in bytes have correct default length';
128 is $b, $d,'len: get utf8 scalar length in bytes correctly';
134 is $c, 1, 'len: get utf8 scalar length triggers magic correctly';
135 is $d, 5, 'len: get utf8 scalar length have correct default length';
136 is $b, $d, 'len: get utf8 scalar length correctly';
141 # length magic on scalars needs also get magic to be triggered.
142 my $wiz = wizard get => sub { 0 },
143 len => sub { $d = $_[2]; ++$c; return $_[2] };
151 is $c, 1, 'len: pos scalar triggers magic correctly';
152 is $d, 6, 'len: pos scalar have correct default length';
153 is $x, 'banana', 'len: pos scalar works correctly'
157 my $x = "hl\x{20AB}gh"; # Force utf8 on string
161 substr($x, 2, 1) = 'a';
162 is $c, 1, 'len: substr utf8 scalar triggers magic correctly';
163 is $d, 5, 'len: substr utf8 scalar have correct default length';
164 is $x, 'hlagh', 'len: substr utf8 scalar correctly';
172 my $wv = init_value @val, 'len', 'len';
174 value { $val[-1] = 8 } [ 4, 5, 6 ];
177 is_deeply \@val, [ 4, 5, 8 ], 'len: after value';
183 my $wua = eval { wizard len => \undef };
184 is $@, '', 'len: noop wizard (for arrays) creation does not croak';
186 my @a = ('a' .. 'z');
187 eval { cast @a, $wua };
188 is $@, '', 'len: noop wizard (for arrays) cast does not croak';
192 is $@, '', 'len: noop wizard (for arrays) invocation does not croak';
193 is $l, 25, 'len: noop magic on an array returns the previous length';
195 my $wus = eval { wizard get => \undef, len => \undef };
196 is $@, '', 'len: noop wizard (for strings) creation does not croak';
198 for ([ 'euro', 'string' ], [ "\x{20AC}uro", 'unicode string' ]) {
199 my ($euro, $desc) = @$_;
201 eval { cast $euro, $wus };
202 is $@, '', 'len: noop wizard (for strings) cast does not croak';
204 eval { pos($euro) = 2 };
205 is $@, '', 'len: noop wizard (for strings) invocation does not croak';
207 my ($rest) = ($euro =~ /(.*)/g);
208 is $rest, 'ro', "len: noop magic on a $desc returns the previous length";