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