6 use Test::More tests => 2 * 27 + 13 + 1;
8 use Variable::Magic qw<
10 VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
11 VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID
12 VMG_COMPAT_ARRAY_UNDEF_CLEAR
16 use Variable::Magic::TestWatcher;
18 my $wiz = init_watcher
19 [ qw<get set len clear free copy dup local fetch store exists delete> ],
22 my @n = map { int rand 1000 } 1 .. 5;
25 watch { cast @a, $wiz } { }, 'cast';
27 my $b = watch { $a[2] } { }, 'assign element to';
28 is $b, $n[2], 'array: assign element to correctly';
30 my @b = watch { @a } { len => 1 }, 'assign to';
31 is_deeply \@b, \@n, 'array: assign to correctly';
33 $b = watch { "X@{a}Y" } { len => 1 }, 'interpolate';
34 is $b, "X@{n}Y", 'array: interpolate correctly';
36 $b = watch { \@a } { }, 'reference';
38 @b = watch { @a[2 .. 4] } { }, 'slice';
39 is_deeply \@b, [ @n[2 .. 4] ], 'array: slice correctly';
41 watch { @a = qw<a b d> } { set => 3, clear => 1 }, 'assign';
43 watch { $a[2] = 'c' } { }, 'assign old element';
45 watch { $a[4] = 'd' } { set => 1 }, 'assign new element';
47 $b = watch { exists $a[4] } { }, 'exists';
48 is $b, 1, 'array: exists correctly';
50 $b = watch { delete $a[4] } { set => 1 }, 'delete';
51 is $b, 'd', 'array: delete correctly';
53 $b = watch { @a } { len => 1 }, 'length @';
54 is $b, 3, 'array: length @ correctly';
56 # $b has to be set inside the block for the test to pass on 5.8.3 and lower
57 watch { $b = $#a } { len => 1 }, 'length $#';
58 is $b, 2, 'array: length $# correctly';
60 watch { push @a, 'x'; () } # push looks at the static context
61 { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID },
64 $b = watch { push @a, 'y' }
65 { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN },
67 is $b, 5, 'array: push (scalar) correctly';
69 $b = watch { pop @a } { set => 1, len => 1 }, 'pop';
70 is $b, 'y', 'array: pop correctly';
72 watch { unshift @a, 'z'; () } # unshift looks at the static context
73 { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID },
76 $b = watch { unshift @a, 't' } { set => 1, len => 1 }, 'unshift (scalar)';
77 is $b, 6, 'unshift (scalar) correctly';
79 $b = watch { shift @a } { set => 1, len => 1 }, 'shift';
80 is $b, 't', 'array: shift correctly';
82 watch { my $i; @a = map ++$i, @a } { set => 5, len => 1, clear => 1}, 'map';
84 @b = watch { grep { $_ >= 4 } @a } { len => 1 }, 'grep';
85 is_deeply \@b, [ 4 .. 5 ], 'array: grep correctly';
87 watch { 1 for @a } { len => 5 + 1 }, 'for';
91 watch { cast @b, $wiz } { }, 'cast 2';
92 } { free => 1 }, 'scope end';
94 watch { undef @a } +{ (clear => 1) x VMG_COMPAT_ARRAY_UNDEF_CLEAR }, 'undef';
96 watch { dispell @a, $wiz } { }, 'dispell';