X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F28-uvar.t;h=4906e72f1a867ce0bf8fd8ccea04c491fb0046a3;hb=421d111537612cdd34ef89f02ff1468bdf9932e4;hp=ad8dd59e35debcb2e0d1a2d430f62e31501b8b1b;hpb=2aeba77195a5533f86ad063f8e61c1d698c2f830;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/28-uvar.t b/t/28-uvar.t index ad8dd59..4906e72 100644 --- a/t/28-uvar.t +++ b/t/28-uvar.t @@ -3,12 +3,14 @@ use strict; use warnings; +use Config qw/%Config/; + use Test::More; use Variable::Magic qw/wizard cast dispell VMG_UVAR/; if (VMG_UVAR) { - plan tests => 2 * 9 + 7 + 4 + 1; + plan tests => 2 * 9 + 7 + 12 + 1; } else { plan skip_all => 'No nice uvar magic for this perl'; } @@ -45,7 +47,7 @@ is $x, 5, 'uvar: delete existing key correctly'; check { $x = delete $h{z} } { delete => 1 }, 'delete non-existing key'; ok !defined $x, 'uvar: delete non-existing key correctly'; -my $wiz2 = wizard 'fetch' => sub { 0 }; +my $wiz2 = wizard fetch => sub { 0 }; my %h2 = (a => 37, b => 2, c => 3); cast %h2, $wiz2; @@ -53,12 +55,41 @@ eval { local $SIG{__WARN__} = sub { die }; $x = $h2{a}; }; -is $@, '', 'uvar: fetch with incomplete magic'; +is $@, '', 'uvar: fetch with incomplete magic doesn\'t croak'; 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 $@, '', 'uvar: store with incomplete magic doesn\'t croak'; is $h2{a}, 73, 'uvar: store with incomplete magic correctly'; + +my $wiz3 = wizard store => sub { ++$_[2]; 0 }; +my %h3 = (a => 3); +cast %h3, $wiz3; + +for my $i (1 .. 2) { + eval { my $key = 'a'; $h3{$key} = 3 + $i }; + is $@, '', "uvar: change key in store doesn't croak ($i)"; + is_deeply \%h3, { a => 3, b => 3 + $i }, + "uvar: change key in store correcty ($i)"; +} + +my $ro_bare_hk = $] >= 5.010 && $Config{useithreads}; +diag 'This perl has readonly bare hash keys' if $ro_bare_hk; + +for my $i (1 .. 2) { + eval { $h3{b} = 5 + $i }; + if ($ro_bare_hk) { + like $@, qr/Modification\s+of\s+a\s+read-only\s+value/, + "uvar: change readonly key in store croaks ($i)"; + is_deeply \%h3, { a => 3, b => 5 }, + "uvar: change readonly key in store correcty ($i)"; + } else { + is $@, '', "uvar: change readonly key in store croaks ($i)"; + is_deeply \%h3, { a => 3, b => 5, c => 6, (d => 7) x ($i >= 2) }, + "uvar: change readonly key in store correcty ($i)"; + } +} +