X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F32-hash.t;h=a7408fce4cf83985e9008ba4407b7a75a9c30bf3;hb=800e9f939f181af9766c3a3024ac9f57b9799510;hp=e12b810a4ee39e625355561a0dfa168a77f16ee6;hpb=da422089a9a5dfbf84e72ec3ba867063471ff41c;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/32-hash.t b/t/32-hash.t index e12b810..a7408fc 100644 --- a/t/32-hash.t +++ b/t/32-hash.t @@ -3,18 +3,18 @@ use strict; use warnings; -use Test::More tests => 2 * 21 + 7 + 1; +use Test::More tests => (2 * 21 + 7) + (2 * 5 + 5) + 1; -use Variable::Magic qw/cast dispell MGf_COPY VMG_UVAR/; +use Variable::Magic qw; use lib 't/lib'; use Variable::Magic::TestWatcher; my $wiz = init_watcher - [ qw/get set len clear free copy dup local fetch store exists delete/ ], + [ qw ], 'hash'; -my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/; +my %n = map { $_ => int rand 1000 } qw; my %h = %n; watch { cast %h, $wiz } { }, 'cast'; @@ -34,9 +34,9 @@ is_deeply \%b, \%n, 'hash: assign to correctly'; $s = watch { \%h } { }, 'reference'; -my @b = watch { @h{qw/bar qux/} } +my @b = watch { @h{qw} } +{ (fetch => 2) x VMG_UVAR }, 'slice'; -is_deeply \@b, [ @n{qw/bar qux/} ], 'hash: slice correctly'; +is_deeply \@b, [ @n{qw} ], 'hash: slice correctly'; watch { %h = () } { clear => 1 }, 'empty in list context'; @@ -44,7 +44,7 @@ watch { %h = (a => 1, d => 3); () } +{ (store => 2, copy => 2) x VMG_UVAR, clear => 1 }, 'assign from list in void context'; -watch { %h = map { $_ => 1 } qw/a b d/; } +watch { %h = map { $_ => 1 } qw; } +{ (exists => 3, store => 3, copy => 3) x VMG_UVAR, clear => 1 }, 'assign from map in list context'; @@ -57,7 +57,7 @@ watch { $h{c} = 3; () } +{ (store => 1, copy => 1) x VMG_UVAR }, $s = watch { %h } { }, 'buckets'; @b = watch { keys %h } { }, 'keys'; -is_deeply [ sort @b ], [ qw/a b c d/ ], 'hash: keys correctly'; +is_deeply [ sort @b ], [ qw ], 'hash: keys correctly'; @b = watch { values %h } { }, 'values'; is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly'; @@ -72,3 +72,48 @@ watch { watch { undef %h } { clear => 1 }, 'undef'; watch { dispell %h, $wiz } { }, 'dispell'; + +SKIP: { + my $SKIP; + + if (!VMG_UVAR) { + $SKIP = 'uvar magic'; + } else { + local $@; + unless (eval { require B::Deparse; 1 }) { + $SKIP = 'B::Deparse'; + } + } + if ($SKIP) { + $SKIP .= ' required to test uvar/clear interaction fix'; + skip $SKIP => 2 * 5 + 5; + } + + my $bd = B::Deparse->new; + + my %h = (a => 13, b => 15); + watch { cast %h, $wiz } { }, 'cast clear/uvar'; + + my $code = sub { my $x = $h{$_[0]}; ++$x; $x }; + my $before = $bd->coderef2text($code); + my $res; + + watch { $res = $code->('a') } { fetch => 1 }, 'fixed fetch "a"'; + is $res, 14, 'uvar: fixed fetch "a" returned the right thing'; + + my $after = $bd->coderef2text($code); + is $before, $after, 'uvar: fixed fetch deparse correctly'; + + watch { $res = $code->('b') } { fetch => 1 }, 'fixed fetch "b"'; + is $res, 16, 'uvar: fixed fetch "b" returned the right thing'; + + $after = $bd->coderef2text($code); + is $before, $after, 'uvar: fixed fetch deparse correctly'; + + watch { %h = () } { clear => 1 }, 'fixed clear'; + + watch { dispell %h, $wiz } { }, 'dispell clear/uvar'; + + require B; + ok(!(B::svref_2object(\%h)->FLAGS & B::SVs_RMG()), '%h no longer has the RMG flag set'); +}