The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test2::Event - Base class for events

DESCRIPTION

Base class for all event objects that get passed through Test2.

SYNOPSIS

    package Test2::Event::MyEvent;
    use strict;
    use warnings;

    # This will make our class an event subclass (required)
    use base 'Test2::Event';

    # Add some accessors (optional)
    # You are not obligated to use HashBase, you can use any object tool you
    # want, or roll your own accessors.
    use Test2::Util::HashBase qw/foo bar baz/;

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

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

        ...
    }

    1;

METHODS

$trace = $e->trace

Get a snapshot of the Test2::Util::Trace 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.

$bool = $e->increments_count

Should be true if this event should result in a test count increment.

$e->callback($hub)

If your event needs to have extra effects on the Test2::Hub you can override this method.

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

$num = $e->nested

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

$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 immediately.

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 Test2::Event::Plan to exit 0 when the plan is 'skip_all'. This is also used by Test2::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.

$msg = $e->summary

This is intended to be a human readable summary of the event. This should ideally only be one line long, but you can use multiple lines if necessary. This is intended for human consumption. You do not need to make it easy for machines to understand.

The default is to simply return the event package name.

($count, $directive, $reason) = $e->sets_plan()

Check if this event sets the testing plan. It will return an empty list if it does not. If it does set the plan it will return a list of 1 to 3 items in order: Expected Test Count, Test Directive, Reason for directive.

$bool = $e->diagnostics

True if the event contains diagnostics info. This is useful because a non-verbose harness may choose to hide events that are not in this category. Some formatters may choose to send these to STDERR instead of STDOUT to ensure they are seen.

$bool = $e->no_display

False by default. This will return true on events that should not be displayed by formatters.

$id = $e->in_subtest

If the event is inside a subtest this should have the subtest ID.

$id = $e->subtest_id

If the event is a final subtest event, this should contain the subtest ID.

$bool_or_undef = $e->related($e2)

Check if 2 events are related. In this case related means their traces share a signature meaning they were created with the same context (or at the very least by contexts which share an id, which is the same thing unless someone is doing something very bad).

This can be used to reliably link multiple events created by the same tool. For instance a failing test like ok(0, "fail" will generate 2 events, one being a Test2::Event::Ok, the other being a Test2::Event::Diag, both of these events are related having been created under the same context and by the same initial tool (though multiple tools may have been nested under the initial one).

This will return undef if the relationship cannot be checked, which happens if either event has an incomplete or missing trace. This will return 0 if the traces are complete, but do not match. 1 will be returned if there is a match.

$hashref = $e->TO_JSON

This returns a hashref suitable for passing to the Test2::Event->from_json constructor. It is intended for use with the JSON family of modules, which will look for a TO_JSON method when convert_blessed is true.

$e = Test2::Event->from_json(%$hashref)

Given the hash of data returned by $e->TO_JSON, this method returns a new event object of the appropriate subclass.

THIRD PARTY META-DATA

This object consumes Test2::Util::ExternalMeta which provides a consistent way for you to attach meta-data to instances of this class. This is useful for tools, plugins, and other extensions.

SOURCE

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

MAINTAINERS

Chad Granum <exodist@cpan.org>

AUTHORS

Chad Granum <exodist@cpan.org>

COPYRIGHT

Copyright 2017 Chad Granum <exodist@cpan.org>.

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

See http://dev.perl.org/licenses/