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