davies has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to write a modulino that will accept various parameters through GetOpt::Long. I want to write unit tests for subs that will behave differently depending on the parameters. A SSCCE modulino and test file demonstrate the problem. The modulino:
package mdlno; use strict; use warnings; use feature 'say'; my $param = shift; say main() unless caller(); sub main { $param //= 'World'; return 'Hello ' . $param; }
Tests:
use strict; use warnings; use Test::More tests => 2; use FindBin qw( $RealBin ); use lib $RealBin; use mdlno; my $rtn = mdlno->main(); is $rtn, 'Hello World', 'Works without parameter'; $mdlno::param = 'and goodbye'; $rtn = mdlno->main(); is $rtn, 'Hello and goodbye', 'Works with parameter';
The first test passes but the second fails. I have tried all sorts of variations including Exporter and local, but using the debugger tells me every time that the address used in the test code differs from the address of the parameter in the modulino. All clues gratefully received.
Regards,
John Davies
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parameter injection for testing
by Corion (Patriarch) on Aug 24, 2022 at 15:02 UTC | |
by davies (Monsignor) on Aug 24, 2022 at 15:17 UTC | |
|
Re: Parameter injection for testing
by LanX (Saint) on Aug 24, 2022 at 19:32 UTC | |
by LanX (Saint) on Aug 24, 2022 at 20:18 UTC | |
|
Re: Parameter injection for testing
by Anonymous Monk on Aug 25, 2022 at 10:41 UTC | |
by Corion (Patriarch) on Aug 25, 2022 at 11:28 UTC | |
by davies (Monsignor) on Aug 25, 2022 at 13:37 UTC | |
by LanX (Saint) on Aug 25, 2022 at 21:05 UTC |