| 1 | 3 | ▶ | use v5.14; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | |||||||||||||||
| 4 | package Test::BDD::Cucumber::Harness::JSON 0.87; | ||||||||||||||
| 5 | |||||||||||||||
| 6 | =head1 NAME | ||||||||||||||
| 7 | |||||||||||||||
| 8 | Test::BDD::Cucumber::Harness::JSON - Generate results to JSON file | ||||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||
| 11 | |||||||||||||||
| 12 | version 0.87 | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||
| 15 | |||||||||||||||
| 16 | A L<Test::BDD::Cucumber::Harness> subclass that generates JSON output file. | ||||||||||||||
| 17 | |||||||||||||||
| 18 | So that it is possible use tools like | ||||||||||||||
| 19 | L<"Publish pretty cucumber reports"|https://github.com/masterthought/cucumber-reporting>. | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =cut | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 3 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 3 | ▶ | use Types::Standard qw( Num HashRef ArrayRef FileHandle ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 3 | ▶ | use JSON::MaybeXS; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 3 | ▶ | use Time::HiRes qw ( time ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | 3 | extends 'Test::BDD::Cucumber::Harness::Data'; | |||||||||||||
| 29 | |||||||||||||||
| 30 | =head1 CONFIGURABLE ATTRIBUTES | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =head2 fh | ||||||||||||||
| 33 | |||||||||||||||
| 34 | A filehandle to write output to; defaults to C<STDOUT> | ||||||||||||||
| 35 | |||||||||||||||
| 36 | =cut | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 6 | ▶ | has 'fh' => ( is => 'rw', isa => FileHandle, default => sub { \*STDOUT } ); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | |||||||||||||||
| 40 | =head2 json_args | ||||||||||||||
| 41 | |||||||||||||||
| 42 | List of options to be passed to L<JSON::MaybeXS>'s C<new()> method | ||||||||||||||
| 43 | |||||||||||||||
| 44 | =cut | ||||||||||||||
| 45 | |||||||||||||||
| 46 | has json_args => ( | ||||||||||||||
| 47 | is => 'ro', | ||||||||||||||
| 48 | isa => HashRef, | ||||||||||||||
| 49 | 7 | ▶ | default => sub { { utf8 => 1, pretty => 1 } } | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 50 | 3 | ); | |||||||||||||
| 51 | |||||||||||||||
| 52 | # | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 8 | ▶ | has all_features => ( is => 'ro', isa => ArrayRef, default => sub { [] } ); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 55 | 3 | has current_feature => ( is => 'rw', isa => HashRef ); | |||||||||||||
| 56 | 3 | has current_scenario => ( is => 'rw', isa => HashRef ); | |||||||||||||
| 57 | 3 | has step_start_at => ( is => 'rw', isa => Num ); | |||||||||||||
| 58 | |||||||||||||||
| 59 | 8 | ▶ | sub feature { | ||||||||||||
|
Subroutine
feature: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 60 | 8 | my ( $self, $feature ) = @_; | |||||||||||||
| 61 | 8 | $self->current_feature( $self->format_feature($feature) ); | |||||||||||||
| 62 | 8 | push @{ $self->all_features }, $self->current_feature; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 24 | ▶ | sub scenario { | ||||||||||||
|
Subroutine
scenario: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 66 | 24 | my ( $self, $scenario, $dataset ) = @_; | |||||||||||||
| 67 | 24 | $self->current_scenario( $self->format_scenario($scenario) ); | |||||||||||||
| 68 | 24 | push @{ $self->current_feature->{elements} }, $self->current_scenario; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 24 | ▶ | sub scenario_done { | ||||||||||||
|
Subroutine
scenario_done: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 72 | 24 | my $self = shift; | |||||||||||||
| 73 | 24 | $self->current_scenario( {} ); | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 74 | ▶ | sub step { | ||||||||||||
|
Subroutine
step: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 77 | 74 | my ( $self, $context ) = @_; | |||||||||||||
| 78 | 74 | $self->step_start_at( time() ); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 74 | ▶ | sub step_done { | ||||||||||||
|
Subroutine
step_done: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 82 | 74 | my ( $self, $context, $result ) = @_; | |||||||||||||
| 83 | 74 | my $duration = time() - $self->step_start_at; | |||||||||||||
| 84 | 74 | my $step_data = $self->format_step( $context, $result, $duration ); | |||||||||||||
| 85 | 74 | push @{ $self->current_scenario->{steps} }, $step_data; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 6 | ▶ | sub shutdown { | ||||||||||||
|
Subroutine
shutdown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 89 | 6 | my ($self) = @_; | |||||||||||||
| 90 | 6 | my $json = JSON::MaybeXS->new( %{ $self->json_args } ); | |||||||||||||
| 91 | 6 | my $fh = $self->fh; | |||||||||||||
| 92 | 6 | print $fh $json->encode( $self->all_features ); | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | ################################## | ||||||||||||||
| 96 | ### Internal formating methods ### | ||||||||||||||
| 97 | ################################## | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 32 | ▶ | sub format_tags { | ||||||||||||
|
Subroutine
format_tags: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 100 | 32 | my ( $self, $tags_ref ) = @_; | |||||||||||||
| 101 | 32 | return [ map { { name => $_ } } @$tags_ref ]; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 32 | ▶ | sub format_description { | ||||||||||||
|
Subroutine
format_description: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 105 | 32 | my ( $self, $description ) = @_; | |||||||||||||
| 106 | 32 | return join "\n", map { $_->content } @{ $description }; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | 8 | ▶ | sub format_feature { | ||||||||||||
|
Subroutine
format_feature: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 110 | 8 | my ( $self, $feature ) = @_; | |||||||||||||
| 111 | return { | ||||||||||||||
| 112 | 8 | uri => $feature->name_line->filename, | |||||||||||||
| 113 | keyword => $feature->keyword_original, | ||||||||||||||
| 114 | id => $self->_generate_stable_id( $feature->name_line ), | ||||||||||||||
| 115 | name => $feature->name, | ||||||||||||||
| 116 | line => $feature->name_line->number, | ||||||||||||||
| 117 | description => $self->format_description($feature->satisfaction), | ||||||||||||||
| 118 | tags => $self->format_tags( $feature->tags ), | ||||||||||||||
| 119 | elements => [] | ||||||||||||||
| 120 | }; | ||||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 24 | ▶ | sub format_scenario { | ||||||||||||
|
Subroutine
format_scenario: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||
| 124 | 24 | my ( $self, $scenario, $dataset ) = @_; | |||||||||||||
| 125 | return { | ||||||||||||||
| 126 | 24 | ▶ | keyword => $scenario->keyword_original, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | id => $self->_generate_stable_id( $scenario->line ), | ||||||||||||||
| 128 | name => $scenario->name, | ||||||||||||||
| 129 | line => $scenario->line->number, | ||||||||||||||
| 130 | description => $self->format_description($scenario->description), | ||||||||||||||
| 131 | tags => $self->format_tags( $scenario->tags ), | ||||||||||||||
| 132 | type => $scenario->background ? 'background' : 'scenario', | ||||||||||||||
| 133 | steps => [] | ||||||||||||||
| 134 | }; | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 32 | ▶ | sub _generate_stable_id { | ||||||||||||
|
Subroutine
_generate_stable_id: called CC=1 SCAR=0.0
| |||||||||||||||
| 138 | 32 | my ( $self, $line ) = @_; | |||||||||||||
| 139 | 32 | return $line->filename . ":" . $line->number; | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 74 | ▶ | sub format_step { | ||||||||||||
|
Subroutine
format_step: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||
| 143 | 74 | my ( $self, $step_context, $result, $duration ) = @_; | |||||||||||||
| 144 | 74 | my $step = $step_context->step; | |||||||||||||
| 145 | return { | ||||||||||||||
| 146 | 74 | ▶ | keyword => $step ? $step->verb_original : $step_context->verb, | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 147 | name => $step_context->text, | ||||||||||||||
| 148 | line => $step ? $step->line->number : 0, | ||||||||||||||
| 149 | result => $self->format_result( $result, $duration ) | ||||||||||||||
| 150 | }; | ||||||||||||||
| 151 | } | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 3 | my %OUTPUT_STATUS = ( | |||||||||||||
| 154 | passing => 'passed', | ||||||||||||||
| 155 | failing => 'failed', | ||||||||||||||
| 156 | pending => 'pending', | ||||||||||||||
| 157 | undefined => 'skipped', | ||||||||||||||
| 158 | ); | ||||||||||||||
| 159 | |||||||||||||||
| 160 | 74 | ▶ | sub format_result { | ||||||||||||
|
Subroutine
format_result: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||
| 161 | 74 | my ( $self, $result, $duration ) = @_; | |||||||||||||
| 162 | 74 | ▶ | return { status => "undefined" } if not $result; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | return { | ||||||||||||||
| 164 | 74 | ▶ | status => $OUTPUT_STATUS{ $result->result }, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | error_message => $result->output, | ||||||||||||||
| 166 | defined $duration | ||||||||||||||
| 167 | ? ( duration => int( $duration * 1_000_000_000 ) ) | ||||||||||||||
| 168 | : (), # nanoseconds | ||||||||||||||
| 169 | }; | ||||||||||||||
| 170 | } | ||||||||||||||
| 171 | |||||||||||||||
| 172 | =head1 SEE ALSO | ||||||||||||||
| 173 | |||||||||||||||
| 174 | L<https://github.com/masterthought/cucumber-reporting> | ||||||||||||||
| 175 | |||||||||||||||
| 176 | L<http://cucumber-reporting.masterthought.net> | ||||||||||||||
| 177 | |||||||||||||||
| 178 | L<https://www.relishapp.com/cucumber/cucumber/docs/json-output-formatter> | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =cut | ||||||||||||||
| 181 | |||||||||||||||
| 182 | 3 | 1; | |||||||||||||