JUST FOR FUN

Twitter:@okwra Facebook:ayato.ookawara GitHub:@tearon4 other:@taiga006

perlでHashの一部分をテストで確認したいときにsuperhashof便利やん。

f:id:taiga006:20180509225412p:plain:w500
use Test::More;
use Test::Deep;

my $user = {
        name => '齋藤飛鳥',
        age => 19,
        height => 158,
        blood_type => 'O',
        center => '裸足でSummer',
    };

is $user->{age}, 19;
cmp_deeply $user, superhashof { name => '齋藤飛鳥', blood_type => 'O' };

done_testing;

[output]

₍ ᕕ( ‘ω’)ᕗ⁾ $ prove -lvf superhashof.pl
superhashof.pl ..
ok 1
ok 2
1..2
ok
All tests successful.
Files=1, Tests=2,  0 wallclock secs ( 0.03 usr  0.01 sys +  0.07 cusr  0.01 csys =  0.1
2 CPU)
Result: PASS

こける場合はこんな感じ。

use Test::More;
use Test::Deep;

my $user_2 = {
        name => '斉藤優里',
        age => 24,
        height => 157,
        blood_type => 'O',
        center => '13日の金曜日',
    };

cmp_deeply $user_2, superhashof { name => '齋藤優里', height => 157 };

done_testing;

[output]

₍ ᕕ( ‘ω’)ᕗ⁾ $ prove -lvf superhashof.pl
superhashof.pl ..
not ok 1
#   Failed test at superhashof.pl line 12.
# Compared $data->{"name"}
#    got : '斉藤優里'
# expect : '齋藤優里'
1..1
# Looks like you failed 1 test of 1.
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests

Test Summary Report
-------------------
superhashof.pl (Wstat: 256 Tests: 1 Failed: 1)
  Failed test:  1
  Non-zero exit status: 1
Files=1, Tests=1,  0 wallclock secs ( 0.02 usr  0.00 sys +  0.05 cusr  0.01 csys =  0.08 CPU)
Result: FAIL

[参考]

Test::Deep - search.cpan.org