X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F32-hash.t;h=8ae273e73037b3171dd35de0b4b5b5749e29519b;hb=12570a1f39c0588d6ae68281b45ef1b7e6083958;hp=4686d27dbdaa75547a71594e692bb92392946990;hpb=997fe5643a5c203315ff26f4e4bf7840fb33a9d8;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/32-hash.t b/t/32-hash.t index 4686d27..8ae273e 100644 --- a/t/32-hash.t +++ b/t/32-hash.t @@ -3,74 +3,130 @@ use strict; use warnings; -use Test::More tests => 2 * 19 + 5 + 1; +use Test::More tests => (2 * 24 + 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 - [ qw/get set len free dup local fetch store exists delete/ ], # clear copy +my $wiz = init_watcher + [ qw ], 'hash'; -my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/; +my %n = map { $_ => int rand 1000 } qw; my %h = %n; -check { cast %h, $wiz } { }, 'cast'; +watch { cast %h, $wiz } { }, 'cast'; -my $s; -check { $s = $h{foo} } +{ (fetch => 1) x VMG_UVAR }, - # (copy => 1) x MGf_COPY # if clear magic +my $s = watch { $h{foo} } +{ (fetch => 1) x VMG_UVAR }, 'assign element to'; is $s, $n{foo}, 'hash: assign element to correctly'; my %b; -check { %b = %h } { }, 'assign to'; +watch { %b = %h } { }, 'assign to'; is_deeply \%b, \%n, 'hash: assign to correctly'; -check { $s = \%h } { }, 'reference'; +$s = watch { \%h } { }, 'reference'; -my @b; -check { @b = @h{qw/bar qux/} } +my @b = watch { @h{qw} } +{ (fetch => 2) x VMG_UVAR }, 'slice'; - # (copy => 2) x MGf_COPY # if clear magic -is_deeply \@b, [ @n{qw/bar qux/} ], 'hash: slice correctly'; +is_deeply \@b, [ @n{qw} ], 'hash: slice correctly'; -check { %h = () } { }, 'empty in list context'; # clear => 1 +# exists -check { %h = (a => 1, d => 3); () } - +{ (store => 2) x VMG_UVAR }, - # clear => 1, (copy => 2) x VMG_UVAR +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 ($_)"; +} + +watch { %h = () } { clear => 1 }, 'empty in list context'; + +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'; -check { %h = map { $_ => 1 } qw/a b d/; } - +{ (exists => 3, store => 3) x VMG_UVAR }, - # clear => 1, (copy => 3) x VMG_UVAR - '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'; -check { $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'; -check { $h{c} = 3; () } +{ (store => 1) x VMG_UVAR }, - # (copy => 1) x VMG_UVAR # maybe also if clear magic +watch { $h{c} = 3 } +{ (store => 1, copy => 1) x VMG_UVAR }, 'assign new element'; -check { $s = %h } { }, 'buckets'; +$s = watch { %h } { }, 'buckets'; -check { @b = keys %h } { }, 'keys'; -is_deeply [ sort @b ], [ qw/a b c d/ ], 'hash: keys correctly'; +@b = watch { keys %h } { }, 'keys'; +is_deeply [ sort @b ], [ qw ], 'hash: keys correctly'; -check { @b = values %h } { }, 'values'; +@b = watch { values %h } { }, 'values'; is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly'; -check { while (my ($k, $v) = each %h) { } } { }, 'each'; +watch { while (my ($k, $v) = each %h) { } } { }, 'each'; -check { +watch { my %b = %n; - check { cast %b, $wiz } { }, 'cast 2'; + watch { cast %b, $wiz } { }, 'cast 2'; } { free => 1 }, 'scope end'; -check { undef %h } { }, 'undef'; # clear => 1 +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'; -check { dispell %h, $wiz } { }, 'dispell'; + require B; + ok(!(B::svref_2object(\%h)->FLAGS & B::SVs_RMG()), '%h no longer has the RMG flag set'); +}