Debian Codegraph @

upstream/debian-unstable/man3/Test2::Manual::Tooling::Formatter.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "Test2::Manual::Tooling::Formatter 3perl"
58 .TH Test2::Manual::Tooling::Formatter 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Test2::Manual::Tooling::Formatter \- How to write a custom formatter, in our
65 case a JSONL formatter.
66 .SH DESCRIPTION
67 .IX Header "DESCRIPTION"
68 This tutorial explains a minimal formatter that outputs each event as a json
69 string on its own line. A true formatter will probably be significantly more
70 complicated, but this will give you the basics needed to get started.
71 .SH "COMPLETE CODE UP FRONT"
72 .IX Header "COMPLETE CODE UP FRONT"
73 .Vb 3
74 \& package Test2::Formatter::MyFormatter;
75 \& use strict;
76 \& use warnings;
77 \&
78 \& use JSON::MaybeXS qw/encode_json/;
79 \&
80 \& use base qw/Test2::Formatter/;
81 \&
82 \& sub new { bless {}, shift }
83 \&
84 \& sub encoding {};
85 \&
86 \& sub write {
87 \& my ($self, $e, $num, $f) = @_;
88 \& $f ||= $e\->facet_data;
89 \&
90 \& print encode_json($f), "\en";
91 \& }
92 \&
93 \& 1;
94 .Ve
95 .SH "LINE BY LINE"
96 .IX Header "LINE BY LINE"
97 .IP "use base qw/Test2::Formatter/;" 4
98 .IX Item "use base qw/Test2::Formatter/;"
99 All formatters should inherit from Test2::Formatter.
100 .IP "sub new { bless {}, shift }" 4
101 .IX Item "sub new { bless {}, shift }"
102 Formatters need to be instantiable objects, this is a minimal \f(CWnew()\fR method.
103 .IP "sub encoding {};" 4
104 .IX Item "sub encoding {};"
105 For this example we leave this sub empty. In general you should implement this
106 sub to make sure you honor situations where the encoding is set. Test2::V0
107 itself will try to set the encoding to UTF8.
108 .IP "sub write { ... }" 4
109 .IX Item "sub write { ... }"
110 The \f(CWwrite()\fR method is the most important, each event is sent here.
111 .ie n .IP "my ($self, $e, $num, $f) = @_;" 4
112 .el .IP "my ($self, \f(CW$e\fR, \f(CW$num\fR, \f(CW$f\fR) = \f(CW@_\fR;" 4
113 .IX Item "my ($self, $e, $num, $f) = @_;"
114 The \f(CWwrite()\fR method receives 3 or 4 arguments, the fourth is optional.
115 .RS 4
116 .ie n .IP $self 4
117 .el .IP \f(CW$self\fR 4
118 .IX Item "$self"
119 The formatter itself.
120 .ie n .IP $e 4
121 .el .IP \f(CW$e\fR 4
122 .IX Item "$e"
123 The event being written
124 .ie n .IP $num 4
125 .el .IP \f(CW$num\fR 4
126 .IX Item "$num"
127 The most recent assertion number. If the event being processed is an assertion
128 then this will have been bumped by 1 since the last call to write. For non
129 assertions this number is set to the most recent assertion.
130 .ie n .IP $f 4
131 .el .IP \f(CW$f\fR 4
132 .IX Item "$f"
133 This MAY be a hashref containing all the facet data from the event. More often
134 then not this will be undefined. This is only set if the facet data was needed
135 by the hub, and it usually is not.
136 .RE
137 .RS 4
138 .RE
139 .ie n .IP "$f ||= $e\->facet_data;" 4
140 .el .IP "\f(CW$f\fR ||= \f(CW$e\fR\->facet_data;" 4
141 .IX Item "$f ||= $e->facet_data;"
142 We want to dump the event facet data. This will set \f(CW$f\fR to the facet data
143 unless we already have the facet data.
144 .IP "print encode_json($f), ""\en"";" 4
145 .IX Item "print encode_json($f), ""n"";"
146 This line prints the JSON encoded facet data, and a newline.
147 .SH "SEE ALSO"
148 .IX Header "SEE ALSO"
149 Test2::Manual \- Primary index of the manual.
150 .SH SOURCE
151 .IX Header "SOURCE"
152 The source code repository for Test2\-Manual can be found at
153 \&\fIhttps://github.com/Test\-More/Test2\-Suite/\fR.
154 .SH MAINTAINERS
155 .IX Header "MAINTAINERS"
156 .IP "Chad Granum <[email protected]>" 4
157 .IX Item "Chad Granum <[email protected]>"
158 .SH AUTHORS
159 .IX Header "AUTHORS"
160 .PD 0
161 .IP "Chad Granum <[email protected]>" 4
162 .IX Item "Chad Granum <[email protected]>"
163 .PD
164 .SH COPYRIGHT
165 .IX Header "COPYRIGHT"
166 Copyright 2018 Chad Granum <[email protected]>.
167 .PP
168 This program is free software; you can redistribute it and/or
169 modify it under the same terms as Perl itself.
170 .PP
171 See \fIhttp://dev.perl.org/licenses/\fR