perl – Test ::更多is_deeply在比较字符串时不会打印数组/ hash
当Test :: More与arrayrefs和hashrefs相互比较时,相应的诊断消息是真实的信息,并显示结构不同的第一个索引,无论嵌套深度如何.然而,当将arrayref或hashref与简单标量进行比较时,它会在诊断消息中产生带有字符串的标量(带有内存地址和引用类型),这很难解释.
有没有办法使用自定义方式(例如使用Data :: Dumper)将Test :: More配置为漂亮的数组或hashrefs? 以下是两个测试用例的示例.第一个给你一些洞察你的程序中存在的内容,但是意外的.第二个通知用户字符串和arrayref之间的类型不匹配,但不打印arrayref中的任何项目. #!/usr/bin/env perl use strict; use warnings; use Test::More tests => 2; is_deeply( { a => [5],},{ a => [5,6,8],'compare two hashrefs structurally (very informative output)',); is_deeply( [5,"",'compare two "incompatible" values structurally (uninformative output)',); 和TAP输出: 1..2 not ok 1 - compare two hashrefs structurally (very informative output) # Failed test 'compare two hashrefs structurally (very informative output)' # at test-more-failure.pl line 6. # Structures begin differing at: # $got->{a}[1] = Does not exist # $expected->{a}[1] = '6' not ok 2 - compare two "incompatible" values structurally (uninformative output) # Failed test 'compare two "incompatible" values structurally (uninformative output)' # at test-more-failure.pl line 16. # Structures begin differing at: # $got = ARRAY(0x7fe66b82cde8) # $expected = '' # Looks like you failed 2 tests of 2. 看看在Test :: More中的is_deeply的实现,似乎没有办法使用自定义漂亮打印机或配置模块的冗长度.至少没有一个对我来说很明显. 当我们比较引用和非引用时,会发生什么: https://metacpan.org/source/EXODIST/Test-Simple-1.302062/lib/Test/More.pm#L1121 似乎正在调用_format_stack({vals => […]})而不是_format_stack(…) https://metacpan.org/source/EXODIST/Test-Simple-1.302062/lib/Test/More.pm#L1139 解决方法
tl; dr使用is_deeply($this,$that)|| diag根据具体情况解释$this.
你好. I’m the one to blame for 没有支持更改其诊断的方法,对不起,这不太可能. Test :: More被替换为Test2. Test :: More已经在Test2之上实现了,但由于向后兼容性原因没有利用它的功能. 您可以使用Test2::Bundle::More更直接的访问Test2的功能,但它不是100%兼容的,它的显示方式类似于is_deeply.然而,它更灵活,您可以找出改变其诊断行为的方法.看看Test2::Compare. 回到你的问题…根据具体情况使用 my $stuff = [5,8]; is_deeply $stuff,"" || diag explain $stuff; not ok 2 # Failed test at /Users/schwern/tmp/test.plx line 17. # Structures begin differing at: # $got = ARRAY(0x7f80b3803078) # $expected = '' # [ # 5,# 6,# 8 # ] diag是如何打印故障诊断(这是打印到STDERR的更客气的方式). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |