blib/lib/Test/BDD/Cucumber/Executor.pm

filter: Statementstmt 92.2% 272 / 295 Branchbran 73.7% 59 / 80 Conditioncond 64.8% 35 / 54 MC/DCmcdc 53.5% 15 / 28 Subroutinesub 100% 45 / 45 Podpod 100% 9 / 9 totaltotal 85.1% 435 / 511 CC 78 SCAR 21.8
CC
78
Cov
85%
find_and_dispatch
37.2
execute_outline
10.9
_extract_match_strings
10.4
CRAP 8.8
« blib/lib/TAP/Parser/SourceHandler/Feature.pm ↑ Summary blib/lib/Test/BDD/Cucumber/Parser.pm »
1  
2 22 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Test::BDD::Cucumber::Executor 0.87;
6  
7 =head1 NAME
8  
9 Test::BDD::Cucumber::Executor - Run through Feature and Harness objects
10  
11 =head1 VERSION
12  
13 version 0.87
14  
15 =head1 DESCRIPTION
16  
17 The Executor runs through Features, matching up the Step Lines with Step
18 Definitions, and reporting on progress through the passed-in harness.
19  
20 =cut
21  
22 22 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 22 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 22 ▶use Types::Standard qw( Bool Str ArrayRef HashRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 22 ▶use List::Util qw/first any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 22 ▶use Module::Runtime qw/use_module/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 22 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 22 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 22 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 22 ▶use Test2::API qw/intercept/;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # Use-ing the formatter results in a
34 # 'loaded too late to be used globally' warning
35 # But we only need it locally anyway.
36 22 require Test2::Formatter::TAP;
37  
38 22 ▶use Test2::Tools::Basic qw/ pass fail done_testing /;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 # Needed for subtest() -- we don't want to import all its functions though
40 22 require Test::More;
41  
42 22 ▶use Test::BDD::Cucumber::StepFile ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 22 ▶use Test::BDD::Cucumber::StepContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 22 ▶use Test::BDD::Cucumber::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 22 ▶use Test::BDD::Cucumber::Model::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 22 ▶use Test::BDD::Cucumber::Errors qw/parse_error_from_line/;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 =head1 ATTRIBUTES
49  
50 =head2 matching
51  
52 The value of this attribute should be one of C<first> (default), C<relaxed> and C<strict>.
53  
54 By default (C<first>), the first matching step is executed immediately,
55 terminating the search for (further) matching steps. When C<matching> is set
56 to anything other than C<first>, all steps are checked for matches. When set
57 to C<relaxed>, a warning will be generated on multiple matches. When set to
58 C<strict>, an exception will be thrown.
59  
60 =cut
61  
62 22 has matching => ( is => 'rw', isa => Str, default => 'first');
63  
64 =head1 METHODS
65  
66 =head2 extensions
67  
68 =head2 add_extensions
69  
70 The attributes C<extensions> is an arrayref of
71 L<Test::BDD::Cucumber::Extension> extensions. Extensions have their
72 hook-functions called by the Executor at specific points in the BDD feature
73 execution.
74  
75 B<C<<add_extensions>> adds items in FIFO using unshift()>, and are called in
76 reverse order at the end hook; this means that if you:
77  
78 add_extensions( 1 );
79 add_extensions( 2, 3 );
80  
81 The C<pre_*> will be called in order 2, 3, 1, and C<post_*> will be called in
82 1, 3, 2.
83  
84 =cut
85  
86 has extensions => (
87 is => 'ro',
88 isa => ArrayRef,
89 47 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 22 handles_via => 'Array',
91 handles => { add_extensions => 'unshift' },
92 );
93  
94 =head2 steps
95  
96 =head2 add_steps
97  
98 The attributes C<steps> is a hashref of arrayrefs, storing steps by their Verb.
99 C<add_steps()> takes step definitions of the item list form:
100  
101 (
102 [ Given => qr//, sub {} ],
103 ),
104  
105 Or, when metadata is specified with the step, of the form:
106  
107 (
108 [ Given => qr//, { meta => $data }, sub {} ]
109 ),
110  
111 (where the hashref stores step metadata) and populates C<steps> with them.
112  
113 =cut
114  
115 47 ▶has 'steps' => ( is => 'rw', isa => HashRef, default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
116  
117 53 ▶sub add_steps {
Subroutine add_steps: called CC=6 SCAR=17.9
Pod documented
118 53 my ( $self, @steps ) = @_;
119  
120 # Map the steps to be lower case...
121 53 for (@steps) {
122 196 my ( $verb, $match, $meta, $code );
123  
124 196 ▶ if (@$_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$_ == 3) { } 46 150 196
125 46 ( $verb, $match, $code ) = @$_;
126 46 $meta = {};
127 }
128 else {
129 150 ( $verb, $match, $meta, $code ) = @$_;
130 }
131 196 $verb = lc $verb;
132  
133 196 ▶ unless ( ref($match) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $match) 59 137 196
134 59 $match =~ s/:\s*$//;
135 59 $match = quotemeta($match);
136 59 $match = qr/^$match:?/i;
137 }
138  
139 196 ▶ if ( $verb eq 'transform' or $verb eq 'after' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb eq 'transform' or $verb eq 'after') { } 12 184 196
Condition100%3/3
$verb eq "transform" or $verb eq "after"
l!l&&r!l&&!r
84184
MC/DC100%2/2
$verb eq "transform" or $verb eq "after"
$verb eq "transform" $verb eq "after"
Truth table100%3/3
A or B
A= $verb eq "transform"B= $verb eq "after"
ABresult
1X1
011
000
140  
141 # Most recently defined Transform takes precedence
142 # and After blocks need to be run in reverse order
143 12 unshift( @{ $self->{'steps'}->{$verb} }, [ $match, $meta, $code ] );
144 } else {
145 184 push( @{ $self->{'steps'}->{$verb} }, [ $match, $meta, $code ] );
146 }
147  
148 }
149 }
150  
151 =head2 execute
152  
153 Execute accepts a feature object, a harness object, and an optional
154 L<Cucumber::TagExpressions::ExpressionNode> object and for each scenario in the
155 feature which meets the tag requirements (or all of them, if you
156 haven't specified one), runs C<execute_scenario>.
157  
158 =cut
159  
160 40 ▶sub execute {
Subroutine execute: called CC=5 SCAR=16.1
Pod documented
161 40 my ( $self, $feature, $harness, $tag_spec ) = @_;
162 40 my $feature_stash = {};
163  
164 40 $harness->feature($feature);
165 40 ▶ my @background =
Branch: 1 branch
BranchTrueFalseTotal
$feature->background ? : 15 25 40
166 ( $feature->background ? ( background => $feature->background ) : () );
167  
168 # Get all scenarios
169 40 my @scenarios = @{ $feature->scenarios() };
170  
171 40 $_->pre_feature( $feature, $feature_stash ) for @{ $self->extensions };
172 40 for my $outline (@scenarios) {
173  
174 # Execute the scenario itself
175 141 $self->execute_outline(
176 {
177 @background,
178 scenario => $outline,
179 feature => $feature,
180 feature_stash => $feature_stash,
181 harness => $harness,
182 tagspec => $tag_spec,
183 }
184 );
185 }
186 $_->post_feature( $feature, $feature_stash, 'no' )
187 40 for reverse @{ $self->extensions };
188  
189 40 $harness->feature_done($feature);
190 }
191  
192 =head2 execute_outline
193  
194 Accepts a hashref of options and executes each scenario definition in the
195 scenario outline, or, lacking an outline, executes the single defined
196 scenario.
197  
198 Options:
199  
200 C< feature > - A L<Test::BDD::Cucumber::Model::Feature> object
201  
202 C< feature_stash > - A hashref that should live the lifetime of
203 feature execution
204  
205 C< harness > - A L<Test::BDD::Cucumber::Harness> subclass object
206  
207 C< outline > - A L<Test::BDD::Cucumber::Model::Scenario> object
208  
209 C< background > - An optional L<Test::BDD::Cucumber::Model::Scenario> object
210 representing the Background
211  
212 =cut
213  
214 12 ▶sub _match_tags {
Subroutine _match_tags: called CC=3 SCAR=13.6
215 12 my ($spec, @tagged_components) = @_;
216 12 state $deprecation_warned = 0;
217  
218 12 ▶ if ($spec->isa('Cucumber::TagExpressions::ExpressionNode')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->isa('Cucumber::TagExpressions::ExpressionNode')) { } 12 0 12
219 return grep {
220 12 $spec->evaluate( @{ $_->tags } )
221 } @tagged_components;
222 }
223 else {
224 0 ▶ $deprecation_warned ||=
Condition0%0/3
$deprecation_warned ||= carp("Test::BDD::Cucumber::Model::TagSpec is deprecated; replace with Cucumber::TagExpressions")
l!l&&r!l&&!r
000
MC/DC0%0/2
$deprecation_warned ||= carp("Test::BDD::Cucumber::Model::TagSpec is deprecated; replace with Cucumber::TagExpressions")
$deprecation_warned carp("Test::BDD::Cucumber::Model::TagSpec is deprecated; replace with Cucumber::TagExpressions")
Truth table0%0/3
A ||= B
A= $deprecation_warnedB= carp("Test::BDD::Cucumber::Model::TagSpec is deprecated; replace with Cucumber::TagExpressions")
ABresult
1X1
011
000
225 carp 'Test::BDD::Cucumber::Model::TagSpec is deprecated; replace with Cucumber::TagExpressions';
226  
227 0 return $spec->filter( @tagged_components );
228 }
229 }
230  
231 141 ▶sub execute_outline {
Subroutine execute_outline: called CC=10 SCAR=23.9
Pod documented
232 141 my ( $self, $options ) = @_;
233 my ( $feature, $feature_stash, $harness, $outline, $background, $tagspec )
234 141 = @$options{qw/ feature feature_stash harness scenario background tagspec /};
235  
236 # Multiply out Scenario Outlines as appropriate
237 141 my @datasets = @{ $outline->datasets };
238 141 ▶ if (not @datasets) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@datasets) 116 25 141
239 116 ▶ if (not $tagspec or _match_tags( $tagspec, $outline )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $tagspec or _match_tags($tagspec, $outline)) 110 6 116
Condition100%3/3
not $tagspec or _match_tags($tagspec, $outline)
l!l&&r!l&&!r
10466
MC/DC100%2/2
not $tagspec or _match_tags($tagspec, $outline)
not $tagspec _match_tags($tagspec, $outline)
Truth table100%3/3
not(A) or B
A= not $tagspecB= _match_tags($tagspec, $outline)
ABresult
1X1
011
000
240 110 $self->execute_scenario(
241 {
242 feature => $feature,
243 feature_stash => $feature_stash,
244 harness => $harness,
245 scenario => $outline,
246 background => $background,
247 scenario_stash => {},
248 dataset => {},
249 });
250 }
251 116 $harness->scenario_skip( $outline, {} );
252  
253 116 return;
254 }
255  
256 25 ▶ if ($tagspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagspec) 0 25 25
257 0 @datasets = _match_tags( $tagspec, @datasets );
258  
259 0 ▶ unless (@datasets) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@datasets) 0 0 0
260 0 $harness->scenario_skip( $outline, {} );
261 0 return;
262 }
263 }
264  
265  
266 25 foreach my $rows (@datasets) {
267  
268 25 foreach my $row (@{$rows->data}) {
269  
270 71 ▶ my $name = $outline->{name} || "";
Condition100%2/2
$outline->{'name'} || ''
l!l
647
MC/DC100%1/1
$outline->{'name'} || ''
$outline->{'name'}
Truth table100%2/2
A
A= $outline->{'name'}
Aresult
11
00
271 $name =~ s/\Q<$_>\E/$row->{$_}/g
272 71 for (keys %$row);
273 71 local $outline->{name} = $name;
274  
275 71 $self->execute_scenario(
276 {
277 feature => $feature,
278 feature_stash => $feature_stash,
279 harness => $harness,
280 scenario => $outline,
281 background => $background,
282 scenario_stash => {},
283 dataset => $row,
284 });
285 }
286 }
287 }
288  
289 =head2 execute_scenario
290  
291 Accepts a hashref of options, and executes each step in a scenario. Options:
292  
293 C<feature> - A L<Test::BDD::Cucumber::Model::Feature> object
294  
295 C<feature_stash> - A hashref that should live the lifetime of feature execution
296  
297 C<harness> - A L<Test::BDD::Cucumber::Harness> subclass object
298  
299 C<scenario> - A L<Test::BDD::Cucumber::Model::Scenario> object
300  
301 C<background_obj> - An optional L<Test::BDD::Cucumber::Model::Scenario> object
302 representing the Background
303  
304 C<scenario_stash> - A hashref that lives the lifetime of the scenario execution
305  
306 For each step, a L<Test::BDD::Cucumber::StepContext> object is created, and
307 passed to C<dispatch()>. Nothing is returned - everything is played back through
308 the Harness interface.
309  
310 =cut
311  
312  
313 258 ▶sub _execute_steps {
Subroutine _execute_steps: called CC=6 SCAR=17.9
314 258 my ( $self, $options ) = @_;
315 my ( $feature, $feature_stash, $harness, $outline,
316 $scenario_stash, $scenario_state, $dataset, $context_defaults )
317 = @$options{
318 258 qw/ feature feature_stash harness scenario scenario_stash
319 scenario_state dataset context_defaults
320 /
321 };
322  
323  
324 258 foreach my $step ( @{ $outline->steps } ) {
325  
326 # Multiply out any placeholders
327 729 my $text =
328 $self->add_placeholders( $step->text, $dataset, $step->line );
329 729 my $data = $step->data;
330 729 ▶ $data = (ref $data) ?
Branch: 2 branches
BranchTrueFalseTotal
ref $data ? : 11 718 729
defined $data ? : 31 687 718
331 $self->add_table_placeholders( $data, $dataset, $step->line )
332 : (defined $data) ?
333 $self->add_placeholders( $data, $dataset, $step->line )
334 : '';
335  
336 # Set up a context
337 729 ▶ my $context = Test::BDD::Cucumber::StepContext->new(
Condition100%2/2
$step->columns || []
l!l
11718
MC/DC100%1/1
$step->columns || []
$step->columns
Truth table100%2/2
A
A= $step->columns
Aresult
11
00
338 {
339 %$context_defaults,
340  
341 # Data portion
342 columns => $step->columns || [],
343 data => $data,
344  
345 # Step-specific info
346 step => $step,
347 verb => lc( $step->verb ),
348 text => $text,
349 }
350 );
351  
352 my $result =
353 $self->find_and_dispatch( $context,
354 729 $scenario_state->{'short_circuit'}, 0 );
355  
356 # If it didn't pass, short-circuit the rest
357 729 ▶ unless ( $result->result eq 'passing' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->result eq "passing") 35 694 729
358 35 $scenario_state->{'short_circuit'}++;
359 }
360  
361 }
362  
363 258 return;
364 }
365  
366  
367 384 ▶sub _execute_hook_steps {
Subroutine _execute_hook_steps: called CC=6 SCAR=19.1
368 384 my ( $self, $phase, $context_defaults, $scenario_state ) = @_;
369 384 my $want_short = ($phase eq 'before');
370  
371 384 ▶ for my $step ( @{ $self->{'steps'}->{$phase} || [] } ) {
Condition100%2/2
$self->{'steps'}{$phase} || []
l!l
162222
MC/DC100%1/1
$self->{'steps'}{$phase} || []
$self->{'steps'}{$phase}
Truth table100%2/2
A
A= $self->{'steps'}{$phase}
Aresult
11
00
372  
373 258 my $context = Test::BDD::Cucumber::StepContext->new(
374 { %$context_defaults, verb => $phase, } );
375  
376 my $result =
377 $self->dispatch(
378 $context, $step,
379 258 ▶ ($want_short ? $scenario_state->{'short_circuit'} : 0),
Branch: 1 branch
BranchTrueFalseTotal
$want_short ? : 129 129 258
380 0 );
381  
382 # If it didn't pass, short-circuit the rest
383 258 ▶ unless ( $result->result eq 'passing' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->result eq "passing") 0 258 258
384 0 ▶ if ($want_short) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_short) 0 0 0
385 0 $scenario_state->{'short_circuit'} = 1;
386 }
387 }
388 }
389  
390 384 return;
391 }
392  
393  
394 192 ▶sub execute_scenario {
Subroutine execute_scenario: called CC=5 SCAR=16.1
Pod documented
395 192 my ( $self, $options ) = @_;
396 my ( $feature, $feature_stash, $harness, $outline, $background_obj,
397 $scenario_stash, $dataset )
398 = @$options{
399 192 qw/ feature feature_stash harness scenario background scenario_stash
400 dataset
401 /
402 };
403 192 my $scenario_state = {};
404  
405 my %context_defaults = (
406 executor => $self, # Held weakly by StepContext
407  
408 # Data portion
409 data => '',
410 stash => {
411 feature => $feature_stash,
412 step => {},
413 },
414  
415 # Step-specific info
416 feature => $feature,
417 scenario => $outline,
418  
419 # Communicators
420 harness => $harness,
421  
422 192 ▶ transformers => $self->{'steps'}->{'transform'} || [],
Condition100%2/2
$self->{'steps'}{'transform'} || []
l!l
81111
MC/DC100%1/1
$self->{'steps'}{'transform'} || []
$self->{'steps'}{'transform'}
Truth table100%2/2
A
A= $self->{'steps'}{'transform'}
Aresult
11
00
423 );
424 192 $context_defaults{stash}->{scenario} = $scenario_stash;
425  
426 $harness->scenario( $outline, $dataset,
427 192 $scenario_stash->{'longest_step_line'} );
428  
429 $_->pre_scenario( $outline, $feature_stash, $scenario_stash )
430 192 for @{ $self->extensions };
431  
432 192 $self->_execute_hook_steps( 'before', \%context_defaults, $scenario_state );
433  
434 192 ▶ if ($background_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($background_obj) 66 126 192
435 $harness->background( $outline, $dataset,
436 66 $scenario_stash->{'longest_step_line'} );
437 66 $self->_execute_steps(
438 {
439 scenario => $background_obj,
440 feature => $feature,
441 feature_stash => $feature_stash,
442 harness => $harness,
443 scenario_stash => $scenario_stash,
444 scenario_state => $scenario_state,
445 context_defaults => \%context_defaults,
446 }
447 );
448 66 $harness->background_done( $outline, $dataset );
449 }
450  
451 $self->_execute_steps(
452 {
453 192 scenario => $outline,
454 feature => $feature,
455 feature_stash => $feature_stash,
456 harness => $harness,
457 scenario_stash => $scenario_stash,
458 scenario_state => $scenario_state,
459 dataset => $dataset,
460 context_defaults => \%context_defaults,
461 });
462  
463 192 $self->_execute_hook_steps( 'after', \%context_defaults, $scenario_state );
464  
465 $_->post_scenario( $outline, $feature_stash, $scenario_stash,
466 $scenario_state->{'short_circuit'} )
467 192 for reverse @{ $self->extensions };
468  
469 192 $harness->scenario_done( $outline, $dataset );
470  
471 192 return;
472 }
473  
474 =head2 add_placeholders
475  
476 Accepts a text string and a hashref, and replaces C< <placeholders> > with the
477 values in the hashref, returning a string.
478  
479 =cut
480  
481 829 ▶sub add_placeholders {
Subroutine add_placeholders: called CC=2 SCAR=7.0
Pod documented
482 829 my ( $self, $text, $dataset, $line ) = @_;
483 829 my $quoted_text = Test::BDD::Cucumber::Util::bs_quote($text);
484 829 $quoted_text =~ s/(<([^>]+)>)/
485 167 ▶ exists $dataset->{$2} ? $dataset->{$2} :
Branch: 1 branch
BranchTrueFalseTotal
exists $dataset->{$2} ? : 167 0 167
486 die parse_error_from_line( "No mapping to placeholder $1", $line )
487 /eg;
488 829 return Test::BDD::Cucumber::Util::bs_unquote($quoted_text);
489 }
490  
491  
492 =head2 add_table_placeholders
493  
494 Accepts a hash with parsed table data and a hashref, and replaces
495 C< <placeholders> > with the values in the hashref, returning a copy of the
496 parsed table hashref.
497  
498 =cut
499  
500 11 ▶sub add_table_placeholders {
Subroutine add_table_placeholders: called CC=1 SCAR=0.0
Pod documented
501 11 my ($self, $tbl, $dataset, $line) = @_;
502 my @rv = map {
503 11 my $row = $_;
504 my %inner_rv =
505 29 map { $_ => $self->add_placeholders($row->{$_}, $dataset, $line)
506 } keys %$row;
507 29 \%inner_rv;
508 } @$tbl;
509 11 return \@rv;
510 }
511  
512  
513 =head2 find_and_dispatch
514  
515 Accepts a L<Test::BDD::Cucumber::StepContext> object, and searches through
516 the steps that have been added to the executor object, executing against the
517 first matching one (unless C<$self->matching> indicates otherwise).
518  
519 You can also pass in a boolean 'short-circuit' flag if the Scenario's remaining
520 steps should be skipped, and a boolean flag to denote if it's a redispatched
521 step.
522  
523 =cut
524  
525 753 ▶sub find_and_dispatch {
Subroutine find_and_dispatch: called CC=14 SCAR=36.2
Pod documented
526 753 my ( $self, $context, $short_circuit, $redispatch ) = @_;
527  
528 # Short-circuit if we need to
529 753 ▶ return $self->skip_step( $context, 'pending',
Branch: 1 branch
BranchTrueFalseTotal
if $short_circuit 19 734 753
530 "Short-circuited from previous tests", 0 )
531 if $short_circuit;
532  
533 # Try and find a matching step
534 734 my $stepdef;
535 734 my $text = $context->text;
536 734 ▶ if ($self->matching eq 'first') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->matching eq 'first') { } 734 0 734
537 1687 ▶ $stepdef = first { $text =~ $_->[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
538 734 ▶ @{ $self->{'steps'}->{ $context->verb } || [] },
Condition100%2/2
$self->{'steps'}{$context->verb} || []
l!l
7295
MC/DC100%1/1
$self->{'steps'}{$context->verb} || []
$self->{'steps'}{$context->verb}
Truth table100%2/2
A
A= $self->{'steps'}{$context->verb}
Aresult
11
00
539 734 ▶ @{ $self->{'steps'}->{'step'} || [] };
Condition100%2/2
$self->{'steps'}{'step'} || []
l!l
5729
MC/DC100%1/1
$self->{'steps'}{'step'} || []
$self->{'steps'}{'step'}
Truth table100%2/2
A
A= $self->{'steps'}{'step'}
Aresult
11
00
540 }
541 else {
542 0 my @stepdefs = grep { $text =~ $_->[0] }
543 0 ▶ @{ $self->{'steps'}->{ $context->verb } || [] },
Condition0%0/2
$self->{'steps'}{$context->verb} || []
l!l
00
MC/DC0%0/1
$self->{'steps'}{$context->verb} || []
$self->{'steps'}{$context->verb}
Truth table0%0/2
A
A= $self->{'steps'}{$context->verb}
Aresult
11
00
544 0 ▶ @{ $self->{'steps'}->{'step'} || [] };
Condition0%0/2
$self->{'steps'}{'step'} || []
l!l
00
MC/DC0%0/1
$self->{'steps'}{'step'} || []
$self->{'steps'}{'step'}
Truth table0%0/2
A
A= $self->{'steps'}{'step'}
Aresult
11
00
545  
546 0 ▶ if (@stepdefs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stepdefs > 1) 0 0 0
547 0 my $filename = $context->step->line->document->filename;
548 0 my $line = $context->step->line->number;
549 my $msg =
550 join("\n ",
551 qq(Step "$text" ($filename:$line) matches multiple step functions:),
552 map {
553 0 qq{matcher $_->[0] defined at } .
554 (($_->[1]->{source} && $_->[1]->{line})
555 0 ▶ ? "$_->[1]->{source}:$_->[1]->{line}"
Branch: 1 branch
BranchTrueFalseTotal
$_->[1]{'source'} && $_->[1]{'line'} ? : 0 0 0
Condition0%0/3
$_->[1]{"source"} and $_->[1]{"line"}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->[1]{"source"} and $_->[1]{"line"}
$_->[1]{"source"} $_->[1]{"line"}
Truth table0%0/3
A and B
A= $_->[1]{"source"}B= $_->[1]{"line"}
ABresult
0X0
100
111
556 : '<unknown>') } @stepdefs);
557  
558 0 ▶ if ($self->matching eq 'relaxed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->matching eq 'relaxed') { } 0 0 0
559 0 warn $msg;
560 }
561 else {
562 0 die $msg;
563 }
564 }
565 0 $stepdef = shift @stepdefs;
566 }
567  
568 # Deal with the simple case of no-match first of all
569 734 ▶ unless ($stepdef) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stepdef) 6 728 734
570 6 my $message =
571 "No matching step definition for: "
572 . $context->verb . ' '
573 . $context->text;
574 6 my $result =
575 $self->skip_step( $context, 'undefined', $message, $redispatch );
576 6 return $result;
577 }
578  
579 728 $_->pre_step( $stepdef, $context ) for @{ $self->extensions };
580 728 my $result = $self->dispatch( $context, $stepdef, 0, $redispatch );
581 $_->post_step( $stepdef, $context,
582 ( $result->result ne 'passing' ), $result )
583 728 for reverse @{ $self->extensions };
584 728 return $result;
585 }
586  
587 =head2 dispatch($context, $stepdef, $short_circuit, $redispatch)
588  
589 Accepts a L<Test::BDD::Cucumber::StepContext> object, and a
590 reference to a step definition triplet (verb, metadata hashref, coderef)
591 and executes it the coderef.
592  
593 You can also pass in a boolean 'short-circuit' flag if the Scenario's remaining
594 steps should be skipped.
595  
596 =cut
597  
598 986 ▶sub dispatch {
Subroutine dispatch: called CC=6 SCAR=17.9
Pod documented
599 986 my ( $self, $context, $stepdef, $short_circuit, $redispatch ) = @_;
600  
601 986 ▶ return $self->skip_step( $context, 'pending',
Branch: 1 branch
BranchTrueFalseTotal
if $short_circuit 0 986 986
602 "Short-circuited from previous tests", $redispatch )
603 if $short_circuit;
604  
605 # Execute the step definition
606 986 my ( $regular_expression, $meta, $coderef ) = @$stepdef;
607  
608 986 ▶ my $step_name = $redispatch ? 'sub_step' : 'step';
Branch: 1 branch
BranchTrueFalseTotal
$redispatch ? : 24 962 986
609 986 my $step_done_name = $step_name . '_done';
610  
611 # Say we're about to start it up
612 986 $context->harness->$step_name($context);
613  
614 986 my @match_locations;
615 986 my $stash_keys = join ';', sort keys %{$context->stash};
616 # Using `intercept()`, run the step function in an isolated
617 # environment -- this should not affect the enclosing scope
618 # which might be a TAP::Harness scope.
619 #
620 # Instead, we want the tests inside this scope to map to
621 # status values
622 986 ▶ my $events = intercept {
Subroutine __ANON__: called CC=1 SCAR=0.0
623 # This is a hack to make Test::More's $TODO variable work
624 # inside the intercepted scope.
625  
626 ###TODO: Both intercept() and Test::More::subtest() should
627 # be replaced by a specific Hub implementation for T::B::C
628 986 ▶ Test::More::subtest( 'execute step', sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
629  
630 # Take a copy of this. Turns out actually matching against it
631 # directly causes all sorts of weird-ass heisenbugs which mst has
632 # promised to investigate.
633 986 my $text = $context->text;
634  
635 # Save the matches
636 986 $context->matches( [ $text =~ $regular_expression ] );
637  
638 # Save the location of matched subgroups for highlighting hijinks
639 986 my @starts = @-;
640 986 my @ends = @+;
641  
642 # Store the string position of matches for highlighting
643 986 @match_locations = map { [ $_, shift @ends ] } @starts;
644  
645 # OK, actually execute
646 986 local $@;
647 986 eval {
648 22 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
649  
650 1067 ▶ local *Test::BDD::Cucumber::StepFile::_S = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
651 1067 return $context->stash->{'scenario'};
652 986 };
653 536 ▶ local *Test::BDD::Cucumber::StepFile::_C = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
654 536 return $context;
655 986 };
656  
657 986 $coderef->($context)
658 };
659 986 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 986 986
660 0 fail("Step ran to completion", "Exception: ", $@);
661 }
662 else {
663 986 pass("Step ran to completion");
664 }
665  
666 986 done_testing();
667 986 });
668 986 };
669  
670 986 my $status = $self->_test_status( $events );
671  
672 my $result = Test::BDD::Cucumber::Model::Result->new(
673 {
674 result => $status,
675 # due to the hack above with the subtest inside the
676 # interception scope, we need to grovel the subtest
677 # from out of the other results first.
678 output => $self->_test_output(
679 1972 ▶ (first { $_->isa('Test2::Event::Subtest') }
Subroutine __ANON__: called CC=1 SCAR=0.0
680 986 @$events)->{subevents}),
681 });
682 warn qq|Unsupported: Step modified C->stash instead of C->stash->{scenario} or C->stash->{feature}|
683 986 ▶ if $stash_keys ne (join ';', sort keys %{$context->stash});
Branch: 1 branch
BranchTrueFalseTotal
if $stash_keys ne join(";", sort(keys %{$context->stash;})) 0 986 986
684  
685 986 my @clean_matches =
686 $self->_extract_match_strings( $context->text, \@match_locations );
687 986 ▶ @clean_matches = [ 0, $context->text ] unless @clean_matches;
Branch: 1 branch
BranchTrueFalseTotal
unless @clean_matches 415 571 986
688  
689 # Say the step is done, and return the result. Happens outside
690 # the above block so that we don't have the localized harness
691 # anymore...
692 986 ▶ $context->harness->add_result($result) unless $redispatch;
Branch: 1 branch
BranchTrueFalseTotal
unless $redispatch 962 24 986
693 986 $context->harness->$step_done_name( $context, $result, \@clean_matches );
694 986 return $result;
695 }
696  
697 986 ▶sub _extract_match_strings {
Subroutine _extract_match_strings: called CC=10 SCAR=23.4
698 986 my ( $self, $text, $locations ) = @_;
699  
700 # Clean up the match locations
701 my @match_locations = grep {
702 1689 ▶ ( $_->[0] != $_->[1] ) && # No zero-length matches
Condition100%6/6
$_->[0] != $_->[1] && !($_->[0] == 0 && $_->[1] == length $text)
!ll&&!rl&&r
273724692
$_->[0] == 0 && $_->[1] == length $text
!ll&&!rl&&r
6911724
MC/DC100%3/3
$_->[0] != $_->[1] && !($_->[0] == 0 && $_->[1] == length $text)
$_->[0] != $_->[1] $_->[0] == 0 $_->[1] == length $text
Truth table100%4/4
A && not(B && C)
A= $_->[0] != $_->[1]B= $_->[0] == 0C= $_->[1] == length $text
ABCresult
0XX0
1110
10X1
1101
703 # And nothing that matched the full string
704 ( !( ( $_->[0] == 0 ) && ( ( $_->[1] == length $text ) ) ) )
705 } grep {
706 986 ▶ defined $_ && ref $_ && defined $_->[0] && defined $_->[1]
Condition33%3/9
defined $_ && ref $_ && defined $_->[0] && defined $_->[1]
!ll&&!rl&&r
001689
defined $_ and ref $_ and defined $_->[0]
!ll&&!rl&&r
001689
defined $_ and ref $_
!ll&&!rl&&r
001689
MC/DC0%0/4
defined $_ && ref $_ && defined $_->[0] && defined $_->[1]
defined $_ ref $_ defined $_->[0] defined $_->[1]
Truth table20%1/5
A and B and C && D
A= defined $_B= ref $_C= defined $_->[0]D= defined $_->[1]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
707 } @$locations;
708  
709 986 ▶ return unless @match_locations;
Branch: 1 branch
BranchTrueFalseTotal
unless @match_locations 415 571 986
710  
711 my %range =
712 571 map { $_ => 1 } map { $_->[0] .. ($_->[1] - 1) } @match_locations;
713  
714 # Walk the string, splitting
715 571 my @parts = ( [ 0, '' ] );
716 571 for ( 0 .. ( ( length $text ) - 1 ) ) {
717 17571 ▶ my $to_highlight = $range{$_} || 0;
Condition100%2/2
$range{$_} || 0
l!l
586311708
MC/DC100%1/1
$range{$_} || 0
$range{$_}
Truth table100%2/2
A
A= $range{$_}
Aresult
11
00
718 17571 my $character = substr( $text, $_, 1 );
719  
720 17571 ▶ if ( $parts[-1]->[0] != $to_highlight ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[-1][0] != $to_highlight) 1154 16417 17571
721 1154 push( @parts, [ $to_highlight, '' ] );
722 }
723  
724 17571 $parts[-1]->[1] .= $character;
725 }
726  
727 571 return @parts;
728 }
729  
730 986 ▶sub _test_output {
Subroutine _test_output: called CC=1 SCAR=0.0
731 986 my ($self, $events) = @_;
732 986 my $fmt = Test2::Formatter::TAP->new();
733 986 open my $stdout, '>:encoding(UTF-8)', \my $out_text;
734 986 my $idx = 0;
735  
736 986 $fmt->set_handles([ $stdout, $stdout ]);
737 986 $self->_test_output_from_subevents($events, $fmt, \$idx);
738 986 close $stdout;
739  
740 986 return Encode::decode('utf8', $out_text);
741 }
742  
743 986 ▶sub _test_output_from_subevents {
Subroutine _test_output_from_subevents: called CC=3 SCAR=11.7
744 986 my ($self, $events, $fmt, $idx) = @_;
745  
746 986 for my $event (@$events) {
747 2618 ▶ if ($event->{subevents}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{'subevents'}) { } 0 2618 2618
748 $self->_test_output_from_subevents(
749 0 $event->{subevents}, $fmt, $idx);
750 }
751 else {
752 2618 $fmt->write($event, $$idx++);
753 }
754 }
755 }
756  
757 986 ▶sub _test_status {
Subroutine _test_status: called CC=3 SCAR=11.0
758 986 my $self = shift;
759 986 my $events = shift;
760  
761 986 ▶ if (any { defined $_->{effective_pass}
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { defined $_->{'effective_pass'} and not $_->{'effective_pass'}; } , @$events)) { } 7 979 986
762 1972 ▶ and ! $_->{effective_pass} } @$events) {
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{"effective_pass"} and not $_->{"effective_pass"} 986 986 1972
763 7 return 'failing';
764 }
765 else {
766 979 ▶ return $self->_test_status_from_subevents($events) ? 'pending' : 'passing';
Branch: 1 branch
BranchTrueFalseTotal
$self->_test_status_from_subevents($events) ? : 3 976 979
767 }
768 }
769  
770 1958 ▶sub _test_status_from_subevents {
Subroutine _test_status_from_subevents: called CC=8 SCAR=20.9
771 1958 my $self = shift;
772 1958 my $events = shift;
773  
774 1958 for my $e (@$events) {
775 4527 ▶ if (exists $e->{subevents}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $e->{'subevents'}) { } 979 3548 4527
elsif (defined $e->{'amnesty'} and $e->{'effective_pass'} and not $e->{'pass'} and &any(sub { $_->{'tag'} eq 'TODO'; } , @{$$e{'amnesty'};})) { } 3 7090 7093
776 $self->_test_status_from_subevents($e->{subevents})
777 979 ▶ and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_test_status_from_subevents($e->{"subevents"}) and return 1 3 976 979
778 }
779 ▶ elsif (defined $e->{amnesty}
Condition66%6/9
defined $e->{"amnesty"} and $e->{"effective_pass"} and not $e->{"pass"} and &any(sub { $_->{"tag"} eq "TODO"; } , @{$$e{"amnesty"};})
!ll&&!rl&&r
354503
defined $e->{"amnesty"} and $e->{"effective_pass"} and not $e->{"pass"}
!ll&&!rl&&r
354503
defined $e->{"amnesty"} and $e->{"effective_pass"}
!ll&&!rl&&r
354503
MC/DC25%1/4
defined $e->{"amnesty"} and $e->{"effective_pass"} and not $e->{"pass"} and &any(sub { $_->{"tag"} eq "TODO"; } , @{$$e{"amnesty"};})
defined $e->{"amnesty"} $e->{"effective_pass"} not $e->{"pass"} &any(sub { $_->{"tag"} eq "TODO"; } , @{$$e{"amnesty"};})
Truth table40%2/5
A and B and not(C) and D
A= defined $e->{"amnesty"}B= $e->{"effective_pass"}C= not $e->{"pass"}D= &any(sub { $_->{"tag"} eq "TODO"; } , @{$$e{"amnesty"};})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
780 and $e->{effective_pass}
781 and (not $e->{pass})
782 3 ▶ and any { $_->{tag} eq 'TODO' } @{$e->{amnesty}}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
783 3 return 1;
784 }
785 }
786  
787 1952 return 0;
788 }
789  
790 =head2 skip_step
791  
792 Accepts a step-context, a result-type, and a textual reason, exercises the
793 Harness's step start and step_done methods, and returns a skipped-test result.
794  
795 =cut
796  
797 25 ▶sub skip_step {
Subroutine skip_step: called CC=3 SCAR=11.1
Pod documented
798 25 my ( $self, $context, $type, $reason, $redispatch ) = @_;
799  
800 25 ▶ my $step_name = $redispatch ? 'sub_step' : 'step';
Branch: 1 branch
BranchTrueFalseTotal
$redispatch ? : 0 25 25
801 25 my $step_done_name = $step_name . '_done';
802  
803 # Pretend to start step execution
804 25 $context->harness->$step_name($context);
805  
806 # Create a result object
807 25 my $result = Test::BDD::Cucumber::Model::Result->new(
808 {
809 result => $type,
810 output => '1..0 # SKIP ' . $reason
811 }
812 );
813  
814 # Pretend we executed it
815 25 ▶ $context->harness->add_result($result) unless $redispatch;
Branch: 1 branch
BranchTrueFalseTotal
unless $redispatch 25 0 25
816 25 $context->harness->$step_done_name( $context, $result );
817 25 return $result;
818 }
819  
820 =head1 AUTHOR
821  
822 Peter Sergeant C<pete@clueball.com>
823  
824 =head1 LICENSE
825  
826 Copyright 2019-2023, Erik Huelsmann
827 Copyright 2011-2019, Peter Sergeant; Licensed under the same terms as Perl
828  
829 =cut
830  
831 22 1;
« blib/lib/TAP/Parser/SourceHandler/Feature.pm ↑ Summary blib/lib/Test/BDD/Cucumber/Parser.pm »