6 use Test::More tests => 2 * 27 + 13 + 1;
8 use Variable::Magic qw/cast dispell VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID VMG_COMPAT_ARRAY_UNDEF_CLEAR/;
11 use Variable::Magic::TestWatcher;
13 my $wiz = init_watcher
14 [ qw/get set len clear free copy dup local fetch store exists delete/ ],
17 my @n = map { int rand 1000 } 1 .. 5;
20 watch { cast @a, $wiz } { }, 'cast';
22 my $b = watch { $a[2] } { }, 'assign element to';
23 is $b, $n[2], 'array: assign element to correctly';
25 my @b = watch { @a } { len => 1 }, 'assign to';
26 is_deeply \@b, \@n, 'array: assign to correctly';
28 $b = watch { "X@{a}Y" } { len => 1 }, 'interpolate';
29 is $b, "X@{n}Y", 'array: interpolate correctly';
31 $b = watch { \@a } { }, 'reference';
33 @b = watch { @a[2 .. 4] } { }, 'slice';
34 is_deeply \@b, [ @n[2 .. 4] ], 'array: slice correctly';
36 watch { @a = qw/a b d/ } { set => 3, clear => 1 }, 'assign';
38 watch { $a[2] = 'c' } { }, 'assign old element';
40 watch { $a[4] = 'd' } { set => 1 }, 'assign new element';
42 $b = watch { exists $a[4] } { }, 'exists';
43 is $b, 1, 'array: exists correctly';
45 $b = watch { delete $a[4] } { set => 1 }, 'delete';
46 is $b, 'd', 'array: delete correctly';
48 $b = watch { @a } { len => 1 }, 'length @';
49 is $b, 3, 'array: length @ correctly';
51 # $b has to be set inside the block for the test to pass on 5.8.3 and lower
52 watch { $b = $#a } { len => 1 }, 'length $#';
53 is $b, 2, 'array: length $# correctly';
55 watch { push @a, 'x'; () }
56 { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN },'push (void)';
58 $b = watch { push @a, 'y' }
59 { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN }, 'push (scalar)';
60 is $b, 5, 'array: push (scalar) correctly';
62 $b = watch { pop @a } { set => 1, len => 1 }, 'pop';
63 is $b, 'y', 'array: pop correctly';
65 watch { unshift @a, 'z'; () }
66 { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID },
69 $b = watch { unshift @a, 't' } { set => 1, len => 1 }, 'unshift (scalar)';
70 is $b, 6, 'unshift (scalar) correctly';
72 $b = watch { shift @a } { set => 1, len => 1 }, 'shift';
73 is $b, 't', 'array: shift correctly';
75 watch { my $i; @a = map ++$i, @a; () } { set => 5, len => 1, clear => 1}, 'map';
77 @b = watch { grep { $_ >= 4 } @a } { len => 1 }, 'grep';
78 is_deeply \@b, [ 4 .. 5 ], 'array: grep correctly';
80 watch { 1 for @a } { len => 5 + 1 }, 'for';
84 watch { cast @b, $wiz } { }, 'cast 2';
85 } { free => 1 }, 'scope end';
87 watch { undef @a } +{ (clear => 1) x VMG_COMPAT_ARRAY_UNDEF_CLEAR }, 'undef';
89 watch { dispell @a, $wiz } { }, 'dispell';