X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F31-array.t;h=3613dba460618f33ac1e085e713c065c67c54b47;hb=HEAD;hp=bb2f96e402b5101e61e627c2739229709fa7ae72;hpb=14f66d40970bef63105be046a109c1a32859a8a0;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/31-array.t b/t/31-array.t index bb2f96e..3613dba 100644 --- a/t/31-array.t +++ b/t/31-array.t @@ -3,101 +3,94 @@ use strict; use warnings; -use Test::More tests => 21; +use Test::More tests => 2 * 27 + 13 + 1; -use Variable::Magic qw/wizard cast dispell/; +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 +>; -my @c = (0) x 5; -my @x = (0) x 5; +use lib 't/lib'; +use Variable::Magic::TestWatcher; -sub check { - for (0 .. 4) { return 0 unless $c[$_] == $x[$_]; } - return 1; -} - -my $wiz = wizard get => sub { ++$c[0] }, - set => sub { ++$c[1] }, - len => sub { ++$c[2]; $_[2] }, - clear => sub { ++$c[3] }, - free => sub { ++$c[4] }; -ok(check(), 'array : create wizard'); +my $wiz = init_watcher + [ qw ], + 'array'; my @n = map { int rand 1000 } 1 .. 5; my @a = @n; -cast @a, $wiz; -ok(check(), 'array : cast'); +watch { cast @a, $wiz } { }, 'cast'; + +my $b = watch { $a[2] } { }, 'assign element to'; +is $b, $n[2], 'array: assign element to correctly'; + +my @b = watch { @a } { len => 1 }, 'assign to'; +is_deeply \@b, \@n, 'array: assign to correctly'; + +$b = watch { "X@{a}Y" } { len => 1 }, 'interpolate'; +is $b, "X@{n}Y", 'array: interpolate correctly'; + +$b = watch { \@a } { }, 'reference'; + +@b = watch { @a[2 .. 4] } { }, 'slice'; +is_deeply \@b, [ @n[2 .. 4] ], 'array: slice correctly'; -my $b = $a[2]; -ok(check(), 'array : assign element to'); +watch { @a = qw } { set => 3, clear => 1 }, 'assign'; -my @b = @a; -++$x[2]; -ok(check(), 'array : assign to'); +watch { $a[2] = 'c' } { }, 'assign old element'; -$b = "X@{a}Y"; -++$x[2]; -ok(check(), 'array : interpolate'); +watch { $a[4] = 'd' } { set => 1 }, 'assign new element'; -$b = \@a; -ok(check(), 'array : reference'); +$b = watch { exists $a[4] } { }, 'exists'; +is $b, 1, 'array: exists correctly'; -@b = @a[2 .. 4]; -ok(check(), 'array : slice'); +$b = watch { delete $a[4] } { set => 1 }, 'delete'; +is $b, 'd', 'array: delete correctly'; -@a = qw/a b d/; -$x[1] += 3; ++$x[3]; -ok(check(), 'array : assign'); +$b = watch { @a } { len => 1 }, 'length @'; +is $b, 3, 'array: length @ correctly'; -$a[2] = 'c'; -ok(check(), 'array : assign old element'); +# $b has to be set inside the block for the test to pass on 5.8.3 and lower +watch { $b = $#a } { len => 1 }, 'length $#'; +is $b, 2, 'array: length $# correctly'; -$a[3] = 'd'; -++$x[1]; -ok(check(), 'array : assign new element'); +watch { push @a, 'x'; () } # push looks at the static context + { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID }, + 'push (void)'; -push @a, 'x'; -++$x[1]; ++$x[2] unless $^V && $^V gt 5.9.2; # since 5.9.3 -ok(check(), 'array : push'); +$b = watch { push @a, 'y' } + { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN }, + 'push (scalar)'; +is $b, 5, 'array: push (scalar) correctly'; -pop @a; -++$x[1]; ++$x[2]; -ok(check(), 'array : pop'); +$b = watch { pop @a } { set => 1, len => 1 }, 'pop'; +is $b, 'y', 'array: pop correctly'; -unshift @a, 'x'; -++$x[1]; ++$x[2]; -ok(check(), 'array : unshift'); +watch { unshift @a, 'z'; () } # unshift looks at the static context + { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID }, + 'unshift (void)'; -shift @a; -++$x[1]; ++$x[2]; -ok(check(), 'array : shift'); +$b = watch { unshift @a, 't' } { set => 1, len => 1 }, 'unshift (scalar)'; +is $b, 6, 'unshift (scalar) correctly'; -$b = @a; -++$x[2]; -ok(check(), 'array : length'); +$b = watch { shift @a } { set => 1, len => 1 }, 'shift'; +is $b, 't', 'array: shift correctly'; -@a = map ord, @a; -$x[1] += 4; ++$x[2]; ++$x[3]; -ok(check(), 'array : map'); +watch { my $i; @a = map ++$i, @a } { set => 5, len => 1, clear => 1}, 'map'; -@b = grep { defined && $_ >= ord('b') } @a; -++$x[2]; -ok(check(), 'array : grep'); +@b = watch { grep { $_ >= 4 } @a } { len => 1 }, 'grep'; +is_deeply \@b, [ 4 .. 5 ], 'array: grep correctly'; -for (@a) { } -$x[2] += 5; -ok(check(), 'array : for'); +watch { 1 for @a } { len => 5 + 1 }, 'for'; -{ +watch { my @b = @n; - cast @b, $wiz; -} -++$x[4]; -ok(check(), 'array : scope end'); + watch { cast @b, $wiz } { }, 'cast 2'; +} { free => 1 }, 'scope end'; -undef @a; -++$x[3] if $^V && $^V gt 5.9.4; # since 5.9.5 - see #43357 -ok(check(), 'array : undef'); +watch { undef @a } +{ (clear => 1) x VMG_COMPAT_ARRAY_UNDEF_CLEAR }, 'undef'; -dispell @a, $wiz; -ok(check(), 'array : dispel'); +watch { dispell @a, $wiz } { }, 'dispell';