X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F32-hash.t;h=5072484cf323ec2929856ec67b4c9af2886b0407;hb=e5e39f5e8bec67b3a08f861da63c5eb4cafed09f;hp=7f1669a734941076b70eb60df223d15e820af06d;hpb=fee1a480bc5d827590dc7394e0a77741bad86dc3;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/32-hash.t b/t/32-hash.t index 7f1669a..5072484 100644 --- a/t/32-hash.t +++ b/t/32-hash.t @@ -3,101 +3,112 @@ use strict; use warnings; -use Test::More tests => 18; +use Test::More tests => (2 * 21 + 7) + (2 * 5 + 4) + 1; -use Variable::Magic qw/wizard cast dispell MGf_COPY VMG_UVAR VMG_COMPAT_HASH_LISTASSIGN_COPY/; +use Variable::Magic qw/cast dispell MGf_COPY VMG_UVAR/; -my @c = (0) x 12; -my @x = (0) x 12; +use lib 't/lib'; +use Variable::Magic::TestWatcher; -sub check { - for (0 .. 11) { return 0 unless $c[$_] == $x[$_]; } - return 1; -} - -my $wiz = wizard get => sub { ++$c[0] }, - set => sub { ++$c[1] }, - len => sub { ++$c[2]; $_[2] }, - clear => sub { ++$c[3] }, - free => sub { ++$c[4] }, - copy => sub { ++$c[5] }, - dup => sub { ++$c[6] }, - local => sub { ++$c[7] }, - fetch => sub { ++$c[8] }, - store => sub { ++$c[9] }, - 'exists' => sub { ++$c[10] }, - 'delete' => sub { ++$c[11] }; -ok(check(), 'hash : create wizard'); +my $wiz = init_watcher + [ qw/get set len clear free copy dup local fetch store exists delete/ ], + 'hash'; my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/; -my %a = %n; +my %h = %n; -cast %a, $wiz; -ok(check(), 'hash : cast'); +watch { cast %h, $wiz } { }, 'cast'; -my $b = $a{foo}; -++$x[5] if MGf_COPY; -++$x[8] if VMG_UVAR; -ok(check(), 'hash : assign element to'); +my $s = watch { $h{foo} } +{ (fetch => 1) x VMG_UVAR }, + 'assign element to'; +is $s, $n{foo}, 'hash: assign element to correctly'; -my %b = %a; -ok(check(), 'hash : assign to'); +for (1 .. 2) { + $s = watch { exists $h{foo} } +{ (exists => 1) x VMG_UVAR }, "exists ($_)"; + ok $s, "hash: exists correctly ($_)"; +} -$b = "X%{a}Y"; -ok(check(), 'hash : interpolate'); +my %b; +watch { %b = %h } { }, 'assign to'; +is_deeply \%b, \%n, 'hash: assign to correctly'; -$b = \%a; -ok(check(), 'hash : reference'); +$s = watch { \%h } { }, 'reference'; -my @b = @a{qw/bar qux/}; -$x[5] += 2 if MGf_COPY; -$x[8] += 2 if VMG_UVAR; -ok(check(), 'hash : slice'); +my @b = watch { @h{qw/bar qux/} } + +{ (fetch => 2) x VMG_UVAR }, 'slice'; +is_deeply \@b, [ @n{qw/bar qux/} ], 'hash: slice correctly'; -%a = (a => 1, d => 3); -++$x[3]; -$x[5] += 2 if VMG_COMPAT_HASH_LISTASSIGN_COPY; -$x[9] += 2 if VMG_UVAR; -ok(check(), 'hash : assign from list'); +watch { %h = () } { clear => 1 }, 'empty in list context'; -%a = map { $_ => 1 } qw/a b d/; -++$x[3]; -$x[5] += 3 if VMG_COMPAT_HASH_LISTASSIGN_COPY; -$x[9] += 3 if VMG_UVAR; -ok(check(), 'hash : assign from map'); +watch { %h = (a => 1, d => 3); () } + +{ (store => 2, copy => 2) x VMG_UVAR, clear => 1 }, + 'assign from list in void context'; -$a{d} = 2; -++$x[5] if MGf_COPY; -++$x[9] if VMG_UVAR; -ok(check(), 'hash : assign old element'); +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'; -$a{c} = 3; -++$x[5] if MGf_COPY; -++$x[9] if VMG_UVAR; -ok(check(), 'hash : assign new element'); +watch { $h{d} = 2; () } +{ (store => 1) x VMG_UVAR }, + 'assign old element'; -$b = %a; -ok(check(), 'hash : buckets'); +watch { $h{c} = 3; () } +{ (store => 1, copy => 1) x VMG_UVAR }, + 'assign new element'; -@b = keys %a; -ok(check(), 'hash : keys'); +$s = watch { %h } { }, 'buckets'; -@b = values %a; -ok(check(), 'hash : values'); +@b = watch { keys %h } { }, 'keys'; +is_deeply [ sort @b ], [ qw/a b c d/ ], 'hash: keys correctly'; -while (my ($k, $v) = each %a) { } -ok(check(), 'hash : each'); +@b = watch { values %h } { }, 'values'; +is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly'; -{ +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'; + +watch { dispell %h, $wiz } { }, 'dispell'; + +SKIP: { + my $SKIP; + + unless (VMG_UVAR) { + $SKIP = 'uvar magic'; + } else { + eval "use B::Deparse"; + $SKIP = 'B::Deparse' if $@; + } + if ($SKIP) { + $SKIP .= ' required to test uvar/clear interaction fix'; + skip $SKIP => 2 * 5 + 4; + } -undef %a; -++$x[3]; -ok(check(), 'hash : undef'); + my $bd = B::Deparse->new; -dispell %a, $wiz; -ok(check(), 'hash : dispel'); + 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'; +}