X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F33-code.t;h=a2d6711c2ff0af41ce60de4c21e74d7db10ccfa7;hb=aae88f4cf6f2f32553f8cc29cb8f0792cb5cd910;hp=21d900ff20bc1ae4e9fdfd7ecbeade20bc847ed6;hpb=14f66d40970bef63105be046a109c1a32859a8a0;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/33-code.t b/t/33-code.t index 21d900f..a2d6711 100644 --- a/t/33-code.t +++ b/t/33-code.t @@ -3,61 +3,56 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 2 * 12 + 11 + 1; -use Variable::Magic qw/wizard cast dispell/; +use Variable::Magic qw; -my @c = (0) x 5; -my @x = (0) x 5; +use lib 't/lib'; +use Variable::Magic::TestWatcher; -sub check { - for (0 .. 4) { return 0 unless $c[$_] == $x[$_]; } - return 1; -} - -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] }; -ok(check(), 'code : create wizard'); +my $wiz = init_watcher + [ qw ], + '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; -ok(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]; -ok(check(), 'code : assign to'); +my $y = 0; +watch { *hlagh = sub { ++$y } } { }, 'redefining sub'; -$b = "X${a}Y"; -++$x[0]; -ok(check(), 'code : interpolate'); +watch { cast &hlagh, $wiz } { }, 're-cast'; +is $y, 0, 'code: re-cast didn\'t called code'; -$b = \$a; -ok(check(), 'code : reference'); +my ($r) = watch { \&hlagh } { }, 'reference'; +is $y, 0, 'code: reference didn\'t called code'; -$a = $n; -++$x[1]; -ok(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->(); -ok(check(), 'code : call'); +my $z = 0; +watch { + no warnings 'redefine'; + *hlagh = sub { ++$z } +} { }, 'redefining sub 2'; -{ - my $b = $n; - cast $b, $wiz; -} -++$x[4]; -ok(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]; -ok(check(), 'code : undef'); +watch { dispell &hlagh, $wiz } { }, 'dispell'; +is $z, 1, 'code: dispell didn\'t called code'; -dispell $a, $wiz; -ok(check(), 'code : dispell'); +$Variable::Magic::TestWatcher::mg_end = { free => 1 };