當Test :: More比較像arrayrefs和hashrefs彼此時,相應的診斷消息是非常有用的,並顯示結構不同的第一個索引,無論嵌套深度如何。但是,在將arrayref或hashref與簡單標量進行比較時,它會在診斷消息中生成字符串標量(帶有內存地址和引用類型),這很難解釋。Test :: More is_deeply在與字符串比較時不會很漂亮地打印數組/字符串
有沒有一種方法來配置Test :: More以漂亮的打印陣列或hashrefs以自定義的方式(如使用Data :: Dumper)?
下面是兩個測試用例的示例。第一個給你一些洞察你的程序中出現的內容,但意想不到的。第二個通知用戶字符串和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, 6, 8],
"",
'compare two "incompatible" values structurally (uninformative output)',
);
和抽頭輸出:
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.
綜觀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
如果你想要一個數據結構的並排比較,這真的很不錯。但是,如果您正在開發,並且數據結構發生了變化,您只需要替換測試模塊中的定義,則'diag explain $ got'可以提供更容易的複製和粘貼輸出。 – Randall