Subject: | [wish] permit access to number of tests from outside package |
Short version: I'd like a method like num_method_tests that doesn't rely
on caller() but instead walks the @ISA if calling as an instance method.
Long version: I've been working on some helper attributes to annotate my
tests and have come across a problem. I want my helper to be able to
skip the right number of tests conditionally (wrapping the test method
along the lines of http://use.perl.org/~ChrisDolan/journal/34906). This
works great as long as the wrapping code is in the same package as the
test method. However, as soon as I subclass my own test class, the
helper is no longer able to get the number of tests because
_find_calling_test_class uses caller() to decide which class the
requested test belongs to.
Contrived, but simple, problem scenario:
---- test.pl ----
use Foo;
my $test = Bar->new;
Test::More::plan tests => 2;
$test->do_skip("test1", 1);
$test->do_skip("test2", 1);
---- Foo.pm ----
package Foo;
use base 'Test::Class';
use Test::More;
sub do_skip {
my ($self, $test_name, $bool) = @_;
SKIP: {
skip "skip requested", $self->num_method_tests($test_name)
if $bool;
$self->$test_name();
}
}
sub test1 : Test(1) {
fail();
}
package Bar;
use base 'Foo';
use Test::More;
sub test2 : Test(1) {
fail();
}
1;
----------------
Output:
% perl test.pl
1..2
ok 1 # skip skip requested
test2 is not a test method of class Foo at test.pl line 5
# Looks like you planned 2 tests but only ran 1.