in reply to Re: Checking parameters passed to a third party module during testing (wrap)
in thread Checking parameters passed to a third party module during testing

    return $new->( @_ };

Since this is a test module, it may be better to use a goto, rather than just making the call to the old method. If the test module is also testing error conditions that generate messages with any sort of stack trace, then just using return could cause those tests to fail.

Replies are listed 'Best First'.
Re^3: Checking parameters passed to a third party module during testing (wrap)
by GrandFather (Saint) on Oct 18, 2006 at 20:28 UTC

    How about some sample code? While I understand tye's sample, I fail to see how I can replace return $new->( @_ }; with a goto to accomplish the same result. How would the following change?

    sub MIME::Lite::new { my ($self, %params) = @_; ok (exists $params{To}, 'To address present'); ok ($params{To} eq 'wibble@wobble', 'To address correct'); ... return $new->( @_ ); }

    DWIM is Perl's answer to Gödel
      It wouldn't change much:

      sub MIME::Lite::new { my ($self, %params) = @_; ok (exists $params{To}, 'To address present'); ok ($params{To} eq 'wibble@wobble', 'To address correct'); ... goto $new; # <-------------- }

      You'd be making use of the goto &NAME functionality (see perldoc -f goto), where your wrapper for Mime::Lite::new() gets replaced on the call stack with the real Mime::Lite::new, which was saved in $new. The goto-ed function is called with the contents of @_. According to the perldoc:

      After the "goto", not even "caller" will be able to tell that this routine was called first.

      The consequence of using goto makes your wrapper function, which does the testing, invisible to the script being tested. That reduces the possibility where addition of a wrapper function inadvertantly causes a bug (that only manifests itself while running under the test-suite, making it harder to find), or causes a test to fail that wouldn't otherwise fail.

      Does that clarify things?

      Cheers,