in reply to Diagnostic messages in Test::Deep
Hello KeighleHawk,
Naturally, the array can be returned in random order.
Actually, the elements of an array are always in a fixed order — that’s the nature of arrays. I think you mean the array will be populated in a random order?
...part of the structure I am comparing includes an array of hashes.... Initially, I was using a sort on one of the hash fields, but as my data set increased, I started to hit return values with different hash structures so my sorting column needed to change.
I don’t understand this. If you are comparing an array of hashes with an unordered list of hashes, it seems to me that you have to proceed in two steps:
- Pair the hashes;
- For each pair, the hashes are equal if and only if they contain exactly the same key/value pairs (in any order).
From which (I think) it follows that a generic procedure is all that’s required:
#! perl use strict; use warnings; use Test::More tests => 2; use Test::Deep; my @x = ( { j => 12, a => 1 }, { b => [1, 11] }, { c => 3 } ); my @y = ( { c => 3 }, { b => [0, 11] }, { a => 1, j => 12 } ); cmp_deeply( \@x, bag(@y), 'Deep' ); print '-' x 20, "\n"; is_deeply( sort_hash(@x), sort_hash(@y), 'More' ); sub sort_hash { return [ sort { (sort keys %$a)[0] cmp (sort keys %$b)[0] } @_ ]; }
Output:
16:56 >perl 1314_SoPW.pl 1..2 not ok 1 - Deep # Failed test 'Deep' # at 1314_SoPW.pl line 22. # Comparing $data as a Bag # Missing: 1 reference # Extra: 1 reference -------------------- not ok 2 - More # Failed test 'More' # at 1314_SoPW.pl line 24. # Structures begin differing at: # $got->[1]{b}[0] = '1' # $expected->[1]{b}[0] = '0' # Looks like you failed 2 tests of 2. 16:56 >
Clearly, Test::More::is_deeply returns a much more informative message on failure than does Test::Deep::cmp_deeply — as you have stated. But in what way does my sub sort_hash fall short of the functionality provided by Test::Deep::bag for your purposes? Please clarify by providing sample input for which the comparison with Test::Deep::bag will work, but a generic comparison based on hash keys will not.
Hope that helps,
Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Diagnostic messages in Test::Deep
by KeighleHawk (Scribe) on Jul 21, 2015 at 13:31 UTC | |
by Athanasius (Archbishop) on Jul 22, 2015 at 04:43 UTC |