perl - Test::More is_deeply does not pretty-print array/hashrefs when comparing against strings -
when test::more compares arrayrefs , hashrefs each other, corresponding diagnostic message informative , shows first index structures differ, regardless of nesting depth. however, when comparing arrayref or hashref simple scalar, produces stringified scalar (with memory address , reference type) in diagnostic message, harder interpret.
is there way configure test::more pretty-print array or hashrefs in custom way (such using data::dumper)?
here's example 2 test cases. first gives insight present in program unexpected. second informs user of type mismatch between string , arrayref, not print items in arrayref.
#!/usr/bin/env perl use strict; use warnings; use test::more tests => 2; is_deeply( { => [5], }, { => [5, 6, 8], }, 'compare 2 hashrefs structurally (very informative output)', ); is_deeply( [5, 6, 8], "", 'compare 2 "incompatible" values structurally (uninformative output)', );
and tap output:
1..2 not ok 1 - compare 2 hashrefs structurally (very informative output) # failed test 'compare 2 hashrefs structurally (very informative output)' # @ test-more-failure.pl line 6. # structures begin differing at: # $got->{a}[1] = not exist # $expected->{a}[1] = '6' not ok 2 - compare 2 "incompatible" values structurally (uninformative output) # failed test 'compare 2 "incompatible" values structurally (uninformative output)' # @ test-more-failure.pl line 16. # structures begin differing at: # $got = array(0x7fe66b82cde8) # $expected = '' # looks failed 2 tests of 2.
looking @ implementation of is_deeply
in test::more, there doesn't seem way use custom pretty-printer or configure verbosity of module. @ least none that's obvious me.
here happens when compare reference , non-reference:
https://metacpan.org/source/exodist/test-simple-1.302062/lib/test/more.pm#l1121
it seems calling _format_stack({vals => [...]})
instead of _format_stack(...)
https://metacpan.org/source/exodist/test-simple-1.302062/lib/test/more.pm#l1139
tl;dr use is_deeply($this, $that) || diag explain $this
on case case basis.
hi. i'm 1 blame is_deeply
. it's deliberately designed not vomit out potentially enormous data structure when fails. instead stops @ first difference. reason don't want globally make is_deeply
dump arguments. if types wrong, if expected apples , got zebras, there's not point in knowing how many zebras , life stories.
there no supported way change diagnostics, sorry, , it's unlikely there be. test::more being replaced test2. test::more implemented on top of test2, doesn't take advantage of features backwards compatibility reasons.
you can use test2::bundle::more more direct access test2's features, not 100% compatible, , displays similar how is_deeply
does. however, more flexible , can figure out way alter diagnostic behavior. test2::compare.
back problem... use explain
on case case basis. explain
uses data::dumper, configured properly, dump data structure. since test::more functions return whether passed or failed, can write is_deeply($this, $that) || diag explain $this
. example...
my $stuff = [5, 6, 8]; is_deeply $stuff, "" || diag explain $stuff; not ok 2 # failed test @ /users/schwern/tmp/test.plx line 17. # structures begin differing at: # $got = array(0x7f80b3803078) # $expected = '' # [ # 5, # 6, # 8 # ]
diag
how print failure diagnostics (it's more polite way print stderr).
Comments
Post a Comment