Debian Codegraph @

lib/Test2/Tools/HarnessTester.pm

raw
1 package Test2::Tools::HarnessTester;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Test2::Harness::Util::UUID qw/gen_uuid/;
8
9 use App::Yath::Tester qw/make_example_dir/;
10
11 use Importer Importer => qw/import/;
12 our @EXPORT_OK = qw/make_example_dir summarize_events/;
13
14 my $HARNESS_ID = 1;
15 sub summarize_events {
16 my ($events) = @_;
17
18 my @caller = caller(0);
19
20 my $id = $HARNESS_ID++;
21 my $run_id = "run-$id";
22 my $job_id = "job-$id";
23
24 require Test2::Harness::Auditor::Watcher;
25 my $watcher = Test2::Harness::Auditor::Watcher->new(job => 1, try => 0);
26
27 require Test2::Harness::Event;
28 for my $e (@$events) {
29 my $fd = $e->facet_data;
30 my $he = Test2::Harness::Event->new(
31 facet_data => $fd,
32 event_id => gen_uuid(),
33 run_id => $run_id,
34 job_id => $job_id,
35 stamp => time,
36 job_try => 0,
37 );
38
39 $watcher->process($he);
40 }
41
42 return {
43 plan => $watcher->plan,
44 pass => $watcher->pass ? 1 : 0,
45 fail => $watcher->fail ? 1 : 0,
46 errors => $watcher->_errors,
47 failures => $watcher->_failures,
48 assertions => $watcher->assertion_count,
49 };
50 }
51
52 1;
53
54 __END__
55
56 =pod
57
58 =encoding UTF-8
59
60 =head1 NAME
61
62 Test2::Tools::HarnessTester - Run events through a harness for a summary
63
64 =head1 DESCRIPTION
65
66 This tool allows you to process events through the L<Test2::Harness> auditor.
67 The main benefit here is to get a pass/fail result, as well as counts for
68 assertions, failures, and errors.
69
70 =head1 SYNOPSIS
71
72 use Test2::V0;
73 use Test2::API qw/intercept/;
74 use Test2::Tools::HarnessTester qw/summarize_events/;
75
76 my $events = intercept {
77 ok(1, "pass");
78 ok(2, "pass gain");
79 done_testing;
80 };
81
82 is(
83 summarize_events($events),
84 {
85 # Each of these is the negation of the other, no need to check both
86 pass => 1,
87 fail => 0,
88
89 # The plan facet, see Test2::EventFacet::Plan
90 plan => {count => 2},
91
92 # Statistics
93 assertions => 2,
94 errors => 0,
95 failures => 0,
96 }
97 );
98
99 =head1 EXPORTS
100
101 =head2 $summary = summarize_events($events)
102
103 This takes an arrayref of events, such as that produced by C<intercept {...}>
104 from L<Test2::API>. The result is a hashref that summarizes the results of the
105 events as processed by L<Test2::Harness>, specifically the
106 L<Test2::Harness::Auditor::Watcher> module.
107
108 Fields in the summary hash:
109
110 =over 4
111
112 =item pass => $BOOL
113
114 =item fail => $BOOL
115
116 These are negatives of each other. These represent the pass/fail state after
117 processing the events. When one is true the other should be false. These are
118 normalized to C<1> and C<0>.
119
120 =item plan => $HASHREF
121
122 If a plan was provided this will have the L<Test2::EventFacet::Plan> facet, but
123 as a hashref, not a blessed instance.
124
125 B<Note:> This is reference to the original data, not a copy, if you modify it
126 you will modify the event as well.
127
128 =item assertions => $INT
129
130 Count of assertions made.
131
132 =item errors => $INT
133
134 Count of errors seen.
135
136 =item failures => $INT
137
138 Count of failures seen.
139
140 =back
141
142 =head2 $path = make_example_dir()
143
144 This will create a temporary directory with 't', 't2', and 'xt' subdirectories
145 each of which will contain a single passing test.
146
147 This is re-exported from L<App::Yath::Tester>.
148
149 =head1 SOURCE
150
151 The source code repository for Test2-Harness can be found at
152 F<http://github.com/Test-More/Test2-Harness/>.
153
154 =head1 MAINTAINERS
155
156 =over 4
157
158 =item Chad Granum E<lt>[email protected]<gt>
159
160 =back
161
162 =head1 AUTHORS
163
164 =over 4
165
166 =item Chad Granum E<lt>[email protected]<gt>
167
168 =back
169
170 =head1 COPYRIGHT
171
172 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
173
174 This program is free software; you can redistribute it and/or
175 modify it under the same terms as Perl itself.
176
177 See F<http://dev.perl.org/licenses/>
178
179 =cut