Gherkin/AstBuilder.pm

filter: Statementstmt 54.8% 96 / 175 Branchbran 38.4% 20 / 52 Conditioncond 35.2% 6 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 88.0% 22 / 25 Podpod 0.0% 0 / 17 totaltotal 48.6% 144 / 296 CC 39 SCAR 40.8
CC
39
Cov
50%
transform_node
139.6
ensure_cell_count
30.0
get_tags
10.2
CRAP 59.4
« Gherkin/Generated/Parser.pm ↑ Summary Gherkin.pm »
1 package Gherkin::AstBuilder;
2 1 $Gherkin::AstBuilder::VERSION = '42.0.1';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Cucumber::Messages;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Gherkin::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Gherkin::AstNode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
13 3 my $class = shift;
14 3 my ($id_generator) = @_;
15  
16 3 my $id_counter = 0;
17 my $self = bless {
18 stack => [],
19 comments => [],
20 18 ▶ id_generator => $id_generator // sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 18 return $id_counter++;
22 },
23 3 ▶ uri => '',
Condition33%1/3
$id_generator // sub { return $id_counter++; }
l!l&&r!l&&!r
030
MC/DC0%0/2
$id_generator // sub { return $id_counter++; }
$id_generator sub { return $id_counter++; }
Truth table33%1/3
A // B
A= $id_generatorB= sub { return $id_counter++; }
ABresult
1X1
011
000
24 }, $class;
25 3 $self->reset;
26 3 return $self;
27 }
28  
29 # Simple builder sugar
30 39 ▶sub ast_node { Gherkin::AstNode->new( $_[0] ) }
Subroutine ast_node: called CC=1 SCAR=0.0
Pod undocumented
31  
32 6 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
33 6 my $self = shift;
34 6 my ($uri) = @_;
35 6 $self->{'stack'} = [ ast_node('None') ];
36 6 $self->{'comments'} = [];
37 6 $self->{'uri'} = $uri;
38 }
39  
40 72 ▶sub current_node {
Subroutine current_node: called CC=1 SCAR=0.0
Pod undocumented
41 72 my $self = shift;
42 72 return $self->{'stack'}->[-1];
43 }
44  
45 33 ▶sub start_rule {
Subroutine start_rule: called CC=1 SCAR=0.0
Pod undocumented
46 33 my ( $self, $rule_type ) = @_;
47 33 push( @{ $self->{'stack'} }, ast_node($rule_type) );
48 }
49  
50 33 ▶sub end_rule {
Subroutine end_rule: called CC=1 SCAR=0.0
Pod undocumented
51 33 my ( $self, $rule_type ) = @_;
52 33 my $node = pop( @{ $self->{'stack'} } );
53 33 $self->current_node->add( $node->rule_type, $self->transform_node($node) );
54 }
55  
56 36 ▶sub build {
Subroutine build: called CC=2 SCAR=8.4
Pod undocumented
57 36 my ( $self, $token ) = @_;
58 36 ▶ if ( $token->matched_type eq 'Comment' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->matched_type eq 'Comment') { } 0 36 36
59 0 push @{ $self->{'comments'} },
60 Cucumber::Messages::Comment->new(
61 location => $self->get_location($token),
62 text => $token->matched_text
63 );
64 } else {
65 36 $self->current_node->add( $token->matched_type, $token );
66 }
67 }
68  
69 3 ▶sub get_result {
Subroutine get_result: called CC=1 SCAR=0.0
Pod undocumented
70 3 my $self = shift;
71 3 return $self->current_node->get_single('GherkinDocument');
72 }
73  
74 21 ▶sub get_location {
Subroutine get_location: called CC=3 SCAR=11.6
Pod undocumented
75 21 my ( $self, $token, $column ) = @_;
76  
77 1 ▶ use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 21 ▶ confess "What no token?" unless $token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 0 21 21
79  
80 return Cucumber::Messages::Location->new(
81 line => $token->location->{'line'},
82 21 ▶ column => $column // $token->location->{'column'}
Condition33%1/3
$column // $token->location->{"column"}
l!l&&r!l&&!r
0210
MC/DC0%0/2
$column // $token->location->{"column"}
$column $token->location->{"column"}
Truth table33%1/3
A // B
A= $columnB= $token->location->{"column"}
ABresult
1X1
011
000
83 );
84 }
85  
86 9 ▶sub get_tags {
Subroutine get_tags: called CC=4 SCAR=23.2
Pod undocumented
87 9 my ( $self, $node ) = @_;
88  
89 9 ▶ my $tags_node = $node->get_single('Tags') || return [];
Condition50%1/2
$node->get_single('Tags') || (return [])
l!l
09
MC/DC0%0/1
$node->get_single('Tags') || (return [])
$node->get_single('Tags')
Truth table50%1/2
A
A= $node->get_single('Tags')
Aresult
11
00
90 0 my @tags;
91  
92 0 for my $token ( @{ $tags_node->get_tokens('TagLine') } ) {
93 0 for my $item ( @{ $token->matched_items } ) {
94 push @tags,
95 Cucumber::Messages::Tag->new(
96 id => $self->next_id,
97 location => $self->get_location( $token, $item->{'column'} ),
98 0 name => $item->{'text'}
99 );
100 }
101 }
102  
103 0 return \@tags;
104 }
105  
106 0 ▶sub get_table_rows {
Subroutine get_table_rows: not called CC=2 SCAR=17.9
Pod undocumented
107 0 my ( $self, $node ) = @_;
108 0 my @rows;
109  
110 0 for my $token ( @{ $node->get_tokens('TableRow') } ) {
111 0 push @rows, Cucumber::Messages::TableRow->new(
112 id => $self->next_id,
113 location => $self->get_location($token),
114 cells => $self->get_cells($token)
115 );
116 }
117  
118 0 $self->ensure_cell_count( \@rows );
119 0 return \@rows;
120 }
121  
122 0 ▶sub ensure_cell_count {
Subroutine ensure_cell_count: not called CC=5 SCAR=34.0
Pod undocumented
123 0 my ( $self, $rows ) = @_;
124 0 ▶ return unless @{$rows};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rows;} 0 0 0
125  
126 0 my $cell_count;
127  
128 0 for my $row (@{$rows}) {
129 0 my $this_row_count = @{ $row->cells };
130 0 ▶ $cell_count = $this_row_count unless defined $cell_count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cell_count 0 0 0
131 0 ▶ unless ( $cell_count == $this_row_count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cell_count == $this_row_count) 0 0 0
132 0 Gherkin::Exceptions::AstBuilder->throw(
133 "inconsistent cell count within the table",
134 $row->location );
135 }
136 }
137 }
138  
139 0 ▶sub get_cells {
Subroutine get_cells: not called CC=2 SCAR=17.9
Pod undocumented
140 0 my ( $self, $table_row_token ) = @_;
141 0 my @cells;
142 0 for my $cell_item ( @{ $table_row_token->matched_items } ) {
143 push @cells,
144 Cucumber::Messages::TableCell->new(
145 location => $self->get_location(
146 $table_row_token, $cell_item->{'column'}
147 ),
148 0 value => $cell_item->{'text'}
149 );
150 }
151  
152 0 return \@cells;
153 }
154  
155 12 ▶sub get_description { return ($_[1]->get_single('Description') || '') }
Subroutine get_description: called CC=2 SCAR=7.6
Pod undocumented
Condition50%1/2
$_[1]->get_single('Description') || ''
l!l
012
MC/DC0%0/1
$_[1]->get_single('Description') || ''
$_[1]->get_single('Description')
Truth table50%1/2
A
A= $_[1]->get_single('Description')
Aresult
11
00
156 9 ▶sub get_steps { return $_[1]->get_items('Step') }
Subroutine get_steps: called CC=1 SCAR=0.0
Pod undocumented
157  
158 18 ▶sub next_id {
Subroutine next_id: called CC=1 SCAR=0.0
Pod undocumented
159 18 my $self = shift;
160 18 return $self->{'id_generator'}->();
161 }
162  
163 ## no critic (ProhibitExcessComplexity, ProhibitCascadingIfElse)
164 33 ▶sub transform_node {
Subroutine transform_node: called CC=25 SCAR=49.4
Pod undocumented
165 33 my ( $self, $node ) = @_;
166  
167 33 ▶ if ( $node->rule_type eq 'Step' ) {
Branch: 10 branches
BranchTrueFalseTotal
if ($node->rule_type eq 'Step') { } 9 24 33
elsif ($node->rule_type eq 'DocString') { } 0 24 24
elsif ($node->rule_type eq 'DataTable') { } 0 24 24
elsif ($node->rule_type eq 'Background') { } 3 21 24
elsif ($node->rule_type eq 'ScenarioDefinition') { } 6 15 21
elsif ($node->rule_type eq 'Rule') { } 0 15 15
elsif ($node->rule_type eq 'ExamplesDefinition') { } 0 15 15
elsif ($node->rule_type eq 'Description') { } 0 15 15
elsif ($node->rule_type eq 'Feature') { } 3 12 15
elsif ($node->rule_type eq 'GherkinDocument') { } 3 9 12
168 9 my $step_line = $node->get_token('StepLine');
169 9 ▶ my $data_table = $node->get_single('DataTable') || undef;
Condition50%1/2
$node->get_single('DataTable') || undef
l!l
09
MC/DC0%0/1
$node->get_single('DataTable') || undef
$node->get_single('DataTable')
Truth table50%1/2
A
A= $node->get_single('DataTable')
Aresult
11
00
170 9 ▶ my $doc_string = $node->get_single('DocString') || undef;
Condition50%1/2
$node->get_single('DocString') || undef
l!l
09
MC/DC0%0/1
$node->get_single('DocString') || undef
$node->get_single('DocString')
Truth table50%1/2
A
A= $node->get_single('DocString')
Aresult
11
00
171  
172 9 return Cucumber::Messages::Step->new(
173 id => $self->next_id,
174 location => $self->get_location($step_line),
175 keyword => $step_line->matched_keyword,
176 keyword_type => $step_line->matched_keyword_type,
177 text => $step_line->matched_text,
178 doc_string => $doc_string,
179 data_table => $data_table,
180 );
181 } elsif ( $node->rule_type eq 'DocString' ) {
182 0 my $separator_token = $node->get_tokens('DocStringSeparator')->[0];
183 0 my $media_type = $separator_token->matched_text;
184 0 my $delimiter = $separator_token->matched_keyword;
185 0 my $line_tokens = $node->get_tokens('Other');
186 0 my $content = join( "\n", map { $_->matched_text } @{$line_tokens} );
187  
188 0 ▶ return Cucumber::Messages::DocString->new(
Branch: 1 branch
BranchTrueFalseTotal
$media_type eq '' ? : 0 0 0
189 location => $self->get_location($separator_token),
190 content => $content,
191 media_type => ($media_type eq '' ) ? undef : $media_type,
192 delimiter => $delimiter
193 );
194 } elsif ( $node->rule_type eq 'DataTable' ) {
195 0 my $rows = $self->get_table_rows($node);
196 return Cucumber::Messages::DataTable->new(
197 0 location => $rows->[0]->{'location'},
198 rows => $rows
199 );
200 } elsif ( $node->rule_type eq 'Background' ) {
201 3 my $background_line = $node->get_token('BackgroundLine');
202 3 my $description = $self->get_description($node);
203 3 my $steps = $self->get_steps($node);
204  
205 3 return Cucumber::Messages::Background->new(
206 id => $self->next_id,
207 location => $self->get_location($background_line),
208 keyword => $background_line->matched_keyword,
209 name => $background_line->matched_text,
210 description => $description,
211 steps => $steps
212 );
213 } elsif ( $node->rule_type eq 'ScenarioDefinition' ) {
214 6 my $tags = $self->get_tags($node);
215 6 my $scenario_node = $node->get_single('Scenario');
216 6 my $scenario_line = $scenario_node->get_token('ScenarioLine');
217 6 my $description = $self->get_description($scenario_node);
218 6 my $steps = $self->get_steps($scenario_node);
219 6 my $examples = $scenario_node->get_items('ExamplesDefinition');
220  
221 6 return Cucumber::Messages::Scenario->new(
222 id => $self->next_id,
223 tags => $tags,
224 location => $self->get_location($scenario_line),
225 keyword => $scenario_line->matched_keyword,
226 name => $scenario_line->matched_text,
227 description => $description,
228 steps => $steps,
229 examples => $examples
230 );
231 } elsif ( $node->rule_type eq 'Rule' ) {
232 0 my $header = $node->get_single('RuleHeader');
233 0 ▶ unless ($header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header) 0 0 0
234 0 warn "Missing RuleHeader!";
235 0 return;
236 }
237 0 my $rule_line = $header->get_token('RuleLine');
238 0 ▶ unless ($rule_line) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule_line) 0 0 0
239 0 warn "Missing RuleLine";
240 0 return;
241 }
242 0 my $tags = $self->get_tags($header);
243  
244 0 my @children;
245 0 my $background = $node->get_single('Background');
246 0 ▶ if ( $background ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($background) 0 0 0
247 0 push @children,
248 Cucumber::Messages::RuleChild->new(
249 background => $background
250 );
251 }
252 push @children, (
253 map {
254 0 Cucumber::Messages::RuleChild->new(
255 scenario => $_
256 )
257 0 } @{ $node->get_items('ScenarioDefinition') }
258 );
259  
260 0 my $description = $self->get_description($header);
261  
262 0 return Cucumber::Messages::Rule->new(
263 id => $self->next_id,
264 tags => $tags,
265 location => $self->get_location($rule_line),
266 keyword => $rule_line->matched_keyword,
267 name => $rule_line->matched_text,
268 description => $description,
269 children => \@children
270 );
271 } elsif ( $node->rule_type eq 'ExamplesDefinition' ) {
272 0 my $examples_node = $node->get_single('Examples');
273 0 my $examples_line = $examples_node->get_token('ExamplesLine');
274 0 my $description = $self->get_description($examples_node);
275 0 my $examples_table = $examples_node->get_single('ExamplesTable');
276 0 ▶ my $rows =
Branch: 1 branch
BranchTrueFalseTotal
$examples_table ? : 0 0 0
277 $examples_table ? $self->get_table_rows($examples_table) : undef;
278 0 my $tags = $self->get_tags($node);
279  
280 return Cucumber::Messages::Examples->new(
281 id => $self->next_id,
282 tags => $tags,
283 location => $self->get_location($examples_line),
284 keyword => $examples_line->matched_keyword,
285 name => $examples_line->matched_text,
286 description => $description,
287 0 ▶ table_header => ($rows ? shift @{ $rows } : undef),
Branch: 2 branches
BranchTrueFalseTotal
$rows ? : 0 0 0
$rows ? : 0 0 0
288 table_body => ($rows ? $rows : [])
289 );
290 } elsif ( $node->rule_type eq 'Description' ) {
291 0 my @description = @{ $node->get_tokens('Other') };
292  
293 # Trim trailing empty lines
294 pop @description
295 0 ▶ while ( @description && !$description[-1]->matched_text );
Branch: 1 branch
BranchTrueFalseTotal
@description and not $description[-1]->matched_text and do { pop @description } 0 0 0
Condition0%0/3
@description and not $description[-1]->matched_text
!ll&&!rl&&r
000
MC/DC0%0/2
@description and not $description[-1]->matched_text
@description not $description[-1]->matched_text
Truth table0%0/3
A and not(B)
A= @descriptionB= not $description[-1]->matched_text
ABresult
0X0
100
111
296  
297 0 return join "\n", map { $_->matched_text } @description;
298 } elsif ( $node->rule_type eq 'Feature' ) {
299 3 my $header = $node->get_single('FeatureHeader');
300 3 ▶ unless ($header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header) 0 3 3
301 0 warn "Missing FeatureHeader!";
302 0 return;
303 }
304 3 my $feature_line = $header->get_token('FeatureLine');
305 3 ▶ unless ($feature_line) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($feature_line) 0 3 3
306 0 warn "Missing FeatureLine";
307 0 return;
308 }
309 3 my $tags = $self->get_tags($header);
310  
311 3 my @children;
312 3 my $background = $node->get_single('Background');
313 3 ▶ if ( $background ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($background) 3 0 3
314 3 push @children,
315 Cucumber::Messages::FeatureChild->new(
316 background => $background
317 );
318 }
319 push @children,
320 map {
321 6 Cucumber::Messages::FeatureChild->new(
322 scenario => $_
323 )
324 3 } @{ $node->get_items('ScenarioDefinition') };
325 push @children,
326 map {
327 0 Cucumber::Messages::FeatureChild->new(
328 rule => $_
329 )
330 3 } @{ $node->get_items('Rule') };
331  
332 3 my $description = $self->get_description($header);
333 3 my $language = $feature_line->matched_gherkin_dialect;
334  
335 3 return Cucumber::Messages::Feature->new(
336 tags => $tags,
337 location => $self->get_location($feature_line),
338 language => $language,
339 keyword => $feature_line->matched_keyword,
340 name => $feature_line->matched_text,
341 description => $description,
342 children => \@children
343 );
344 } elsif ( $node->rule_type eq 'GherkinDocument' ) {
345 3 my $feature = $node->get_single('Feature');
346  
347 return Cucumber::Messages::Envelope->new(
348 gherkin_document => Cucumber::Messages::GherkinDocument->new(
349 uri => $self->{'uri'},
350 feature => $feature,
351 3 comments => $self->{'comments'},
352 ));
353 } else {
354 9 return $node;
355 }
356 }
357 ## use critic
358  
359 1 1;
« Gherkin/Generated/Parser.pm ↑ Summary Gherkin.pm »