X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F31-array.t;h=3397ceb639f1de5e6b0cfcb0972b7c9d74b0c829;hb=54a1b1e4382ad55a54cbd874d6ffd87bf58dffbc;hp=62f41456e6103af03504cf15f9b0267f2c8030c4;hpb=ad7c749baf8ebc2ff3e49d44b414f67f13f4ebf2;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/31-array.t b/t/31-array.t index 62f4145..3397ceb 100644 --- a/t/31-array.t +++ b/t/31-array.t @@ -1,100 +1,81 @@ #!perl -T -use Test::More tests => 21; +use strict; +use warnings; -use Variable::Magic qw/wizard cast dispell/; +use Test::More tests => 2 * 25 + 9 + 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_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 + [ qw/get set len clear free copy dup local fetch store exists delete/ ], + 'array'; my @n = map { int rand 1000 } 1 .. 5; my @a = @n; -cast @a, $wiz; -ok(check(), 'array : cast'); +check { cast @a, $wiz } { }, 'cast'; -my $b = $a[2]; -ok(check(), 'array : assign element to'); +my $b; +check { $b = $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; +check { @b = @a } { len => 1 }, 'assign to'; +is_deeply \@b, \@n, 'array: assign to correctly'; -$b = "X@{a}Y"; -++$x[2]; -ok(check(), 'array : interpolate'); +check { $b = "X@{a}Y" } { len => 1 }, 'interpolate'; +is $b, "X@{n}Y", 'array: interpolate correctly'; -$b = \@a; -ok(check(), 'array : reference'); +check { $b = \@a } { }, 'reference'; -@b = @a[2 .. 4]; -ok(check(), 'array : slice'); +check { @b = @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'); +check { @a = qw/a b d/ } { set => 3, clear => 1 }, 'assign'; -$a[2] = 'c'; -ok(check(), 'array : assign old element'); +check { $a[2] = 'c' } { }, 'assign old element'; -$a[3] = 'd'; -++$x[1]; -ok(check(), 'array : assign new element'); +check { $a[3] = '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'); +check { push @a, 'x'; () } + { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN },'push (void)'; +check { $b = push @a, 'y' } + { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN }, 'push (scalar)'; -pop @a; -++$x[1]; ++$x[2]; -ok(check(), 'array : pop'); +check { $b = pop @a } { set => 1, len => 1 }, 'pop'; +is $b, 'y', 'array: pop correctly'; -unshift @a, 'x'; -++$x[1]; ++$x[2]; -ok(check(), 'array : unshift'); +check { unshift @a, 'z'; () } + { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID }, + 'unshift (void)'; -shift @a; -++$x[1]; ++$x[2]; -ok(check(), 'array : shift'); +check { $b = unshift @a, 't' } { set => 1, len => 1 }, 'unshift (scalar)'; -$b = @a; -++$x[2]; -ok(check(), 'array : length'); +check { $b = 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'); +check { $b = @a } { len => 1 }, 'length @'; +is $b, 6, 'array: length @ correctly'; -@b = grep { defined && $_ >= ord('b') } @a; -++$x[2]; -ok(check(), 'array : grep'); +check { $b = $#a } { len => 1 }, 'length $#'; +is $b, 5, 'array: length $# correctly'; -for (@a) { } -$x[2] += 5; -ok(check(), 'array : for'); +check { my $i; @a = map ++$i, @a; () } { set => 6, len => 1, clear => 1}, 'map'; -{ +check { @b = grep { $_ >= 4 } @a } { len => 1 }, 'grep'; +is_deeply \@b, [ 4 .. 6 ], 'array: grep correctly'; + +check { 1 for @a } { len => 6 + 1 }, 'for'; + +check { my @b = @n; - cast @b, $wiz; -} -++$x[4]; -ok(check(), 'array : scope end'); + check { 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'); +check { undef @a } +{ (clear => 1) x VMG_COMPAT_ARRAY_UNDEF_CLEAR }, 'undef'; -dispell @a, $wiz; -ok(check(), 'array : dispel'); +check { dispell @a, $wiz } { }, 'dispell';