App-SimpleBackuper

 view release on metacpan or  search on metacpan

local/lib/perl5/Test/Trap/Builder.pm  view on Meta::CPAN

    croak "No capture strategy found for " . dump(@strategy);
  }
}

sub layer_implementation {
  my $self = shift;
  # Directly querying layer implementation, we should know what we're doing:
  local( GOT_CARP_NOT ? @CARP_NOT : @ISA ) = _carpnot_for caller;
  my $trapper = shift;
  my @r;
  for (@_) {
    if ( length ref and eval { exists &$_ } ) {
      push @r, $_;
      next;
    }
    my ($name, $arg) =
      /^ ( [^\(]+ )      # layer name: anything but '('
         (?:             # begin optional group
             \(          # literal '('
             ( [^\)]* )  # arg: anything but ')'
             \)          # literal ')'
         )?              # end optional group
      \z/x;
    my $meth = $trapper->can("layer:$name")
      or croak qq[Unknown trap layer "$_"];
    push @r, $trapper->$meth($arg);
  }
  return @r;
}

1; # End of Test::Trap::Builder

__END__

=head1 NAME

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

=head1 VERSION

Version 0.3.5

=head1 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 );

=head1 DESCRIPTION

L<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 C<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 I<ACCESSOR>_I<TEST>.

=head1 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 C<: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.

=head2 Prop [PACKAGE]

A method returning a reference to a hash, holding the I<PACKAGE>'s (by
default the caller's) tag-on properties for the (current) trap object.
Currently, Test::Trap::Builder defines the following properties:

=over

=item layers

While the trap is springing, the queue of layers remaining.  Usually
set by the L</"trap"> method and consumed by the L</"Next"> method.

=item teardown

While the trap is springing, the queue of teardown actions remaining.
Usually accumulated through the L</"Teardown"> method and invoked by
the L</"trap"> method.

=item code

The user code trapped.  Usually set by the L</"trap"> method and
invoked by the L</"Run"> method.

=item exception

An internal exception.  Usually set through the L</"Exception">
method and examined by the L</"trap"> method.

=item on_test_failure

A callback invoked by the L</"TestFailure"> method.  Layers in
particular may want to set this.

=item test_accessor

The name and (optionally) the index of the accessor, the contents of
which we're currently testing.  Best accessed through the
L</"TestAccessor"> method, and usually set by the L</"test"> and
L</"accessor"> methods, but if you are writing your own tests or
accessors directly, you just might need to set it.  Perhaps.

=back

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;

=head2 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 C<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
  }

=head2 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.

=head2 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).

=head2 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.

=head2 TestAccessor

Returns a string of the form C<"I<NAME>(I<INDEX>)">, where I<NAME> and
I<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),
      );

=head2 TestFailure

Runs the C<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;

=head2 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
L</"ExceptionFunction"> and call it from the closure instead.

=head2 ExceptionFunction

This method returns a function that may be called with the same effect
as calling the L</"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.");
  };

=head1 METHODS

=head2 new

local/lib/perl5/Test/Trap/Builder.pm  view on Meta::CPAN


=head2 first_output_layer_backend SPEC

Back-compat alias of the above.

=head2 multi_layer NAME, LAYERS

Registers (by I<NAME>) a layer that just pushes a number of other
I<LAYERS> on the stack of layers.  If any of the I<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.

=head2 layer_implementation TRAPPER, LAYERS

Returns the subroutines that implement the requested I<LAYERS>.  If
any of the I<LAYERS> is neither an anonymous method nor the name of a
layer registered to or inherited by the I<TRAPPER>, an exception is
raised.

=head2 accessor NAMED_ARGS

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

The following named arguments are recognized:

=over

=item is_leaveby

If true, the tests methods will generate better diagnostics if the
trap was not left as specified.  Also, a special did_I<ACCESSOR> test
method will be generated (unless already present), simply passing as
long as the trap was left as specified.

=item 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.

=item 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.

=item flexible

Should be a reference to a hash.  For each pair, a name and an
implementation, an accessor is generated and registered.

=back

=head2 test NAME, ARGSPEC, CODE

Registers a test callback by I<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 I<ACCESSOR>_I<TEST> will be made available
(directly or by inheritance) to every trapper that registers or
inherits both the accessor named I<ACCESSOR> and the test named
I<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 I<CODE> is
called with arguments according to the words found in the I<ARGSPEC>
string:

=over

=item trap

The trap object.

=item entirety

The I<ACCESSOR>'s return value when called without arguments.

=item element

The I<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.

=item predicate

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

=item name

The test name (taken from the test method's arguments).

=back

=head1 EXAMPLE

A complete example, implementing a I<timeout> layer (depending on
Time::HiRes::ualarm being present), a I<simpletee> layer (printing the
trapped stdout/stderr to the original file handles after the trap has
sprung), and a I<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;



( run in 2.186 seconds using v1.01-cache-2.11-cpan-d8267643d1d )