Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Test::More - equal_set

by edoc (Chaplain)
on Jun 01, 2003 at 00:40 UTC ( [id://262148]=note: print w/replies, xml ) Need Help??


in reply to Test::More - equal_set

mmm.. I been bit by this, but I think it's what you could call a bug in the Test::More docs.. maybe, depending on how you read it..

"This is a deep check, but the irrelevancy of order only applies to the top level."

Basically, the test below shows that order doesn't matter for a shallow test, but once you have more than the "top level", order does matter.

#!/usr/bin/perl use Test::More qw(no_plan); my (@this,@that); @this = ('Fred','Wilma'); @that = ('Fred','Wilma'); ok(eq_set(\@this, \@that), 'Shallow Order Same'); @this = ('Fred','Wilma'); @that = ('Wilma','Fred'); ok(eq_set(\@this, \@that), 'Shallow Order Diff'); @this = ( { fred => 1 }, { wilma => 1 } ); @that = ( { fred => 1 }, { wilma => 1 } ); ok(eq_set(\@this, \@that), 'Deep Order Same'); @this = ( { fred => 1 }, { wilma => 1 } ); @that = ( { wilma => 1 }, { fred => 1 } ); ok(eq_set(\@this, \@that), 'Deep Order Diff'); __END__ prints: ok 1 - Shallow Order Same ok 2 - Shallow Order Diff ok 3 - Deep Order Same not ok 4 - Deep Order Diff # Failed test (testmore.pl at line 21) 1..4 # Looks like you failed 1 tests of 4.

cheers,

J

Replies are listed 'Best First'.
Re: Test::More - equal_set
by brewer (Sexton) on Jun 01, 2003 at 01:21 UTC

    Thanks for that, edoc. That makes at least two of us who've interpreted the docs in a way that wasn't intended.

    So, is the best way to proceed to sort my array before I do the test? This time I've already defined a way to order my hashrefs, but I can see that sometimes I might have to write a sorting function when I otherwise might not have needed to.

    So now I'm thinking that it should be possible to write a standard comparison function, that works by examining the output of Data::Dumper to force a well defined (but otherwise meaningless) order on an array, so that it could make equal_set work as I thought the docs said it should.

    Anyone see why this shouldn't work? Can anybody see any traps I might fall into, if i were to try to write this?

    Kind regards,

    brewer

      ok, knew there had to be something out there... Test::Deep using 'bag' (or set maybe, they look the same in the docs..) Unfortunately it doesn't seem to be a standard Perl module.

      #!/usr/bin/perl use Test::More qw(no_plan); use Test::Deep; my (@this,@that); @this = ( { fred => 1 }, { wilma => 1 } ); @that = ( { wilma => 1 }, { fred => 1 } ); cmp_deeply(\@this, bag(@that), 'cmp_deeply Deep Order Diff');

      cheers,

      J

        Thanks again, It looks like I don't get to write anything - cmp_bag() from Test::Deep gives me one more passed test.

        I'm really starting to see the value in writing tests - I may try writing the tests before the code for the next itch that I scratch.

        Kind regards,

        brewer

Log In?
Username:
Password:

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

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

    No recent polls found