X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F32-hash.t;h=df46de2fbac67a768fc96f3777e6df373fb61e09;hb=HEAD;hp=0ad2e96413adb937e21f6d1d6bb8acb3b7f44c4c;hpb=159e8ad7c747edb4a25db2f2673bbe143531f347;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/32-hash.t b/t/32-hash.t index 0ad2e96..df46de2 100644 --- a/t/32-hash.t +++ b/t/32-hash.t @@ -3,18 +3,22 @@ use strict; use warnings; -use Test::More tests => (2 * 21 + 7) + (2 * 5 + 5) + 1; +use Test::More tests => (2 * 27 + 9) + 2 * (2 * 5 + 5) + 1; -use Variable::Magic qw/cast dispell MGf_COPY VMG_UVAR/; +use Variable::Magic qw< + cast dispell + VMG_UVAR + VMG_COMPAT_HASH_DELETE_NOUVAR_VOID +>; 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'; @@ -23,41 +27,69 @@ my $s = watch { $h{foo} } +{ (fetch => 1) x VMG_UVAR }, 'assign element to'; is $s, $n{foo}, 'hash: assign element to correctly'; -for (1 .. 2) { - $s = watch { exists $h{foo} } +{ (exists => 1) x VMG_UVAR }, "exists ($_)"; - ok $s, "hash: exists correctly ($_)"; -} - my %b; watch { %b = %h } { }, 'assign to'; 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'; + +# exists + +watch { exists $h{bar} } +{ (exists => 1) x VMG_UVAR },'exists in void context'; + +for (1 .. 2) { + $s = watch { exists $h{bar} } +{ (exists => 1) x VMG_UVAR }, + "exists in scalar context ($_)"; + ok $s, "hash: exists correctly ($_)"; +} + +# delete + +watch { delete $h{bar} } +{ + ((delete => 1) x !VMG_COMPAT_HASH_DELETE_NOUVAR_VOID, copy => 1) x VMG_UVAR +}, 'delete in void context'; + +for (1 .. 2) { + $s = watch { delete $h{baz} } +{ (delete => 1, copy => 1) x VMG_UVAR }, + "delete in scalar context ($_)"; + my $exp = $_ == 1 ? $n{baz} : undef; + is $s, $exp, "hash: delete correctly ($_)"; +} + +# clear watch { %h = () } { clear => 1 }, 'empty in list context'; -watch { %h = (a => 1, d => 3); () } +watch { $h{a} = -1; %h = (b => $h{a}) } + +{ (fetch => 1, store => 2, copy => 2) x VMG_UVAR, clear => 1 }, + 'empty and set in void context'; + +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/; } - +{ (exists => 3, store => 3, copy => 3) x VMG_UVAR, clear => 1 }, - 'assign from map in list context'; +@b = watch { %h = (a => 1, d => 3) } + +{ (exists => 2, store => 2, copy => 2) x VMG_UVAR, clear => 1 }, + 'assign from list in void context'; -watch { $h{d} = 2; () } +{ (store => 1) x VMG_UVAR }, +watch { %h = map { $_ => 1 } qw; } + +{ (store => 3, copy => 3) x VMG_UVAR, clear => 1 }, + 'assign from map in void context'; + +watch { $h{d} = 2 } +{ (store => 1) x VMG_UVAR }, 'assign old element'; -watch { $h{c} = 3; () } +{ (store => 1, copy => 1) x VMG_UVAR }, +watch { $h{c} = 3 } +{ (store => 1, copy => 1) x VMG_UVAR }, 'assign new element'; $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'; @@ -76,42 +108,58 @@ watch { dispell %h, $wiz } { }, 'dispell'; SKIP: { my $SKIP; - unless (VMG_UVAR) { + if (!VMG_UVAR) { $SKIP = 'uvar magic'; } else { - eval "use B::Deparse"; - $SKIP = 'B::Deparse' if $@; + local $@; + unless (eval { require B; require B::Deparse; 1 }) { + $SKIP = 'B and B::Deparse'; + } } if ($SKIP) { $SKIP .= ' required to test uvar/clear interaction fix'; - skip $SKIP => 2 * 5 + 5; + skip $SKIP => 2 * ( 2 * 5 + 5); } my $bd = B::Deparse->new; - my %h = (a => 13, b => 15); - watch { cast %h, $wiz } { }, 'cast clear/uvar'; + my %h1 = (a => 13, b => 15); + my %h2 = (a => 17, b => 19); + + my @tests = ( + [ \%h1 => 'first hash' => (14, 16) ], + [ \%h2 => 'second hash' => (18, 20) ], + ); + + for my $test (@tests) { + my ($h, $desc, @exp) = @$test; - my $code = sub { my $x = $h{$_[0]}; ++$x; $x }; - my $before = $bd->coderef2text($code); - my $res; + watch { &cast($h, $wiz) } { }, "cast clear/uvar on $desc"; - watch { $res = $code->('a') } { fetch => 1 }, 'fixed fetch "a"'; - is $res, 14, 'uvar: fixed fetch "a" returned the right thing'; + my $code = sub { my $x = $h->{$_[0]}; ++$x; $x }; + my $before = $bd->coderef2text($code); + my $res; - my $after = $bd->coderef2text($code); - is $before, $after, 'uvar: fixed fetch deparse correctly'; + watch { $res = $code->('a') } { fetch => 1 }, "fetch constant 'a' from $desc"; + is $res, $exp[0], "uvar: fetch constant 'a' from $desc was correct"; - watch { $res = $code->('b') } { fetch => 1 }, 'fixed fetch "b"'; - is $res, 16, 'uvar: fixed fetch "b" returned the right thing'; + my $after = $bd->coderef2text($code); + is $before, $after, + "uvar: code deparses correctly after constant fetch from $desc"; - $after = $bd->coderef2text($code); - is $before, $after, 'uvar: fixed fetch deparse correctly'; + my $key = 'b'; + watch { $res = $code->($key) } { fetch => 1 },"fetch variable 'b' from $desc"; + is $res, $exp[1], "uvar: fetch variable 'b' from $desc was correct"; - watch { %h = () } { clear => 1 }, 'fixed clear'; + $after = $bd->coderef2text($code); + is $before, $after, + "uvar: code deparses correctly after variable fetch from $desc"; - watch { dispell %h, $wiz } { }, 'dispell clear/uvar'; + watch { %$h = () } { clear => 1 }, "fixed clear for $desc"; - require B; - ok(!(B::svref_2object(\%h)->FLAGS & B::SVs_RMG()), '%h no longer has the RMG flag set'); + watch { &dispell($h, $wiz) } { }, "dispell clear/uvar from $desc"; + + ok(!(B::svref_2object($h)->FLAGS & B::SVs_RMG()), + "$desc no longer has the RMG flag set"); + } }