X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F33-code.t;h=da26dc6b8721ffe9dff6a5c7c6bd4c2cf9f18bb7;hb=ed45fb4404201c2e17ffa5c26a2320ceeb132e61;hp=7b6339960c037a2df50fe280dfb70bb1de87b0dd;hpb=763ba8093427f3668368fa885741618ac6289d41;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/33-code.t b/t/33-code.t index 7b63399..da26dc6 100644 --- a/t/33-code.t +++ b/t/33-code.t @@ -3,68 +3,44 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 2 * 10 + 9 + 1; -use Variable::Magic qw/wizard cast dispell/; +use Variable::Magic qw/cast dispell/; -my @c = (0) x 12; -my @x = (0) x 12; +use lib 't/lib'; +use Variable::Magic::TestWatcher; -sub check { - for (0 .. 11) { 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] }, - 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] }; -ok(check(), 'code : create wizard'); +my $wiz = init + [ 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 }; -cast $a, $wiz; -ok(check(), 'code : cast'); +check { cast &hlagh, $wiz } { }, 'cast'; +is $x, 0, 'code: cast didn\'t called code'; -my $b = $a; -++$x[0]; -ok(check(), 'code : assign to'); +check { hlagh() } { }, 'call without arguments'; +is $x, 1, 'code: call without arguments succeeded'; -$b = "X${a}Y"; -++$x[0]; -ok(check(), 'code : interpolate'); +check { hlagh(1, 2, 3) } { }, 'call with arguments'; +is $x, 2, 'code: call with arguments succeeded'; -$b = \$a; -ok(check(), 'code : reference'); +check { 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; +check { *hlagh = sub { ++$y } } { }, 'redefining sub'; -$a->(); -ok(check(), 'code : call'); +check { 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) = check { \&hlagh } { }, 'reference'; +is $y, 0, 'code: reference didn\'t called code'; -undef $a; -++$x[1]; -ok(check(), 'code : undef'); +check { $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'); +check { dispell &hlagh, $wiz } { }, 'dispell'; +is $y, 1, 'code: dispell didn\'t called code';