Perl 单元测试数据结构

Joh*_*ert 5 perl unit-testing data-structures

我正在寻找类似 is_deeply 或 Test::Deep 的 cmp_deeply 的东西,但这只是检查数据结构的键/类型,而不是值。例如,我关心一个键是一个标量数组 ref,而不是值是什么。

谁有想法?我确信我不是第一个必须理解不同数据结构的人。我想测试以确保数据结构的“签名”完好无损,但我不太关心其中的数据或与正则表达式等匹配的内容。

Eth*_*her 2

您可以使用Test::BuilderTest::More中提供的函数相当轻松地编写自己的测试函数。

我编写的测试假设您的意思是非引用的 arrayref ,因为您可以在数组中存储的唯一内容是标量。您可能想要做出调整。

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');
}
Run Code Online (Sandbox Code Playgroud)