I've created a module with three exported subroutines and around 25 private subroutines. I'm now trying to write tests for both the exported and private subs. I can test for the exported subroutines just fine. I can test the private subroutines as long as I correctly use the fully qualified name. The problem is that I have to explicitly qualify the full module package and subroutine. What I want to do is put the package name(only) in a variable and then test the fully qualified package name using the variable followed by ::subroutine.
I think it's a simple concatenation problem, but I can't get it right.
In the tests below, 1, 2, 3, and 4 give the expected behavior.
However tests 5, 6, and 7 give me false positives.
I am interpreting the false positives as defined is only looking to see whether a value exists, which it does. However, &Mysimple_mod::bogus does not exist. I'm aware of using not_ok, but for this question I'm not trying to test for non-existent subroutines, I want to accurately make sure it is there. Later on in my journey I plan to write tests that actually use the subroutines. You'll see a few of my failed attempts in the .t file below.
The results:
1..7
ok 1 - use Mysimple_mod;
ok 2 - \#2 external is defined
ok 3 - \#3 internal is defined
not ok 4 - \#4 bogus subroutine
# Failed test '\#4 bogus subroutine'
# in test_Mysimple_mod.t at line 11.
ok 5 - \#5 bogus subroutine
ok 6 - \#6 bogus subroutine
&Mysimple_mod::bogus
ok 7 - \#7 bogus subroutine
# Looks like you failed 1 test of 7.
Here's my test file:
use strict;
use warnings;
use Test::More tests => 7;
my $val = '&Mysimple_mod';
BEGIN { use_ok('Mysimple_mod', qw (external) ) };
ok ( defined( &external ) , "#2 external is defined");
ok ( defined( &Mysimple_mod::internal ) , "#3 internal is defined");
ok ( defined( &Mysimple_mod::bogus ) , "#4 bogus subroutine");
ok ( defined( "${val}".'::bogus' ) , "#5 bogus subroutine");
ok ( defined( ${val}.'::bogus' ) , "#6 bogus subroutine");
my $catval = "${val}" .'::bogus';
print "\n$catval\n";
ok ( defined( $catval ) , "#7 bogus subroutine");
My module:
#!/usr/bin/perl
package Mysimple_mod;
use strict;
use warnings;
require Exporter;
our @ISA=qw(Exporter);
our @EXPORT_OK = qw( external );
sub external {
my $val = shift;
my $return_val = internal($val);
return $return_val;
}
sub internal {
my $val = shift;
my $calc = $val * 3;
return $calc;
}
1;
I'm frequently asked
"what are you trying to do, what is your overall goal?" The primary objective is to learn Perl and attempt to help out somehow, someplace so I can contribute back. The snippet of the module is just an example. The real module let's me take one of three exported subroutines and a)fetches an RPM file via FTP from a server to install on a remote machine and then installs it, or b)just fetches it for manual installation, or c) lists various things(help on the script, current RPM version installed, RPM packages already fetched and on the machine, OS name, etc. about the remote machine). I'm just getting started on the testing phase. Right now, it's easier just to run the module/script to test the functionality but I want to understand how to test.
Any and all feedback welcome. Thank-you.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.