Test2::...ormatter(3)
β¨ NAME β¨
okay bestie, welcome to the User Contributed Perl Documentation page for Test2::Manual::Tooling::Formatter(3) π β and it's literally about How to write a custom formatter, in our case a JSONL formatter. slay.
π DESCRIPTION π
This tutorial explains a minimal formatter that outputs each event as a json string on its own line. A true formatter will probably be significantly more complicated, but this will give you the basics needed to get started. no cap.
π§ COMPLETE CODE UP FRONT π§
here's the whole vibe up front, bestie β copy this and you're so back:
package Test2::Formatter::MyFormatter;
use strict;
use warnings;
use JSON::MaybeXS qw/encode_json/;
use base qw/Test2::Formatter/;
sub new { bless {}, shift }
sub encoding {};
sub write {
my ($self, $e, $num, $f) = @_;
$f ||= $e->facet_data;
print encode_json($f), "\n";
}
1;
πΈ LINE BY LINE πΈ
Let's break it down line by line, iykyk:
use base qw/Test2::Formatter/;β all formatters should inherit fromTest2::Formatter. periodt.sub new { bless {}, shift }β formatters need to be instantiable objects, and this is a minimalnew()method. iconic.sub encoding {};β for this example we leave this sub empty. In general you should implement this sub to make sure you honor situations where the encoding is set.Test2::V0itself will try to set the encoding to UTF8. πsub write { ... }β thewrite()method is the most important, each event is sent here. main character energy fr fr.my ($self, $e, $num, $f) = @_;β thewrite()method receives 3 or 4 arguments, the fourth is optional. π«Ά$selfβ the formatter itself.$eβ the event being written.$numβ the most recent assertion number. If the event being processed is an assertion then this will have been bumped by 1 since the last call towrite. For non assertions this number is set to the most recent assertion.$fβ this MAY be a hashref containing all the facet data from the event. More often than not this will be undefined. This is only set if the facet data was needed by the hub, and it usually is not.
$f ||= $e->facet_data;β we want to dump the event facet data. This will set$fto the facet data unless we already have the facet data.print encode_json($f), "\n";β this line prints the JSON encoded facet data, and a newline. π
π₯ SEE ALSO π₯
Test2::Manualβ Primary index of the manual.
π SOURCE π
The source code repository for Test2-Manual can be found at https://github.com/Test-More/test-more/. Go click it bestie.
π MAINTAINERS π
- Chad Granum
<exodist@cpan.org>
π AUTHORS π
- Chad Granum
<exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://dev.perl.org/licenses/
okay that's the whole manual, bestie. footer tea β: this page comes from perl v5.42.0, dated 2025-07-28, and it's the Test2::...ormatter(3) manual page. mwah πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-Simple 1.302214-bp160.1.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms