When Test::More compares like arrayrefs and hashrefs with each other, the corresponding diagnostic message is really informative and shows the first index where the structures differ, regardless of nesting depth. However, when it is comparing an arrayref or hashref to a simple scalar, it produces a stringified scalar (with a memory address and reference type) in the diagnostic message, which is harder to interpret.
Is there a way to configure Test::More to pretty-print array or hashrefs in a custom way (such as using Data::Dumper)?
Here's an example with two test cases. The first gives you some insight into what is present in your program but unexpected. The second informs the user of a type mismatch between the string and the arrayref, but does not print any items in the 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)',
);
And the TAP 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.
Looking at the implementation of is_deeply
in Test::More, there doesn't seem to be way to use a custom pretty-printer or configure the verbosity of the module. At least none that's obvious to me.
Here is what happens when we compare a reference and non-reference:
https://metacpan.org/source/EXODIST/Test-Simple-1.302062/lib/Test/More.pm#L1121
It seems to be calling _format_stack({vals => [...]})
instead of _format_stack(...)
https://metacpan.org/source/EXODIST/Test-Simple-1.302062/lib/Test/More.pm#L1139
diag explain $got
provides an easier output to copy and paste. – Dukes