HTMLMatrix.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Model/Subtest/Visual.pm ↑ Summary Model/Colorful.pm »
1 #!/usr/bin/perl
2  
3 package Test::TAP::HTMLMatrix;
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶use Test::TAP::Model::Visual;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Test::TAP::Model::Consolidated;
10 use Petal;
11 use Petal::Utils qw/:logic/;
12 use Carp qw/croak/;
13 use File::Spec;
14 use File::Path;
15 use URI::file;
16  
17 use overload '""' => "html";
18  
19 our $VERSION = "0.09";
20  
21 sub new {
22 my ( $pkg, @models ) = @_;
23  
24 my $ext = pop @models unless eval { $models[-1]->isa("Test::TAP::Model") };
25  
26 @models || croak "must supply a model to graph";
27  
28 my $self = bless {}, $pkg;
29  
30 $self->model(@models);
31 $self->extra($ext);
32
33 $self;
34 }
35  
36 sub title { "TAP Matrix - " . gmtime() . " GMT" }
37  
38 sub tests {
39 my $self = shift;
40 [ sort { $a->name cmp $b->name } $self->model->test_files ];
41 }
42  
43 sub model {
44 my $self = shift;
45 if (@_) {
46 $self->{model} = $_[0]->isa("Test::TAP::Model::Consolidated")
47 ? shift
48 : Test::TAP::Model::Consolidated->new(@_);
49 }
50  
51 $self->{model};
52 }
53  
54 sub extra {
55 my $self = shift;
56 $self->{extra} = shift if @_;
57 $self->{extra};
58 }
59  
60 sub petal {
61 my $self = shift;
62 my $file = shift;
63  
64 Petal->new(
65 file => File::Spec->abs2rel($file), # damn petal requires rel path
66 input => "XHTML",
67 output => "XHTML",
68  
69 encode_charset => "utf8",
70 decode_charset => "utf8",
71 );
72 }
73  
74 sub html {
75 my $self = shift;
76 $self->detail_html;
77 }
78  
79 sub detail_html {
80 my $self = shift;
81 $self->template_to_html($self->detail_template);
82 }
83  
84 sub summary_html {
85 my $self = shift;
86 $self->template_to_html($self->summary_template);
87 }
88  
89 sub output_dir {
90 my $self = shift;
91 my $dir = shift || die "You need to specify a directory";
92  
93 mkpath $dir or die "can't create directory '$dir': $!" unless -e $dir;
94  
95 local $self->{_css_uri} = "htmlmatrix.css";
96  
97 my %outputs = (
98 "summary.html" => $self->summary_html,
99 "detail.html" => $self->detail_html,
100 ( $self->has_inline_css ? () : "htmlmatrix.css" => $self->_slurp_css ),
101 );
102  
103 foreach my $file ( keys %outputs ) {
104 open my $fh, ">", File::Spec->catfile( $dir, $file ) or die "can't open '$file' for writing: $!";
105 print $fh $outputs{$file};
106 close $fh or die "can't close '$file'";
107 }
108 }
109  
110 sub template_to_html {
111 my $self = shift;
112 my $path = shift;
113 $self->process_petal($self->petal($path));
114 }
115  
116 sub process_petal {
117 my $self = shift;
118 my $petal = shift;
119 $petal->process(page => $self);
120 }
121  
122 sub _find_in_INC {
123 my $self = shift;
124 my $file = shift;
125  
126 foreach my $str (grep { not ref } @INC){
127 my $target = File::Spec->catfile($str, $file);
128 return $target if -e $target;
129 }
130  
131 die "couldn't find $file in \@INC";
132 }
133  
134 sub _find_in_my_INC {
135 my $self = shift;
136 $self->_find_in_INC(File::Spec->catfile(split("::", __PACKAGE__), shift));
137 }
138  
139 sub detail_template {
140 my $self = shift;
141 $self->_find_in_my_INC("detailed_view.html");
142 }
143  
144 sub summary_template {
145 my $self = shift;
146 $self->_find_in_my_INC("summary_view.html");
147 }
148  
149 sub css_file {
150 my $self = shift;
151 $self->_find_in_my_INC("htmlmatrix.css");
152 }
153  
154 sub css_uri {
155 my $self = shift;
156 $self->{_css_uri} || URI::file->new($self->css_file);
157 }
158  
159 sub has_inline_css {
160 my $self = shift;
161 $self->{has_inline_css} = shift if @_;
162 $self->{has_inline_css};
163 }
164  
165 sub no_javascript {
166 my $self = shift;
167 $self->{no_javascript} = shift if @_;
168 $self->{no_javascript};
169 }
170  
171 sub has_javascript {
172 my $self = shift;
173 !$self->no_javascript;
174 }
175  
176 sub _slurp_css {
177 my $self = shift;
178 local $/;
179 open my $fh, $self->css_file or die "open: " . $self->css_file. ": $!";
180 <$fh>;
181 }
182  
183 sub inline_css {
184 my $self = shift;
185 "\n<!--\n" . $self->_slurp_css . "-->/\n";
186 }
187  
188 __PACKAGE__
189  
190 __END__
« Model/Subtest/Visual.pm ↑ Summary Model/Colorful.pm »