X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F32-hash.t;h=4686d27dbdaa75547a71594e692bb92392946990;hb=997fe5643a5c203315ff26f4e4bf7840fb33a9d8;hp=74eabb81ab038980349add35fc474a56eae7e036;hpb=77a84f75f33e3ee44e61182dec76699e23025375;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/32-hash.t b/t/32-hash.t index 74eabb8..4686d27 100644 --- a/t/32-hash.t +++ b/t/32-hash.t @@ -1,77 +1,76 @@ #!perl -T -use Test::More tests => 17; +use strict; +use warnings; -use Variable::Magic qw/wizard cast dispell/; +use Test::More tests => 2 * 19 + 5 + 1; -my @c = (0) x 5; -my @x = (0) x 5; +use Variable::Magic qw/cast dispell MGf_COPY VMG_UVAR/; -sub check { - for (0 .. 4) { return 0 unless $c[$_] == $x[$_]; } - return 1; -} +use lib 't/lib'; +use Variable::Magic::TestWatcher; -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'); +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 %a = %n; +my %h = %n; -cast %a, $wiz; -ok(check(), 'hash : cast'); +check { cast %h, $wiz } { }, 'cast'; -my $b = $a{foo}; -ok(check(), 'hash : assign element 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'; -my %b = %a; -ok(check(), 'hash : assign to'); +my %b; +check { %b = %h } { }, 'assign to'; +is_deeply \%b, \%n, 'hash: assign to correctly'; -$b = "X%{a}Y"; -ok(check(), 'hash : interpolate'); +check { $s = \%h } { }, 'reference'; -$b = \%a; -ok(check(), 'hash : reference'); +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'; -my @b = @a{qw/bar qux/}; -ok(check(), 'hash : slice'); +check { %h = () } { }, 'empty in list context'; # clear => 1 -%a = map { $_ => 1 } qw/a b d/; -++$x[3]; -ok(check(), 'hash : assign'); +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{d} = 2; -ok(check(), 'hash : assign old element'); +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{c} = 3; -ok(check(), 'hash : assign new element'); +check { $h{d} = 2; () } +{ (store => 1) x VMG_UVAR }, + 'assign old element'; -$b = %a; -ok(check(), 'hash : buckets'); +check { $h{c} = 3; () } +{ (store => 1) x VMG_UVAR }, + # (copy => 1) x VMG_UVAR # maybe also if clear magic + 'assign new element'; -@b = keys %a; -ok(check(), 'hash : keys'); +check { $s = %h } { }, 'buckets'; -@b = values %a; -ok(check(), 'hash : values'); +check { @b = 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'); +check { @b = values %h } { }, 'values'; +is_deeply [ sort { $a <=> $b } @b ], [ 1, 1, 2, 3 ], 'hash: values correctly'; -{ +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';