X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F28-uvar.t;h=4800c44bbc0c1c8baf5161eb88cc213bc455ba79;hb=d8122850f4c201c5b849cebb1f9b7f2630ae4c15;hp=b6c8959605c8c2173adbaa1c622369f61d3234c7;hpb=763ba8093427f3668368fa885741618ac6289d41;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/28-uvar.t b/t/28-uvar.t index b6c8959..4800c44 100644 --- a/t/28-uvar.t +++ b/t/28-uvar.t @@ -8,7 +8,7 @@ use Test::More; use Variable::Magic qw/wizard cast dispell VMG_UVAR/; if (VMG_UVAR) { - plan tests => 16; + plan tests => 20; } else { plan skip_all => 'No nice uvar magic for this perl'; } @@ -17,53 +17,70 @@ my @c = (0) x 4; my @x = (0) x 4; sub check { - for (0 .. 3) { return 0 unless $c[$_] == $x[$_]; } - return 1; + is join(':', map { (defined) ? $_ : 'u' } @c[0 .. 3]), + join(':', map { (defined) ? $_ : 'u' } @x[0 .. 3]), + $_[0]; } my $wiz = wizard 'fetch' => sub { ++$c[0] }, 'store' => sub { ++$c[1] }, 'exists' => sub { ++$c[2] }, 'delete' => sub { ++$c[3] }; -ok(check(), 'uvar : create wizard'); +check('uvar : create wizard'); my %h = (a => 1, b => 2, c => 3); my $res = cast %h, $wiz; - -ok($res, 'uvar : cast succeeded'); -ok(check(), 'uvar : cast didn\'t triggered the callback'); +ok($res, 'uvar : cast succeeded'); +check( 'uvar : cast didn\'t triggered the callback'); my $x = $h{a}; ++$x[0]; -ok(check(), 'uvar : fetch directly'); -ok($x, 'uvar : fetch directly correctly'); +check( 'uvar : fetch directly'); +ok($x, 'uvar : fetch directly correctly'); $x = "$h{b}"; ++$x[0]; -ok(check(), 'uvar : fetch by interpolation'); -ok($x == 2, 'uvar : fetch by interpolation correctly'); +check( 'uvar : fetch by interpolation'); +is($x, 2, 'uvar : fetch by interpolation correctly'); $h{c} = 4; ++$x[1]; -ok(check(), 'uvar : store directly'); +check('uvar : store directly'); $x = $h{c} = 5; ++$x[1]; -ok(check(), 'uvar : fetch and store'); -ok($x == 5, 'uvar : fetch and store correctly'); +check( 'uvar : fetch and store'); +is($x, 5, 'uvar : fetch and store correctly'); $x = exists $h{c}; ++$x[2]; -ok(check(), 'uvar : exists'); -ok($x, 'uvar : exists correctly'); +check( 'uvar : exists'); +ok($x, 'uvar : exists correctly'); $x = delete $h{c}; ++$x[3]; -ok(check(), 'uvar : delete existing key'); -ok($x == 5, 'uvar : delete existing key correctly'); +check( 'uvar : delete existing key'); +is($x, 5, 'uvar : delete existing key correctly'); $x = delete $h{z}; ++$x[3]; -ok(check(), 'uvar : delete non-existing key'); +check( 'uvar : delete non-existing key'); ok(!defined $x, 'uvar : delete non-existing key correctly'); +my $wiz2 = wizard 'fetch' => sub { 0 }; +my %h2 = (a => 37, b => 2, c => 3); +cast %h2, $wiz2; + +eval { + local $SIG{__WARN__} = sub { die }; + $x = $h2{a}; +}; +is($@, '', 'uvar : fetch with incomplete magic'); +is($x, 37, 'uvar : fetch with incomplete magic correctly'); + +eval { + local $SIG{__WARN__} = sub { die }; + $h2{a} = 73; +}; +is($@, '', 'uvar : store with incomplete magic'); +is($h2{a}, 73, 'uvar : store with incomplete magic correctly');