xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/lib/Test2/EventFacet/Info/Table.pm (revision 58fbf5d6aa35e3d66f2c32c61d2f38824a990e85)
1package Test2::EventFacet::Info::Table;
2use strict;
3use warnings;
4
5use Carp qw/confess/;
6
7use Test2::Util::HashBase qw{-header -rows -collapse -no_collapse -as_string};
8
9sub init {
10    my $self = shift;
11
12    confess "Table may not be empty" unless ref($self->{+ROWS}) eq 'ARRAY' && @{$self->{+ROWS}};
13
14    $self->{+AS_STRING} ||= '<TABLE NOT DISPLAYED>';
15}
16
17sub as_hash { my $out = +{%{$_[0]}}; delete $out->{as_string}; $out }
18
19sub info_args {
20    my $self = shift;
21
22    my $hash = $self->as_hash;
23    my $desc = $self->as_string;
24
25    return (table => $hash, details => $desc);
26}
27
281;
29
30__END__
31
32=pod
33
34=encoding UTF-8
35
36=head1 NAME
37
38Test2::EventFacet::Info::Table - Intermediary representation of a table.
39
40=head1 DESCRIPTION
41
42Intermediary representation of a table for use in specialized
43L<Test::API::Context> methods which generate L<Test2::EventFacet::Info> facets.
44
45=head1 SYNOPSIS
46
47    use Test2::EventFacet::Info::Table;
48    use Test2::API qw/context/;
49
50    sub my_tool {
51        my $ctx = context();
52
53        ...
54
55        $ctx->fail(
56            $name,
57            "failure diag message",
58            Test2::EventFacet::Info::Table->new(
59                # Required
60                rows => [['a', 'b'], ['c', 'd'], ...],
61
62                # Strongly Recommended
63                as_string => "... string to print when table cannot be rendered ...",
64
65                # Optional
66                header => ['col1', 'col2'],
67                collapse => $bool,
68                no_collapse => ['col1', ...],
69            ),
70        );
71
72        ...
73
74        $ctx->release;
75    }
76
77    my_tool();
78
79=head1 ATTRIBUTES
80
81=over 4
82
83=item $header_aref = $t->header()
84
85=item $rows_aref = $t->rows()
86
87=item $bool = $t->collapse()
88
89=item $aref = $t->no_collapse()
90
91The above are all directly tied to the table hashref structure described in
92L<Test2::EventFacet::Info>.
93
94=item $str = $t->as_string()
95
96This returns the string form of the table if it was set, otherwise it returns
97the string C<< "<TABLE NOT DISPLAYED>" >>.
98
99=item $href = $t->as_hash()
100
101This returns the data structure used for tables by L<Test2::EventFacet::Info>.
102
103=item %args = $t->info_args()
104
105This returns the arguments that should be used to construct the proper
106L<Test2::EventFacet::Info> structure.
107
108    return (table => $t->as_hash(), details => $t->as_string());
109
110=back
111
112=head1 SOURCE
113
114The source code repository for Test2 can be found at
115F<http://github.com/Test-More/test-more/>.
116
117=head1 MAINTAINERS
118
119=over 4
120
121=item Chad Granum E<lt>exodist@cpan.orgE<gt>
122
123=back
124
125=head1 AUTHORS
126
127=over 4
128
129=item Chad Granum E<lt>exodist@cpan.orgE<gt>
130
131=back
132
133=head1 COPYRIGHT
134
135Copyright 2019 Chad Granum E<lt>exodist@cpan.orgE<gt>.
136
137This program is free software; you can redistribute it and/or
138modify it under the same terms as Perl itself.
139
140See F<http://dev.perl.org/licenses/>
141
142=cut
143