1Test2::Manual::Tooling:U:sFeorrmCaotntterri(b3u)ted PerlTeDsotc2u:m:eMnatnautailo:n:Tooling::Formatter(3)
2
3
4

NAME

6       Test2::Manual::Tooling::Formatter - How to write a custom formatter, in
7       our case a JSONL formatter.
8

DESCRIPTION

10       This tutorial explains a minimal formatter that outputs each event as a
11       json string on its own line. A true formatter will probably be
12       significantly more complicated, but this will give you the basics
13       needed to get started.
14

COMPLETE CODE UP FRONT

16           package Test2::Formatter::MyFormatter;
17           use strict;
18           use warnings;
19
20           use JSON::MaybeXS qw/encode_json/;
21
22           use base qw/Test2::Formatter/;
23
24           sub new { bless {}, shift }
25
26           sub encoding {};
27
28           sub write {
29               my ($self, $e, $num, $f) = @_;
30               $f ||= $e->facet_data;
31
32               print encode_json($f), "\n";
33           }
34
35           1;
36

LINE BY LINE

38       use base qw/Test2::Formatter/;
39           All formatters should inherit from Test2::Formatter.
40
41       sub new { bless {}, shift }
42           Formatters need to be instantiable objects, this is a minimal new()
43           method.
44
45       sub encoding {};
46           For this example we leave this sub empty. In general you should
47           implement this sub to make sure you honor situations where the
48           encoding is set. Test2::V0 itself will try to set the encoding to
49           UTF8.
50
51       sub write { ... }
52           The write() method is the most important, each event is sent here.
53
54       my ($self, $e, $num, $f) = @_;
55           The write() method receives 3 or 4 arguments, the fourth is
56           optional.
57
58           $self
59               The formatter itself.
60
61           $e  The event being written
62
63           $num
64               The most recent assertion number. If the event being processed
65               is an assertion then this will have been bumped by 1 since the
66               last call to write. For non assertions this number is set to
67               the most recent assertion.
68
69           $f  This MAY be a hashref containing all the facet data from the
70               event. More often then not this will be undefined. This is only
71               set if the facet data was needed by the hub, and it usually is
72               not.
73
74       $f ||= $e->facet_data;
75           We want to dump the event facet data. This will set $f to the facet
76           data unless we already have the facet data.
77
78       print encode_json($f), "\n";
79           This line prints the JSON encoded facet data, and a newline.
80

SEE ALSO

82       Test2::Manual - Primary index of the manual.
83

SOURCE

85       The source code repository for Test2-Manual can be found at
86       https://github.com/Test-More/Test2-Suite/.
87

MAINTAINERS

89       Chad Granum <exodist@cpan.org>
90

AUTHORS

92       Chad Granum <exodist@cpan.org>
93
95       Copyright 2018 Chad Granum <exodist@cpan.org>.
96
97       This program is free software; you can redistribute it and/or modify it
98       under the same terms as Perl itself.
99
100       See http://dev.perl.org/licenses/
101
102
103
104perl v5.36.0                      2023-03-2T3est2::Manual::Tooling::Formatter(3)
Impressum