NAME
Test::Trap::Builder - Backend for building test traps
VERSION
Version 0.0.7
SYNOPSIS
package My::Test::Trap;
use Test::Trap::Builder;
my $B = Test::Trap::Builder->new;
$B->layer( $layer_name => \&layer_implementation );
$B->accessor( simple => [ $layer_name ] );
$B->multi_layer( $multi_name => @names );
$B->test_method( $test_name => 0, $name_index, \&test_function );
DESCRIPTION
Test::Trap's standard trap layers don't trap everything you might want to trap. So, Test::Trap::Builder provides methods to write your own trap layers -- preferrably for use with your own test trap module.
Note that layers are methods with mangled names (names are prefixed with layer:
), and so inherited like any other method.
EXPORTS
Test trap modules should not inherit from Test::Trap::Builder, but may import a few convenience methods for use in layer implementations. Layers should be implemented as methods, and while they need not call these convenience methods in turn, that likely makes for more readable code than any alternative.
Do not use them as methods of Test::Trap::Builder -- they are intended to be methods of test trap objects, and won't work otherwise. In fact, they should probably not be called outside of layer implementations.
Run
A terminating layer may call this method to run the user code.
Next
Every non-terminating layer should call this method (or an equivalent) to progress to the next layer. Note that this method need not return, so any teardown actions should probably be registered with the Teardown method (see below).
Teardown SUBS
If your layer wants to clean up its setup, it may use this method to register any number of teardown actions, to be performed (in reverse registration order) once the user code has been executed.
Exception STRINGS
Layer implementations may run into exceptional situations, in which they want the entire trap to fail. Unfortunately, another layer may be trapping ordinary exceptions, so you need some kind of magic in order to throw an untrappable exception. This is one convenient way.
Note: The Exception method won't work if called from outside of the regular control flow, like inside a DESTROY method or signal handler. If anything like this happens, CORE::exit will be called with an exit code of 8.
TestAccessor
Returns the name with index (if any) of the accessor for which the current test implementation is called.
METHODS
new
Returns a singleton object. Don't expect this module to work with a different object of this class.
trap MODULE, GLOBREF, LAYERARRAYREF, CODE
Implements a trap for MODULE, using the scalar slot of GLOBREF for the result object, applying the layers of LAYERARRAYREF, trapping results of the user CODE.
In most cases, the test trap module may conveniently export a function calling this method.
layer NAME, CODE
Makes a layer NAME implemented by CODE. It should expect to be invoked on the result object being built, with no arguments, and should call either the Next() or Run() method or equivalent.
output_layer NAME, GLOBREF
Makes a layer NAME for trapping output on the file handle of the GLOBREF, using NAME also as the attribute name.
output_layer_backend NAME, CODE
Registers, by NAME, a CODE implementing an output trap layer backend. The CODE will be called on the result object, with the layer name and the output handle's fileno and globref as parameters.
default_output_layer_backends NAMES
For the calling trapper package and those that inherit from it, the first found among the output layer backends named by NAMES will be used when no backend is specified.
multi_layer NAME, LAYERS
Makes a layer NAME that just pushes a number of other LAYERS on the queue of layers. If any of the LAYERS is neither an anonymous method nor the name of a layer known to the caller, an exception is raised.
layer_implementation PAKCAGE, LAYERS
Returns the subroutines that implement the requested LAYERS. If any of the LAYERS is neither an anonymous method nor the name of a layer known to the PACKAGE, an exception is raised.
test NAME, ARGSPEC, CODE
Registers a test method template for the calling trapper package. Test methods of the form ACCESSOR_NAME will be generated in the proper (i.e. inheriting) package for every registered ACCESSOR of a package that either inherits or is inherited by the calling package. To perform the test, the implicit leaveby condition will be tested, before the CODE eventually is called with arguments according to the words found in the ARGSPEC string:
- object
-
The result object.
- all
-
The ACCESSOR's return value when called without argements.
- indexed
-
An array ACCESSOR's return value when called with proper index (taken from the test method's parameters); a scalar ACCESSOR's return value when called without arguments.
- predicate
-
What the ACCESSOR's return value should be tested against (taken from the test method's parameters). (There may be more than one predicate.)
- name
-
The test name.
accessor NAMED_PARAMS
Generates and registers any number of accessors according to the NAMED_PARAMS. Will also make the proper test methods for these accessors (see above). The following parameters are recognized:
- is_leaveby
-
If true, the tests methods will generate better diagnostics if the trap was not left as specified. Also, a special did_ACCESSOR test method will be generated (unless already present), simply passing as long as the trap was left as specified.
- is_array
-
If true, the simple accessor(s) will be smart about context and parameters, returning an arrayref on no parameter, an array slice in list context, and the element indexed by the first parameters otherwise.
- simple
-
Should be a reference to an array of accessor names. For each name, an accessor, simply looking up by the name in the result object, will be generated and registered,
- flexible
-
Should be a reference to a hash. For each pair, a name and an implementation, an accessord is generated and registered.
EXAMPLE
A complete example, implementing a timeout layer (depending on Time::HiRes::ualarm being present), a simpletee layer (printing the trapped stdout/stderr to the original file handles after the trap has sprung), and a cmp_ok test method template:
package My::Test::Trap;
use base 'Test::Trap'; # for example
use Test::Trap::Builder;
my $B = Test::Trap::Builder->new;
# example (layer:timeout):
use Time::HiRes qw/ualarm/;
$B->layer( timeout => $_ ) for sub {
my $self = shift;
eval {
local $SIG{ALRM} = sub {
$self->{timeout} = 1; # simple truth
$SIG{ALRM} = sub {die};
die;
};
ualarm 1000, 1; # one second max, then die repeatedly!
$self->Next;
};
alarm 0;
if ($self->{timeout}) {
$self->{leaveby} = 'timeout';
delete $self->{$_} for qw/ die exit return /;
}
};
$B->accessor( is_leaveby => 1,
simple => ['timeout'],
);
# example (layer:simpletee):
$B->layer( simpletee => $_ ) for sub {
my $self = shift;
for (qw/ stdout stderr /) {
next unless exists $self->{$_};
die "Too late to tee $_";
}
$self->Teardown($_) for sub {
print STDOUT $self->{stdout} if exists $self->{stdout};
print STDERR $self->{stderr} if exists $self->{stderr};
};
$self->Next;
};
# no accessor for this layer
$B->multi_layer(flow => qw/ raw die exit timeout /);
$B->multi_layer(default => qw/ flow stdout stderr warn simpletee /);
$B->test_method( cmp_ok => 1, 2, \&Test::More::cmp_ok );
CAVEATS
The interface of this module is likely to remain somewhat in flux for a while yet.
The different implementations of output trap layers have their own caveats; see Test::Trap::Builder::Tempfile, Test::Trap::Builder::PerlIO, Test::Trap::Builder::SystemSafe.
Threads? No idea. It might even work correctly.
BUGS
Please report any bugs or feature requests directly to the author.
AUTHOR
Eirik Berg Hanssen, <ebhanssen@allverden.no>
COPYRIGHT & LICENSE
Copyright 2006 Eirik Berg Hanssen, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.