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