X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F31-array.t;h=3613dba460618f33ac1e085e713c065c67c54b47;hb=HEAD;hp=62f41456e6103af03504cf15f9b0267f2c8030c4;hpb=ad7c749baf8ebc2ff3e49d44b414f67f13f4ebf2;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/31-array.t b/t/31-array.t index 62f4145..3613dba 100644 --- a/t/31-array.t +++ b/t/31-array.t @@ -1,100 +1,96 @@ #!perl -T -use Test::More tests => 21; +use strict; +use warnings; -use Variable::Magic qw/wizard cast dispell/; +use Test::More tests => 2 * 27 + 13 + 1; -my @c = (0) x 5; -my @x = (0) x 5; +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 +>; -sub check { - for (0 .. 4) { return 0 unless $c[$_] == $x[$_]; } - return 1; -} +use lib 't/lib'; +use Variable::Magic::TestWatcher; -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 = $a[2]; -ok(check(), 'array : assign element to'); +my $b = watch { $a[2] } { }, 'assign element to'; +is $b, $n[2], 'array: assign element to correctly'; -my @b = @a; -++$x[2]; -ok(check(), 'array : assign to'); +my @b = watch { @a } { len => 1 }, 'assign to'; +is_deeply \@b, \@n, 'array: assign to correctly'; -$b = "X@{a}Y"; -++$x[2]; -ok(check(), 'array : interpolate'); +$b = watch { "X@{a}Y" } { len => 1 }, 'interpolate'; +is $b, "X@{n}Y", 'array: interpolate correctly'; -$b = \@a; -ok(check(), 'array : reference'); +$b = watch { \@a } { }, 'reference'; -@b = @a[2 .. 4]; -ok(check(), 'array : slice'); +@b = watch { @a[2 .. 4] } { }, 'slice'; +is_deeply \@b, [ @n[2 .. 4] ], 'array: slice correctly'; -@a = qw/a b d/; -$x[1] += 3; ++$x[3]; -ok(check(), 'array : assign'); +watch { @a = qw } { set => 3, clear => 1 }, 'assign'; -$a[2] = 'c'; -ok(check(), 'array : assign old element'); +watch { $a[2] = 'c' } { }, 'assign old element'; -$a[3] = 'd'; -++$x[1]; -ok(check(), 'array : assign new element'); +watch { $a[4] = 'd' } { set => 1 }, 'assign new element'; -push @a, 'x'; -++$x[1]; ++$x[2] unless $^V && $^V gt 5.9.2; # since 5.9.3 -ok(check(), 'array : push'); +$b = watch { exists $a[4] } { }, 'exists'; +is $b, 1, 'array: exists correctly'; -pop @a; -++$x[1]; ++$x[2]; -ok(check(), 'array : pop'); +$b = watch { delete $a[4] } { set => 1 }, 'delete'; +is $b, 'd', 'array: delete correctly'; -unshift @a, 'x'; -++$x[1]; ++$x[2]; -ok(check(), 'array : unshift'); +$b = watch { @a } { len => 1 }, 'length @'; +is $b, 3, 'array: length @ correctly'; -shift @a; -++$x[1]; ++$x[2]; -ok(check(), 'array : shift'); +# $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'; -$b = @a; -++$x[2]; -ok(check(), 'array : length'); +watch { push @a, 'x'; () } # push looks at the static context + { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID }, + 'push (void)'; -@a = map ord, @a; -$x[1] += 4; ++$x[2]; ++$x[3]; -ok(check(), 'array : map'); +$b = watch { push @a, 'y' } + { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN }, + 'push (scalar)'; +is $b, 5, 'array: push (scalar) correctly'; -@b = grep { defined && $_ >= ord('b') } @a; -++$x[2]; -ok(check(), 'array : grep'); +$b = watch { pop @a } { set => 1, len => 1 }, 'pop'; +is $b, 'y', 'array: pop correctly'; -for (@a) { } -$x[2] += 5; -ok(check(), 'array : for'); +watch { unshift @a, 'z'; () } # unshift looks at the static context + { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID }, + 'unshift (void)'; -{ +$b = watch { unshift @a, 't' } { set => 1, len => 1 }, 'unshift (scalar)'; +is $b, 6, 'unshift (scalar) correctly'; + +$b = watch { shift @a } { set => 1, len => 1 }, 'shift'; +is $b, 't', 'array: shift correctly'; + +watch { my $i; @a = map ++$i, @a } { set => 5, len => 1, clear => 1}, 'map'; + +@b = watch { grep { $_ >= 4 } @a } { len => 1 }, 'grep'; +is_deeply \@b, [ 4 .. 5 ], 'array: grep correctly'; + +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';