File Coverage

blib/lib/Test2/EventFacet/Info/Table.pm
Criterion Covered Total %
statement 24 24 100.0
branch 1 2 50.0
condition 3 5 60.0
subroutine 7 7 100.0
pod 2 3 66.6
total 37 41 90.2


line stmt bran cond sub pod time code
1             package Test2::EventFacet::Info::Table;
2 1     1   783 use strict;
  1         3  
  1         34  
3 1     1   7 use warnings;
  1         2  
  1         52  
4              
5             our $VERSION = '1.302181';
6              
7 1     1   9 use Carp qw/confess/;
  1         2  
  1         77  
8              
9 1     1   7 use Test2::Util::HashBase qw{-header -rows -collapse -no_collapse -as_string};
  1         2  
  1         10  
10              
11             sub init {
12 2     2 0 4 my $self = shift;
13              
14 2 50 33     10 confess "Table may not be empty" unless ref($self->{+ROWS}) eq 'ARRAY' && @{$self->{+ROWS}};
  2         7  
15              
16 2   100     8 $self->{+AS_STRING} ||= '<TABLE NOT DISPLAYED>';
17             }
18              
19 2     2 1 12 sub as_hash { my $out = +{%{$_[0]}}; delete $out->{as_string}; $out }
  2         10  
  2         5  
  2         3  
20              
21             sub info_args {
22 2     2 1 2 my $self = shift;
23              
24 2         5 my $hash = $self->as_hash;
25 2         6 my $desc = $self->as_string;
26              
27 2         11 return (table => $hash, details => $desc);
28             }
29              
30             1;
31              
32             __END__
33              
34             =pod
35              
36             =encoding UTF-8
37              
38             =head1 NAME
39              
40             Test2::EventFacet::Info::Table - Intermediary representation of a table.
41              
42             =head1 DESCRIPTION
43              
44             Intermediary representation of a table for use in specialized
45             L<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              
93             The above are all directly tied to the table hashref structure described in
94             L<Test2::EventFacet::Info>.
95              
96             =item $str = $t->as_string()
97              
98             This returns the string form of the table if it was set, otherwise it returns
99             the string C<< "<TABLE NOT DISPLAYED>" >>.
100              
101             =item $href = $t->as_hash()
102              
103             This returns the data structure used for tables by L<Test2::EventFacet::Info>.
104              
105             =item %args = $t->info_args()
106              
107             This returns the arguments that should be used to construct the proper
108             L<Test2::EventFacet::Info> structure.
109              
110             return (table => $t->as_hash(), details => $t->as_string());
111              
112             =back
113              
114             =head1 SOURCE
115              
116             The source code repository for Test2 can be found at
117             F<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              
137             Copyright 2020 Chad Granum E<lt>exodist@cpan.orgE<gt>.
138              
139             This program is free software; you can redistribute it and/or
140             modify it under the same terms as Perl itself.
141              
142             See F<http://dev.perl.org/licenses/>
143              
144             =cut