Test::...Builder(3pm)
π Test::Trap::Builder π
Okay bestie, this is the Test::...Builder(3pm) page from User Contributed Perl Documentation β and it's literally the backend for building test traps. No cap, it's a whole builder situation.
β¨ NAME β¨
Test::Trap::Builder β backend for building test traps, periodt.
πΈ VERSION πΈ
Version 0.3.5 π
π¦ 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( $test_name => 'trap, predicate, name', \&test_function );
π DESCRIPTION π
Test::Trap neither traps nor tests everything you may want to trap or test. So obviously Test::Trap::Builder swoops in with methods so you can write your own trap layers, accessors, and test callbacks β preferably for use with your own modules, aka 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. iykyk π
π EXPORTS π
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.
π§Έ Prop [PACKAGE] π§Έ
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:
layersβ While the trap is springing, the queue of layers remaining. Usually set by thetrapmethod and consumed by theNextmethod.teardownβ While the trap is springing, the queue of teardown actions remaining. Usually accumulated through theTeardownmethod and invoked by thetrapmethod.codeβ The user code trapped. Usually set by thetrapmethod and invoked by theRunmethod.exceptionβ An internal exception. Usually set through theExceptionmethod and examined by thetrapmethod.on_test_failureβ A callback invoked by theTestFailuremethod. Layers in particular may want to set this.test_accessorβ The name and (optionally) the index of the accessor, the contents of which we're currently testing. Best accessed through theTestAccessormethod, and usually set by thetestandaccessormethods, 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.
Example:
# in a layer, setting the callback for TestFailure:
$self->Prop('Test::Trap::Builder')->{on_test_failure} = \&mydiag;
π§Ό DESTROY π§Ό
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
$self->Test::Trap::Builder::DESTROY;
# and more things
}
π Run π
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. π¨
β‘οΈ Next β‘οΈ
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. π«§
π TestAccessor π
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),
);
π TestFailure π
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: TheExceptionmethod won't work if called from outside of the regular control flow, like inside aDESTROYmethod or signal handler. If anything like this happens,CORE::exitwill be called with an exit code of8.
Note: Direct calls to theExceptionmethod within closures may cause circular references and so leakage. To avoid this, fetch anExceptionFunctionand call it from the closure instead.
πͺ ExceptionFunction πͺ
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.");
};
π METHODS π
π¦ new π¦
Okay so new returns a singleton object. Don't expect this module to work with a different instance object of this class, bestie β it's giving one-true-instance energy. β¨
π₯ trap TRAPPER, GLOBREF, LAYERARRAYREF, CODE π₯
This 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. iykyk π
πΈ 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. Periodt. π
π« 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. Cute and functional, we love to see it. π«Ά
π§ capture_strategy NAME, [CODE] π§
When called with two arguments, this registers (by NAME and globally) a strategy for output trap layers. When called with a single argument, it looks up and returns the strategy registered by NAME (or undef). No cap. π
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. Slay. β¨
π output_layer_backend SPEC π
Back-compat alias of the above. π old-school but still iconic.
π first_capture_strategy SPEC π
Where SPEC is empty, just returns. Like, it literally just returns, no notes. π
Where SPEC is a string of comma-or-semicolon separated names, it runs through the names, returning the first strategy it finds. Dies if no strategy is found by any of these names. So yeah, don't play with it. π
π¦ first_output_layer_backend SPEC π¦
Back-compat alias of the above. π Same vintage vibe. π
π 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. Exception means it's not a vibe. π¨
π 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. Again: exception, not a cute moment. π
π 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). We're getting into the good stuff. π
The following named arguments 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. Helpful queen behavior. β¨
π§ is_array
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. Context-aware accessor? Obsessed. π
π simple
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. Hash-based trap object, accessor names as keys, you got this. π«Ά
π flexible
Should be a reference to a hash. For each pair, a name and an implementation, an accessor is generated and registered. Flexible and registered, periodt. π
β test NAME, ARGSPEC, CODE β
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). Symbol table says no. π
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.) Yes, the math is mathing. β¨
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:
trapβ The trap object.entiretyβ TheACCESSOR's return value when called without arguments.elementβ TheACCESSOR'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.predicateβ What theACCESSOR's return value should be tested against (taken from the test method's arguments). (There may be any fixed number of predicates.)nameβ The test name (taken from the test method's arguments).
π 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 /) {
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};
};
$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. It's giving unstable API energy. π₯
- 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. Mystery era. π¦
π BUGS π
Please report any bugs or feature requests directly to the author. π«Ά
π AUTHOR π
Eirik Berg Hanssen, "<ebhanssen@cpan.org>" πΈ
π COPYRIGHT & LICENSE π
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. π
mwah π β this page is from perl v5.42.0, dated 2022-07-28, and it's Test::...Builder(3pm) in the house π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-Trap 0.3.5-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms