xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/lib/Test2/Formatter.pm (revision 5486feefcc8cb79b19e014ab332cc5dfd05b3b33)
15759b3d2Safresh1package Test2::Formatter;
25759b3d2Safresh1use strict;
35759b3d2Safresh1use warnings;
45759b3d2Safresh1
5*5486feefSafresh1our $VERSION = '1.302199';
65759b3d2Safresh1
75759b3d2Safresh1
85759b3d2Safresh1my %ADDED;
95759b3d2Safresh1sub import {
105759b3d2Safresh1    my $class = shift;
115759b3d2Safresh1    return if $class eq __PACKAGE__;
125759b3d2Safresh1    return if $ADDED{$class}++;
135759b3d2Safresh1    require Test2::API;
145759b3d2Safresh1    Test2::API::test2_formatter_add($class);
155759b3d2Safresh1}
165759b3d2Safresh1
175759b3d2Safresh1sub new_root {
185759b3d2Safresh1    my $class = shift;
195759b3d2Safresh1    return $class->new(@_);
205759b3d2Safresh1}
215759b3d2Safresh1
22f3efcd01Safresh1sub supports_tables { 0 }
23f3efcd01Safresh1
245759b3d2Safresh1sub hide_buffered { 1 }
255759b3d2Safresh1
265759b3d2Safresh1sub terminate { }
275759b3d2Safresh1
285759b3d2Safresh1sub finalize { }
295759b3d2Safresh1
305759b3d2Safresh11;
315759b3d2Safresh1
325759b3d2Safresh1__END__
335759b3d2Safresh1
345759b3d2Safresh1=pod
355759b3d2Safresh1
365759b3d2Safresh1=encoding UTF-8
375759b3d2Safresh1
385759b3d2Safresh1=head1 NAME
395759b3d2Safresh1
405759b3d2Safresh1Test2::Formatter - Namespace for formatters.
415759b3d2Safresh1
425759b3d2Safresh1=head1 DESCRIPTION
435759b3d2Safresh1
445759b3d2Safresh1This is the namespace for formatters. This is an empty package.
455759b3d2Safresh1
465759b3d2Safresh1=head1 CREATING FORMATTERS
475759b3d2Safresh1
485759b3d2Safresh1A formatter is any package or object with a C<write($event, $num)> method.
495759b3d2Safresh1
505759b3d2Safresh1    package Test2::Formatter::Foo;
515759b3d2Safresh1    use strict;
525759b3d2Safresh1    use warnings;
535759b3d2Safresh1
545759b3d2Safresh1    sub write {
555759b3d2Safresh1        my $self_or_class = shift;
565759b3d2Safresh1        my ($event, $assert_num) = @_;
575759b3d2Safresh1        ...
585759b3d2Safresh1    }
595759b3d2Safresh1
605759b3d2Safresh1    sub hide_buffered { 1 }
615759b3d2Safresh1
625759b3d2Safresh1    sub terminate { }
635759b3d2Safresh1
645759b3d2Safresh1    sub finalize { }
655759b3d2Safresh1
66f3efcd01Safresh1    sub supports_tables { return $BOOL }
67f3efcd01Safresh1
685759b3d2Safresh1    sub new_root {
695759b3d2Safresh1        my $class = shift;
705759b3d2Safresh1        ...
715759b3d2Safresh1        $class->new(@_);
725759b3d2Safresh1    }
735759b3d2Safresh1
745759b3d2Safresh1    1;
755759b3d2Safresh1
765759b3d2Safresh1The C<write> method is a method, so it either gets a class or instance. The two
775759b3d2Safresh1arguments are the C<$event> object it should record, and the C<$assert_num>
785759b3d2Safresh1which is the number of the current assertion (ok), or the last assertion if
795759b3d2Safresh1this event is not itself an assertion. The assertion number may be any integer 0
805759b3d2Safresh1or greater, and may be undefined in some cases.
815759b3d2Safresh1
825759b3d2Safresh1The C<hide_buffered()> method must return a boolean. This is used to tell
835759b3d2Safresh1buffered subtests whether or not to send it events as they are being buffered.
845759b3d2Safresh1See L<Test2::API/"run_subtest(...)"> for more information.
855759b3d2Safresh1
865759b3d2Safresh1The C<terminate> and C<finalize> methods are optional methods called that you
875759b3d2Safresh1can implement if the format you're generating needs to handle these cases, for
885759b3d2Safresh1example if you are generating XML and need close open tags.
895759b3d2Safresh1
905759b3d2Safresh1The C<terminate> method is called when an event's C<terminate> method returns
915759b3d2Safresh1true, for example when a L<Test2::Event::Plan> has a C<'skip_all'> plan, or
925759b3d2Safresh1when a L<Test2::Event::Bail> event is sent. The C<terminate> method is passed
935759b3d2Safresh1a single argument, the L<Test2::Event> object which triggered the terminate.
945759b3d2Safresh1
955759b3d2Safresh1The C<finalize> method is always the last thing called on the formatter, I<<
965759b3d2Safresh1except when C<terminate> is called for a Bail event >>. It is passed the
975759b3d2Safresh1following arguments:
985759b3d2Safresh1
99f3efcd01Safresh1The C<supports_tables> method should be true if the formatter supports directly
100f3efcd01Safresh1rendering table data from the C<info> facets. This is a newer feature and many
101f3efcd01Safresh1older formatters may not support it. When not supported the formatter falls
102f3efcd01Safresh1back to rendering C<detail> instead of the C<table> data.
103f3efcd01Safresh1
104f3efcd01Safresh1The C<new_root> method is used when constructing a root formatter. The default
105f3efcd01Safresh1is to just delegate to the regular C<new()> method, most formatters can ignore
106f3efcd01Safresh1this.
1075759b3d2Safresh1
1085759b3d2Safresh1=over 4
1095759b3d2Safresh1
1105759b3d2Safresh1=item * The number of tests that were planned
1115759b3d2Safresh1
1125759b3d2Safresh1=item * The number of tests actually seen
1135759b3d2Safresh1
1145759b3d2Safresh1=item * The number of tests which failed
1155759b3d2Safresh1
1165759b3d2Safresh1=item * A boolean indicating whether or not the test suite passed
1175759b3d2Safresh1
1185759b3d2Safresh1=item * A boolean indicating whether or not this call is for a subtest
1195759b3d2Safresh1
1205759b3d2Safresh1=back
1215759b3d2Safresh1
122f3efcd01Safresh1The C<new_root> method is called when C<Test2::API::Stack> Initializes the root
123f3efcd01Safresh1hub for the first time. Most formatters will simply have this call C<<
124f3efcd01Safresh1$class->new >>, which is the default behavior. Some formatters however may want
125f3efcd01Safresh1to take extra action during construction of the root formatter, this is where
126f3efcd01Safresh1they can do that.
127f3efcd01Safresh1
1285759b3d2Safresh1=head1 SOURCE
1295759b3d2Safresh1
1305759b3d2Safresh1The source code repository for Test2 can be found at
131*5486feefSafresh1L<https://github.com/Test-More/test-more/>.
1325759b3d2Safresh1
1335759b3d2Safresh1=head1 MAINTAINERS
1345759b3d2Safresh1
1355759b3d2Safresh1=over 4
1365759b3d2Safresh1
1375759b3d2Safresh1=item Chad Granum E<lt>exodist@cpan.orgE<gt>
1385759b3d2Safresh1
1395759b3d2Safresh1=back
1405759b3d2Safresh1
1415759b3d2Safresh1=head1 AUTHORS
1425759b3d2Safresh1
1435759b3d2Safresh1=over 4
1445759b3d2Safresh1
1455759b3d2Safresh1=item Chad Granum E<lt>exodist@cpan.orgE<gt>
1465759b3d2Safresh1
1475759b3d2Safresh1=back
1485759b3d2Safresh1
1495759b3d2Safresh1=head1 COPYRIGHT
1505759b3d2Safresh1
151256a93a4Safresh1Copyright 2020 Chad Granum E<lt>exodist@cpan.orgE<gt>.
1525759b3d2Safresh1
1535759b3d2Safresh1This program is free software; you can redistribute it and/or
1545759b3d2Safresh1modify it under the same terms as Perl itself.
1555759b3d2Safresh1
156*5486feefSafresh1See L<https://dev.perl.org/licenses/>
1575759b3d2Safresh1
1585759b3d2Safresh1=cut
159