]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - t/31-array.t
This is 0.64
[perl/modules/Variable-Magic.git] / t / 31-array.t
index bc942c7d55e99204bcd61ee457e1be0622c0915b..3613dba460618f33ac1e085e713c065c67c54b47 100644 (file)
 use strict;
 use warnings;
 
-use Test::More tests => 24;
-
-use Variable::Magic qw/wizard cast dispell VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID VMG_COMPAT_ARRAY_UNDEF_CLEAR/;
-
-my @c = (0) x 12;
-my @x = (0) x 12;
-
-sub check {
- is join(':', map { (defined) ? $_ : 'u' } @c[0 .. 11]),
-    join(':', map { (defined) ? $_ : 'u' } @x[0 .. 11]),
-    $_[0];
-}
-
-my $wiz = wizard get   => sub { ++$c[0] },
-                 set   => sub { ++$c[1] },
-                 len   => sub { ++$c[2]; $_[2] },
-                 clear => sub { ++$c[3] },
-                 free  => sub { ++$c[4] },
-                 copy  => sub { ++$c[5] },
-                 dup   => sub { ++$c[6] },
-                 local => sub { ++$c[7] },
-                 fetch => sub { ++$c[8] },
-                 store => sub { ++$c[9] },
-                 'exists' => sub { ++$c[10] },
-                 'delete' => sub { ++$c[11] };
-check('array : create wizard');
+use Test::More tests => 2 * 27 + 13 + 1;
+
+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_watcher
+        [ 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;
-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';
 
-my $b = $a[2];
-check('array : assign element to');
+$b = watch { "X@{a}Y" } { len => 1 }, 'interpolate';
+is $b, "X@{n}Y", 'array: interpolate correctly';
 
-my @b = @a;
-++$x[2];
-check('array : assign to');
+$b = watch { \@a } { }, 'reference';
 
-$b = "X@{a}Y";
-++$x[2];
-check('array : interpolate');
+@b = watch { @a[2 .. 4] } { }, 'slice';
+is_deeply \@b, [ @n[2 .. 4] ], 'array: slice correctly';
 
-$b = \@a;
-check('array : reference');
+watch { @a = qw<a b d> } { set => 3, clear => 1 }, 'assign';
 
-@b = @a[2 .. 4];
-check('array : slice');
+watch { $a[2] = 'c' } { }, 'assign old element';
 
-@a = qw/a b d/;
-$x[1] += 3; ++$x[3];
-check('array : assign');
+watch { $a[4] = 'd' } { set => 1 }, 'assign new element';
 
-$a[2] = 'c';
-check('array : assign old element');
+$b = watch { exists $a[4] } { }, 'exists';
+is $b, 1, 'array: exists correctly';
 
-$a[3] = 'd';
-++$x[1];
-check('array : assign new element');
+$b = watch { delete $a[4] } { set => 1 }, 'delete';
+is $b, 'd', 'array: delete correctly';
 
-push @a, 'x';
-++$x[1]; ++$x[2] unless VMG_COMPAT_ARRAY_PUSH_NOLEN;
-check('array : push (void)');
+$b = watch { @a } { len => 1 }, 'length @';
+is $b, 3, 'array: length @ correctly';
 
-$b = push @a, 'x';
-++$x[1]; ++$x[2] unless VMG_COMPAT_ARRAY_PUSH_NOLEN;
-check('array : push (scalar)');
+# $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';
 
-pop @a;
-++$x[1]; ++$x[2];
-check('array : pop');
+watch { push @a, 'x'; () } # push looks at the static context
+                   { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID },
+                   'push (void)';
 
-unshift @a, 'x';
-++$x[1]; ++$x[2] unless VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID;
-check('array : unshift (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 = unshift @a, 'x';
-++$x[1]; ++$x[2];
-check('array : unshift (scalar)');
+$b = watch { pop @a } { set => 1, len => 1 }, 'pop';
+is $b, 'y', 'array: pop correctly';
 
-shift @a;
-++$x[1]; ++$x[2];
-check('array : shift');
+watch { unshift @a, 'z'; () } # unshift looks at the static context
+                { set => 1, (len => 1) x !VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID },
+                'unshift (void)';
 
-$b = @a;
-++$x[2];
-check('array : length @');
+$b = watch { unshift @a, 't' } { set => 1, len => 1 }, 'unshift (scalar)';
+is $b, 6, 'unshift (scalar) correctly';
 
-$b = $#a;
-++$x[2];
-check('array : length $#');
+$b = watch { shift @a } { set => 1, len => 1 }, 'shift';
+is $b, 't', 'array: shift correctly';
 
-@a = map ord, @a; 
-$x[1] += 6; ++$x[2]; ++$x[3];
-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];
-check('array : grep');
+@b = watch { grep { $_ >= 4 } @a } { len => 1 }, 'grep';
+is_deeply \@b, [ 4 .. 5 ], 'array: grep correctly';
 
-for (@a) { }
-$x[2] += 7;
-check('array : for');
+watch { 1 for @a } { len => 5 + 1 }, 'for';
 
-{
+watch {
  my @b = @n;
- cast @b, $wiz;
-}
-++$x[4];
-check('array : scope end');
+ watch { cast @b, $wiz } { }, 'cast 2';
+} { free => 1 }, 'scope end';
 
-undef @a;
-++$x[3] if VMG_COMPAT_ARRAY_UNDEF_CLEAR;
-check('array : undef');
+watch { undef @a } +{ (clear => 1) x VMG_COMPAT_ARRAY_UNDEF_CLEAR }, 'undef';
 
-dispell @a, $wiz;
-check('array : dispel');
+watch { dispell @a, $wiz } { }, 'dispell';