perl Testing读书笔记(测试复杂的数据结构)

上一篇 / 下一篇  2012-04-11 23:28:55 / 个人分类:perl相关

 

Test::Deep模块可以对复杂数据结构进行测试,下面是个示例cmp_deeply.pl

#!/usr/bin/perl

use Test::More tests => 1;
use Test::Deep;

my $points =
[
    {x => 50, y => 75 },
    {x => 19, y => -29 },
];

my $is_integer = re( '^-?\d+$');

cmp_deeply ( $points,
    array_each(
     {
        x => $is_integer,
        y => $is_integer,
     }
    ),
    'both sets of points should be integers' );

运行结果

D:\loveperl>prove cmp_deeply.pl
cmp_deeply....ok
All tests successful.
Files=1, Tests=1,  0 wallclock secs ( 0.00 cusr +  0.00 csys =  0.00 CPU)

首先是通过re()函数创建一个正则表达式,来匹配正负的整数。然后调用cmp_deeply()进行比较。

修改$points 的第二个成员的键值为字符'Q',然后再运行一次看下结果。cmp_deeply2.pl

#!/usr/bin/perl

use Test::More tests => 1;
use Test::Deep;

my $points =
[
    {x => 50, y => 75 },
    {x => 19, y => 'Q' },
];

my $is_integer = re( '^-?\d+$');

cmp_deeply ( $points,
    array_each(
     {
        x => $is_integer,
        y => $is_integer,
     }
    ),
    'both sets of points should be integers' );

运行结果如下

D:\loveperl>prove cmp_deeply.pl
cmp_deeply....
cmp_deeply....NOK 1/1#   Failed test 'both sets of points should be integers'
#   at cmp_deeply.pl line 14.
# Using Regexp on $data->[1]{"y"}
#    got : 'Q'
# expect : (?-xism:^-?\d+$)
# Looks like you failed 1 test of 1.
cmp_deeply....dubious
        Test returned status 1 (wstat 256, 0x100)
DIED. FAILED test 1
        Failed 1/1 tests, 0.00% okay
Failed Test   Stat Wstat Total Fail  List of Failed
-----------------------------------------------------------------------------
cmp_deeply.pl    1   256     1    1  1
Failed 1/1 test scripts. 1/1 subtests failed.
Files=1, Tests=1,  0 wallclock secs ( 0.00 cusr +  0.00 csys =  0.00 CPU)
Failed 1/1 test programs. 1/1 subtests failed.


TAG:

 

评分:0

我来说两句

Open Toolbar