Gherkin/Pickles/Compiler.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 257 CC 22 SCAR 35.0
CC
22
Cov
0%
_pickle_step_props
90.0
_compile_feature
20.0
_compile_scenario_outline
20.0
CRAP 33.0
1 package Gherkin::Pickles::Compiler;
2 $Gherkin::Pickles::Compiler::VERSION = '42.0.1';
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  
209 my $data_table_argument_index = undef;
210 my $doc_string_argument_index = undef;
211 if ($step->data_table && $step->doc_string) {
212 my $table_first = $step->doc_string->location->line > $step->data_table->location->line;
213 $data_table_argument_index = $table_first ? 1 : 2;
214 $doc_string_argument_index = $table_first ? 2 : 1;
215 }
216  
217 my $data_table;
218 if (my $data = $step->data_table) {
219 $data_table = Cucumber::Messages::PickleTable->new(
220 argument_index => $data_table_argument_index,
221 rows => [
222 map {
223 my $row = $_;
224 Cucumber::Messages::PickleTableRow->new(
225 cells => [
226 map {
227 my $cell = $_;
228 Cucumber::Messages::PickleTableCell->new(
229 value => $class->_interpolate(
230 $cell->value, $variables,
231 $value_cells
232 ))
233 } @{ $row->cells || [] }
234 ]);
235 } @{ $data->rows || [] }
236 ]
237 );
238 }
239  
240 my $doc_string;
241 if (my $docstring = $step->doc_string) {
242 $doc_string = Cucumber::Messages::PickleDocString->new(
243 argument_index => $doc_string_argument_index,
244 media_type => $class->_interpolate( $docstring->media_type,
245 $variables, $value_cells),
246 content => $class->_interpolate( $docstring->content,
247 $variables, $value_cells)
248 );
249 }
250  
251 if ($data_table || $doc_string) {
252 $props{argument} =
253 Cucumber::Messages::PickleStepArgument->new(
254 data_table => $data_table,
255 doc_string => $doc_string,
256 );
257 }
258 return %props; # returns a list
259 }
260  
261 sub _pickle_tags {
262 my ( $class, $tags ) = @_;
263 return [ map { $class->_pickle_tag( $_ ) } @{$tags} ];
264 }
265  
266 sub _pickle_tag {
267 my ( $class, $tag ) = @_;
268 return Cucumber::Messages::PickleTag->new(
269 name => $tag->name,
270 ast_node_id => $tag->id
271 );
272 }
273  
274 1;
275  
276 __END__