Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Alright, I've spent way too long trying to figure out a way to make this work like I want. Perhaps the perlmonks can help me. :)

I basically want what array_each does only I want it to support a hash OR an array. If it gets a hash it should just do a standard hash to hash comparison, if it gets an array it should do what array_each normally does.

I at first tried to make my own sub called array_each_orHash that took a comparator value. Here's the code I wrote...

sub array_each_orHash { my $comparator = shift; my $handleArrayOrHash = sub { my $data = shift; return array_each($comparator) if (ref($data) eq "ARRAY"); return $comparator if (ref($data) eq "HASH"); }; return code( \&$handleArrayOrHash ); }

This was mysteriously always resulting in a passed test, until I realized the mystery. I wasn't supposed to return an object when using the "code( ... )" sub I was supposed to return 0 or 1, or an array with 0 and the reason it failed. Okay, so then I modified it a bit...

sub array_each_orHash { my $comparator = shift; my $handleArrayOrHash = sub { my $data = shift; my $result = (0, 'must pass an ARRAY or HASH'); $result = eq_deeply($data, array_each($comparator)) if (ref($data) +eq "ARRAY"); $result = eq_deeply($data, $comparator) if (ref($data) eq "HASH"); return $result; }; return code( \&$handleArrayOrHash ); }

This actually works except I get no useful debugging diagnostic info except that the test failed. And the diagnostic I do get tells me a CODE block was run. I don't really want that to be displayed. The "code" was really just a means to an end so I could try to overload array_each.

I thought about making my own cmp_deeply sub that actually just checks for array_each with a non-array and if found calls the real cmp_deeply without the array_each. If not found then it could call the real cmp_deeply with the array value and array_each. But the problem with this approach is in handling cases where the array_each is embedded deeply within a value's structure. Do I write my own recursive deep diving routine to search for array_each (Test::Deep::ArrayEach) class instances and look for what they are comparing against somehow? No way!

Ok, so what is the correct way to do this? It seems like simply making my own version of Test::Deep::ArrayEach is really the best solution here. Adding something like Test::Deep::ArrayEachOrHash. But I don't know how to make my own ArrayEach like this and haven't found useful information on how to do something like that. I did find the following code here: http://searchcode.com/codesearch/view/16731024 (Shown below)

But, how do I make my own "array_each_orHash" keyword and connected it to some code in package Test::Deep::ArrayEachOrHash? Am I grossly over-complicating things here or going the wrong direction with this? Any help would be appreciated. Thanks...

use strict; use warnings; package Test::Deep::ArrayEach; use Test::Deep::Cmp; sub init { my $self = shift; my $val = shift; $self->{val} = $val; } sub descend { my $self = shift; my $got = shift; my $exp = [ ($self->{val}) x @$got ]; return Test::Deep::descend($got, $exp); } 1;

In reply to Overloadding array_each when Unit testing with Test::Deep by willjones

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (6)
    As of 2014-12-21 02:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (101 votes), past polls