Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Testing hash equality and reporting differences

by davidrw (Prior)
on Oct 18, 2006 at 23:30 UTC ( [id://579234]=note: print w/replies, xml ) Need Help??


in reply to Testing hash equality and reporting differences

alternatively (esp since you mention it's for a test suite), the docs for Test::More's is_deeply() method mention Test::Differences and Test::Deep:

Header:
#!/usr/bin/perl use strict; use warnings; my %h1 = ( a=>1, b=>2, c=>3 ); my %h2 = ( a=>3, b=>2, d=>4, e=>5 );
Using Test::More
use Test::More qw/no_plan/; is_deeply( \%h1, \%h2, "h1 vs h2" ); __END__ not ok 1 - h1 vs h2 # Failed test 'h1 vs h2' # in /tmp/h at line 10. # Structures begin differing at: # $got->{e} = Does not exist # $expected->{e} = '5' 1..1 # Looks like you failed 1 test of 1.
Using Test::Differences
use Test::More qw/no_plan/; use Test::Differences; eq_or_diff \%h1, \%h2, "testing hashes"; __END__ not ok 1 - testing hashes # Failed test 'testing hashes' # in /tmp/h at line 11. # +----+-----------+----+-----------+ # | Elt|Got | Elt|Expected | # +----+-----------+----+-----------+ # | 0|{ | 0|{ | # * 1| a => 1, * 1| e => 5, * # | | * 2| a => 3, * # | 2| b => 2, | 3| b => 2, | # * 3| c => 3 * 4| d => 4 * # | 4|} | 5|} | # +----+-----------+----+-----------+ 1..1 # Looks like you failed 1 test of 1.
Using Test::Deep
use Test::More qw/no_plan/; use Test::Deep; cmp_deeply \%h1, \%h2, "testing hashes"; __END__ not ok 1 - testing hashes # Failed test 'testing hashes' # in /tmp/h at line 11. # Comparing hash keys of $data # Missing: 'd', 'e' # Extra: 'c' 1..1 # Looks like you failed 1 test of 1.

Replies are listed 'Best First'.
Re^2: Testing hash equality and reporting differences
by GrandFather (Saint) on Oct 18, 2006 at 23:31 UTC

    Thanks for that - even better!


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found