我正在编写一个单元测试,需要检查两个哈希变量(哈希的哈希)的键结构是否相同。键值可以不同。哈希的深度是任意的。
看起来
如果哈希具有已知结构,我可以使用测试变量并附上
我尝试使用
有没有适合的比较工具的想法?
看起来
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
值中获取当前键。有没有适合的比较工具的想法?