This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
999ccdab788865fadcd41db07b2e5cb037b3bb33
[perl5.git] / cpan / Test-Simple / lib / Test2 / Event / Ok.pm
1 package Test2::Event::Ok;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.302169';
6
7
8 BEGIN { require Test2::Event; our @ISA = qw(Test2::Event) }
9 use Test2::Util::HashBase qw{
10     pass effective_pass name todo
11 };
12
13 sub init {
14     my $self = shift;
15
16     # Do not store objects here, only true or false
17     $self->{+PASS} = $self->{+PASS} ? 1 : 0;
18     $self->{+EFFECTIVE_PASS} = $self->{+PASS} || (defined($self->{+TODO}) ? 1 : 0);
19 }
20
21 {
22     no warnings 'redefine';
23     sub set_todo {
24         my $self = shift;
25         my ($todo) = @_;
26         $self->{+TODO} = $todo;
27         $self->{+EFFECTIVE_PASS} = defined($todo) ? 1 : $self->{+PASS};
28     }
29 }
30
31 sub increments_count { 1 };
32
33 sub causes_fail { !$_[0]->{+EFFECTIVE_PASS} }
34
35 sub summary {
36     my $self = shift;
37
38     my $name = $self->{+NAME} || "Nameless Assertion";
39
40     my $todo = $self->{+TODO};
41     if ($todo) {
42         $name .= " (TODO: $todo)";
43     }
44     elsif (defined $todo) {
45         $name .= " (TODO)"
46     }
47
48     return $name;
49 }
50
51 sub extra_amnesty {
52     my $self = shift;
53     return unless defined($self->{+TODO}) || ($self->{+EFFECTIVE_PASS} && !$self->{+PASS});
54     return {
55         tag       => 'TODO',
56         details   => $self->{+TODO},
57     };
58 }
59
60 sub facet_data {
61     my $self = shift;
62
63     my $out = $self->common_facet_data;
64
65     $out->{assert}  = {
66         no_debug => 1,                # Legacy behavior
67         pass     => $self->{+PASS},
68         details  => $self->{+NAME},
69     };
70
71     if (my @exra_amnesty = $self->extra_amnesty) {
72         unshift @{$out->{amnesty}} => @exra_amnesty;
73     }
74
75     return $out;
76 }
77
78 1;
79
80 __END__
81
82 =pod
83
84 =encoding UTF-8
85
86 =head1 NAME
87
88 Test2::Event::Ok - Ok event type
89
90 =head1 DESCRIPTION
91
92 Ok events are generated whenever you run a test that produces a result.
93 Examples are C<ok()>, and C<is()>.
94
95 =head1 SYNOPSIS
96
97     use Test2::API qw/context/;
98     use Test2::Event::Ok;
99
100     my $ctx = context();
101     my $event = $ctx->ok($bool, $name, \@diag);
102
103 or:
104
105     my $ctx   = context();
106     my $event = $ctx->send_event(
107         'Ok',
108         pass => $bool,
109         name => $name,
110     );
111
112 =head1 ACCESSORS
113
114 =over 4
115
116 =item $rb = $e->pass
117
118 The original true/false value of whatever was passed into the event (but
119 reduced down to 1 or 0).
120
121 =item $name = $e->name
122
123 Name of the test.
124
125 =item $b = $e->effective_pass
126
127 This is the true/false value of the test after TODO and similar modifiers are
128 taken into account.
129
130 =back
131
132 =head1 SOURCE
133
134 The source code repository for Test2 can be found at
135 F<http://github.com/Test-More/test-more/>.
136
137 =head1 MAINTAINERS
138
139 =over 4
140
141 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
142
143 =back
144
145 =head1 AUTHORS
146
147 =over 4
148
149 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
150
151 =back
152
153 =head1 COPYRIGHT
154
155 Copyright 2019 Chad Granum E<lt>exodist@cpan.orgE<gt>.
156
157 This program is free software; you can redistribute it and/or
158 modify it under the same terms as Perl itself.
159
160 See F<http://dev.perl.org/licenses/>
161
162 =cut