Code Comments
Programming Forum and web based access to our favorite programming groups.Hello, I am writing tests for a CPAN module, which contains a script. I found it convenient to make this script a Modulino, so that I could 'use' it from my test.pl (thus being sure I use the same perl, the same context etc.) Now, this script does call exit in different places, which is not suitable for my purpose. The solution I used so far strikes me as inelegant: - pass a $noexit when calling from make test - replace everywhere: exit $rc with ($noexit ? return $rc : exit $rc) There is even a signal handler... Marc
Post Follow-up to this messageMarc Girod <marc.girod@gmail.com> writes:
> I am writing tests for a CPAN module, which contains a script.
> I found it convenient to make this script a Modulino, so that
> I could 'use' it from my test.pl (thus being sure I use the same
> perl, the same context etc.)
> Now, this script does call exit in different places, which is not
> suitable for my purpose.
For testing purposes you can override the exit function to do
something else. I would probaly do something like:
sub My::Module::exit {
die "exit called with return code $_[0]\n";
}
My::Module::somefunction();
is($@, "exit called with return code 1",
"somefunction exited as expected");
For an even better test you could have your overridden exit function
note where it was called from. If you need to override exit globally,
and not just within you My::Module, you can use CORE::GLOBAL::exit
instead of My::Module::exit.
You are using Test::More or another Test::Harness module to du you
testing, right?
> The solution I used so far strikes me as inelegant:
>
> - pass a $noexit when calling from make test
> - replace everywhere:
> exit $rc
> with
> ($noexit ? return $rc : exit $rc)
This wouldn't work well if you code actually expects to exit when it
calls exit().
//Makholm
Post Follow-up to this messageOn Mar 27, 7:06 am, Peter Makholm <pe...@makholm.net> wrote: > For testing purposes you can override the exit function to do > something else. I would probaly do something like: ... Thanks. That's exactly the kind of answer I was hoping for. > You are using Test::More or another Test::Harness module to du you > testing, right? Right. Test::More for now. Marc
Post Follow-up to this messageOn Mar 28, 4:38 pm, Marc Girod <marc.gi...@gmail.com> wrote: > Thanks. That's exactly the kind of answer I was hoping for. Sorry, I had left my critical spirit home. The problem is then: how to return to the caller context (when there is one, i.e. when I do not choose to exit), instead of to the caller of my exit subroutine? Some kind of exception mechanism. Thanks, Marc
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.