X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F32-hash.t;h=d04c366a3a1688f38baa4eff3ff307291a8941c1;hb=54a1b1e4382ad55a54cbd874d6ffd87bf58dffbc;hp=572f5e668e42fe18033cd0765ba7e4ef652bc152;hpb=9e65e9bfbd5fa2ee747e865d1471b26ec229383c;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/32-hash.t b/t/32-hash.t index 572f5e6..d04c366 100644 --- a/t/32-hash.t +++ b/t/32-hash.t @@ -3,101 +3,72 @@ use strict; use warnings; -use Test::More tests => 18; - -use Variable::Magic qw/wizard cast dispell MGf_COPY VMG_UVAR/; - -my @c = (0) x 12; -my @x = (0) x 12; - -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'); +use Test::More tests => 2 * 18 + 5 + 1; -my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/; -my %a = %n; +use Variable::Magic qw/cast dispell MGf_COPY VMG_UVAR/; + +use lib 't/lib'; +use Variable::Magic::TestWatcher; -cast %a, $wiz; -ok(check(), 'hash : cast'); +my $wiz = init + [ qw/get set len free dup local fetch store exists delete/ ], # clear copy + 'hash'; + +my %n = map { $_ => int rand 1000 } qw/foo bar baz qux/; +my %h = %n; -my $b = $a{foo}; -++$x[5] if MGf_COPY; -++$x[8] if VMG_UVAR; -ok(check(), 'hash : assign element to'); +check { cast %h, $wiz } { }, 'cast'; -my %b = %a; -ok(check(), 'hash : assign to'); +my $s; +check { $s = $h{foo} } +{ (fetch => 1) x VMG_UVAR }, + # (copy => 1) x MGf_COPY # if clear magic + 'assign element to'; +is $s, $n{foo}, 'hash: assign element to correctly'; -$b = "X%{a}Y"; -ok(check(), 'hash : interpolate'); +my %b; +check { %b = %h } { }, 'assign to'; +is_deeply \%b, \%n, 'hash: assign to correctly'; -$b = \%a; -ok(check(), 'hash : reference'); +check { $s = \%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; +check { @b = @h{qw/bar qux/} } + +{ (fetch => 2) x VMG_UVAR }, 'slice'; + # (copy => 2) x MGf_COPY # if clear magic +is_deeply \@b, [ @n{qw/bar qux/} ], 'hash: slice correctly'; -%a = (a => 1, d => 3); -++$x[3]; -$x[5] += 2 if VMG_UVAR; -$x[9] += 2 if VMG_UVAR; -ok(check(), 'hash : assign from list'); +check { %h = (a => 1, d => 3); () } + +{ (store => 2) x VMG_UVAR }, + # clear => 1, (copy => 2) x VMG_UVAR + 'assign from list in void context'; -%a = map { $_ => 1 } qw/a b d/; -++$x[3]; -$x[5] += 3 if VMG_UVAR; -$x[9] += 3 if VMG_UVAR; -ok(check(), 'hash : assign from map'); +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'; -$a{d} = 2; -++$x[5] if MGf_COPY; -++$x[9] if VMG_UVAR; -ok(check(), 'hash : assign old element'); +check { $h{d} = 2; () } +{ (store => 1) x VMG_UVAR }, + 'assign old element'; -$a{c} = 3; -++$x[5] if MGf_COPY; -++$x[9] if VMG_UVAR; -ok(check(), 'hash : assign new element'); +check { $h{c} = 3; () } +{ (store => 1) x VMG_UVAR }, + # (copy => 1) x VMG_UVAR # maybe also if clear magic + 'assign new element'; -$b = %a; -ok(check(), 'hash : buckets'); +check { $s = %h } { }, 'buckets'; -@b = keys %a; -ok(check(), 'hash : keys'); +check { @b = keys %h } { }, 'keys'; +is_deeply [ sort @b ], [ qw/a b c d/ ], 'hash: keys correctly'; -@b = values %a; -ok(check(), 'hash : values'); +check { @b = 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'); +check { while (my ($k, $v) = each %h) { } } { }, 'each'; -{ +check { my %b = %n; - cast %b, $wiz; -} -++$x[4]; -ok(check(), 'hash : scope end'); + check { cast %b, $wiz } { }, 'cast 2'; +} { free => 1 }, 'scope end'; -undef %a; -++$x[3]; -ok(check(), 'hash : undef'); +check { undef %h } { }, 'undef'; # clear => 1 -dispell %a, $wiz; -ok(check(), 'hash : dispel'); +check { dispell %h, $wiz } { }, 'dispell';