]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - t/25-copy.t
Update VPIT::TestHelpers to e8344578
[perl/modules/Variable-Magic.git] / t / 25-copy.t
index 58cf8e91db6df1ce407174a6a2ebe20528ba8937..394449541b3ab123f7ff015b0bf16834ab77c3a1 100644 (file)
@@ -5,59 +5,78 @@ use warnings;
 
 use Test::More;
 
-use Variable::Magic qw/wizard cast MGf_COPY/;
+use lib 't/lib';
+use VPIT::TestHelpers;
 
-if (!MGf_COPY) {
- plan skip_all => "this perl doesn't handle copy magic";
-} else {
- plan tests => 16;
-}
+use Variable::Magic qw<cast dispell>;
+
+plan tests => 2 + ((2 * 5 + 3) + (2 * 2 + 1)) + (2 * 9 + 6) + 1;
+
+use lib 't/lib';
+use Variable::Magic::TestWatcher;
+use Variable::Magic::TestValue;
+
+my $wiz = init_watcher 'copy', 'copy';
+
+SKIP: {
+ load_or_skip('Tie::Array', undef, undef, (2 * 5 + 3) + (2 * 2 + 1));
+
+ tie my @a, 'Tie::StdArray';
+ @a = (1 .. 10);
+
+ my $res = watch { cast @a, $wiz } { }, 'cast on tied array';
+ ok $res, 'copy: cast on tied array succeeded';
 
-my $c = 0;
-my $wiz = wizard copy => sub { ++$c };
-ok($c == 0, 'copy : create wizard');
+ watch { $a[3] = 13 } { copy => 1 }, 'tied array store';
 
-use Tie::Array;
+ my $s = watch { $a[3] } { copy => 1 }, 'tied array fetch';
+ is $s, 13, 'copy: tied array fetch correctly';
 
-tie my @a, 'Tie::StdArray';
-cast @a, $wiz;
-ok($c == 0, 'copy (array) : cast');
+ $s = watch { exists $a[3] } { copy => 1 }, 'tied array exists';
+ ok $s, 'copy: tied array exists correctly';
 
-my $n = time;
-$a[0] = $n;
-ok($c == 1, 'copy (array) : store element');
+ watch { undef @a } { }, 'tied array undef';
 
-my $e = exists $a[0];
-ok($c == 2, 'copy (array) : exists element');
-ok($e,      'copy (array) : exists element, really'); 
+ {
+  tie my @val, 'Tie::StdArray';
+  @val = (4 .. 6);
 
-my $b = $a[0];
-ok($c == 3, 'copy (array) : fetch element');
-ok($b == $n, 'copy (array) : fetch element correctly');
+  my $wv = init_value @val, 'copy', 'copy';
 
-use Tie::Hash;
+  value { $val[3] = 8 } [ 4 .. 6 ];
 
-$c = 0;
+  dispell @val, $wv;
+  is_deeply \@val, [ 4 .. 6, 8 ], 'copy: value after';
+ }
+}
+
+SKIP: {
+ load_or_skip('Tie::Hash', undef, undef, 2 * 9 + 6);
+
+ tie my %h, 'Tie::StdHash';
+ %h = (a => 1, b => 2, c => 3);
+
+ my $res = watch { cast %h, $wiz } { }, 'cast on tied hash';
+ ok $res, 'copy: cast on tied hash succeeded';
 
-tie my %h, 'Tie::StdHash';
-cast %h, $wiz;
-ok($c == 0, 'copy (hash) : cast');
+ watch { $h{b} = 7 } { copy => 1 }, 'tied hash store';
 
-my ($k, $v) = (time, int rand time);
-$h{$k} = $v;
-ok($c == 1, 'copy (hash) : store element');
+ my $s = watch { $h{c} } { copy => 1 }, 'tied hash fetch';
+ is $s, 3, 'copy: tied hash fetch correctly';
 
-$e = exists $h{$k};
-ok($c == 2, 'copy (hash) : exists element');
-ok($e,      'copy (hash) : exists element, really');
+ $s = watch { exists $h{a} } { copy => 1 }, 'tied hash exists';
+ ok $s, 'copy: tied hash exists correctly';
 
-my $w = $h{$k};
-ok($c == 3, 'copy (hash) : fetch element');
-ok($w == $v, 'copy (hash) : fetch element correctly');
+ $s = watch { delete $h{b} } { copy => 1 }, 'tied hash delete';
+ is $s, 7, 'copy: tied hash delete correctly';
 
-my ($K, $V) = each %h;
-ok($c == 4, 'copy (hash) : iterate');
-ok($k == $K && $v == $V, 'copy (hash) : iterate correctly');
+ watch { my ($k, $v) = each %h } { copy => 1 }, 'tied hash each';
 
-delete $h{$k};
-ok($c == 5, 'copy (hash) : delete');
+ my @k = watch { keys %h } { }, 'tied hash keys';
+ is_deeply [ sort @k ], [ qw<a c> ], 'copy: tied hash keys correctly';
+
+ my @v = watch { values %h } { copy => 2 }, 'tied hash values';
+ is_deeply [ sort { $a <=> $b } @v ], [ 1, 3 ], 'copy: tied hash values correctly';
+
+ watch { undef %h } { }, 'tied hash undef';
+}