加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – Test ::更多is_deeply在比较字符串时不会打印数组/ hash

发布时间:2020-12-15 22:02:10 所属栏目:大数据 来源:网络整理
导读:当Test :: More与arrayrefs和hashrefs相互比较时,相应的诊断消息是真实的信息,并显示结构不同的第一个索引,无论嵌套深度如何.然而,当将arrayref或hashref与简单标量进行比较时,它会在诊断消息中产生带有字符串的标量(带有内存地址和引用类型),这很难解释. 有
当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 is_deeply.故意设计为在某些事情发生时不会呕吐出潜在的巨大数据结构.相反,它停止在第一个差异.因此,您可能不希望全局使is_deeply转储其参数.如果类型错误,如果你想要苹果和斑马,知道有多少斑马和他们的生活故事没有太多的意义.

没有支持更改其诊断的方法,对不起,这不太可能. Test :: More被替换为Test2. Test :: More已经在Test2之上实现了,但由于向后兼容性原因没有利用它的功能.

您可以使用Test2::Bundle::More更直接的访问Test2的功能,但它不是100%兼容的,它的显示方式类似于is_deeply.然而,它更灵活,您可以找出改变其诊断行为的方法.看看Test2::Compare.

回到你的问题…根据具体情况使用explain.解释使用Data :: Dumper,配置正确,转储数据结构.由于Test :: More函数返回是否通过或失败,可以编写is_deeply($this,$that)|| diag解释$这个.例如…

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的更客气的方式).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读