NAME

Test::Stream::Event - Base class for events

EXPERIMENTAL CODE WARNING

This is an experimental release! Test-Stream, and all its components are still in an experimental phase. This dist has been released to cpan in order to allow testers and early adopters the chance to write experimental new tools with it, or to add experimental support for it into old tools.

PLEASE DO NOT COMPLETELY CONVERT OLD TOOLS YET. This experimental release is very likely to see a lot of code churn. API's may break at any time. Test-Stream should NOT be depended on by any toolchain level tools until the experimental phase is over.

DESCRIPTION

Base class for all event objects that get passed through Test::Stream.

SYNOPSIS

package Test::Stream::Event::MyEvent;
use strict;
use warnings;

# This will make our class an event subclass, add the specified accessors,
# add constants for all our fields, and fields we inherit.
use Test::Stream::Event(
    accessors  => [qw/foo bar baz/],
);

# Chance to initialize some defaults
sub init {
    my $self = shift;
    # no other args in @_

    $self->SUPER::init();

    $self->set_foo('xxx') unless defined $self->foo;

    # Events are arrayrefs, all accessors have a constant defined with
    # their index.
    $self->[BAR] ||= "";

    ...
}

1;

IMPORTING

ARGUMENTS

In addition to the arguments listed here, you may pass in any arguments accepted by Test::Stream::HashBase.

base => $BASE_CLASS

This lets you specify an event class to subclass. THIS MUST BE AN EVENT CLASS. If you do not specify anything here then Test::Stream::Event will be used.

accessors => \@FIELDS

This lets you define any fields you wish to be present in your class. This is the only way to define storage for your event. Each field specified will get a read-only accessor with the same name as the field, as well as a setter set_FIELD(). You will also get a constant that returns the index of the field in the classes arrayref. The constant is the name of the field in all upper-case.

SUBCLASSING

Test::Stream::Event is added to your @INC for you, unless you specify an alternative base class, which must itself subclass Test::Stream::Event.

METHODS

$dbg = $e->debug

Get a snapshot of the debug info as it was when this event was generated

$bool = $e->causes_fail

Returns true if this event should result in a test failure. In general this should be false.

$call = $e->created

Get the caller() details from when the event was generated. This is usually inside a tools package. This is typically used for debugging.

$num = $e->nested

If this event is nested inside of other events, this should be the depth of nesting. (This is mainly for subtests)

$e->update_state($state)

This callback is used by Test::Stream::Hub to give your event a chance to update the state.

This is called BEFORE your event is passed to the formatter.

$bool = $e->global

Set this to true if your event is global, that is ALL threads and processes should see it no matter when or where it is generated. This is not a common thing to want, it is used by bail-out and skip_all to end testing.

$code = $e->terminate

This is called AFTER your event has been passed to the formatter. This should normally return undef, only change this if your event should cause the test to exit immedietly.

If you want this event to cause the test to exit you should return the exit code here. Exit code of 0 means exit success, any other integer means exit with failure.

This is used by Test::Stream::Event::Plan to exit 0 when the plan is 'skip_all'. This is also used by Test::Stream::Event:Bail to force the test to exit with a failure.

This is called after the event has been sent to the formatter in order to ensure the event is seen and understood.

@output = $e->to_tap($num)

This is where you get the chance to produce TAP output. The input argument $num will either be the most recent test number, or undefined. The output should be a list of arrayrefs, each arrayref should have exactly 2 values: $HID, $TEXT. The HID tells the formatter which output handle to use (see the constants provided by Test::Stream::Formatter::TAP), $TEXT should be the text that is output to the specified handle.

Example:

package Test::Stream::Event::MyEvent;
use Test::Stream::Event;
use Test::Stream::Formatter::TAP qw/OUT_STD OUT_TODO OUT_ERR/;

sub to_tap {
    my $self = shift;
    my ($num) = @_;

    # Using test numbers
    if (defined $num) {
        return (
            [OUT_STD, "# Got MyEvent!"],
            [OUT_ERR, "# The last test was $num"],
        );
    }

    # Not using test numbers.
    return (
        [OUT_STD, "# Got MyEvent!"],
    );
}

SOURCE

The source code repository for Test::Stream can be found at http://github.com/Test-More/Test-Stream/.

MAINTAINERS

Chad Granum <exodist@cpan.org>

AUTHORS

Chad Granum <exodist@cpan.org>

COPYRIGHT

Copyright 2015 Chad Granum <exodist7@gmail.com>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html