The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::Trap::Builder - Backend for building test traps

VERSION

Version 0.0.3

SYNOPSIS

  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; my $next = pop;
    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; my $next = pop;
    for (qw/ stdout stderr /) {
      next unless exists $self->{$_};
      die "Too late to tee $_";
    }
    $self->$next(@_);
    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 );

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.

METHODS

new

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

layer NAME, CODE

Makes a layer NAME implemented by CODE. Unless it is a terminating or otherwise special layer, the implementation should expect the result object as the first argument and the next layer as the final argument.

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 with the result object, the (layer) name, and the output handle's globref as parameters. It may return an object, which then will be kept alive until after the call to lower levels (that is to say, DESTROY methods may be useful -- see the Test::Trap::Builder::TempFile implementation).

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_method NAME, IS_INDEXING, TEST_NAME_INDEX, CODE

Registers a test NAME for the calling trapper package. Makes test methods of the form ACCESSOR_NAME in the proper (i.e. inheriting) package for every registered ACCESSOR of a package that either inherits or is inherited by the calling package.

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.

CAVEATS

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

If File::Temp is not availible, the layers generated by output_layer() use in-memory files, and so will not (indeed cannot) trap output from forked-off processes -- including system() calls.

Even if File::Temp is availible, the file descriptors aren't right, so you won't in general be able to trap output from exec'ed commands -- including system() calls.

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.