]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - t/33-code.t
This is 0.64
[perl/modules/Variable-Magic.git] / t / 33-code.t
index 40c32a4b00fff8e0cd40bf1ac1afbdbb884f64e4..a2d6711c2ff0af41ce60de4c21e74d7db10ccfa7 100644 (file)
@@ -3,69 +3,56 @@
 use strict;
 use warnings;
 
-use Test::More tests => 10;
-
-use Variable::Magic qw/wizard cast dispell/;
-
-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 $i = -1;
-my $wiz = wizard get   => sub { ++$c[0] },
-                 set   => sub { ++$c[1] },
-                 len   => sub { ++$c[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('code : create wizard');
+use Test::More tests => 2 * 12 + 11 + 1;
+
+use Variable::Magic qw<cast dispell>;
+
+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> ],
+        'code';
 
 my $x = 0;
-my $n = sub { ++$x };
-my $a = $n;
+sub hlagh { ++$x };
+
+watch { cast &hlagh, $wiz } { }, 'cast';
+is $x, 0, 'code: cast didn\'t called code';
+
+watch { hlagh() } { }, 'call without arguments';
+is $x, 1, 'code: call without arguments succeeded';
+
+watch { hlagh(1, 2, 3) } { }, 'call with arguments';
+is $x, 2, 'code: call with arguments succeeded';
 
-cast $a, $wiz;
-check('code : cast');
+watch { undef *hlagh } { free => 1 }, 'undef symbol table entry';
+is $x, 2, 'code: undef symbol table entry didn\'t call code';
 
-my $b = $a;
-++$x[0];
-check('code : assign to');
+my $y = 0;
+watch { *hlagh = sub { ++$y } } { }, 'redefining sub';
 
-$b = "X${a}Y";
-++$x[0];
-check('code : interpolate');
+watch { cast &hlagh, $wiz } { }, 're-cast';
+is $y, 0, 'code: re-cast didn\'t called code';
 
-$b = \$a;
-check('code : reference');
+my ($r) = watch { \&hlagh } { }, 'reference';
+is $y, 0, 'code: reference didn\'t called code';
 
-$a = $n;
-++$x[1];
-check('code : assign');
+watch { $r->() } { }, 'call reference';
+is $y, 1, 'code: call reference succeeded';
+is $x, 2, 'code: call reference didn\'t called the previous code';
 
-$a->();
-check('code : call');
+my $z = 0;
+watch {
+ no warnings 'redefine';
+ *hlagh = sub { ++$z }
+} { }, 'redefining sub 2';
 
-{
- my $b = $n;
- cast $b, $wiz;
-}
-++$x[4];
-check('code : scope end');
+watch { hlagh() } { }, 'call without arguments 2';
+is $z, 1, 'code: call without arguments 2 succeeded';
+is $y, 1, 'code: call without arguments 2 didn\'t called the previous code';
 
-undef $a;
-++$x[1];
-check('code : undef');
+watch { dispell &hlagh, $wiz } { }, 'dispell';
+is $z, 1, 'code: dispell didn\'t called code';
 
-dispell $a, $wiz;
-check('code : dispell');
+$Variable::Magic::TestWatcher::mg_end = { free => 1 };