Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: Diagnostic messages in Test::Deep

by Athanasius (Bishop)
on Jul 22, 2015 at 04:43 UTC ( #1135796=note: print w/replies, xml ) Need Help??


in reply to Re^2: Diagnostic messages in Test::Deep
in thread Diagnostic messages in Test::Deep

OK, I think I understand now; I hadn’t realised that separate hashes within the one array could legitimately contain the same keys with different values.

I still think a generic procedure will work, it just needs a comprehensive sorting algorithm:

#! perl use strict; use warnings; use Test::More tests => 2; use Test::Deep; my @x = ( { key_3 => 'A', key_2 => 'CC', key_4 => 'E', key_5 => 0 }, { key_3 => 'A', key_2 => 'DD', key_4 => 'F' }, ); my @y = ( { key_3 => 'A', key_2 => 'DD', key_4 => 'F' }, { key_5 => 0, key_3 => 'A', key_2 => 'CC', key_4 => 'E' }, ); cmp_deeply( \@x, bag(@y), 'Deep' ); print '-' x 20, "\n"; is_deeply( my_sort(@x), my_sort(@y), 'More' ); sub my_sort { my $f = sub { my @keys_a = sort keys %$a; my @keys_b = sort keys %$b; return -1 if @keys_a < @keys_b; return 1 if @keys_a > @keys_b; my $result = 0; $result ||= $keys_a[$_] cmp $keys_b[$_] || $a->{$keys_a[$_]} cmp $b->{$keys_b[$_]} for 0 .. $#k +eys_a; return $result; }; return [ sort $f @_ ]; }

(For the basic idea, see perlfaq4#How-do-I-sort-an-array-by-anything.) Output:

14:38 >perl 1314a_SoPW.pl 1..2 ok 1 - Deep -------------------- ok 2 - More 14:38 >

Disclaimer: I’ve tested this only minimally; there may be corner cases I haven’t found. But I think this should allow you to return to using Test::More::is_deeply to get the benefit of its superior failure messages.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1135796]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2019-10-17 02:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?