【发布时间】:2021-11-23 18:41:33
【问题描述】:
我正在编写一个单元测试,我必须检查两个散列变量(散列的散列)的关键结构是否相同。键值可以不同。哈希的深度是任意的。
Test::Deep 看起来很理想,但我不知道如何让cmp_deeply 忽略这些值。
use Test::Deep;
my %hash1 = ( key1 => "foo", key2 => {key21 => "bar", key22 => "yeah"});
my %hash2 = ( key1 => "foo", key2 => {key21 => "bar", key22 => "wow"});
cmp_deeply(\%hash1, \%hash2, "This test should not fail");
输出:
not ok 1 - This test should not fail
# Failed test 'This test should not fail'
# at demo.pl line 13.
# Compared $data->{"key2"}{"key22"}
# got : 'yeah'
# expect : 'wow'
如果哈希具有已知结构,我可以使用值为ignore() 的测试变量。但是,就我而言,最好的解决方案是我不必更新测试代码中的结构。
我尝试使用Data::Walk 遍历%hash1 并检查%hash2 中是否存在每个键,但发现很难从$Data::Walk::container 值中获取当前键。
对于合适的比较工具有什么想法吗?
【问题讨论】:
标签: perl testing data-structures hash