Gherkin/TokenMatcher.pm

filter: Statementstmt 77.5% 121 / 156 Branchbran 66.6% 32 / 48 Conditioncond 40.0% 8 / 20 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 93.1% 27 / 29 Podpod 22.2% 4 / 18 totaltotal 67.9% 193 / 284 CC 35 SCAR 14.1
CC
35
Cov
72%
_set_token_matched
8.0
_match_DocStringSeparator
7.4
match_Other
6.0
CRAP 4.1
« Gherkin/TokenFormatterBuilder.pm ↑ Summary Gherkin/Dialect.pm »
1 package Gherkin::TokenMatcher;
2 1 $Gherkin::TokenMatcher::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  
6 1 ▶use List::Util qw(any first reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $LANGUAGE_RE = qr/^\s*#\s*language\s*:\s*([a-zA-Z\-_]+)\s*$/o;
9  
10 1 ▶use Class::XSAccessor accessors => [
Subroutine BEGIN: called CC=1 SCAR=0.0
11 qw/dialect _default_dialect_name _indent_to_remove _active_doc_string_separator _keyword_types _sorted_step_keywords /,
12 1 ];
13  
14 1 ▶use Cucumber::Messages;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Gherkin::Dialect;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
19 3 my ( $class, $options ) = @_;
20 3 ▶ $options->{'dialect'} ||= Gherkin::Dialect->new( { dialect => 'en' } );
Condition33%1/3
$options->{"dialect"} ||= "Gherkin::Dialect"->new({"dialect", "en"})
l!l&&r!l&&!r
030
MC/DC0%0/2
$options->{"dialect"} ||= "Gherkin::Dialect"->new({"dialect", "en"})
$options->{"dialect"} "Gherkin::Dialect"->new({"dialect", "en"})
Truth table33%1/3
A ||= B
A= $options->{"dialect"}B= "Gherkin::Dialect"->new({"dialect", "en"})
ABresult
1X1
011
000
21 3 my $self = bless $options, $class;
22 3 $self->_default_dialect_name( $self->dialect_name );
23 3 $self->reset();
24 3 return $self;
25 }
26  
27 24 ▶sub _add_keyword_type_mappings {
Subroutine _add_keyword_type_mappings: called CC=3 SCAR=11.0
28 24 my ($keyword_types, $keywords, $type) = @_;
29  
30 24 for my $keyword (@{$keywords}) {
31 60 ▶ if (not exists $keyword_types->{$keyword}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $keyword_types->{$keyword}) 36 24 60
32 36 $keyword_types->{$keyword} = [];
33 }
34 60 push @{$keyword_types->{$keyword}}, $type;
35 }
36 }
37  
38 39 ▶sub dialect_name { $_[0]->dialect->dialect }
Subroutine dialect_name: called CC=1 SCAR=0.0
Pod documented
39 6 ▶sub change_dialect {
Subroutine change_dialect: called CC=1 SCAR=0.0
Pod documented
40 6 my $self = shift;
41 6 $self->dialect->change_dialect(@_);
42  
43 6 my $keyword_types = {};
44 6 _add_keyword_type_mappings($keyword_types, $self->dialect->Given,
45 Cucumber::Messages::Step::KEYWORDTYPE_CONTEXT);
46 6 _add_keyword_type_mappings($keyword_types, $self->dialect->When,
47 Cucumber::Messages::Step::KEYWORDTYPE_ACTION);
48 6 _add_keyword_type_mappings($keyword_types, $self->dialect->Then,
49 Cucumber::Messages::Step::KEYWORDTYPE_OUTCOME);
50 _add_keyword_type_mappings($keyword_types,
51 6 [ @{ $self->dialect->And }, @{ $self->dialect->But } ],
52 Cucumber::Messages::Step::KEYWORDTYPE_CONJUNCTION);
53 6 $self->_keyword_types( $keyword_types );
54 $self->_sorted_step_keywords(
55 [ sort {
56 144 length $b <=> length $a # longest keyword first (See #400)
57 } map {
58 6 @{ $self->dialect->$_ }
59 } qw/Given When Then And But/ ]
60 );
61 }
62  
63 6 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
64 6 my $self = shift;
65 6 $self->change_dialect( $self->_default_dialect_name );
66 6 $self->_indent_to_remove(0);
67 6 $self->_active_doc_string_separator(undef);
68  
69 }
70  
71 3 ▶sub match_FeatureLine {
Subroutine match_FeatureLine: called CC=1 SCAR=0.0
Pod undocumented
72 3 my ( $self, $token ) = @_;
73 3 $self->_match_title_line( $token, FeatureLine => $self->dialect->Feature );
74 }
75  
76 9 ▶sub match_RuleLine {
Subroutine match_RuleLine: called CC=1 SCAR=0.0
Pod undocumented
77 9 my ( $self, $token ) = @_;
78 9 $self->_match_title_line( $token,
79 RuleLine => $self->dialect->Rule );
80 }
81  
82 15 ▶sub match_ScenarioLine {
Subroutine match_ScenarioLine: called CC=2 SCAR=6.9
Pod undocumented
83 15 my ( $self, $token ) = @_;
84 15 ▶ $self->_match_title_line(
Branch: 1 branch
BranchTrueFalseTotal
$self->_match_title_line($token, "ScenarioLine", $self->dialect->Scenario) or $self->_match_title_line($token, "ScenarioLine", $self->dialect->ScenarioOutline) 6 9 15
85 $token,
86 ScenarioLine => $self->dialect->Scenario )
87 or $self->_match_title_line(
88 $token,
89 ScenarioLine => $self->dialect->ScenarioOutline );
90 }
91  
92 3 ▶sub match_BackgroundLine {
Subroutine match_BackgroundLine: called CC=1 SCAR=0.0
Pod undocumented
93 3 my ( $self, $token ) = @_;
94 3 $self->_match_title_line( $token,
95 BackgroundLine => $self->dialect->Background );
96 }
97  
98 6 ▶sub match_ExamplesLine {
Subroutine match_ExamplesLine: called CC=1 SCAR=0.0
Pod undocumented
99 6 my ( $self, $token ) = @_;
100 6 $self->_match_title_line( $token,
101 ExamplesLine => $self->dialect->Examples );
102 }
103  
104 3 ▶sub match_Language {
Subroutine match_Language: called CC=3 SCAR=16.8
Pod undocumented
105 3 my ( $self, $token ) = @_;
106 3 ▶ if ( $token->line and $token->line->get_line_text =~ $LANGUAGE_RE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->line and $token->line->get_line_text =~ /$Gherkin::TokenMatcher::LANGUAGE_RE/) { } 0 3 3
Condition33%1/3
$token->line and $token->line->get_line_text =~ /$Gherkin::TokenMatcher::LANGUAGE_RE/
!ll&&!rl&&r
030
MC/DC0%0/2
$token->line and $token->line->get_line_text =~ /$Gherkin::TokenMatcher::LANGUAGE_RE/
$token->line $token->line->get_line_text =~ /$Gherkin::TokenMatcher::LANGUAGE_RE/
Truth table33%1/3
A and B
A= $token->lineB= $token->line->get_line_text =~ /$Gherkin::TokenMatcher::LANGUAGE_RE/
ABresult
0X0
100
111
107 0 my $dialect_name = $1;
108 0 $self->_set_token_matched( $token,
109 Language => { text => $dialect_name } );
110 0 local $@;
111 0 eval { $self->change_dialect( $dialect_name, $token->location ) };
112 0 return (1, $@);
113 } else {
114 3 return;
115 }
116 }
117  
118 39 ▶sub match_TagLine {
Subroutine match_TagLine: called CC=3 SCAR=16.0
Pod undocumented
119 39 my ( $self, $token ) = @_;
120 39 ▶ return unless $token->line and $token->line->startswith('@');
Branch: 1 branch
BranchTrueFalseTotal
unless $token->line and $token->line->startswith("\@") 39 0 39
Condition33%1/3
$token->line and $token->line->startswith("\@")
!ll&&!rl&&r
0390
MC/DC0%0/2
$token->line and $token->line->startswith("\@")
$token->line $token->line->startswith("\@")
Truth table33%1/3
A and B
A= $token->lineB= $token->line->startswith("\@")
ABresult
0X0
100
111
121  
122 0 my ($tags, $err) = $token->line->tags;
123 0 $self->_set_token_matched( $token,
124 TagLine => { items => $tags } );
125 0 return (1, $err);
126 }
127  
128 45 ▶sub _match_title_line {
Subroutine _match_title_line: called CC=4 SCAR=13.9
129 45 my ( $self, $token, $token_type, $keywords ) = @_;
130 45 ▶ return unless $token->line;
Branch: 1 branch
BranchTrueFalseTotal
unless $token->line 0 45 45
131  
132 45 for my $keyword (@{$keywords}) {
133 75 ▶ if ( $token->line->startswith_title_keyword($keyword) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->line->startswith_title_keyword($keyword)) 12 63 75
134 12 my $title =
135 $token->line->get_rest_trimmed( length( $keyword . ': ' ) );
136 12 $self->_set_token_matched( $token, $token_type,
137 { text => $title, keyword => $keyword } );
138 12 return 1;
139 }
140 }
141  
142 33 return;
143 }
144  
145 36 ▶sub _set_token_matched {
Subroutine _set_token_matched: called CC=8 SCAR=20.8
146 36 my ( $self, $token, $matched_type, $options ) = @_;
147 36 ▶ $options->{'items'} ||= [];
Condition50%1/2
$options->{"items"} ||= []
l!l
036
MC/DC0%0/1
$options->{"items"} ||= []
$options->{"items"}
Truth table50%1/2
A
A= $options->{"items"}
Aresult
11
00
148 36 $token->matched_type($matched_type);
149  
150 36 ▶ if ( defined $options->{'text'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"text"}) 21 15 36
151 21 chomp( $options->{'text'} );
152 21 $token->matched_text( $options->{'text'} );
153 }
154  
155 $token->matched_keyword( $options->{'keyword'} )
156 36 ▶ if defined $options->{'keyword'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"keyword"} 21 15 36
157 $token->matched_keyword_type( $options->{'keyword_type'} )
158 36 ▶ if defined $options->{'keyword_type'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"keyword_type"} 9 27 36
159  
160 36 ▶ if ( defined $options->{'indent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{'indent'}) { } 12 24 36
161 12 $token->matched_indent( $options->{'indent'} );
162 } else {
163 24 ▶ $token->matched_indent( $token->line ? $token->line->indent : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$token->line ? : 21 3 24
164 }
165  
166 $token->matched_items( $options->{'items'} )
167 36 ▶ if defined $options->{'items'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"items"} 36 0 36
168  
169 36 $token->location->{'column'} = $token->matched_indent + 1;
170 36 $token->matched_gherkin_dialect( $self->dialect_name );
171 }
172  
173 36 ▶sub match_EOF {
Subroutine match_EOF: called CC=2 SCAR=6.9
Pod undocumented
174 36 my ( $self, $token ) = @_;
175 36 ▶ return unless $token->is_eof;
Branch: 1 branch
BranchTrueFalseTotal
unless $token->is_eof 33 3 36
176 3 $self->_set_token_matched( $token, 'EOF' );
177 3 return 1;
178 }
179  
180 24 ▶sub match_Empty {
Subroutine match_Empty: called CC=3 SCAR=11.0
Pod undocumented
181 24 my ( $self, $token ) = @_;
182 24 ▶ return unless $token->line and $token->line->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $token->line and $token->line->is_empty 12 12 24
Condition66%2/3
$token->line and $token->line->is_empty
!ll&&!rl&&r
01212
MC/DC50%1/2
$token->line and $token->line->is_empty
$token->line $token->line->is_empty
Truth table66%2/3
A and B
A= $token->lineB= $token->line->is_empty
ABresult
0X0
100
111
183 12 $self->_set_token_matched( $token, Empty => { indent => 0 } );
184 12 return 1;
185 }
186  
187 21 ▶sub match_Comment {
Subroutine match_Comment: called CC=3 SCAR=16.7
Pod undocumented
188 21 my ( $self, $token ) = @_;
189 21 ▶ return unless $token->line and $token->line->startswith('#');
Branch: 1 branch
BranchTrueFalseTotal
unless $token->line and $token->line->startswith("#") 21 0 21
Condition33%1/3
$token->line and $token->line->startswith("#")
!ll&&!rl&&r
0210
MC/DC0%0/2
$token->line and $token->line->startswith("#")
$token->line $token->line->startswith("#")
Truth table33%1/3
A and B
A= $token->lineB= $token->line->startswith("#")
ABresult
0X0
100
111
190  
191 0 my $comment_text = $token->line->line_text;
192 0 $comment_text =~ s/\r\n$//; # Why?
193  
194 0 $self->_set_token_matched( $token,
195 Comment => { text => $comment_text, indent => 0 } );
196 0 return 1;
197 }
198  
199 0 ▶sub match_Other {
Subroutine match_Other: not called CC=2 SCAR=17.9
Pod undocumented
200 0 my ( $self, $token ) = @_;
201 0 ▶ return unless $token->line;
Branch: 1 branch
BranchTrueFalseTotal
unless $token->line 0 0 0
202  
203 # take the entire line, except removing DocString indents
204 0 my $text = $token->line->get_line_text( $self->_indent_to_remove );
205 0 $self->_set_token_matched( $token,
206 Other => { indent => 0, text => $self->_unescaped_docstring($text) } );
207 0 return 1;
208 }
209  
210 0 ▶sub _unescaped_docstring {
Subroutine _unescaped_docstring: not called CC=2 SCAR=17.9
211 0 my ( $self, $text ) = @_;
212 0 ▶ if ( $self->_active_doc_string_separator ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_active_doc_string_separator) { } 0 0 0
213 0 $text =~ s!\\"\\"\\"!"""!;
214 0 $text =~ s!\\`\\`\\`!```!;
215 0 return $text;
216 } else {
217 0 return $text;
218 }
219 }
220  
221 24 ▶sub match_StepLine {
Subroutine match_StepLine: called CC=4 SCAR=13.9
Pod undocumented
222 24 my ( $self, $token ) = @_;
223 24 my @keywords = @{ $self->_sorted_step_keywords };
224 24 my $line = $token->line;
225  
226 24 for my $keyword (@keywords) {
227 159 ▶ if ( $line->startswith($keyword) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->startswith($keyword)) 9 150 159
228 9 my $title = $line->get_rest_trimmed( length($keyword) );
229 my $keyword_type =
230 9 (scalar @{$self->_keyword_types->{$keyword}} > 1)
231 ? Cucumber::Messages::Step::KEYWORDTYPE_UNKNOWN
232 9 ▶ : $self->_keyword_types->{$keyword}->[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$self->_keyword_types->{$keyword};} > 1 ? : 0 9 9
233 9 $self->_set_token_matched(
234 $token,
235 StepLine => {
236 text => $title,
237 keyword => $keyword,
238 keyword_type => $keyword_type,
239 } );
240 9 return 1;
241 }
242 }
243 15 return;
244 }
245  
246 15 ▶sub match_DocStringSeparator {
Subroutine match_DocStringSeparator: called CC=4 SCAR=17.2
Pod undocumented
247 15 my ( $self, $token ) = @_;
248 15 ▶ if ($token->is_eof) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is_eof) 0 15 15
249 0 return 0;
250 }
251 15 ▶ if ( !$self->_active_doc_string_separator ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_active_doc_string_separator) { } 15 0 15
252 15 ▶ return $self->_match_DocStringSeparator( $token, '"""', 1 )
Condition33%1/3
$self->_match_DocStringSeparator($token, '"""', 1) || $self->_match_DocStringSeparator($token, '```', 1)
l!l&&r!l&&!r
0150
MC/DC0%0/2
$self->_match_DocStringSeparator($token, '"""', 1) || $self->_match_DocStringSeparator($token, '```', 1)
$self->_match_DocStringSeparator($token, '"""', 1) $self->_match_DocStringSeparator($token, '```', 1)
Truth table33%1/3
A || B
A= $self->_match_DocStringSeparator($token, '"""', 1)B= $self->_match_DocStringSeparator($token, '```', 1)
ABresult
1X1
011
000
253 || $self->_match_DocStringSeparator( $token, '```', 1 );
254 } else {
255 0 return $self->_match_DocStringSeparator( $token,
256 $self->_active_doc_string_separator, 0 );
257 }
258 }
259  
260 30 ▶sub _match_DocStringSeparator {
Subroutine _match_DocStringSeparator: called CC=3 SCAR=20.0
261 30 my ( $self, $token, $separator, $is_open ) = @_;
262 30 ▶ return unless $token->line->startswith($separator);
Branch: 1 branch
BranchTrueFalseTotal
unless $token->line->startswith($separator) 30 0 30
263  
264 0 my $content_type;
265 0 ▶ if ($is_open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_open) { } 0 0 0
266 0 $content_type = $token->line->get_rest_trimmed( length($separator) );
267 0 $self->_active_doc_string_separator($separator);
268 0 $self->_indent_to_remove( $token->line->indent );
269 } else {
270 0 $self->_active_doc_string_separator(undef);
271 0 $self->_indent_to_remove(0);
272 }
273  
274 0 $self->_set_token_matched( $token,
275 DocStringSeparator => { text => $content_type, keyword => $separator } );
276 }
277  
278 15 ▶sub match_TableRow {
Subroutine match_TableRow: called CC=2 SCAR=8.1
Pod undocumented
279 15 my ( $self, $token ) = @_;
280 15 ▶ return unless $token->line->startswith('|');
Branch: 1 branch
BranchTrueFalseTotal
unless $token->line->startswith("|") 15 0 15
281  
282 0 $self->_set_token_matched( $token,
283 TableRow => { items => $token->line->table_cells } );
284 }
285  
286 1 1;
287  
288  
289 __END__
« Gherkin/TokenFormatterBuilder.pm ↑ Summary Gherkin/Dialect.pm »