X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F31-array.t;h=3613dba460618f33ac1e085e713c065c67c54b47;hb=HEAD;hp=1c8f03731700c89cb097c1c0452608e5dcd55c1e;hpb=c78c1790ebe43372ca405385d4cc053121b53ba0;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/31-array.t b/t/31-array.t index 1c8f037..3613dba 100644 --- a/t/31-array.t +++ b/t/31-array.t @@ -3,77 +3,94 @@ use strict; use warnings; -use Test::More tests => 2 * 25 + 9 + 1; +use Test::More tests => 2 * 27 + 13 + 1; -use Variable::Magic qw/cast dispell VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID VMG_COMPAT_ARRAY_UNDEF_CLEAR/; +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 +>; use lib 't/lib'; use Variable::Magic::TestWatcher; -my $wiz = init - [ qw/get set len clear free copy dup local fetch store exists delete/ ], +my $wiz = init_watcher + [ qw ], 'array'; my @n = map { int rand 1000 } 1 .. 5; my @a = @n; -check { cast @a, $wiz } { }, 'cast'; +watch { cast @a, $wiz } { }, 'cast'; -my $b = check { $a[2] } { }, 'assign element to'; +my $b = watch { $a[2] } { }, 'assign element to'; is $b, $n[2], 'array: assign element to correctly'; -my @b = check { @a } { len => 1 }, 'assign to'; +my @b = watch { @a } { len => 1 }, 'assign to'; is_deeply \@b, \@n, 'array: assign to correctly'; -$b = check { "X@{a}Y" } { len => 1 }, 'interpolate'; +$b = watch { "X@{a}Y" } { len => 1 }, 'interpolate'; is $b, "X@{n}Y", 'array: interpolate correctly'; -$b = check { \@a } { }, 'reference'; +$b = watch { \@a } { }, 'reference'; -@b = check { @a[2 .. 4] } { }, 'slice'; +@b = watch { @a[2 .. 4] } { }, 'slice'; is_deeply \@b, [ @n[2 .. 4] ], 'array: slice correctly'; -check { @a = qw/a b d/ } { set => 3, clear => 1 }, 'assign'; +watch { @a = qw } { set => 3, clear => 1 }, 'assign'; -check { $a[2] = 'c' } { }, 'assign old element'; +watch { $a[2] = 'c' } { }, 'assign old element'; -check { $a[3] = 'd' } { set => 1 }, 'assign new element'; +watch { $a[4] = 'd' } { set => 1 }, 'assign new element'; -check { push @a, 'x'; () } - { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN },'push (void)'; -$b = check { push @a, 'y' } - { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN }, 'push (scalar)'; +$b = watch { exists $a[4] } { }, 'exists'; +is $b, 1, 'array: exists correctly'; -$b = check { pop @a } { set => 1, len => 1 }, 'pop'; +$b = watch { delete $a[4] } { set => 1 }, 'delete'; +is $b, 'd', 'array: delete correctly'; + +$b = watch { @a } { len => 1 }, 'length @'; +is $b, 3, 'array: length @ correctly'; + +# $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'; + +watch { push @a, 'x'; () } # push looks at the static context + { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID }, + 'push (void)'; + +$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 = watch { pop @a } { set => 1, len => 1 }, 'pop'; is $b, 'y', 'array: pop correctly'; -check { unshift @a, 'z'; () } +watch { unshift @a, 'z'; () } # unshift looks at the static context { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID }, 'unshift (void)'; -$b = check { unshift @a, 't' } { set => 1, len => 1 }, 'unshift (scalar)'; +$b = watch { unshift @a, 't' } { set => 1, len => 1 }, 'unshift (scalar)'; +is $b, 6, 'unshift (scalar) correctly'; -$b = check { shift @a } { set => 1, len => 1 }, 'shift'; +$b = watch { shift @a } { set => 1, len => 1 }, 'shift'; is $b, 't', 'array: shift correctly'; -$b = check { @a } { len => 1 }, 'length @'; -is $b, 6, 'array: length @ correctly'; - -$b = check { $#a } { len => 1 }, 'length $#'; -is $b, 5, 'array: length $# correctly'; - -check { my $i; @a = map ++$i, @a; () } { set => 6, len => 1, clear => 1}, 'map'; +watch { my $i; @a = map ++$i, @a } { set => 5, len => 1, clear => 1}, 'map'; -@b = check { grep { $_ >= 4 } @a } { len => 1 }, 'grep'; -is_deeply \@b, [ 4 .. 6 ], 'array: grep correctly'; +@b = watch { grep { $_ >= 4 } @a } { len => 1 }, 'grep'; +is_deeply \@b, [ 4 .. 5 ], 'array: grep correctly'; -check { 1 for @a } { len => 6 + 1 }, 'for'; +watch { 1 for @a } { len => 5 + 1 }, 'for'; -check { +watch { my @b = @n; - check { cast @b, $wiz } { }, 'cast 2'; + watch { cast @b, $wiz } { }, 'cast 2'; } { free => 1 }, 'scope end'; -check { undef @a } +{ (clear => 1) x VMG_COMPAT_ARRAY_UNDEF_CLEAR }, 'undef'; +watch { undef @a } +{ (clear => 1) x VMG_COMPAT_ARRAY_UNDEF_CLEAR }, 'undef'; -check { dispell @a, $wiz } { }, 'dispell'; +watch { dispell @a, $wiz } { }, 'dispell';