X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F33-code.t;h=a2d6711c2ff0af41ce60de4c21e74d7db10ccfa7;hb=HEAD;hp=4b9cd207ee1c801feb41e28b766057fdc2f1964d;hpb=c471e8c9f86ad8817761816101358f8ae1035915;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/33-code.t b/t/33-code.t index 4b9cd20..a2d6711 100644 --- a/t/33-code.t +++ b/t/33-code.t @@ -3,66 +3,56 @@ use strict; use warnings; -use Test::More tests => 14; - -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; + +use lib 't/lib'; +use Variable::Magic::TestWatcher; + +my $wiz = init_watcher + [ qw ], + 'code'; my $x = 0; sub hlagh { ++$x }; -cast &hlagh, $wiz; -check('code : cast'); +watch { cast &hlagh, $wiz } { }, 'cast'; +is $x, 0, 'code: cast didn\'t called code'; -hlagh(); -check('code : call without arguments'); -is($x, 1, 'code : call without arguments succeeded'); +watch { hlagh() } { }, 'call without arguments'; +is $x, 1, 'code: call without arguments succeeded'; -hlagh(1, 2, 3); -check('code : call with arguments'); -is($x, 2, 'code : call with arguments succeeded'); +watch { hlagh(1, 2, 3) } { }, 'call with arguments'; +is $x, 2, 'code: call with arguments succeeded'; -undef *hlagh; -++$x[4]; -check('code : undef symbol table'); -is($x, 2, 'code : undef symbol table didn\'t call'); +watch { undef *hlagh } { free => 1 }, 'undef symbol table entry'; +is $x, 2, 'code: undef symbol table entry didn\'t call code'; my $y = 0; -*hlagh = sub { ++$y }; +watch { *hlagh = sub { ++$y } } { }, 'redefining sub'; + +watch { cast &hlagh, $wiz } { }, 're-cast'; +is $y, 0, 'code: re-cast didn\'t called code'; + +my ($r) = watch { \&hlagh } { }, 'reference'; +is $y, 0, 'code: reference didn\'t called code'; + +watch { $r->() } { }, 'call reference'; +is $y, 1, 'code: call reference succeeded'; +is $x, 2, 'code: call reference didn\'t called the previous code'; -cast &hlagh, $wiz; -check('code : re-cast'); +my $z = 0; +watch { + no warnings 'redefine'; + *hlagh = sub { ++$z } +} { }, 'redefining sub 2'; -my $r = \&hlagh; -check('code : take reference'); +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'; -$r->(); -check('code : call reference'); -is($y, 1, 'code : call reference succeeded'); -is($x, 2, 'code : call reference didn\'t triggered the previous code'); +watch { dispell &hlagh, $wiz } { }, 'dispell'; +is $z, 1, 'code: dispell didn\'t called code'; -dispell &hlagh, $wiz; -check('code : dispell'); +$Variable::Magic::TestWatcher::mg_end = { free => 1 };