Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
yes, the array can be populated in random order :-).

The data set is slightly different than your example. The keys for each hash within the test run are the same. They differ between runs/tests.

Test 1 sample.

$got = [ { key_1 => "A", key_2 => "CC"} , { key_1 => "B", key_2 => "CC"} ] $expected = [ { key_1 => "B", key_2 => "CC"} , { key_1 => "A", key_2 => "CC"} ]

I want these to evaluate as equal, and they do with code like this (similar to yours):

return is deeply ( $got ? [ sort {$a->{key_1} cmp $b->{key_1}} $got ] : [] , $expected ? [ sort {$a->{key_1} cmp $b->{key_1}} $expected ] : [] , "Test array of hashes"

(the ternary operator is there because "no data returned" is also a valid/possible response).

The problem arises with Test 2 which has the following data:

$got = [ { key_3 => "A", key_2 => "CC"} , { key_3 => "B", key_2 => "CC"} ] $expected = [ { key_3 => "B", key_2 => "CC"} , { key_3 => "A", key_2 => "CC"} ]

The problem is the sorting key name has changed from 'key_1' to 'key_3' so my sort:

sort {$a->{key_1} cmp $b->{key_1}}

fails on the second data set. I could pass in the key names as variables, but was hoping to avoid that, especially since Test::Deep->bag() does exactly what I want.

The working Test::Deep code looks like this:

return = cmp_deeply ( $got ? [ $got ] : [] , bag ( $expected ? $expected : () ) );

(different brackets on bag due to return type, I need to double check that...)

So Test::Deep->bag() works for me because it does magic I don't have to when deciding how to sort the second array.

Robert Kuropkat

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

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (2)
    As of 2020-10-22 07:02 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (225 votes). Check out past polls.