blib/lib/Test/BDD/Cucumber/Harness/Html.pm

filter: Statementstmt 33.0% 35 / 106 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 28.0% 7 / 25 Podpod 41.6% 5 / 12 totaltotal 28.1% 47 / 167 CC 13 SCAR 23.6
CC
13
Cov
27%
__ANON__
30.0
format_step
30.0
format_result
12.0
CRAP 10.6
↑ Summary
1 package Test::BDD::Cucumber::Harness::Html;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: html output for Test::BDD::Cucumber
6 1 our $VERSION = '1.006'; # VERSION
7  
8  
9 1 ▶use Time::HiRes qw ( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 extends 'Test::BDD::Cucumber::Harness::Data';
18  
19  
20 0 ▶has 'fh' => ( is => 'rw', isa => 'FileHandle', default => sub { \*STDOUT } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
21  
22  
23 0 ▶has all_features => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
24 1 has current_feature => ( is => 'rw', isa => 'HashRef' );
25 1 has current_scenario => ( is => 'rw', isa => 'HashRef' );
26 1 has step_start_at => ( is => 'rw', isa => 'Num' );
27  
28 has 'template' => ( is => 'ro', isa => 'Template', lazy => 1,
29 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 0 my $self = shift;
31 0 return Template->new(
32 ABSOLUTE => 1,
33 EVAL_PERL => 1,
34 );
35 },
36 1 );
37  
38  
39 1 has 'template_file' => ( is => 'rw', isa => 'Maybe[Str]' );
40  
41  
42 has 'template_content' => ( is => 'rw', isa => 'Str',
43 0 ▶ default => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
44 0 my $self = shift;
45 0 my $c = '';
46 0 my $h;
47 0 ▶ if( defined $self->template_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->template_file) { } 0 0 0
48 0 ▶ $h = IO::File->new($self->template_file, 'r')
Branch: 1 branch
BranchTrueFalseTotal
$h = "IO::File"->new($self->template_file, "r") or die 'error opening output template: ' . $! 0 0 0
49 or die('error opening output template: '.$!);
50 } else {
51 0 ▶ $h = IO::Handle->new_from_fd(*DATA,'r')
Branch: 1 branch
BranchTrueFalseTotal
$h = "IO::Handle"->new_from_fd(*DATA, "r") or die 'error reading default template from __DATA__: ' . $! 0 0 0
52 or die('error reading default template from __DATA__: '.$!);
53 }
54 0 ▶ while ( my $line = $h->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $h->getline) 0 0 0
55 0 $c .= $line;
56 }
57 0 $h->close;
58 0 return( $c );
59 },
60 1 );
61  
62  
63 1 has title => ( is => 'rw', isa => 'Str', default => "Test Report");
64  
65 has 'statistic' => ( is => 'ro', isa => 'HashRef', lazy => 1,
66 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 my $self = shift;
68 return {
69 0 map { $_ => 0 } values %{$self->_output_status},
70 };
71 },
72 1 );
73  
74 0 ▶sub feature {
Subroutine feature: not called CC=1 SCAR=6.9
Pod documented
75 0 my ( $self, $feature ) = @_;
76 0 $self->current_feature( $self->format_feature($feature) );
77 0 push @{ $self->all_features }, $self->current_feature;
78 }
79  
80 0 ▶sub scenario {
Subroutine scenario: not called CC=1 SCAR=6.9
Pod documented
81 0 my ( $self, $scenario, $dataset ) = @_;
82 0 $self->current_scenario( $self->format_scenario($scenario) );
83 0 push @{ $self->current_feature->{elements} }, $self->current_scenario;
84 }
85  
86 0 ▶sub step {
Subroutine step: not called CC=1 SCAR=6.9
Pod documented
87 0 my ( $self, $context ) = @_;
88 0 $self->step_start_at( time() );
89 }
90  
91 0 ▶sub step_done {
Subroutine step_done: not called CC=1 SCAR=6.9
Pod documented
92 0 my ( $self, $context, $result ) = @_;
93 0 my $duration = time() - $self->step_start_at;
94 0 my $step_data = $self->format_step( $context, $result, $duration );
95 0 my $status = $step_data->{'result'}->{'status'};
96  
97 0 $self->current_feature->{'statistic'}->{ $status }++;
98 0 $self->current_scenario->{'statistic'}->{ $status }++;
99 0 $self->statistic->{ $status }++;
100  
101 0 push @{ $self->current_scenario->{steps} }, $step_data;
102 }
103  
104 0 ▶sub shutdown {
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod documented
105 0 my ($self) = @_;
106 0 my $html;
107 0 my $template = $self->template_content;
108 0 my $vars = {
109 'all_features' => $self->all_features,
110 'statistic' => $self->statistic,
111 'title' => $self->title,
112 'time' => Time::Piece->new(),
113 'hostname' => hostname(),
114 'command' => join(' ', $0, @ARGV),
115 };
116 0 ▶ $self->template->process( \$template, $vars, $self->fh )
Branch: 1 branch
BranchTrueFalseTotal
$self->template->process(\$template, $vars, $self->fh) or die $self->template->error 0 0 0
117 or die $self->template->error;
118 }
119  
120 0 ▶sub get_keyword {
Subroutine get_keyword: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my ( $self, $line_ref ) = @_;
122 0 my ($keyword) = $line_ref->content =~ /^(\w+)/;
123 0 return $keyword;
124 }
125  
126 0 ▶sub format_tags {
Subroutine format_tags: not called CC=1 SCAR=6.9
Pod undocumented
127 0 my ( $self, $tags_ref ) = @_;
128 0 return [ map { { name => '@' . $_ } } @$tags_ref ];
129 }
130  
131 0 ▶sub format_description {
Subroutine format_description: not called CC=1 SCAR=6.9
Pod undocumented
132 0 my ( $self, $feature ) = @_;
133 0 return join "\n", map { $_->content } @{ $feature->satisfaction };
134 }
135  
136 0 ▶sub format_feature {
Subroutine format_feature: not called CC=1 SCAR=6.9
Pod undocumented
137 0 my ( $self, $feature ) = @_;
138 return {
139 uri => $feature->name_line->filename,
140 keyword => $self->get_keyword( $feature->name_line ),
141 id => "feature-" . int($feature),
142 name => $feature->name,
143 line => $feature->name_line->number,
144 description => $self->format_description($feature),
145 tags => $self->format_tags( $feature->tags ),
146 elements => [],
147 0 statistic => { map { $_ => 0 } values %{$self->_output_status} },
148 };
149 }
150  
151 0 ▶sub format_scenario {
Subroutine format_scenario: not called CC=2 SCAR=17.9
Pod undocumented
152 0 my ( $self, $scenario, $dataset ) = @_;
153 return {
154 keyword => $self->get_keyword( $scenario->line ),
155 id => "scenario-" . int($scenario),
156 name => $scenario->name,
157 line => $scenario->line->number,
158 tags => $self->format_tags( $scenario->tags ),
159 type => $scenario->background ? 'background' : 'scenario',
160 steps => [],
161 0 ▶ statistic => { map { $_ => 0 } values %{$self->_output_status} },
Branch: 1 branch
BranchTrueFalseTotal
$scenario->background ? : 0 0 0
162 };
163 }
164  
165 0 ▶sub format_step {
Subroutine format_step: not called CC=5 SCAR=34.0
Pod undocumented
166 0 my ( $self, $step_context, $result, $duration ) = @_;
167 0 my $step = $step_context->step;
168 0 my $rand = int( rand() * 10000000);
169 return {
170 0 ▶ keyword => $step ? $step->verb_original : $step_context->verb,
Branch: 4 branches
BranchTrueFalseTotal
$step ? : 0 0 0
ref $step_context->data ? : 0 0 0
ref $step_context->data eq 'ARRAY' ? : 0 0 0
$step ? : 0 0 0
171 keyword_en => $step_context->verb,
172 id => "step-".$rand,
173 name => $step_context->text,
174 data_text => ref($step_context->data) ? undef : $step_context->data,
175 data_table => ref($step_context->data) eq 'ARRAY' ? $step_context->data : undef,
176 background => $step_context->background,
177 line => $step ? $step->line->number : 0,
178 result => $self->format_result( $result, $duration )
179 };
180 }
181  
182 has '_output_status' => ( is => 'ro', isa => 'HashRef', lazy => 1,
183 0 ▶ default => sub { {
Subroutine __ANON__: not called CC=1 SCAR=6.9
184 0 passing => 'passed',
185 failing => 'failed',
186 pending => 'pending',
187 undefined => 'skipped',
188 } },
189 1 );
190  
191 0 ▶sub format_result {
Subroutine format_result: not called CC=3 SCAR=24.8
Pod undocumented
192 0 my ( $self, $result, $duration ) = @_;
193 0 my $ret;
194  
195 0 ▶ if( $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 0 0 0
196 $ret = {
197 0 ▶ status => $self->_output_status->{ $result->result },
Branch: 1 branch
BranchTrueFalseTotal
defined $duration ? : 0 0 0
198 error_message => $result->output,
199 defined $duration
200 ? ( duration => int( $duration * 1_000_000_000 ) )
201 : (), # nanoseconds
202 };
203 } else {
204 0 $ret = { status => "undefined" };
205 }
206  
207 0 return $ret;
208 }
209  
210 1 1;
211  
212 =pod
213  
214 =encoding UTF-8
215  
216 =head1 NAME
217  
218 Test::BDD::Cucumber::Harness::Html - html output for Test::BDD::Cucumber
219  
220 =head1 VERSION
221  
222 version 1.006
223  
224 =head1 DESCRIPTION
225  
226 A L<Test::BDD::Cucumber::Harness> subclass that generates html reports.
227  
228 =head1 EXAMPLE USAGE
229  
230 $ pherkin -o Html features/ > test-report.html
231  
232 =head1 EXAMPLE OUTPUT
233  
234 The default template uses bootstrap CSS for formatting.
235  
236 Example test reports from the L<Test::BDD::Cucumber> examples
237 are included in the distribution tar ball or can be viewed online at:
238  
239 =over
240  
241 =item Calculator example
242  
243 L<https://markusbenning.de/cucumber-html-examples/calculator-report.html>
244  
245 =item Digest example
246  
247 L<https://markusbenning.de/cucumber-html-examples/digest-report.html>
248  
249 =back
250  
251 =head1 HOW IT WORKS
252  
253 All report data is gathered and stored in $self->all_features.
254  
255 For HTML generation a L<Template> style template file is used.
256  
257 A templated based on bootstrap formating is included in the DATA section of the module
258 an will be used by default.
259  
260 =head1 CONFIGURABLE ATTRIBUTES
261  
262 =head2 fh
263  
264 A filehandle to write output to; defaults to C<STDOUT>
265  
266 =head2 all_features
267  
268 An Array holding a data structure with the results.
269  
270 =head2 template_file (default: undef)
271  
272 A path to a Template Toolkit template file to use for generating the HTML report.
273  
274 If no path is given the content will be read from the DATA section of the module containing
275 the default template.
276  
277 =head2 template_content (default: undef)
278  
279 The source code of the Template Toolkit template.
280  
281 If no content is given it will be read from a file or the DATA section. (see template_file)
282  
283 =head2 title (default: Test Report)
284  
285 This could be used to set a title for the generated report.
286  
287 =head1 AUTHOR
288  
289 Markus Benning <ich@markusbenning.de>
290  
291 =head1 COPYRIGHT AND LICENSE
292  
293 This software is copyright (c) 2014 by Markus Benning.
294  
295 This is free software; you can redistribute it and/or modify it under
296 the same terms as the Perl 5 programming language system itself.
297  
298 =cut
299  
300 __DATA__
↑ Summary