TagExpressions.pm

filter: Statementstmt 91.1% 72 / 79 Branchbran 86.0% 43 / 50 Conditioncond 65.5% 19 / 29 MC/DCmcdc 50.0% 8 / 16 Subroutinesub 90.0% 9 / 10 Podpod 100% 1 / 1 totaltotal 82.1% 152 / 185 CC 36 SCAR 22.7
CC
36
Cov
85%
_get_token
15.0
_term_expr
11.5
_expr
8.0
CRAP 9.7
↑ Summary TagExpressions/Node.pm »
1  
2 package Cucumber::TagExpressions;
3 2 $Cucumber::TagExpressions::VERSION = '11.2.0';
4 =head1 NAME
5  
6 Cucumber::TagExpressions - Tag expression parser
7  
8 =head1 SYNOPSIS
9  
10 use Cucumber::TagExpressions;
11  
12 my $expr = Cucumber::TagExpressions->parse( '@a and @b' );
13 if ( $expr->evaluate( qw/x y z/ ) ) {
14 say "The evaluation returned false";
15 }
16  
17  
18 =head1 DESCRIPTION
19  
20 Cucumber tag expressions allow users to define the subset of Gherkin
21 scenarios they want to run. This library parses the expression and
22 returns an evaluator object which can be used to test the tags specified
23 on a scenario against the filter expression.
24  
25 =head1 CLASS METHODS
26  
27 =cut
28  
29 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 ▶use Cucumber::TagExpressions::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 0 ▶sub _expect_token {
Subroutine _expect_token: not called CC=2 SCAR=17.9
35 0 my ( $state, $token ) = @_;
36  
37 0 my $actual = _get_token( $state );
38 0 ▶ die "Expecting token '$token' but found '$actual'"
Branch: 1 branch
BranchTrueFalseTotal
if $token ne $actual 0 0 0
39 if $token ne $actual;
40 }
41  
42 237 ▶sub _consume_char {
Subroutine _consume_char: called CC=3 SCAR=11.3
43 237 my ( $state, $allow_eof ) = @_;
44  
45 237 ▶ if ( length($state->{text}) <= $state->{pos} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $state->{"text"} <= $state->{"pos"}) 43 194 237
46 43 ▶ return if $allow_eof;
Branch: 1 branch
BranchTrueFalseTotal
if $allow_eof 43 0 43
47 0 die "Unexpected end of string parsing tag expression: $state->{text}";
48 }
49 194 return substr( $state->{text}, $state->{pos}++, 1 );
50 }
51  
52 103 ▶sub _get_token {
Subroutine _get_token: called CC=15 SCAR=27.1
53 103 my ( $state ) = @_;
54  
55 103 ▶ return delete $state->{saved_token} if defined $state->{saved_token};
Branch: 1 branch
BranchTrueFalseTotal
if defined $state->{"saved_token"} 9 94 103
56  
57 94 my $token = '';
58 94 while (1) {
59 233 my $char = _consume_char( $state, 1 );
60 233 ▶ return ($token ? $token : undef)
Branch: 2 branches
BranchTrueFalseTotal
unless defined $char 42 191 233
$token ? : 14 28 42
61 if not defined $char;
62  
63 191 ▶ if ( $char =~ m/\s/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($char =~ /\s/) { } 42 149 191
elsif ($char eq '(' or $char eq ')') { } 10 139 149
64 42 ▶ if ( $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token) { } 41 1 42
65 41 return $token;
66 }
67 else {
68 1 next;
69 }
70 }
71 ▶ elsif ( $char eq '(' or $char eq ')' ) {
Condition100%3/3
$char eq "(" or $char eq ")"
l!l&&r!l&&!r
55139
MC/DC100%2/2
$char eq "(" or $char eq ")"
$char eq "(" $char eq ")"
Truth table100%3/3
A or B
A= $char eq "("B= $char eq ")"
ABresult
1X1
011
000
72 10 ▶ if ( $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token) { } 4 6 10
73 4 _save_token( $state, $char );
74 4 return $token;
75 }
76 else {
77 6 return $char;
78 }
79 }
80 139 ▶ if ( $char eq "\\" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq '\\') { } 4 135 139
81 4 ▶ $char = _consume_char( $state, 1 ) // '<end-of-input>';
Condition100%2/2
_consume_char($state, 1) // "<end-of-input>"
l!l
31
MC/DC100%1/1
_consume_char($state, 1) // "<end-of-input>"
_consume_char($state, 1)
Truth table100%2/2
A
A= _consume_char($state, 1)
Aresult
11
00
82 4 ▶ if ( $char eq '(' or $char eq ')'
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq '(' or $char eq ')' or $char eq '\\' or $char =~ /\s/) { } 3 1 4
Condition66%6/9
$char eq "(" or $char eq ")" or $char eq "\\" or $char =~ /\s/
l!l&&r!l&&!r
211
$char eq "(" or $char eq ")" or $char eq "\\"
l!l&&r!l&&!r
022
$char eq "(" or $char eq ")"
l!l&&r!l&&!r
004
MC/DC50%2/4
$char eq "(" or $char eq ")" or $char eq "\\" or $char =~ /\s/
$char eq "(" $char eq ")" $char eq "\\" $char =~ /\s/
Truth table60%3/5
A or B or C or D
A= $char eq "("B= $char eq ")"C= $char eq "\\"D= $char =~ /\s/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
83 or $char eq "\\" or $char =~ /\s/ ) {
84 3 $token .= $char;
85 }
86 else {
87 1 die qq{Tag expression "$state->{text}" could not be parsed because of syntax error: Illegal escape before "$char".};
88 }
89 }
90 else {
91 135 $token .= $char;
92 }
93 }
94 }
95  
96 9 ▶sub _save_token {
Subroutine _save_token: called CC=1 SCAR=0.0
97 9 my ( $state, $token ) = @_;
98  
99 9 $state->{saved_token} = $token;
100 }
101  
102 47 ▶sub _term_expr {
Subroutine _term_expr: called CC=9 SCAR=24.4
103 47 my ( $state ) = @_;
104  
105 47 my $token = _get_token( $state );
106  
107 46 ▶ die qq{Tag expression "$state->{text}" could not be parsed because of syntax error: Expected operand.}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 2 44 46
108 if not defined $token;
109  
110 44 ▶ if ( $token eq '(' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token eq '(') { } 5 39 44
elsif ($token eq 'not') { } 5 34 39
111 5 my $expr = _expr( $state );
112 4 my $token = _get_token( $state );
113  
114 4 ▶ if ( not $token or $token ne ')' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $token or $token ne ")") 0 4 4
Condition33%1/3
not $token or $token ne ")"
l!l&&r!l&&!r
004
MC/DC0%0/2
not $token or $token ne ")"
not $token $token ne ")"
Truth table33%1/3
not(A) or B
A= not $tokenB= $token ne ")"
ABresult
1X1
011
000
115 0 die qq{Tag expression "$state->{text}" could not be parsed because of syntax error: Unmatched (.}
116 }
117  
118 4 return $expr;
119 }
120 elsif ( $token eq 'not' ) {
121 5 return Cucumber::TagExpressions::NotNode->new(
122 expression => _term_expr( $state )
123 );
124 }
125 else {
126 34 ▶ if ( $token eq 'and' or $token eq 'or' or $token eq 'not' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq "and" or $token eq "or" or $token eq "not") 0 34 34
Condition33%2/6
$token eq "and" or $token eq "or" or $token eq "not"
l!l&&r!l&&!r
0034
$token eq "and" or $token eq "or"
l!l&&r!l&&!r
0034
MC/DC0%0/3
$token eq "and" or $token eq "or" or $token eq "not"
$token eq "and" $token eq "or" $token eq "not"
Truth table25%1/4
A or B or C
A= $token eq "and"B= $token eq "or"C= $token eq "not"
ABCresult
1XX1
01X1
0011
0000
127 0 die qq{Tag expression "$state->{text}" could not be parsed because of syntax error: Expected operand."};
128 }
129 34 return Cucumber::TagExpressions::LiteralNode->new( tag => $token );
130 }
131 }
132  
133 25 ▶sub _expr {
Subroutine _expr: called CC=8 SCAR=20.8
134 25 my ( $state ) = @_;
135  
136 25 my @terms = ( _term_expr( $state ) );
137 23 ▶ while ( my $token = _get_token( $state ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = _get_token($state)) 25 13 38
138 25 ▶ if ( not defined $token or $token eq ')' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $token or $token eq ")") 5 20 25
Condition66%2/3
not defined $token or $token eq ")"
l!l&&r!l&&!r
0520
MC/DC50%1/2
not defined $token or $token eq ")"
not defined $token $token eq ")"
Truth table66%2/3
not(A) or B
A= not defined $tokenB= $token eq ")"
ABresult
1X1
011
000
139 5 _save_token( $state, $token );
140 5 last;
141 }
142 20 ▶ if ( not ( $token eq 'or'
Branch: 1 branch
BranchTrueFalseTotal
unless ($token eq "or" or $token eq "and") 3 17 20
Condition100%3/3
$token eq "or" or $token eq "and"
l!l&&r!l&&!r
1073
MC/DC100%2/2
$token eq "or" or $token eq "and"
$token eq "or" $token eq "and"
Truth table100%3/3
A or B
A= $token eq "or"B= $token eq "and"
ABresult
1X1
011
000
143 or $token eq 'and' ) ) {
144 3 die qq{Tag expression "$state->{text}" could not be parsed because of syntax error: Expected operator.}
145 }
146  
147 17 my $term = _term_expr( $state );
148 15 ▶ if ( $token eq 'and' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq 'and') { } 7 8 15
149 # immediately combine _and_ terms
150 7 push @terms,
151 Cucumber::TagExpressions::AndNode->new(
152 terms => [ pop(@terms), $term ]
153 );
154 }
155 else {
156 # collect _or_ terms
157 8 push @terms, $term;
158 }
159 }
160  
161 18 ▶ if ( scalar(@terms) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @terms > 1) 8 10 18
162 8 return Cucumber::TagExpressions::OrNode->new(
163 terms => \@terms
164 );
165 }
166 # don't wrap a single-term expression in an Or node
167 10 return $terms[0];
168 }
169  
170 =head2 $class->parse( $expression )
171  
172 Parses the string specified in C<$expression> and returns a
173 L<Cucumber::TagExpressions::ExpressionNode> instance.
174  
175 =cut
176  
177 20 ▶sub parse {
Subroutine parse: called CC=4 SCAR=14.1
Pod documented
178 20 my ( $class, $text ) = @_;
179  
180 20 ▶ return Cucumber::TagExpressions::ExpressionNode->new(
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^\s*$/ 0 20 20
181 sub_expression => undef
182 )
183 if $text =~ /^\s*$/; # match the empty string or space-only string as "constant true"
184 20 my $state = { pos => 0, text => $text, saved_token => undef };
185 20 my $expr = _expr( $state );
186  
187 14 my $token = _get_token( $state );
188  
189 14 ▶ if ( defined $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) 1 13 14
190 1 ▶ if ( $token eq ')' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq ")") 1 0 1
191 1 die qq{Tag expression "$state->{text}" could not be parsed because of syntax error: Unmatched ).};
192 }
193  
194 0 die "Junk at end of expression: $token";
195 }
196  
197 13 return Cucumber::TagExpressions::ExpressionNode->new(
198 sub_expression => $expr
199 );
200 }
201  
202 2 1;
203  
204 __END__
↑ Summary TagExpressions/Node.pm »