Unit test that checks for "croak"

Discussion in 'Perl Misc' started by freesoft12@gmail.com, Dec 29, 2008.

  1. Guest

    Hi,

    I have a module that calls 'croak' if the input argument to my
    function is wrong. I want to write a unit test that tests if 'croak'
    is called when I give a bad input.

    ---- PathUtils.t ----
    use Test::More qw( no_plan );
    use PathUtils;

    my $path = '/a/b/../c';
    deleteDoubleDotsInPath(\$path);
    ok($path eq '/a/c'); # success test

    $path = '../c'; # bad input. a fail/negative test

    # how can write a 'ok()' or 'is()' that checks that 'croak' function
    is called
    # ok(deleteDoubleDotsInPath(\$path));

    ------------ end ----------

    ----------- PathUtils.pm ----------

    package PathUtils;
    use Carp;
    require Exporter;

    our @ISA = qw(Exporter);
    our @EXPORT = qw(&deleteDoubleDotsInPath);

    # This function removes the double dots in a path '..' eg: a/b/../c/
    d/../e
    sub deleteDoubleDotsInPath {
    my $text = shift;
    die "Malformed path" if !check_path_($text);

    while($$text =~ m/\.\./) {
    $$text =~ s/\/[^\/]*\/\.\.//;
    }
    }

    sub check_path_ {
    my $text = shift;

    my @dirs = split(/\//,$$text);
    my @double_dots = grep(/\.\./,@dirs);
    if (scalar(@dirs) - scalar(@double_dots) > scalar(@double_dots)) {
    return 1;
    }
    return 0;
    }
    ------------ end ------------

    Regards
    John
    , Dec 29, 2008
    #1
    1. Advertising

  2. Uri Guttman Guest

    >>>>> "fc" == freesoft12@gmail com <> writes:

    fc> I have a module that calls 'croak' if the input argument to my
    fc> function is wrong. I want to write a unit test that tests if 'croak'
    fc> is called when I give a bad input.

    croak is just a wrapper around die so catch the die with a block eval.

    fc> $path = '../c'; # bad input. a fail/negative test

    fc> # how can write a 'ok()' or 'is()' that checks that 'croak' function
    fc> is called
    fc> # ok(deleteDoubleDotsInPath(\$path));

    first call the code in a block eval and then check if $@ is set (which
    means the code called die somewhere). <untested>

    eval { deleteDoubleDotsInPath(\$path) } ;

    ok( !$@, 'delete double dots' ) ;

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Dec 30, 2008
    #2
    1. Advertising

  3. Dr.Ruud Guest

    Uri Guttman wrote:

    > first call the code in a block eval and then check if $@ is set (which
    > means the code called die somewhere). <untested>
    >
    > eval { deleteDoubleDotsInPath(\$path) } ;
    >
    > ok( !$@, 'delete double dots' ) ;


    I promote to test the return value of eval.

    eval {
    deleteDoubleDotsInPath( \$path) };
    1;
    }
    or do {
    $@ ||= "unknown error";
    ...
    };

    --
    Ruud
    Dr.Ruud, Dec 30, 2008
    #3
  4. Guest

    On Tue, 30 Dec 2008 01:44:48 +0100, "Dr.Ruud" <> wrote:

    >Uri Guttman wrote:
    >
    >> first call the code in a block eval and then check if $@ is set (which
    >> means the code called die somewhere). <untested>
    >>
    >> eval { deleteDoubleDotsInPath(\$path) } ;
    >>
    >> ok( !$@, 'delete double dots' ) ;

    >
    >I promote to test the return value of eval.

    ^^^^^^^
    what do you mean?
    >
    > eval {
    > deleteDoubleDotsInPath( \$path) };
    > 1;
    > }
    > or do {
    > $@ ||= "unknown error";

    ^^^
    what does this do?
    > ...
    > };


    sln
    , Dec 30, 2008
    #4
  5. Tim Greer Guest

    wrote:

    > On Tue, 30 Dec 2008 01:44:48 +0100, "Dr.Ruud"
    > <> wrote:
    >
    >>Uri Guttman wrote:
    >>
    >>> first call the code in a block eval and then check if $@ is set
    >>> (which means the code called die somewhere). <untested>
    >>>
    >>> eval { deleteDoubleDotsInPath(\$path) } ;
    >>>
    >>> ok( !$@, 'delete double dots' ) ;

    >>
    >>I promote to test the return value of eval.

    > ^^^^^^^
    > what do you mean?


    Probably he promotes that method of doing it, unless it meant propose.

    >>
    >> eval {
    >> deleteDoubleDotsInPath( \$path) };
    >> 1;
    >> }
    >> or do {
    >> $@ ||= "unknown error";

    > ^^^
    > what does this do?


    Principal Skinner -- A conjunction meaning "in the event that" or "on
    condition of".

    (I know that doesn't apply. Sorry, I couldn't help it.)

    The ||= is just an assignment operator (give it a (probably default)
    value, if it doesn't already exist).

    It will assign $@ the value 'unknown error' if it doesn't have a value
    already.

    (defined are to allow or ignore the zero value, otherwise use without
    defined if that suits your needs)

    I.e., $@ is $@ (defined) or else 'unknown error'.

    I.e., $@ = $@ || 'unknown error'.

    I.e., $@ = 'unknown error' if ! defined $@.

    I.e., $@ = 'unknown error' unless defined $@.

    I.e., if (a conjunction meaning "in the event that") $@ is undefined,
    that 'unknown error' will be the assigned value.


    .... or, were you asking something else?
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
    Tim Greer, Dec 30, 2008
    #5
  6. <> wrote:
    > On Tue, 30 Dec 2008 01:44:48 +0100, "Dr.Ruud" <> wrote:


    >> $@ ||= "unknown error";

    > ^^^
    > what does this do?



    The same as this does:

    $@ = $@ || "unknown error";


    Just as

    $num += 1;

    does the same as this:

    $num = $num + 1;


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Dec 30, 2008
    #6
  7. Dr.Ruud Guest

    Tim Greer wrote:
    > wrote:
    >> Dr.Ruud:


    >>> I promote to test the return value of eval.

    >> ^^^^^^^
    >> what do you mean?

    >
    > Probably he promotes that method of doing it, unless it meant propose.


    I mean "promote". There are known issues in Perl with $@, like $@
    loosing a value, like with DESTROY.
    I already proposed @@, so $@[-1] would hold the last one.

    And I just hate the decoupled way of "if ($@){}", it is far too distant
    for my taste. The Perl compiler needs to do crazy things to try to make
    $@ to be about the previous statement (or block) as much as possible.

    --
    Ruud
    Dr.Ruud, Dec 30, 2008
    #7
  8. Tim Greer Guest

    Dr.Ruud wrote:

    > Tim Greer wrote:
    >> wrote:
    >>> Dr.Ruud:

    >
    >>>> I promote to test the return value of eval.
    >>> ^^^^^^^
    >>> what do you mean?

    >>
    >> Probably he promotes that method of doing it, unless it meant
    >> propose.

    >
    > I mean "promote". There are known issues in Perl with $@, like $@
    > loosing a value, like with DESTROY.
    > I already proposed @@, so $@[-1] would hold the last one.
    >
    > And I just hate the decoupled way of "if ($@){}", it is far too
    > distant for my taste. The Perl compiler needs to do crazy things to
    > try to make $@ to be about the previous statement (or block) as much
    > as possible.
    >


    I assumed so (but thanks for clarifying), and I completely agree with
    you on this matter as well.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
    Tim Greer, Dec 30, 2008
    #8
  9. Guest

    On Tue, 30 Dec 2008 11:01:59 +0100, "Dr.Ruud" <> wrote:

    >Tim Greer wrote:
    >> wrote:
    >>> Dr.Ruud:

    >
    >>>> I promote to test the return value of eval.
    >>> ^^^^^^^
    >>> what do you mean?

    >>
    >> Probably he promotes that method of doing it, unless it meant propose.

    >
    >I mean "promote". There are known issues in Perl with $@, like $@
    >loosing a value, like with DESTROY.
    >I already proposed @@, so $@[-1] would hold the last one.
    >
    >And I just hate the decoupled way of "if ($@){}", it is far too distant
    >for my taste. The Perl compiler needs to do crazy things to try to make
    >$@ to be about the previous statement (or block) as much as possible.


    Wait, I've known about this for along time. How to catch $@ in its pristine
    state. Perhaps and ID, hooked to a state of eval, to be matched later.

    sln
    , Dec 31, 2008
    #9
  10. Guest

    > eval { deleteDoubleDotsInPath(\$path) } ;
    >
    > ok( !$@, 'delete double dots' ) ;
    >
    > uri


    Hi,
    Here is what I have done to solve the problem:

    1) I added a special message in the 'croak' call:

    sub deleteDoubleDotsInPath {
    my $text = shift;
    croak "Bad argument" if !check_path_($path_ref); # check validity of
    argument
    ...
    }

    2) In the Test, I search for the message in the negative test:
    # failure tests
    $path = '../c'; # passing in a bad path
    eval { deleteDoubleDotsInPath(\$path);};
    ok($@ =~ m/Bad argument/,"Test Four"); # check if the message is what
    we expect

    Regards
    John
    , Jan 3, 2009
    #10
  11. Peter Scott Guest

    On Mon, 29 Dec 2008 11:14:55 -0800, wrote:
    > I have a module that calls 'croak' if the input argument to my function
    > is wrong. I want to write a unit test that tests if 'croak' is called
    > when I give a bad input.


    use Test::Exception;

    dies_ok { } ...

    throws_ok { } ...

    --
    Peter Scott
    http://www.perlmedic.com/
    http://www.perldebugged.com/
    Peter Scott, Jan 4, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VvanN
    Replies:
    5
    Views:
    486
    Phlip
    Apr 28, 2006
  2. Bill David
    Replies:
    2
    Views:
    270
    Arne Vajhøj
    Jun 18, 2008
  3. Daniel Berger

    Performing $SAFE checks with test-unit

    Daniel Berger, Jun 21, 2007, in forum: Ruby
    Replies:
    2
    Views:
    105
    Daniel Berger
    Jun 21, 2007
  4. Bill Mosteller
    Replies:
    0
    Views:
    223
    Bill Mosteller
    Oct 22, 2009
  5. timr
    Replies:
    2
    Views:
    164
Loading...

Share This Page