Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Unit testing, context and wantarray

by mikeman (Acolyte)
on Aug 13, 2011 at 10:55 UTC ( #920162=perlquestion: print w/replies, xml ) Need Help??

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

When unit testing, how do I test that a function returns nothing when called in void context?

For example, I can easily test if the following function returns a list or a scalar containing 'value'. But as soon as I test its return value, I am no longer calling the function in void context.

sub context { return unless defined wantarray; return wantarray ? ('value') : 'value'; }

Thanks, and sorry if this is a dumb question!

Replies are listed 'Best First'.
Re: Unit testing, context and wantarray
by BrowserUk (Pope) on Aug 13, 2011 at 11:05 UTC
    how do I test that a function returns nothing when called in void context?

    Two thoughts:

    1. Why would it matter?

      If a function returned a value in a void context, it is just discarded.

      Not returning anything in a void context is not the point, it is not calculating the return values if they are never going to be used that makes a difference.

    2. Testing something that is obvious by inspection is a waste of time.

      If the first line of the sub is return unless defined wantarray;, the sub will not return anything when called in a void context. There is no need to test that.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thanks for your reply.

      I think you answer has pointed me in the right direction: I am trying to test for the wrong thing. I should be testing (perhaps via a mock object or similar), that my function does not do anything expensive (e.g., call a database function) to return a result when called in void context. I was erroneously focusing on the return result, rather than the operations of my function. Testing the return value (or lack thereof) would not prove if my function was performing a pointless operation.

      Thanks!

        As a general thing though, one may often call a method in a void context and expect something expensive to happen (connecting to a remote database for example), especially if you use exception handling to manage errors rather than passing error states back.

        True laziness is hard work

        I should be testing (perhaps via a mock object or similar), that my function does not do anything expensive (e.g., call a database function) to return a result when called in void context.
        Such as playing nethack? :)

Re: Unit testing, context and wantarray
by ikegami (Pope) on Aug 13, 2011 at 16:48 UTC

    Parens don't create lists*.

    return wantarray ? ('value') : 'value';
    is the same as
    return wantarray ? 'value' : 'value';
    which is the same as
    return 'value';

    * — Except on the LHS of an assignment (indirectly) and inside of a list literal.

Re: Unit testing, context and wantarray
by happy.barney (Friar) on Aug 13, 2011 at 15:42 UTC
    If your function call other function that should not be called in void context, try to locally redefine it.
    sub Foo::bar { return 42; } say '1: ', Foo::bar; { no warnings 'redefine'; local *Foo::bar = sub { die }; eval { say '2: ', Foo::bar; }; } say '3: ', Foo::bar; __END__ 1: 42 3: 42

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2021-08-06 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (44 votes). Check out past polls.

    Notices?