Gherkin/Parser.pm

filter: Statementstmt 100% 125 / 125 Branchbran 95.0% 19 / 20 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 28 / 28 Podpod 50.0% 1 / 2 totaltotal 97.8% 181 / 185 CC 14 SCAR 11.6
CC
14
Cov
99%
_subtree_collector
9.0
_aggregate_tokens
4.0
_get_executor
3.0
CRAP 3.2
« Gherkin/Node.pm ↑ Summary Gherkin/Node/Scenario.pm »
1 package Test::Pcuke::Gherkin::Parser;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Test::Pcuke::Gherkin::Executor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Test::Pcuke::Gherkin::Node::Feature;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Test::Pcuke::Gherkin::Node::Background;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Test::Pcuke::Gherkin::Node::Scenario;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Test::Pcuke::Gherkin::Node::Outline;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Test::Pcuke::Gherkin::Node::Scenarios;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Test::Pcuke::Gherkin::Node::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Test::Pcuke::Gherkin::Node::Table::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Test::Pcuke::Gherkin::Node::Step;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 Test::Pcuke::Gherkin::Parser - parses tokens
21  
22 =head1 SYNOPSIS
23  
24 TODO SYNOPSIS
25  
26 use Test::Pcuke::Gherkin::Parser;
27  
28 my $tree = Test::Pcuke::Gherkin::Parser->parse( $tokens );
29 ...
30  
31 =head1 METHODS
32  
33 =head2 parse
34  
35 =cut
36  
37 2 my $processors = {
38 root => {
39 'PRAG' => \&_trash_processor,
40 'TAG' => \&_tag_processor,
41 'FEAT' => \&_feature_processor,
42 },
43 feature => {
44 'NARR' => \&_narrative_processor,
45 'BGR' => \&_background_processor,
46 'SCEN' => \&_scenario_processor,
47 'OUTL' => \&_outline_processor,
48 },
49 background => {
50 'STEP' => \&_step_processor,
51 },
52 scenario => {
53 'STEP' => \&_step_processor,
54 },
55 outline => {
56 'STEP' => \&_step_processor,
57 'SCENS' => \&_examples_processor,
58 },
59 example => {
60 'TROW' => \&_table_processor,
61 },
62 step => {
63 'TEXT' => \&_text_processor,
64 'TROW' => \&_table_processor,
65 },
66 };
67  
68 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
69 3 my ($self, $args) = @_;
70 3 my $instance;
71
72 # TODO executor->can(execute) ?
73 3 $instance->{executor} = $args->{executor};
74
75 3 bless $instance, $self;
76 }
77  
78 24 ▶sub _get_executor {
Subroutine _get_executor: called CC=3 SCAR=11.0
79 24 my ($self) = @_;
80
81 return $self->{executor}
82 24 ▶ if ref $self && $self->{executor};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and $self->{"executor"} 12 12 24
Condition66%2/3
ref $self and $self->{"executor"}
!ll&&!rl&&r
01212
MC/DC50%1/2
ref $self and $self->{"executor"}
ref $self $self->{"executor"}
Truth table66%2/3
A and B
A= ref $selfB= $self->{"executor"}
ABresult
0X0
100
111
83
84 12 return Test::Pcuke::Gherkin::Executor->new();
85
86 }
87  
88 2 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
89 2 my ($self, $tokens, $tree) = @_;
90 2 my $level = 'root';
91
92 2 $tree = $self->_subtree_collector($tokens, $tree, $processors->{$level});
93 2 $tree->{feature}->{tags} = $tree->{tags};
94 2 return Test::Pcuke::Gherkin::Node::Feature->new( $tree->{feature} );
95 }
96  
97  
98 ###
99 ### Processor methods
100 ###
101  
102 ## if a processor returns a list ($key, $value) then $tree->{$key} = $value
103 ## if a processir returns arrayref [$key, $value] then push @{ $tree->{$key} }, $value
104 ## if a processor returns ('_trash', '_trash') then skip
105  
106 2 ▶sub _feature_processor {
Subroutine _feature_processor: called CC=1 SCAR=0.0
107 2 my ($self, $tokens) = @_;
108 2 my $token = shift @$tokens;
109 2 my $tree = { title => $token->[1] };
110 2 return ( 'feature', $self->_subtree_collector($tokens, $tree, $processors->{'feature'}) );
111 }
112  
113 2 ▶sub _tag_processor {
Subroutine _tag_processor: called CC=1 SCAR=0.0
114 2 my ($self, $tokens) = @_;
115
116 2 my @tags = map { $_->[1] } $self->_aggregate_tokens('TAG', $tokens);
117
118 2 return ('tags', \@tags );
119 }
120  
121 2 ▶sub _narrative_processor {
Subroutine _narrative_processor: called CC=1 SCAR=0.0
122 2 my ($self, $tokens) = @_;
123
124 2 my @lines = map { $_->[1] } $self->_aggregate_tokens('NARR', $tokens);
125
126 2 return ('narrative', join ( "\n", @lines ) );
127 }
128  
129 4 ▶sub _text_processor {
Subroutine _text_processor: called CC=1 SCAR=0.0
130 4 my ($self, $tokens) = @_;
131
132 4 my @lines = map { $_->[1] } $self->_aggregate_tokens('TEXT', $tokens);
133
134 4 return ('text', join ( "\n", @lines ) );
135 }
136  
137 2 ▶sub _background_processor {
Subroutine _background_processor: called CC=1 SCAR=0.0
138 2 my ($self, $tokens) = @_;
139
140 2 my $token = shift @$tokens;
141 2 my $tree = { title => $token->[1] };
142
143 2 $tree = $self->_subtree_collector($tokens, $tree, $processors->{'background'});
144
145 2 return ('background', Test::Pcuke::Gherkin::Node::Background->new( $tree ) );
146 }
147  
148 4 ▶sub _scenario_processor {
Subroutine _scenario_processor: called CC=1 SCAR=0.0
149 4 my ($self, $tokens) = @_;
150 4 my $token = shift @$tokens;
151 4 my $tree = { title => $token->[1] };
152
153 4 $tree = $self->_subtree_collector($tokens, $tree, $processors->{'scenario'} );
154 4 $tree->{executor} = $self->_get_executor;
155
156 4 return [ 'scenarios', Test::Pcuke::Gherkin::Node::Scenario->new( $tree ) ];
157 }
158  
159 2 ▶sub _outline_processor {
Subroutine _outline_processor: called CC=1 SCAR=0.0
160 2 my ($self, $tokens) = @_;
161 2 my $token = shift @$tokens;
162 2 my $tree = { title => $token->[1] };
163
164 2 $tree = $self->_subtree_collector($tokens, $tree, $processors->{'outline'});
165 2 return [ 'scenarios', Test::Pcuke::Gherkin::Node::Outline->new( $tree ) ];
166 }
167  
168 2 ▶sub _examples_processor {
Subroutine _examples_processor: called CC=1 SCAR=0.0
169 2 my ($self, $tokens) = @_;
170 2 my $token = shift @$tokens;
171 2 my $tree = { title => $token->[1] };
172
173 2 $tree = $self->_subtree_collector($tokens, $tree, $processors->{'example'} );
174
175 2 return ['examples', Test::Pcuke::Gherkin::Node::Scenarios->new( $tree ) ];
176 }
177
178 16 ▶sub _step_processor {
Subroutine _step_processor: called CC=1 SCAR=0.0
179 16 my ($self, $tokens) = @_;
180 16 my $token = shift @$tokens;
181 16 my $tree = { type => $token->[1], title => $token->[2] };
182 16 $tree = $self->_subtree_collector($tokens, $tree, $processors->{'step'});
183
184 16 $tree->{executor} = $self->_get_executor;
185
186 16 return [ 'steps', Test::Pcuke::Gherkin::Node::Step->new( $tree ) ];
187 }
188  
189 4 ▶sub _table_processor {
Subroutine _table_processor: called CC=1 SCAR=0.0
190 4 my ($self, $tokens) = @_;
191 4 my @rows = map { shift @$_; $_ } $self->_aggregate_tokens('TROW', $tokens);
192
193 4 my $tree = {
194 headings => shift @rows,
195 rows => [@rows],
196 executor => $self->_get_executor
197 };
198
199 4 return ('table', Test::Pcuke::Gherkin::Node::Table->new( $tree ) );
200 }
201  
202 2 ▶sub _trash_processor {
Subroutine _trash_processor: called CC=1 SCAR=0.0
203 2 my ($self, $tokens) = @_;
204 2 shift @$tokens;
205 2 return ('_trash', '_trash');
206 }
207  
208  
209  
210 ### $tokens
211 ### $tree hashref with the initial tree
212 ### $processors hashref, keys are token labels, values are coderefs
213 ### upgrades $tree and returns it
214 30 ▶sub _subtree_collector {
Subroutine _subtree_collector: called CC=9 SCAR=22.0
215 30 my ($self, $tokens, $tree, $processors) = @_;
216
217 30 ▶ while ( @$tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens) 66 8 74
218 66 ▶ if ( !$tokens->[0]->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tokens->[0][0]) 2 64 66
219 2 shift @$tokens; # --- what a trash?
220 2 next;
221 }
222 64 my ($key, $value);
223 64 for ( keys %$processors ) {
224 114 ▶ next unless $tokens->[0]->[0] eq $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $tokens->[0][0] eq $_ 72 42 114
225 42 ($key, $value) = $processors->{$_}->($self, $tokens);
226 42 last;
227 }
228 64 ▶ last if !$key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 22 42 64
229 42 ▶ next if $key eq '_trash';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "_trash" 2 40 42
230 40 ▶ push @{ $tree->{ $key->[0] } }, $key->[1] if ref $key eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $key eq "ARRAY" 24 16 40
231 40 ▶ $tree->{$key} = $value if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 16 24 40
232 }
233
234 30 return $tree;
235 }
236  
237  
238 ###
239 ### aggregates tokens with the label
240 ###
241 12 ▶sub _aggregate_tokens {
Subroutine _aggregate_tokens: called CC=4 SCAR=13.9
242 12 my ($self, $label, $tokens) = @_;
243 12 my @collection;
244
245 12 ▶ confess "tokens are undefined, check the arguments!" unless $tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless $tokens 0 12 12
246
247 12 ▶ while ( $tokens->[0]->[0] && $tokens->[0]->[0] eq $label ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->[0][0] and $tokens->[0][0] eq $label) 40 12 52
Condition100%3/3
$tokens->[0][0] and $tokens->[0][0] eq $label
!ll&&!rl&&r
21040
MC/DC100%2/2
$tokens->[0][0] and $tokens->[0][0] eq $label
$tokens->[0][0] $tokens->[0][0] eq $label
Truth table100%3/3
A and B
A= $tokens->[0][0]B= $tokens->[0][0] eq $label
ABresult
0X0
100
111
248 40 my $token = shift @$tokens;
249 40 push @collection, $token;
250 }
251
252 12 return @collection;
253 }
254  
255  
256 =head1 AUTHOR
257  
258 Andrei V. Toutoukine, C<< <tut at isuct.ru> >>
259  
260 =head1 BUGS
261  
262 Please report any bugs or feature requests to C<bug-/home/tut/bin/src/test-pcuke-gherkin at rt.cpan.org>, or through
263 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=/home/tut/bin/src/Test-Pcuke-Gherkin>. I will be notified, and then you'll
264 automatically be notified of progress on your bug as I make changes.
265  
266  
267  
268  
269 =head1 SUPPORT
270  
271 You can find documentation for this module with the perldoc command.
272  
273 perldoc Test::Pcuke::Gherkin::Parser
274  
275  
276 You can also look for information at:
277  
278 =over 4
279  
280 =item * RT: CPAN's request tracker
281  
282 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=/home/tut/bin/src/Test-Pcuke-Gherkin>
283  
284 =item * AnnoCPAN: Annotated CPAN documentation
285  
286 L<http://annocpan.org/dist//home/tut/bin/src/Test-Pcuke-Gherkin>
287  
288 =item * CPAN Ratings
289  
290 L<http://cpanratings.perl.org/d//home/tut/bin/src/Test-Pcuke-Gherkin>
291  
292 =item * Search CPAN
293  
294 L<http://search.cpan.org/dist//home/tut/bin/src/Test-Pcuke-Gherkin/>
295  
296 =back
297  
298  
299 =head1 ACKNOWLEDGEMENTS
300  
301  
302 =head1 LICENSE AND COPYRIGHT
303  
304 Copyright 2011 Andrei V. Toutoukine.
305  
306 This program is released under the following license: artistic
307  
308  
309 =cut
310  
311 2 1; # End of Test::Pcuke::Gherkin::Parser
« Gherkin/Node.pm ↑ Summary Gherkin/Node/Scenario.pm »