in reply to Test::More subtest parameters
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:
-- Ken
|
|---|