I've recently been trying to put together a module for distribution and it struck me that in the same way that using POD keeps the documentation together with the module, it would be useful if you could do the same thing with a test suite/program. It would also be useful if the test program was useable directly for install time verification.
I thought about various schemes of having a test function or method that could be invoked, which would be okay, but still require another script--even if it is only a -e one-liner to invoke the test code.
Then inspiration hit me and I remembered 2 things I had seen recently.
Putting the two together took a little experimentation, but the result is that you can construct a .pm file that acts in every way that I have tried as a normal module when invoked via either a use my::module; statement or a require 'my::module'; or even a one-liner using the -m switch.
However, if the module is run as a program using the -x switch, it will execute the embedded program, load itself as a module and function as a 'normal' script would.
A trivial example
package My::Module; sub new{ bless \rand, shift(); } sub do_something{ print 'Do what?'; } return 1; =head1 NAME My::Module - Test my idea. =head1 Synopsys/Example See demo/test code at the end of this module. =head1 Description Demo of idea to integrate a runnable test suite into a module =head1 Copyright Copyright 2003, by BrowserUK. All rights reserved. May be used, copied, altered or sold freely under the same terms a +nd conditions as Perl itself. =head1 Demo/test program. The following code serves as both demo program and test suite. To run the integrated version switch to the directory where the mo +dule is located and type: =begin text perl -x Module.pm =end text The program should respond with: =begin text E:\Perl\site\lib\My>perl -x Module.pm Do what? E:\Perl\site\lib\My> =end =cut #! perl use strict; # use My::Module; # Use this instead of the following line in your cod +e. unshift @INC, '.'; require $0; my $test = My::Module->new(); $test->do_something();
Is this a useful idea? Stupid?
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RFC: Runnable test code integrated into Modules.
by adrianh (Chancellor) on Jan 30, 2003 at 11:27 UTC | |
by dda (Friar) on Sep 13, 2003 at 11:48 UTC | |
by adrianh (Chancellor) on Sep 13, 2003 at 12:41 UTC | |
by dda (Friar) on Sep 13, 2003 at 13:50 UTC | |
by adrianh (Chancellor) on Sep 13, 2003 at 14:08 UTC | |
|
Re: RFC: Runnable test code integrated into Modules.
by gjb (Vicar) on Jan 30, 2003 at 13:59 UTC | |
|
Re: RFC: Runnable test code integrated into Modules.
by jmcnamara (Monsignor) on Jan 30, 2003 at 11:45 UTC | |
|
Re: RFC: Runnable test code integrated into Modules.
by PodMaster (Abbot) on Jan 30, 2003 at 11:41 UTC | |
|
Re: RFC: Runnable test code integrated into Modules.
by Anonymous Monk on Jan 31, 2003 at 01:08 UTC |