【问题标题】:Perl Unit Testing Data StructuresPerl 单元测试数据结构
【发布时间】:2011-03-10 03:06:40
【问题描述】:
我正在寻找类似 is_deeply 或 Test::Deep 的 cmp_deeply 的东西,但这只是检查数据结构的键/类型,而不是值。例如,我关心键是标量的数组引用,而不是值是什么。
有人有什么想法吗?我确信我不是第一个必须理解不同数据结构的人。我想测试以确保数据结构的“签名”完好无损,但我不太关心其中的数据或与正则表达式等匹配的内容。
【问题讨论】:
标签:
perl
unit-testing
data-structures
【解决方案1】:
您可以使用Test::Builder 和Test::More 中提供的函数相当轻松地编写自己的测试函数。
我已经编写了测试,假设您的意思是 非引用 的数组引用,因为您可以在数组中存储的唯一内容是标量。您可能需要进行调整。
use Test::Builder;
use Test::More 0.81_01;
sub is_arrayref_of_nonrefs
{
my $value = shift;
local $Test::Builder::Level = $Test::Builder::Level + 1;
return Test::More::ok(0, 'value is an arrayref')
if not ref $value or ref $value ne 'ARRAY';
# fail if any references are found in the arrayref
Test::More::ok((grep { ref } @$value), 'value is an arrayref of non-references');
}
【解决方案2】:
普通的 Test::More isa_ok 方法适用于引用:
isa_ok( $array_ref, 'ARRAY' );