1#!perl -T 2 3use strict; 4use warnings; 5 6use Test::More tests => 2 * 27 + 13 + 1; 7 8use Variable::Magic qw/cast dispell VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID VMG_COMPAT_ARRAY_UNDEF_CLEAR/; 9 10use lib 't/lib'; 11use Variable::Magic::TestWatcher; 12 13my $wiz = init_watcher 14 [ qw/get set len clear free copy dup local fetch store exists delete/ ], 15 'array'; 16 17my @n = map { int rand 1000 } 1 .. 5; 18my @a = @n; 19 20watch { cast @a, $wiz } { }, 'cast'; 21 22my $b = watch { $a[2] } { }, 'assign element to'; 23is $b, $n[2], 'array: assign element to correctly'; 24 25my @b = watch { @a } { len => 1 }, 'assign to'; 26is_deeply \@b, \@n, 'array: assign to correctly'; 27 28$b = watch { "X@{a}Y" } { len => 1 }, 'interpolate'; 29is $b, "X@{n}Y", 'array: interpolate correctly'; 30 31$b = watch { \@a } { }, 'reference'; 32 33@b = watch { @a[2 .. 4] } { }, 'slice'; 34is_deeply \@b, [ @n[2 .. 4] ], 'array: slice correctly'; 35 36watch { @a = qw/a b d/ } { set => 3, clear => 1 }, 'assign'; 37 38watch { $a[2] = 'c' } { }, 'assign old element'; 39 40watch { $a[4] = 'd' } { set => 1 }, 'assign new element'; 41 42$b = watch { exists $a[4] } { }, 'exists'; 43is $b, 1, 'array: exists correctly'; 44 45$b = watch { delete $a[4] } { set => 1 }, 'delete'; 46is $b, 'd', 'array: delete correctly'; 47 48$b = watch { @a } { len => 1 }, 'length @'; 49is $b, 3, 'array: length @ correctly'; 50 51# $b has to be set inside the block for the test to pass on 5.8.3 and lower 52watch { $b = $#a } { len => 1 }, 'length $#'; 53is $b, 2, 'array: length $# correctly'; 54 55watch { push @a, 'x'; () } 56 { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID }, 57 'push (void)'; 58 59$b = watch { push @a, 'y' } 60 { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN }, 61 'push (scalar)'; 62is $b, 5, 'array: push (scalar) correctly'; 63 64$b = watch { pop @a } { set => 1, len => 1 }, 'pop'; 65is $b, 'y', 'array: pop correctly'; 66 67watch { unshift @a, 'z'; () } 68 { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID }, 69 'unshift (void)'; 70 71$b = watch { unshift @a, 't' } { set => 1, len => 1 }, 'unshift (scalar)'; 72is $b, 6, 'unshift (scalar) correctly'; 73 74$b = watch { shift @a } { set => 1, len => 1 }, 'shift'; 75is $b, 't', 'array: shift correctly'; 76 77watch { my $i; @a = map ++$i, @a; () } { set => 5, len => 1, clear => 1}, 'map'; 78 79@b = watch { grep { $_ >= 4 } @a } { len => 1 }, 'grep'; 80is_deeply \@b, [ 4 .. 5 ], 'array: grep correctly'; 81 82watch { 1 for @a } { len => 5 + 1 }, 'for'; 83 84watch { 85 my @b = @n; 86 watch { cast @b, $wiz } { }, 'cast 2'; 87} { free => 1 }, 'scope end'; 88 89watch { undef @a } +{ (clear => 1) x VMG_COMPAT_ARRAY_UNDEF_CLEAR }, 'undef'; 90 91watch { dispell @a, $wiz } { }, 'dispell'; 92