Test2::Event - Base class for events |
Test2::Event - Base class for events
Base class for all event objects that get passed through the Test2 manpage.
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/;
# Use this if you want the legacy API to be written for you, for this to # work you will need to implement a facet_data() method. use Test2::Util::Facets2Legacy;
# Chance to initialize some defaults sub init { my $self = shift; # no other args in @_
$self->set_foo('xxx') unless defined $self->foo;
... }
# This is the new way for events to convey data to the Test2 system sub facet_data { my $self = shift;
# Get common facets such as 'about', 'trace' 'amnesty', and 'meta' my $facet_data = $self->common_facet_data();
# Are you making an assertion? $facet_data->{assert} = {pass => 1, details => 'my assertion'}; ...
return $facet_data; }
1;
related($e2)
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 the Test2::Event::Ok manpage, the other being a the Test2::Event::Diag manpage, 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.
Amnesty will prevent a failed assertion from causing the overall test to fail. In other words it marks a failure as expected and allowed.
Note: This is how 'TODO' is implemented under the hood. TODO is essentially amnesty with the 'TODO' tag. The details are the reason for the TODO.
load_facet($name)
undef
. This caches the result at the class level so that
future calls will be faster.
The $name
variable should be the key used to access the facet in a facets
hashref. For instance the assertion facet has the key 'assert', the information
facet has the 'info' key, and the error facet has the key 'errors'. You may
include or omit the 's' at the end of the name, the method is smart enough to
try both the 's' and no-'s' forms, it will check what you provided first, and
if that is not found it will add or strip the 's and try again.
FACET_TYPES()
FACET_TYPES()
load_facet()
method. This will not return any classes that have not been
loaded, or have been loaded directly without a call to load_facet()
.
Note: The core facet types are automatically loaded and populated in this list.
facet_data()
facets()
facet_data()
and blesses each facet into the
proper Test2::EventFacet::*
subclass. If no class can be found for any given
facet it will be passed along unchanged.
This can be called as an object method with no arguments, in which case the
facet_data()
method will be called to get the facet data to be validated.
When used as an object method the \%facet_data
argument may be omitted.
When used as a class method the \%facet_data
argument is required.
Remaining arguments will be slurped into a %params
hash.
Currently only 1 parameter is defined:
Facets are how events convey their purpose to the Test2 internals and formatters. An event without facets will have no intentional effect on the overall test state, and will not be displayed at all by most formatters, except perhaps to say that an event of an unknown type was seen.
Facets are produced by the facet_data()
subroutine, which you should
nearly-always override. facet_data()
is expected to return a hashref where
each key is the facet type, and the value is either a hashref with the data for
that facet, or an array of hashref's. Some facets must be defined as single
hashrefs, some must be defined as an array of hashrefs, No facets allow both.
facet_data()
MUST NOT bless the data it returns, the main hashref, and
nested facet hashref's MUST be bare, though items contained within each
facet may be blessed. The data returned by this method should also be copies
of the internal data in order to prevent accidental state modification.
facets()
takes the data from facet_data()
and blesses it into the
Test2::EventFacet::*
packages. This is rarely used however, the EventFacet
packages are primarily for convenience and documentation. The EventFacet
classes are not used at all internally, instead the raw data is used.
Here is a list of facet types by package. The packages are not used internally, but are where the documentation for each type is kept.
Note: Every single facet type has the 'details'
field. This field is
always intended for human consumption, and when provided, should explain the
'why' for the facet. All other fields are facet specific.
This contains information about the event itself such as the event package
name. The details
field for this facet is an overall summary of the event.
This facet is used if an assertion was made. The details
field of this facet
is the description of the assertion.
This facet is used to tell the the Test2::Event::Hub manpage about special actions the
event causes. Things like halting all testing, terminating the current test,
etc. In this facet the details
field explains why any special action was
taken.
Note: This is how bail-out is implemented.
The meta facet contains all the meta-data attached to the event. In this case
the details
field has no special meaning, but may be present if something
sets the 'details' meta-key on the event.
This facet contains nested events and similar details for subtests. In this
facet the details
field will typically be the name of the subtest.
This facet tells the system that a plan has been set. The details
field of
this is usually left empty, but when present explains why the plan is what it
is, this is most useful if the plan is to skip-all.
This facet contains information related to when and where the event was generated. This is how the test file and line number of a failure is known. This facet can also help you to tell if tests are related.
In this facet the details
field overrides the ``failed at test_file.t line
42.'' message provided on assertion failure.
The amnesty facet is a list instead of a single item, this is important as amnesty can come from multiple places at once.
For each instance of amnesty the details
field explains why amnesty was
granted.
Note: Outside of formatters amnesty only acts to forgive a failing assertion.
The errors facet is a list instead of a single item, any number of errors can
be listed. In this facet details
describes the error, or may contain the raw
error message itself (such as an exception). In perl exception may be blessed
objects, as such the raw data for this facet may contain nested items which are
blessed.
Not all errors are considered fatal, there is a fail
field that must be set
for an error to cause the test to fail.
Note: This facet is unique in that the field name is 'errors' while the package is 'Error'. This is because this is the only facet type that is both a list, and has a name where the plural is not the same as the singular. This may cause some confusion, but I feel it will be less confusing than the alternative.
The 'info' facet is a list instead of a single item, any quantity of extra
information can be attached to an event. Some information may be critical
diagnostics, others may be simply commentary in nature, this is determined by
the debug
flag.
For this facet the details
flag is the info itself. This info may be a
string, or it may be a data structure to display. This is one of the few facet
types that may contain blessed items.
callback($hub)
This is called BEFORE your event is passed to the formatter.
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 the Test2::Event::Plan manpage to exit 0 when the plan is 'skip_all'. This is also used by the Test2::Event:Bail manpage 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.
The default is to simply return the event package name.
sets_plan()
This object consumes the Test2::Util::ExternalMeta manpage 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.
The source code repository for Test2 can be found at http://github.com/Test-More/test-more/.
Copyright 2019 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/
Test2::Event - Base class for events |