X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F32-hash.t;h=703bcd3df82b5f4c30e1b7495fc24accfe5f1d46;hb=ae4a0a994e98c604732190116636b349e7779311;hp=483d9671e93bfed7e4ac1935f0d6c85740293f14;hpb=14f66d40970bef63105be046a109c1a32859a8a0;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/32-hash.t b/t/32-hash.t index 483d967..703bcd3 100644 --- a/t/32-hash.t +++ b/t/32-hash.t @@ -3,78 +3,149 @@ use strict; use warnings; -use Test::More tests => 17; +use Test::More tests => (2 * 27 + 9) + (2 * 5 + 5) + 1; -use Variable::Magic qw/wizard cast dispell/; +use Variable::Magic qw< + cast dispell + VMG_UVAR + VMG_COMPAT_HASH_DELETE_NOUVAR_VOID +>; -my @c = (0) x 5; -my @x = (0) x 5; +use lib 't/lib'; +use Variable::Magic::TestWatcher; -sub check { - for (0 .. 4) { return 0 unless $c[$_] == $x[$_]; } - return 1; +my $wiz = init_watcher + [ qw ], + 'hash'; + +my %n = map { $_ => int rand 1000 } qw; +my %h = %n; + +watch { cast %h, $wiz } { }, 'cast'; + +my $s = watch { $h{foo} } +{ (fetch => 1) x VMG_UVAR }, + 'assign element to'; +is $s, $n{foo}, 'hash: assign element to 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} } + +{ (fetch => 2) x VMG_UVAR }, 'slice'; +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 ($_)"; } -my $wiz = wizard get => sub { ++$c[0] }, - set => sub { ++$c[1] }, - len => sub { ++$c[2]; $_[2] }, - clear => sub { ++$c[3] }, - free => sub { ++$c[4] }; -ok(check(), 'hash : create wizard'); +# delete -my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/; -my %a = %n; +watch { delete $h{bar} } +{ + ((delete => 1) x !VMG_COMPAT_HASH_DELETE_NOUVAR_VOID, copy => 1) x VMG_UVAR +}, 'delete in void context'; -cast %a, $wiz; -ok(check(), 'hash : cast'); +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 ($_)"; +} -my $b = $a{foo}; -ok(check(), 'hash : assign element to'); +# clear -my %b = %a; -ok(check(), 'hash : assign to'); +watch { %h = () } { clear => 1 }, 'empty in list context'; -$b = "X%{a}Y"; -ok(check(), 'hash : interpolate'); +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'; -$b = \%a; -ok(check(), 'hash : reference'); +watch { %h = (a => 1, d => 3) } + +{ (store => 2, copy => 2) x VMG_UVAR, clear => 1 }, + 'assign from list in void context'; -my @b = @a{qw/bar qux/}; -ok(check(), 'hash : slice'); +@b = watch { %h = (a => 1, d => 3) } + +{ (exists => 2, store => 2, copy => 2) x VMG_UVAR, clear => 1 }, + 'assign from list in void context'; -%a = map { $_ => 1 } qw/a b d/; -++$x[3]; -ok(check(), 'hash : assign'); +watch { %h = map { $_ => 1 } qw; } + +{ (store => 3, copy => 3) x VMG_UVAR, clear => 1 }, + 'assign from map in void context'; -$a{d} = 2; -ok(check(), 'hash : assign old element'); +watch { $h{d} = 2 } +{ (store => 1) x VMG_UVAR }, + 'assign old element'; -$a{c} = 3; -ok(check(), 'hash : assign new element'); +watch { $h{c} = 3 } +{ (store => 1, copy => 1) x VMG_UVAR }, + 'assign new element'; -$b = %a; -ok(check(), 'hash : buckets'); +$s = watch { %h } { }, 'buckets'; -@b = keys %a; -ok(check(), 'hash : keys'); +@b = watch { keys %h } { }, 'keys'; +is_deeply [ sort @b ], [ qw ], 'hash: keys correctly'; -@b = values %a; -ok(check(), 'hash : values'); +@b = watch { values %h } { }, 'values'; +is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly'; -while (my ($k, $v) = each %a) { } -ok(check(), 'hash : each'); +watch { while (my ($k, $v) = each %h) { } } { }, 'each'; -{ +watch { my %b = %n; - cast %b, $wiz; -} -++$x[4]; -ok(check(), 'hash : scope end'); + watch { cast %b, $wiz } { }, 'cast 2'; +} { free => 1 }, 'scope end'; + +watch { undef %h } { clear => 1 }, 'undef'; -undef %a; -++$x[3]; -ok(check(), 'hash : undef'); +watch { dispell %h, $wiz } { }, 'dispell'; -dispell %a, $wiz; -ok(check(), 'hash : dispel'); +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'); +}