GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Test2::Manual::Tooling::Formatter(3) User Contributed Perl Documentation Test2::Manual::Tooling::Formatter(3)

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

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.

    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;

use base qw/Test2::Formatter/;
All formatters should inherit from Test2::Formatter.
sub new { bless {}, shift }
Formatters need to be instantiable objects, this is a minimal "new()" method.
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::V0 itself will try to set the encoding to UTF8.
sub write { ... }
The "write()" method is the most important, each event is sent here.
my ($self, $e, $num, $f) = @_;
The "write()" 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 to write. 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 then 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 $f to 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.

Test2::Manual - Primary index of the manual.

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

Chad Granum <exodist@cpan.org>

Chad Granum <exodist@cpan.org>

Copyright 2018 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/

2022-03-04 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.