lib/Test/FitNesse/Slim/MapResult.pm

filter: Statementstmt 91.8% 34 / 37 Branchbran 68.1% 15 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 85.9% 61 / 71 CC 13 SCAR 17.7
CC
13
Cov
84%
_value_to_string
9.6
convert
4.2
_cell
3.0
CRAP 5.9
« lib/Test/FitNesse/Slim/Statement.pm ↑ Summary lib/Test/FitNesse/Slim/ListExecutor.pm »
1 package Test::FitNesse::Slim::MapResult;
2  
3 83 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 83 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 83 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 83 my $HTML_CELL = qr{
8 \A <
9 (p|hr|pre|ul|ol|dl|div|h[1-6]|hgroup|address|blockquote|ins|del|object|map|
10 video|audio|figure|table|fieldset|canvas|a|em|strong|small|mark|abbr|dfn|
11 i|b|s|u|code|var|samp|kbd|sup|sub|q|cite|span|br|img|embed|label|output|
12 datalist|progress|command|time|meter)
13 (?: [ >].* </\1> | [^>]* /> )
14 \z
15 }isx;
16  
17 4 ▶sub convert {
Subroutine convert: called CC=4 SCAR=14.2
Pod documented
18 4 my($class,$map) = @_;
19 4 ▶ return 'null' unless defined $map;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $map 0 4 4
20 4 ▶ die 'Map result must be a hash reference' unless ref($map) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $map eq "HASH" 0 4 4
21  
22 4 my @keys = sort keys %$map;
23 4 ▶ return '<table class="hash_table"/>' unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 4 4
24  
25 my @rows = map {
26 4 "\t<tr class=\"hash_row\">\n"
27 . "\t\t<td class=\"hash_key\">" . _cell($_) . "</td>\n"
28 10 . "\t\t<td class=\"hash_value\">" . _cell($map->{$_}) . "</td>\n"
29 . "\t</tr>"
30 } @keys;
31  
32 4 '<table class="hash_table">' . "\n"
33 . join("\n", @rows) . "\n"
34 . '</table>';
35 }
36  
37 20 ▶sub _cell {
Subroutine _cell: called CC=3 SCAR=11.0
38 20 my($value) = @_;
39 20 my $text = _value_to_string($value);
40 20 $text =~ s/\A[\x00-\x20]+|[\x00-\x20]+\z//g;
41  
42 20 ▶ return $text if $text =~ $HTML_CELL || $text =~ /\A\[.*\]\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /$HTML_CELL/ or $text =~ /\A\[.*\]\z/s 3 17 20
Condition100%3/3
$text =~ /$HTML_CELL/ or $text =~ /\A\[.*\]\z/s
l!l&&r!l&&!r
2117
MC/DC100%2/2
$text =~ /$HTML_CELL/ or $text =~ /\A\[.*\]\z/s
$text =~ /$HTML_CELL/ $text =~ /\A\[.*\]\z/s
Truth table100%3/3
A or B
A= $text =~ /$HTML_CELL/B= $text =~ /\A\[.*\]\z/s
ABresult
1X1
011
000
43 17 $text =~ s/&/&amp;/g;
44 17 $text =~ s/</&lt;/g;
45 17 $text =~ s/>/&gt;/g;
46 17 return $text;
47 }
48  
49 23 ▶sub _value_to_string {
Subroutine _value_to_string: called CC=8 SCAR=22.6
50 23 my($value) = @_;
51 23 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 21 23
52 21 ▶ return "$value" unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 18 3 21
53 3 ▶ return convert(__PACKAGE__, $value) if ref($value) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 1 2 3
54 2 ▶ return '[' . join(', ', map _value_to_string($_), @$value) . ']'
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 1 1 2
55 if ref($value) eq 'ARRAY';
56  
57 1 ▶ if (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 1 0 1
58 1 ▶ return $value->to_string if $value->can('to_string');
Branch: 1 branch
BranchTrueFalseTotal
if $value->can("to_string") 1 0 1
59 0 ▶ return $value->toString if $value->can('toString');
Branch: 1 branch
BranchTrueFalseTotal
if $value->can("toString") 0 0 0
60 0 die 'Cannot convert object map value without an explicit string method: ' . blessed($value);
61 }
62  
63 0 die 'Cannot convert reference map value of type ' . ref($value);
64 }
65  
66 83 1;
67  
68 =head1 NAME
69  
70 Test::FitNesse::Slim::MapResult - Convert fixture maps to FitNesse table markup
71  
72 =head1 DESCRIPTION
73  
74 FitNesse's Java map converter renders map results as HTML tables. This
75 module provides the corresponding result conversion for Perl hash
76 references. Keys are sorted for deterministic output. Scalar cell text is
77 escaped, while recognized HTML and serialized list markup are preserved.
78 Nested maps and arrays are formatted recursively. Unsupported reference
79 types fail instead of being globally stringified.
80  
81 =head1 METHODS
82  
83 =head2 convert
84  
85 my $html = Test::FitNesse::Slim::MapResult->convert(\%map);
86  
87 Return FitNesse-compatible table markup. An undefined map becomes the
88 protocol string C<null>; an empty map becomes an empty table. A non-hash
89 argument or an unsupported reference value throws an error for the caller
90 to format as a SLiM exception.
91  
92 =cut
« lib/Test/FitNesse/Slim/Statement.pm ↑ Summary lib/Test/FitNesse/Slim/ListExecutor.pm »