]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - t/28-uvar.t
Allow editing the key SV in uvar callbacks by passing a new option 'copy_key'
[perl/modules/Variable-Magic.git] / t / 28-uvar.t
index 7fa30b124301accd0f9a1cf13f5d05e7d5de7ba4..30d7f525e20b5d4e9651d88759d3148bd4067af4 100644 (file)
@@ -8,66 +8,50 @@ use Test::More;
 use Variable::Magic qw/wizard cast dispell VMG_UVAR/;
 
 if (VMG_UVAR) {
- plan tests => 20;
+ plan tests => 2 * 10 + 8 + 14 + 1;
 } else {
  plan skip_all => 'No nice uvar magic for this perl';
 }
 
-my @c = (0) x 4;
-my @x = (0) x 4;
+use lib 't/lib';
+use Variable::Magic::TestWatcher;
 
-sub check {
- is join(':', map { (defined) ? $_ : 'u' } @c[0 .. 3]),
-    join(':', map { (defined) ? $_ : 'u' } @x[0 .. 3]),
-    $_[0];
-}
-
-my $wiz = wizard 'fetch'  => sub { ++$c[0] },
-                 'store'  => sub { ++$c[1] },
-                 'exists' => sub { ++$c[2] },
-                 'delete' => sub { ++$c[3] };
-check('uvar : create wizard');
+my $wiz = init [ qw/fetch store exists delete/ ], 'uvar';
 
 my %h = (a => 1, b => 2, c => 3);
-my $res = cast %h, $wiz;
-ok($res, 'uvar : cast succeeded');
-check(   'uvar : cast didn\'t triggered the callback');
-
-my $x = $h{a};
-++$x[0];
-check( 'uvar : fetch directly');
-ok($x, 'uvar : fetch directly correctly');
-
-$x = "$h{b}";
-++$x[0];
-check(    'uvar : fetch by interpolation');
-is($x, 2, 'uvar : fetch by interpolation correctly');
-
-$h{c} = 4;
-++$x[1];
-check('uvar : store directly');
-
-$x = $h{c} = 5;
-++$x[1];
-check(    'uvar : fetch and store');
-is($x, 5, 'uvar : fetch and store correctly');
-
-$x = exists $h{c};
-++$x[2];
-check( 'uvar : exists');
-ok($x, 'uvar : exists correctly');
-
-$x = delete $h{c};
-++$x[3];
-check(    'uvar : delete existing key');
-is($x, 5, 'uvar : delete existing key correctly');
-
-$x = delete $h{z};
-++$x[3];
-check(          'uvar : delete non-existing key');
-ok(!defined $x, 'uvar : delete non-existing key correctly');
-
-my $wiz2 = wizard 'fetch'  => sub { 0 };
+
+my $res = check { cast %h, $wiz } { }, 'cast';
+ok $res, 'uvar: cast succeeded';
+
+my $x;
+
+check { $x = $h{a} } { fetch => 1 }, 'fetch directly';
+is $x, 1, 'uvar: fetch directly correctly';
+
+check { $x = "$h{b}" } { fetch => 1 }, 'fetch by interpolation';
+is $x, 2, 'uvar: fetch by interpolation correctly';
+
+check { $h{c} = 4 } { store => 1 }, 'store directly';
+
+check { $x = $h{c} = 5 } { store => 1 }, 'fetch and store';
+is $x, 5, 'uvar: fetch and store correctly';
+
+check { $x = exists $h{c} } { exists => 1 }, 'exists';
+ok $x, 'uvar: exists correctly';
+
+check { $x = delete $h{c} } { delete => 1 }, 'delete existing key';
+is $x, 5, 'uvar: delete existing key correctly';
+
+check { $x = delete $h{z} } { delete => 1 }, 'delete non-existing key';
+ok !defined $x, 'uvar: delete non-existing key correctly';
+
+my $wiz2 = wizard get => sub { 0 };
+cast %h, $wiz2;
+
+check { $x = $h{a} } { fetch => 1 }, 'fetch directly with also non uvar magic';
+is $x, 1, 'uvar: fetch directly with also non uvar magic correctly';
+
+$wiz2 = wizard fetch => sub { 0 };
 my %h2 = (a => 37, b => 2, c => 3);
 cast %h2, $wiz2;
 
@@ -75,12 +59,32 @@ eval {
  local $SIG{__WARN__} = sub { die };
  $x = $h2{a};
 };
-ok(!$@,    'uvar : fetch with incomplete magic');
-is($x, 37, 'uvar : fetch with incomplete magic correctly');
+is $@, '', 'uvar: fetch with incomplete magic doesn\'t croak';
+is $x, 37, 'uvar: fetch with incomplete magic correctly';
 
 eval {
  local $SIG{__WARN__} = sub { die };
  $h2{a} = 73;
 };
-ok(!$@,        'uvar : store with incomplete magic');
-is($h2{a}, 73, 'uvar : store with incomplete magic correctly');
+is $@, '',     'uvar: store with incomplete magic doesn\'t croak';
+is $h2{a}, 73, 'uvar: store with incomplete magic correctly';
+
+my $wiz3 = wizard store => sub { ++$_[2]; 0 }, copy_key => 1;
+my %h3 = (a => 3);
+cast %h3, $wiz3;
+
+for my $i (1 .. 2) {
+ my $key = 'a';
+ eval { $h3{$key} = 3 + $i };
+ is        $@,   '',  "uvar: change key in store doesn't croak ($i)";
+ is        $key, 'a', "uvar: change key didn't clobber \$key ($i)";
+ is_deeply \%h3, { a => 3, b => 3 + $i },
+                      "uvar: change key in store correcty ($i)";
+}
+
+for my $i (1 .. 2) {
+ eval { $h3{b} = 5 + $i };
+ is $@, '',                    "uvar: change readonly key in store croaks ($i)";
+ is_deeply \%h3, { a => 3, b => 5, c => 5 + $i },
+                             "uvar: change readonly key in store correcty ($i)";
+}