NAME

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

VERSION

Version 0.0.2

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 trivial singleton object.

layer NAME, CODE

Makes a layer NAME implemented by CODE.

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.

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 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 layers generated by output_layer() use in-memory files, and so will not (indeed cannot) trap output from forked-off processes -- 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.