Test::Trap::Builder(3) Backend for building test traps


Version 0.3.2


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( $test_name => 'trap, predicate, name', \&test_function );


Test::Trap neither traps nor tests everything you may want to trap or test. So, Test::Trap::Builder provides methods to write your own trap layers, accessors, and test callbacks --- preferably for use with your own modules (trappers).

Note that layers are methods with mangled names (names are prefixed with "layer:"), and so inherited like any other method, while accessors are ordinary methods. Meanwhile, test callbacks are not referenced in the symbol table by themselves, but only in combinations with accessors, all methods of the form ACCESSOR_TEST.


Trappers should not inherit from Test::Trap::Builder, but may import a few convenience methods for use in building the trap. Do not use them as methods of Test::Trap::Builder --- they are intended to be methods of trap objects. (If you inherit from another trapper, you need not, and probably should not, import these yourself --- you should inherit these methods like any other.)

Trappers may import any number of these methods, or all of them by way of the ":methods" tag.

Layers should be implemented as methods, and while they need not call any of these convenience methods in turn, that likely makes for more readable code than any alternative. Likewise, test callbacks may use convenience methods for more readable code.

Of course, certain convenience methods may also be useful in more generic methods messing with trap or builder objects.


A method returning a reference to a hash, holding the PACKAGE's (by default the caller's) tag-on properties for the (current) trap object. Currently, Test::Trap::Builder defines the following properties:
While the trap is springing, the queue of layers remaining. Usually set by the ``trap'' method and consumed by the ``Next'' method.
While the trap is springing, the queue of teardown actions remaining. Usually accumulated through the ``Teardown'' method and invoked by the ``trap'' method.
The user code trapped. Usually set by the ``trap'' method and invoked by the ``Run'' method.
An internal exception. Usually set through the ``Exception'' method and examined by the ``trap'' method.
A callback invoked by the ``TestFailure'' method. Layers in particular may want to set this.
The name and (optionally) the index of the accessor, the contents of which we're currently testing. Best accessed through the ``TestAccessor'' method, and usually set by the ``test'' and ``accessor'' methods, but if you are writing your own tests or accessors directly, you just might need to set it. Perhaps.

Be nice: Treat another module's tag-on properties as you would treat another module's global variables. Don't use them except as documented.


  # in a layer, setting the callback for TestFailure:
  $self->Prop('Test::Trap::Builder')->{on_test_failure} = \&mydiag;


This cleans up the tag-on properties when the trap object is destroyed. Don't try to make a trapper that doesn't call this; it will get confused.

If your trapper needs its own "DESTROY", make sure it calls this one as well:

  sub DESTROY {
    my $self = shift;
    # do your thing
    # and more things


A terminating layer should call this method to run the user code. Should only be called in a dynamic context in which layers are being applied.


Every non-terminating layer should call this method (or an equivalent) to progress to the next layer. Should only be called in a dynamic context in which layers are being applied. Note that this method need not return, so any tear-down 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 tear-down actions, to be performed (in reverse registration order) once the user code has been executed. Should only be called in a dynamic context in which layers are being applied.


Returns a string of the form "NAME(INDEX)", where NAME and INDEX are the name of the accessor and the index (if any) being tested. Should only be called in the dynamic context of test callbacks.

This is intended for diagnostics:

  diag( sprintf 'Expected %s in %s; got %s',
        $expected, $self->TestAccessor, dump($got),


Runs the "on_test_failure" tag-on property (if any) on the trap object. If you are writing unregistered tests, you might want to include (some variation of) this call:

  $ok or $self->TestFailure;

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.

Should only be called in a dynamic context in which layers are being applied.

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.

Note: Direct calls to the Exception method within closures may cause circular references and so leakage. To avoid this, fetch an ``ExceptionFunction'' and call it from the closure instead.


This method returns a function that may be called with the same effect as calling the ``Exception'' method, allowing closures to throw exceptions without causing circular references by closing over the trap object itself.

To illustrate:

  # this will create a circular reference chain:
  # trap object has property collection has teardown closure has trap object
  $self->Teardown($_) for sub {
    do_stuff() or $self->Exception("Stuff didn't work.");
  # this will break the circular reference chain:
  # teardown closure no longer has trap object
  $Exception = $self->ExceptionFunction;
  $self->Teardown($_) for sub {
    do_things() or $Exception->("Things didn't work.");



Returns a singleton object. Don't expect this module to work with a different instance object of this class.


Implements a trap for the TRAPPER module, applying the layers of LAYERARRAYREF, trapping various outcomes of the user CODE, and storing the trap object into the scalar slot of GLOBREF.

In most cases, the trapper should conveniently export a function calling this method.

layer NAME, CODE

Registers a layer by NAME to the calling trapper. When the layer is applied, the CODE will be invoked on the trap object being built, with no arguments, and should call either the Next() or Run() method or equivalent.

output_layer NAME, GLOBREF

Registers (by NAME and to the calling trapper) a layer for trapping output on the file handle of the GLOBREF, using NAME also as the attribute name.

capture_strategy NAME, [CODE]

When called with two arguments, registers (by NAME and globally) a strategy for output trap layers. When called with a single argument, looks up and returns the strategy registered by NAME (or undef).

When a layer using this strategy is applied, the CODE will be called on the trap object, with the layer name and the output handle's fileno and globref as arguments.

output_layer_backend SPEC

Back-compat alias of the above.

first_capture_strategy SPEC

Where SPEC is empty, just returns.

Where SPEC is a string of comma-or-semicolon separated names, runs through the names, returning the first strategy it finds. Dies if no strategy is found by any of these names.

first_output_layer_backend SPEC

Back-compat alias of the above.

multi_layer NAME, LAYERS

Registers (by NAME) a layer that just pushes a number of other LAYERS on the stack of layers. If any of the LAYERS is neither an anonymous method nor the name of a layer registered to the caller or a trapper it inherits from, an exception is raised.

layer_implementation TRAPPER, 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 registered to or inherited by the TRAPPER, an exception is raised.

accessor NAMED_ARGS

Generates and registers any number of accessors according to the NAMED_ARGS, and also generates the proper test methods for these accessors (see below).

The following named arguments are recognized:

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.
If true, the simple accessor(s) will be smart about context and arguments, returning an arrayref on no argument (in any context), an array slice in list context (on any number of arguments), and the element indexed by the first argument otherwise.
Should be a reference to an array of accessor names. For each name, an accessor (assuming hash based trap object with accessor names as keys), will be generated and registered.
Should be a reference to a hash. For each pair, a name and an implementation, an accessor is generated and registered.


Registers a test callback by NAME and to the calling trapper.

Trappers inherit test callbacks like methods (though they are not implemented as such; don't expect to find them in the symbol table).

Test methods of the form ACCESSOR_TEST will be made available (directly or by inheritance) to every trapper that registers or inherits both the accessor named ACCESSOR and the test named TEST.

(In more detail, the method will be generated in every trapper that either (1) registers both the test and the accessor, or (2) registers either and inherits the other.)

When the test method is called, any implicit leaveby condition will be tested first, and if it passes (or there were none), the CODE is called with arguments according to the words found in the ARGSPEC string:

The trap object.
The ACCESSOR's return value when called without arguments.
The ACCESSOR's return value when called with index, if applicable (i.e. for array accessors). Index is not applicable to scalar accessors, so such are still called without index.

The index, when applicable, will be taken from the test method's arguments.

What the ACCESSOR's return value should be tested against (taken from the test method's arguments). (There may be any fixed number of predicates.)
The test name (taken from the test method's arguments).


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};
      ualarm 1000, 1; # one second max, then die repeatedly!
    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 /) {
      exists $self->{$_} or $self->Exception("Too late to tee $_");
    $self->Teardown($_) for sub {
      print STDOUT $self->{stdout} if exists $self->{stdout};
      print STDERR $self->{stderr} if exists $self->{stderr};
  # 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 );


The interface of this module is likely to remain somewhat in flux for a while yet.

The different strategies for output trap layers have their own caveats; see Test::Trap::Builder::Tempfile, Test::Trap::Builder::PerlIO, Test::Trap::Builder::SystemSafe.

Multiple inheritance is not (yet?) fully supported. If one parent has registered a test callback "X" and another has registered an accessor "Y", the test method "Y_X" will not be generated.

Threads? No idea. It might even work correctly.


Please report any bugs or feature requests directly to the author.


Eirik Berg Hanssen, "<[email protected]>"


Copyright 2006-2014 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.