Subject: | Expose Test::Builder->_is_diag() as public method |
Hello,
I recently wrote some code to depend on Test::Builder->_is_diag() to do its diagnostic
output. I could have reimplemented it of course, but that would be silly, and of course if you
change the format it will lead to inconsistency.
I have no suggestion as to what this should be called if exposed, whether or not it should be
exported/propogated. I just want it to be "stable" and consistent. (e.g., if you add treatment
for another type other than 'eq', I'd like that "for free".)
My use case:
When I write data-driven tests, (e.g., verifying many fields of a "record") I don't want to have
too many tests (read: "OKs") per iteration, preferring just to have one per record, so I do the
verifications in an eval block without the test functions, having each "test" throw an
exception if it fails. Then I simply check $@, if not set I issue a pass(), if set I issue a fail(). In
the latter case, I follow up with a diag() with the detail, which comes in the exception. This is
where I'm using _is_diag(). I encapsulated the failure part in a method (note, I am using
Test::Class, so $self here is my Test::Class-derived object):
sub fail_test_diag {
my $self = shift;
my($label,$err) = @_;
# Since we're one step away from the actual failure, we need to bump this up
local $Test::Builder::Level = $Test::Builder::Level + 1;
fail( $label );
if ( ref $err and reftype $err eq 'ARRAY' ) {
my($method, @diag) = @$err;
diag( " method: $method" );
$self->is_diag( @diag );
} else {
diag( $err );
}
}
sub is_diag {
my $self = shift;
# XXX Using unpublished interface here, may break later!
$self->builder->_is_diag( @_ );
}