1package Test2::Event::Plan;
2use strict;
3use warnings;
4
5our $VERSION = '1.302199';
6
7
8BEGIN { require Test2::Event; our @ISA = qw(Test2::Event) }
9use Test2::Util::HashBase qw{max directive reason};
10
11use Carp qw/confess/;
12
13my %ALLOWED = (
14    'SKIP'    => 1,
15    'NO PLAN' => 1,
16);
17
18sub init {
19    if ($_[0]->{+DIRECTIVE}) {
20        $_[0]->{+DIRECTIVE} = 'SKIP'    if $_[0]->{+DIRECTIVE} eq 'skip_all';
21        $_[0]->{+DIRECTIVE} = 'NO PLAN' if $_[0]->{+DIRECTIVE} eq 'no_plan';
22
23        confess "'" . $_[0]->{+DIRECTIVE} . "' is not a valid plan directive"
24            unless $ALLOWED{$_[0]->{+DIRECTIVE}};
25    }
26    else {
27        confess "Cannot have a reason without a directive!"
28            if defined $_[0]->{+REASON};
29
30        confess "No number of tests specified"
31            unless defined $_[0]->{+MAX};
32
33        confess "Plan test count '" . $_[0]->{+MAX}  . "' does not appear to be a valid positive integer"
34            unless $_[0]->{+MAX} =~ m/^\d+$/;
35
36        $_[0]->{+DIRECTIVE} = '';
37    }
38}
39
40sub sets_plan {
41    my $self = shift;
42    return (
43        $self->{+MAX},
44        $self->{+DIRECTIVE},
45        $self->{+REASON},
46    );
47}
48
49sub terminate {
50    my $self = shift;
51    # On skip_all we want to terminate the hub
52    return 0 if $self->{+DIRECTIVE} && $self->{+DIRECTIVE} eq 'SKIP';
53    return undef;
54}
55
56sub summary {
57    my $self = shift;
58    my $max = $self->{+MAX};
59    my $directive = $self->{+DIRECTIVE};
60    my $reason = $self->{+REASON};
61
62    return "Plan is $max assertions"
63        if $max || !$directive;
64
65    return "Plan is '$directive', $reason"
66        if $reason;
67
68    return "Plan is '$directive'";
69}
70
71sub facet_data {
72    my $self = shift;
73
74    my $out = $self->common_facet_data;
75
76    $out->{control}->{terminate} = $self->{+DIRECTIVE} eq 'SKIP' ? 0 : undef
77        unless defined $out->{control}->{terminate};
78
79    $out->{plan} = {count => $self->{+MAX}};
80    $out->{plan}->{details} = $self->{+REASON} if defined $self->{+REASON};
81
82    if (my $dir = $self->{+DIRECTIVE}) {
83        $out->{plan}->{skip} = 1 if $dir eq 'SKIP';
84        $out->{plan}->{none} = 1 if $dir eq 'NO PLAN';
85    }
86
87    return $out;
88}
89
90
911;
92
93__END__
94
95=pod
96
97=encoding UTF-8
98
99=head1 NAME
100
101Test2::Event::Plan - The event of a plan
102
103=head1 DESCRIPTION
104
105Plan events are fired off whenever a plan is declared, done testing is called,
106or a subtext completes.
107
108=head1 SYNOPSIS
109
110    use Test2::API qw/context/;
111    use Test2::Event::Plan;
112
113    my $ctx = context();
114
115    # Plan for 10 tests to run
116    my $event = $ctx->plan(10);
117
118    # Plan to skip all tests (will exit 0)
119    $ctx->plan(0, skip_all => "These tests need to be skipped");
120
121=head1 ACCESSORS
122
123=over 4
124
125=item $num = $plan->max
126
127Get the number of expected tests
128
129=item $dir = $plan->directive
130
131Get the directive (such as TODO, skip_all, or no_plan).
132
133=item $reason = $plan->reason
134
135Get the reason for the directive.
136
137=back
138
139=head1 SOURCE
140
141The source code repository for Test2 can be found at
142L<https://github.com/Test-More/test-more/>.
143
144=head1 MAINTAINERS
145
146=over 4
147
148=item Chad Granum E<lt>exodist@cpan.orgE<gt>
149
150=back
151
152=head1 AUTHORS
153
154=over 4
155
156=item Chad Granum E<lt>exodist@cpan.orgE<gt>
157
158=back
159
160=head1 COPYRIGHT
161
162Copyright 2020 Chad Granum E<lt>exodist@cpan.orgE<gt>.
163
164This program is free software; you can redistribute it and/or
165modify it under the same terms as Perl itself.
166
167See L<https://dev.perl.org/licenses/>
168
169=cut
170