| 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
| |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | die 'Map result must be a hash reference' unless ref($map) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | my @keys = sort keys %$map; | |||||||||||||||||||||||||||
| 23 | 4 | ▶ | return '<table class="hash_table"/>' unless @keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$text =~ /$HTML_CELL/ or $text =~ /\A\[.*\]\z/s
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
| |||||||||||||||||||||||||||||
| 43 | 17 | $text =~ s/&/&/g; | |||||||||||||||||||||||||||
| 44 | 17 | $text =~ s/</</g; | |||||||||||||||||||||||||||
| 45 | 17 | $text =~ s/>/>/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
| |||||||||||||||||||||||||||||
| 52 | 21 | ▶ | return "$value" unless ref $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | return convert(__PACKAGE__, $value) if ref($value) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | return '[' . join(', ', map _value_to_string($_), @$value) . ']' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | if ref($value) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | if (blessed($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | return $value->to_string if $value->can('to_string'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | return $value->toString if $value->can('toString'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||