gpt4 book ai didi

Perl 单元测试数据结构

转载 作者:行者123 更新时间:2023-12-04 20:55:29 29 4
gpt4 key购买 nike

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

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

最佳答案

使用 Test::Builder 中提供的函数,您可以相当轻松地编写自己的测试函数。和 Test::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');
}

关于Perl 单元测试数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5235234/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com