Gherkin/Pickles/Compiler.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 215 CC 18 SCAR 27.3
CC
18
Cov
0%
_pickle_step_props
30.0
_compile_feature
20.0
_compile_scenario_outline
20.0
CRAP 15.4
1 package Gherkin::Pickles::Compiler;
2 $Gherkin::Pickles::Compiler::VERSION = '41.0.0';
3 use strict;
4 use warnings;
5 use Scalar::Util qw(reftype);
6  
7 use Cucumber::Messages;
8  
9 sub compile {
10 my ( $class, $envelope, $id_generator, $pickle_sink ) = @_;
11 my @pickles;
12 $pickle_sink ||= sub { push @pickles, $_[0] };
13  
14 my $document = $envelope->gherkin_document;
15 my $feature = $document->feature;
16  
17 return [] if not defined $feature;
18  
19 my $language = $feature->language;
20 my $feature_tags = $feature->tags;
21 my $background_steps = [];
22  
23 $class->_compile_feature(
24 $document->uri, $feature,
25 $language, $id_generator, $pickle_sink);
26  
27 return \@pickles;
28 }
29  
30 sub _compile_feature {
31 my ($class, $uri, $feature,
32 $language, $id_generator, $pickle_sink) = @_;
33 my @feature_background_steps;
34  
35 for my $child ( @{ $feature->children } ) {
36 if ( my $background = $child->background ) {
37 push @feature_background_steps,
38 @{ $background->steps };
39 }
40 elsif ( my $rule = $child->rule ) {
41 $class->_compile_rule(
42 $uri, $feature->tags, \@feature_background_steps,
43 $rule, $language, $id_generator, $pickle_sink);
44 }
45 else {
46 my $scenario = $child->scenario;
47 if (not @{ $scenario->examples }) {
48 $class->_compile_scenario(
49 $uri, $feature->tags, \@feature_background_steps,
50 $scenario, $language, $id_generator, $pickle_sink);
51 }
52 else {
53 $class->_compile_scenario_outline(
54 $uri, $feature->tags, \@feature_background_steps,
55 $scenario, $language, $id_generator, $pickle_sink);
56 }
57 }
58 }
59 }
60  
61 sub _compile_scenario {
62 my ( $class, $uri, $tags, $background_steps,
63 $scenario, $language, $id_generator, $pickle_sink )
64 = @_;
65  
66 my @steps;
67 if ($scenario->steps and @{ $scenario->steps }) {
68 my $last_keyword = Cucumber::Messages::PickleStep::TYPE_UNKNOWN;
69 for my $step (@{ $background_steps }, @{ $scenario->steps } ) {
70 $last_keyword =
71 $step->keyword_type eq Cucumber::Messages::Step::KEYWORDTYPE_CONJUNCTION
72 ? $last_keyword : $step->keyword_type;
73  
74 push @steps,
75 Cucumber::Messages::PickleStep->new(
76 $class->_pickle_step_props($step, [], undef, $last_keyword,
77 $id_generator->()));
78 }
79 }
80  
81 my @all_tags = (@{$tags}, @{ $scenario->tags || [] });
82 $pickle_sink->(
83 Cucumber::Messages::Envelope->new(
84 pickle => Cucumber::Messages::Pickle->new(
85 id => $id_generator->(),
86 name => $scenario->name,
87 language => $language,
88 location => $scenario->location,
89 steps => \@steps,
90 tags => $class->_pickle_tags( \@all_tags ),
91 uri => $uri,
92 ast_node_ids => [ $scenario->id ]
93 )));
94 }
95  
96 sub _compile_scenario_outline {
97 my ( $class, $uri, $feature_tags, $background_steps,
98 $scenario, $language, $id_generator, $pickle_sink )
99 = @_;
100  
101 for my $examples ( grep { $_->table_header } @{ $scenario->examples } ) {
102 my @tags = (
103 @{ $feature_tags },
104 @{ $scenario->tags || [] },
105 @{ $examples->tags || [] },
106 );
107 my $variables = $examples->table_header->cells;
108  
109 for my $values ( @{ $examples->table_body } ) {
110 my $last_keyword;
111 my @steps;
112 if ( @{ $scenario->steps } ) {
113 for my $step (@{ $background_steps }) {
114 $last_keyword =
115 $step->keyword_type eq Cucumber::Messages::Step::KEYWORDTYPE_CONJUNCTION
116 ? $last_keyword : $step->keyword_type;
117 push @steps,
118 Cucumber::Messages::PickleStep->new(
119 $class->_pickle_step_props($step, [], undef,
120 $last_keyword,
121 $id_generator->()));
122 }
123  
124 for my $step (@{ $scenario->steps }) {
125 $last_keyword =
126 $step->keyword_type eq Cucumber::Messages::Step::KEYWORDTYPE_CONJUNCTION
127 ? $last_keyword : $step->keyword_type;
128 push @steps,
129 Cucumber::Messages::PickleStep->new(
130 $class->_pickle_step_props($step, $variables, $values,
131 $last_keyword,
132 $id_generator->()));
133 }
134 }
135 $pickle_sink->(
136 Cucumber::Messages::Envelope->new(
137 pickle => Cucumber::Messages::Pickle->new(
138 id => $id_generator->(),
139 name => $class->_interpolate(
140 $scenario->name,
141 $variables, $values->cells || [] ),
142 language => $language,
143 location => $values->location,
144 steps => \@steps,
145 tags => $class->_pickle_tags( \@tags ),
146 uri => $uri,
147 ast_node_ids => [ $scenario->id, $values->id ]
148 )));
149 }
150 }
151 }
152  
153 sub _compile_rule {
154 my ( $class, $uri, $feature_tags, $feature_background_steps,
155 $rule_definition, $language, $id_generator, $pickle_sink )
156 = @_;
157 my @background_steps = ( @{$feature_background_steps} );
158 my @tags = (
159 @{ $feature_tags || [] },
160 @{ $rule_definition->tags || [] }
161 );
162  
163 for my $child ( @{ $rule_definition->children } ) {
164 if ( my $background = $child->background ) {
165 push @background_steps, @{ $background->steps };
166 } elsif ( my $scenario = $child->scenario ) {
167 if (not @{ $scenario->examples }) {
168 $class->_compile_scenario(
169 $uri, \@tags, \@background_steps, $scenario,
170 $language, $id_generator, $pickle_sink);
171 }
172 else {
173 $class->_compile_scenario_outline(
174 $uri, \@tags, \@background_steps,
175 $scenario, $language, $id_generator, $pickle_sink);
176 }
177 } else {
178 die "Unimplemented";
179 }
180 }
181 }
182  
183 sub _interpolate {
184 my ( $class, $name, $variable_cells, $value_cells ) = @_;
185 my $n = 0;
186 for my $variable_cell ( @{ $variable_cells || [] } ) {
187 my $from = '<' . $variable_cell->value . '>';
188 my $to = $value_cells->[ $n++ ]->value;
189 $name =~ s/$from/$to/g if $name;
190 }
191 return $name;
192 }
193  
194 sub _pickle_step_props {
195 my ($class, $step, $variables, $values, $keyword_type, $id) = @_;
196 my $value_cells = $values ? $values->cells : [];
197 my %props = (
198 id => $id,
199 ast_node_ids => [ $step->id ],
200 type => $keyword_type,
201 text => $class->_interpolate($step->text,
202 $variables, $value_cells),
203 );
204  
205 if ($values) {
206 push @{ $props{ast_node_ids} }, $values->id;
207 }
208 if (my $data = $step->data_table) {
209 $props{argument} =
210 Cucumber::Messages::PickleStepArgument->new(
211 data_table => Cucumber::Messages::PickleTable->new(
212 rows => [
213 map {
214 my $row = $_;
215 Cucumber::Messages::PickleTableRow->new(
216 cells => [
217 map {
218 my $cell = $_;
219 Cucumber::Messages::PickleTableCell->new(
220 value => $class->_interpolate(
221 $cell->value, $variables,
222 $value_cells
223 ))
224 } @{ $row->cells || [] }
225 ]);
226 } @{ $data->rows || [] }
227 ]
228 ));
229 }
230 elsif (my $docstring = $step->doc_string) {
231 $props{argument} =
232 Cucumber::Messages::PickleStepArgument->new(
233 doc_string => Cucumber::Messages::PickleDocString->new(
234 media_type => $class->_interpolate( $docstring->media_type,
235 $variables, $value_cells),
236 content => $class->_interpolate( $docstring->content,
237 $variables, $value_cells)
238 ));
239  
240 }
241 return %props; # returns a list
242 }
243  
244 sub _pickle_tags {
245 my ( $class, $tags ) = @_;
246 return [ map { $class->_pickle_tag( $_ ) } @{$tags} ];
247 }
248  
249 sub _pickle_tag {
250 my ( $class, $tag ) = @_;
251 return Cucumber::Messages::PickleTag->new(
252 name => $tag->name,
253 ast_node_id => $tag->id
254 );
255 }
256  
257 1;
258  
259 __END__