Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Checking multiple named assertions (outside Test::More and co)

by Dallaylaen (Chaplain)
on Aug 18, 2013 at 13:30 UTC ( [id://1049906]=perlquestion: print w/replies, xml ) Need Help??

Dallaylaen has asked for the wisdom of the Perl Monks concerning the following question:

Suppose I have a set of named conditions which should hold true for each element in a set of data. The conditions may be reasonably complex, like $x->foo < $x->bar * 2; etc. And I'd like to see which of the conditions were not met, for each data element.

Now, there is an obvious although a bit verbose way:

my %failed; if (CONDITION) { $failed{"name"} ++; }; # more checks .... return scalar keys %failed ? \%failed : ();

I believe the same can be done via dispatch tables.

However, I'd like to have a more, um, DWIM-ish way:

my $what_failed = complex_check { check { CONDITION; } "name"; check { CONDITION; } "name"; # .... };

I believe this is reasonably simple to write from scratch, but is there a module that does something similar already?

Replies are listed 'Best First'.
Re: Checking multiple named assertions (outside Test::More and co)
by tobyink (Canon) on Aug 18, 2013 at 15:17 UTC

    Although it doesn't allow named assertions, I suggest taking a look at Devel::Assert for this. It's rather nice.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      I would love to put asserts of some kind into our production code, so thanks for the link.

      However, this particular question is more about offline testing/analysis of how isolated code behaves when given production data.

Re: Checking multiple named assertions (outside Test::More and co)
by vsespb (Chaplain) on Aug 18, 2013 at 19:29 UTC
    So, you need something simple like:
    $failed = { "name1" => !! do { COND1 }, "name2" => !! do { COND2 }, "name3" => !! do { COND3 }, };
    ('!!' to avoid possible memory leaks)

    If you still want DSL for this, probably better to implement it by yourself.

    I think it's perfectly OK to implement such small things manually, instead of using CPAN modules for this.

    Also, if you are looking for DSL, it's better to disclose what task your program actually do, because DSL is something related to business logic, rather than to abstract true/false tracking in a named hash.
      Neat, simple and short. I really like this solution. Thanks a lot!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-23 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found