<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test2::Event::Generic;
use strict;
use warnings;

use Carp qw/croak/;
use Scalar::Util qw/reftype/;

our $VERSION = '1.302183';

BEGIN { require Test2::Event; our @ISA = qw(Test2::Event) }
use Test2::Util::HashBase;

my @FIELDS = qw{
    causes_fail increments_count diagnostics no_display callback terminate
    global sets_plan summary facet_data
};
my %DEFAULTS = (
    causes_fail      =&gt; 0,
    increments_count =&gt; 0,
    diagnostics      =&gt; 0,
    no_display       =&gt; 0,
);

sub init {
    my $self = shift;

    for my $field (@FIELDS) {
        my $val = defined $self-&gt;{$field} ? delete $self-&gt;{$field} : $DEFAULTS{$field};
        next unless defined $val;

        my $set = "set_$field";
        $self-&gt;$set($val);
    }
}

for my $field (@FIELDS) {
    no strict 'refs';

    *$field = sub { exists $_[0]-&gt;{$field} ? $_[0]-&gt;{$field} : () }
        unless exists &amp;{$field};

    *{"set_$field"} = sub { $_[0]-&gt;{$field} = $_[1] }
        unless exists &amp;{"set_$field"};
}

sub can {
    my $self = shift;
    my ($name) = @_;
    return $self-&gt;SUPER::can($name) unless $name eq 'callback';
    return $self-&gt;{callback} || \&amp;Test2::Event::callback;
}

sub facet_data {
    my $self = shift;
    return $self-&gt;{facet_data} || $self-&gt;SUPER::facet_data();
}

sub summary {
    my $self = shift;
    return $self-&gt;{summary} if defined $self-&gt;{summary};
    $self-&gt;SUPER::summary();
}

sub sets_plan {
    my $self = shift;
    return unless $self-&gt;{sets_plan};
    return @{$self-&gt;{sets_plan}};
}

sub callback {
    my $self = shift;
    my $cb = $self-&gt;{callback} || return;
    $self-&gt;$cb(@_);
}

sub set_global {
    my $self = shift;
    my ($bool) = @_;

    if(!defined $bool) {
        delete $self-&gt;{global};
        return undef;
    }

    $self-&gt;{global} = $bool;
}

sub set_callback {
    my $self = shift;
    my ($cb) = @_;

    if(!defined $cb) {
        delete $self-&gt;{callback};
        return undef;
    }

    croak "callback must be a code reference"
        unless ref($cb) &amp;&amp; reftype($cb) eq 'CODE';

    $self-&gt;{callback} = $cb;
}

sub set_terminate {
    my $self = shift;
    my ($exit) = @_;

    if(!defined $exit) {
        delete $self-&gt;{terminate};
        return undef;
    }

    croak "terminate must be a positive integer"
       unless $exit =~ m/^\d+$/;

    $self-&gt;{terminate} = $exit;
}

sub set_sets_plan {
    my $self = shift;
    my ($plan) = @_;

    if(!defined $plan) {
        delete $self-&gt;{sets_plan};
        return undef;
    }

    croak "'sets_plan' must be an array reference"
        unless ref($plan) &amp;&amp; reftype($plan) eq 'ARRAY';

    $self-&gt;{sets_plan} = $plan;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Test2::Event::Generic - Generic event type.

=head1 DESCRIPTION

This is a generic event that lets you customize all fields in the event API.
This is useful if you have need for a custom event that does not make sense as
a published reusable event subclass.

=head1 SYNOPSIS

    use Test2::API qw/context/;

    sub send_custom_fail {
        my $ctx = shift;

        $ctx-&gt;send_event('Generic', causes_fail =&gt; 1, summary =&gt; 'The sky is falling');

        $ctx-&gt;release;
    }

    send_custom_fail();

=head1 METHODS

=over 4

=item $e-&gt;facet_data($data)

=item $data = $e-&gt;facet_data

Get or set the facet data (see L&lt;Test2::Event&gt;). If no facet_data is set then
C&lt;&lt; Test2::Event-&gt;facet_data &gt;&gt; will be called to produce facets from the other
data.

=item $e-&gt;callback($hub)

Call the custom callback if one is set, otherwise this does nothing.

=item $e-&gt;set_callback(sub { ... })

Set the custom callback. The custom callback must be a coderef. The first
argument to your callback will be the event itself, the second will be the
L&lt;Test2::Event::Hub&gt; that is using the callback.

=item $bool = $e-&gt;causes_fail

=item $e-&gt;set_causes_fail($bool)

Get/Set the C&lt;causes_fail&gt; attribute. This defaults to C&lt;0&gt;.

=item $bool = $e-&gt;diagnostics

=item $e-&gt;set_diagnostics($bool)

Get/Set the C&lt;diagnostics&gt; attribute. This defaults to C&lt;0&gt;.

=item $bool_or_undef = $e-&gt;global

=item @bool_or_empty = $e-&gt;global

=item $e-&gt;set_global($bool_or_undef)

Get/Set the C&lt;diagnostics&gt; attribute. This defaults to an empty list which is
undef in scalar context.

=item $bool = $e-&gt;increments_count

=item $e-&gt;set_increments_count($bool)

Get/Set the C&lt;increments_count&gt; attribute. This defaults to C&lt;0&gt;.

=item $bool = $e-&gt;no_display

=item $e-&gt;set_no_display($bool)

Get/Set the C&lt;no_display&gt; attribute. This defaults to C&lt;0&gt;.

=item @plan = $e-&gt;sets_plan

Get the plan if this event sets one. The plan is a list of up to 3 items:
C&lt;($count, $directive, $reason)&gt;. C&lt;$count&gt; must be defined, the others may be
undef, or may not exist at all.

=item $e-&gt;set_sets_plan(\@plan)

Set the plan. You must pass in an arrayref with up to 3 elements.

=item $summary = $e-&gt;summary

=item $e-&gt;set_summary($summary_or_undef)

Get/Set the summary. This will default to the event package
C&lt;'Test2::Event::Generic'&gt;. You can set it to any value. Setting this to
C&lt;undef&gt; will reset it to the default.

=item $int_or_undef = $e-&gt;terminate

=item @int_or_empty = $e-&gt;terminate

=item $e-&gt;set_terminate($int_or_undef)

This will get/set the C&lt;terminate&gt; attribute. This defaults to undef in scalar
context, or an empty list in list context. Setting this to undef will clear it
completely. This must be set to a positive integer (0 or larger).

=back

=head1 SOURCE

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

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 AUTHORS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2020 Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;.

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

See F&lt;http://dev.perl.org/licenses/&gt;

=cut
</pre></body></html>