X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F33-code.t;h=a2d6711c2ff0af41ce60de4c21e74d7db10ccfa7;hb=9ad970e109ea4caa9767db1bda9d475444920c7a;hp=538beb2380d05747578b5196ceea75b638f6bf2a;hpb=77a84f75f33e3ee44e61182dec76699e23025375;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/33-code.t b/t/33-code.t index 538beb2..a2d6711 100644 --- a/t/33-code.t +++ b/t/33-code.t @@ -1,60 +1,58 @@ #!perl -T -use Test::More tests => 10; +use strict; +use warnings; -use Variable::Magic qw/wizard cast dispell/; +use Test::More tests => 2 * 12 + 11 + 1; -my @c = (0) x 5; -my @x = (0) x 5; +use Variable::Magic qw; -sub check { - for (0 .. 4) { return 0 unless $c[$_] == $x[$_]; } - return 1; -} +use lib 't/lib'; +use Variable::Magic::TestWatcher; -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 }; -cast $a, $wiz; -ok(check(), 'code : cast'); +watch { cast &hlagh, $wiz } { }, 'cast'; +is $x, 0, 'code: cast didn\'t called code'; -my $b = $a; -++$x[0]; -ok(check(), 'code : assign to'); +watch { hlagh() } { }, 'call without arguments'; +is $x, 1, 'code: call without arguments succeeded'; -$b = "X${a}Y"; -++$x[0]; -ok(check(), 'code : interpolate'); +watch { hlagh(1, 2, 3) } { }, 'call with arguments'; +is $x, 2, 'code: call with arguments succeeded'; -$b = \$a; -ok(check(), 'code : reference'); +watch { undef *hlagh } { free => 1 }, 'undef symbol table entry'; +is $x, 2, 'code: undef symbol table entry didn\'t call code'; -$a = $n; -++$x[1]; -ok(check(), 'code : assign'); +my $y = 0; +watch { *hlagh = sub { ++$y } } { }, 'redefining sub'; -$a->(); -ok(check(), 'code : call'); +watch { cast &hlagh, $wiz } { }, 're-cast'; +is $y, 0, 'code: re-cast didn\'t called code'; -{ - my $b = $n; - cast $b, $wiz; -} -++$x[4]; -ok(check(), 'code : scope end'); +my ($r) = watch { \&hlagh } { }, 'reference'; +is $y, 0, 'code: reference didn\'t called code'; -undef $a; -++$x[1]; -ok(check(), 'code : undef'); +watch { $r->() } { }, 'call reference'; +is $y, 1, 'code: call reference succeeded'; +is $x, 2, 'code: call reference didn\'t called the previous code'; -dispell $a, $wiz; -ok(check(), 'code : dispell'); +my $z = 0; +watch { + no warnings 'redefine'; + *hlagh = sub { ++$z } +} { }, 'redefining sub 2'; + +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'; + +watch { dispell &hlagh, $wiz } { }, 'dispell'; +is $z, 1, 'code: dispell didn\'t called code'; + +$Variable::Magic::TestWatcher::mg_end = { free => 1 };