Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: 'either or' value unit test case in Test::Simple or Test::More

by grinder (Bishop)
on Jan 05, 2007 at 16:23 UTC ( [id://593149]=note: print w/replies, xml ) Need Help??


in reply to 'either or' value unit test case in Test::Simple or Test::More

I would write that as separate pass calls, which allows you to see which one matched.

if ($var eq 'a') { pass( 'var is an a' ); } elsif ($var eq 'b') { pass( 'var is a b' ); } else { fail( "var was $var" ); }

Be careful here and don't go too tricky in the code flow, otherwise you may end up with a pass() in one branch, and nothing in the other, which will lead to strange off-by-one errors in the test plan. KISS.

• another intruder with the mooring in the heart of the Perl

  • Comment on Re: 'either or' value unit test case in Test::Simple or Test::More
  • Download Code

Replies are listed 'Best First'.
Re^2: 'either or' value unit test case in Test::Simple or Test::More
by ferreira (Chaplain) on Jan 05, 2007 at 16:43 UTC

    What about an overkill abstract solution?

    # is_one_of($val, \@list); sub is_one_of { my $val = shift; my $list = shift; my $test_name = "$val is not one of @$list"; for (@$list) { pass($test_name), return if $val eq $_; } diag "never saw [$val] in [@$list]"; fail($test_name); }

    And then palette may say:

    is_one_of($var, [ qw(a b) ]);

    Update: fixed after grinder's commentreturn and diag added.

      If $list winds up with duplicate values that match $val, your plan is in big trouble. I would change that to

      for (@$list) { if ($val eq $_) { pass($test_name); return; } } diag "never saw [$val] in [@$list]"; fail($test_name);

      • another intruder with the mooring in the heart of the Perl

Log In?
Username:
Password:

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

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

    No recent polls found