1package Test2::Event::Subtest;
2use strict;
3use warnings;
4
5our $VERSION = '1.302199';
6
7BEGIN { require Test2::Event::Ok; our @ISA = qw(Test2::Event::Ok) }
8use Test2::Util::HashBase qw{subevents buffered subtest_id subtest_uuid start_stamp stop_stamp};
9
10sub init {
11    my $self = shift;
12    $self->SUPER::init();
13    $self->{+SUBEVENTS} ||= [];
14    if ($self->{+EFFECTIVE_PASS}) {
15        $_->set_effective_pass(1) for grep { $_->can('effective_pass') } @{$self->{+SUBEVENTS}};
16    }
17}
18
19{
20    no warnings 'redefine';
21
22    sub set_subevents {
23        my $self      = shift;
24        my @subevents = @_;
25
26        if ($self->{+EFFECTIVE_PASS}) {
27            $_->set_effective_pass(1) for grep { $_->can('effective_pass') } @subevents;
28        }
29
30        $self->{+SUBEVENTS} = \@subevents;
31    }
32
33    sub set_effective_pass {
34        my $self = shift;
35        my ($pass) = @_;
36
37        if ($pass) {
38            $_->set_effective_pass(1) for grep { $_->can('effective_pass') } @{$self->{+SUBEVENTS}};
39        }
40        elsif ($self->{+EFFECTIVE_PASS} && !$pass) {
41            for my $s (grep { $_->can('effective_pass') } @{$self->{+SUBEVENTS}}) {
42                $_->set_effective_pass(0) unless $s->can('todo') && defined $s->todo;
43            }
44        }
45
46        $self->{+EFFECTIVE_PASS} = $pass;
47    }
48}
49
50sub summary {
51    my $self = shift;
52
53    my $name = $self->{+NAME} || "Nameless Subtest";
54
55    my $todo = $self->{+TODO};
56    if ($todo) {
57        $name .= " (TODO: $todo)";
58    }
59    elsif (defined $todo) {
60        $name .= " (TODO)";
61    }
62
63    return $name;
64}
65
66sub facet_data {
67    my $self = shift;
68
69    my $out = $self->SUPER::facet_data();
70
71    my $start = $self->start_stamp;
72    my $stop  = $self->stop_stamp;
73
74    $out->{parent} = {
75        hid      => $self->subtest_id,
76        children => [map {$_->facet_data} @{$self->{+SUBEVENTS}}],
77        buffered => $self->{+BUFFERED},
78        $start ? (start_stamp => $start) : (),
79        $stop  ? (stop_stamp  => $stop)  : (),
80    };
81
82    return $out;
83}
84
85sub add_amnesty {
86    my $self = shift;
87
88    for my $am (@_) {
89        $am = {%$am} if ref($am) ne 'ARRAY';
90        $am = Test2::EventFacet::Amnesty->new($am);
91
92        push @{$self->{+AMNESTY}} => $am;
93
94        for my $e (@{$self->{+SUBEVENTS}}) {
95            $e->add_amnesty($am->clone(inherited => 1));
96        }
97    }
98}
99
100
1011;
102
103__END__
104
105=pod
106
107=encoding UTF-8
108
109=head1 NAME
110
111Test2::Event::Subtest - Event for subtest types
112
113=head1 DESCRIPTION
114
115This class represents a subtest. This class is a subclass of
116L<Test2::Event::Ok>.
117
118=head1 ACCESSORS
119
120This class inherits from L<Test2::Event::Ok>.
121
122=over 4
123
124=item $arrayref = $e->subevents
125
126Returns the arrayref containing all the events from the subtest
127
128=item $bool = $e->buffered
129
130True if the subtest is buffered, that is all subevents render at once. If this
131is false it means all subevents render as they are produced.
132
133=back
134
135=head1 SOURCE
136
137The source code repository for Test2 can be found at
138L<https://github.com/Test-More/test-more/>.
139
140=head1 MAINTAINERS
141
142=over 4
143
144=item Chad Granum E<lt>exodist@cpan.orgE<gt>
145
146=back
147
148=head1 AUTHORS
149
150=over 4
151
152=item Chad Granum E<lt>exodist@cpan.orgE<gt>
153
154=back
155
156=head1 COPYRIGHT
157
158Copyright 2020 Chad Granum E<lt>exodist@cpan.orgE<gt>.
159
160This program is free software; you can redistribute it and/or
161modify it under the same terms as Perl itself.
162
163See L<https://dev.perl.org/licenses/>
164
165=cut
166