Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Testing of exception during import

by Dirk80 (Pilgrim)
on Jul 01, 2022 at 14:09 UTC ( #11145217=perlquestion: print w/replies, xml ) Need Help??

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

Let's assume I have a module which needs 3 import parameters.

package My::Test; use strict; use warnings; use Carp; sub import { my $class = shift; croak "Number of import parameters is wrong, stopped " unless @_ = += 3; # ... } 1;

The good case I can check with use_ok. But the bad cases are my problem, e.g. use this class with less than 3 parameters.

use My::Test "Param1", "Param2"; # dies because it are 2 params and no +t 3 as expected

I want to test that this exception was thrown and I also want to check its error message for correctness. Usually I use throws_ok for this. But in this case it doesn't work because the use command is at compile time. I have no idea how to test for this exception.

Thanks for your help!

Replies are listed 'Best First'.
Re: Testing of exception during import
by LanX (Sage) on Jul 01, 2022 at 14:53 UTC
    use is just a require + import in a BEGIN block. Without BEGIN no compile-time.

    from the docs:

      It is exactly equivalent to

      BEGIN { require Module; Module->import( LIST ); }

      except that Module must be a bareword.

    alternatively you could try to string eval it, IMHO

    eval 'use MODULE args'; # run-time

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Testing of exception during import
by Anonymous Monk on Jul 01, 2022 at 14:51 UTC

    FWIW, I would either use My::Test (); or require My::Test;. Both load the module without calling its import() method at all. Then call the import method explicitly:

    
    use Test::Exception;
    use My::Test ();
    
    lives_ok { My::Test->import( 1, 2, 5 ) }, 'Three parameters';
    throws_ok { My::Test->import( 1, 2 ) } m/Number of import parameters is wrong/, 'Two parameters';
    ...
    
Re: Testing of exception during import
by haukex (Archbishop) on Jul 01, 2022 at 18:46 UTC

    IMHO testing is one of those places where you can break some of the "rules" like "don't use stringy eval" or "don't check the boolean status of $@ to see if the eval failed or not". The following works on your example code:

    use warnings; use strict; use Test::More tests=>12; ok ! defined eval "use My::Test; 1"; like $@, qr/\bNumber of import parameters is wrong\b/i; ok ! defined eval "use My::Test qw/foo/; 1"; like $@, qr/\bNumber of import parameters is wrong\b/i; ok ! defined eval "use My::Test qw/foo bar/; 1"; like $@, qr/\bNumber of import parameters is wrong\b/i; ok ! defined eval "use My::Test qw/foo bar quz baz/; 1"; like $@, qr/\bNumber of import parameters is wrong\b/i; ok ! defined eval "use My::Test qw/foo bar quz baz blah/; 1"; like $@, qr/\bNumber of import parameters is wrong\b/i; ok defined eval "use My::Test qw/foo bar quz/; 1"; ok !$@;
Re: Testing of exception during import
by kcott (Archbishop) on Jul 02, 2022 at 04:44 UTC

    G'day Dirk80,

    You may already be aware of this, but in case not, I thought I'd point out that your import routine will not be called if your module is loaded like this:

    use My::Test ();

    See the use function, in particular:

    "... there is a distinction between omitting LIST (import called with no arguments) and an explicit empty LIST () (import not called)."

    It's possible that you have an "XY Problem" here. If you provided some information about the specific problem you're trying to solve, we may be able to offer other (better) solutions.

    — Ken

      Thank you all for your answers. The problem was that simple. I fully understood your answers and everything works fine. I'm really glad about this great community and that you help me even if I have sometimes beginner questions.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2022-08-13 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?