G'day space_monk,
I tried a few tests with callback formats I'd come across elsewhere (e.g. [\&function, @args] from Perl/Tk) but subtest emits a warning that the 2nd argument must be a coderef. Given this, your best bet might be something like:
subtest 'test name' => sub { my_subtest($xml, @other_args) };
I played around with this for a bit and it seems to work well. I was able to conditionally run subtest tests based on the arguments passed in: I don't know if that's exactly what you want to do with it but it certainly "can be called a number of times with different parameters".
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 9;
my $xml = '<tag>content</tag>';
pass('PRE subtest');
subtest 'xml_subtest(undef)'
=> sub { xml_subtest(undef) };
subtest 'xml_subtest("blah")'
=> sub { xml_subtest("blah") };
subtest 'xml_subtest("blah", undef)'
=> sub { xml_subtest("blah", undef) };
subtest 'xml_subtest("blah", {})'
=> sub { xml_subtest("blah", {}) };
subtest 'xml_subtest($xml, {test_format => 1})'
=> sub { xml_subtest($xml, {test_format => 1}) };
subtest 'xml_subtest($xml, {test_tag => 1})'
=> sub { xml_subtest($xml, {test_tag => 1}) };
subtest 'xml_subtest($xml, {test_content => 1})'
=> sub { xml_subtest($xml, {test_content => 1}) };
pass('POST subtest');
sub xml_subtest {
my ($sub_xml, $args) = @_;
plan tests => 5;
ok(defined $sub_xml, '$sub_xml defined');
ok(length $sub_xml, '$sub_xml populated');
ok(defined $args, '$args defined');
ok(ref $args eq 'HASH', '$args is hashref');
if ($args->{test_format}) {
like($sub_xml, qr{^<([a-z]+)>[^<]+</\1>$},
'$sub_xml well-formed');
}
elsif ($args->{test_tag}) {
is(($sub_xml =~ m{^<([a-z]+)>})[0], 'tag',
'$sub_xml tag element');
}
elsif ($args->{test_content}) {
cmp_ok(($sub_xml =~ m{>([^<]+)<})[0], 'eq', 'content',
'$sub_xml tag content');
}
else {
fail('No tests indicated');
}
}
Test results:
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.