Pcuke/Executor.pm

filter: Statementstmt 98.3% 60 / 61 Branchbran 77.7% 14 / 18 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 86.6% 13 / 15 Podpod 100% 7 / 7 totaltotal 93.4% 100 / 107 CC 13 SCAR 10.9
CC
13
Cov
94%
add_definition
6.0
execute
4.0
_try_definition
3.0
CRAP 3.0
« Pcuke/Expectation.pm ↑ Summary Pcuke/World.pm »
1 package Test::Pcuke::Executor;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Encode qw{decode};
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Test::Pcuke::World;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Test::Pcuke::Executor::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 {
13 4 my $self; # this is a singleton
14
15 =head1 NAME
16  
17 Test::Pcuke::StepRunner - Runner of the steps.
18  
19 =head1 METHODS
20  
21 =head2 new
22  
23 Returns singleton object
24  
25 =cut
26  
27 5 ▶ sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
28 5 my ($class, $encoding) = @_;
29
30 5 ▶ $self ||= bless ( {
Condition100%2/2
$self ||= bless({"_step_definitions", {}, "_encoding", $encoding}, $class)
l!l
14
MC/DC100%1/1
$self ||= bless({"_step_definitions", {}, "_encoding", $encoding}, $class)
$self
Truth table100%2/2
A
A= $self
Aresult
11
00
31 _step_definitions => {},
32 _encoding => $encoding,
33 }, $class );
34
35 5 $self->set_world();
36
37 5 return $self;
38 }
39
40 =head2 destroy
41  
42 Destroys the singleton so that the next new() creates another object
43  
44 =cut
45  
46 4 ▶ sub destroy { undef $self }
Subroutine destroy: called CC=1 SCAR=0.0
Pod documented
47 }
48  
49 =head2 add_definition %definition
50  
51 Adds a step definition. Keys of the %definition are:
52  
53 =over
54  
55 =item step_type
56 One of 'GIVEN', 'WHEN', 'THEN', 'AND', 'BUT', '*'
57  
58 =item regexp
59 A regular expression against that the step title should match
60  
61 =item coderef
62 A reference to the code which should be executed if the
63 title of the step matches the regexp
64  
65 =back
66  
67 =cut
68
69 4 ▶sub add_definition {
Subroutine add_definition: called CC=6 SCAR=18.0
Pod documented
70 11 my $self = shift;
71 11 my %args = @_;
72 11 my %def;
73
74 11 my @known_keys = qw{step_type regexp code};
75
76 11 @def{ @known_keys } = @args{ @known_keys };
77
78 $def{step_type} = $def{step_type} ?
79 uc $def{step_type}
80 11 ▶ : q{};
Branch: 1 branch
BranchTrueFalseTotal
$def{'step_type'} ? : 11 0 11
81
82 confess 'Incorrect step type. Must be GIVEN|WHEN|THEN|AND'
83 11 ▶ unless grep { $_ eq $def{step_type} } qw{GIVEN WHEN THEN AND BUT *};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $def{"step_type"};} "GIVEN", "WHEN", "THEN", "AND", "BUT", "*" 1 10 11
84  
85 confess 'Regexp required'
86 10 ▶ unless $def{regexp};
Branch: 1 branch
BranchTrueFalseTotal
unless $def{"regexp"} 1 9 10
87
88 $def{regexp} = decode($self->{_encoding}, $def{regexp})
89 9 ▶ if $self->{_encoding};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_encoding"} 0 9 9
90
91 9 ▶ $def{code} ||= \&_nop;
Condition100%2/2
$def{"code"} ||= \&Test::Pcuke::Executor::_nop
l!l
45
MC/DC100%1/1
$def{"code"} ||= \&Test::Pcuke::Executor::_nop
$def{"code"}
Truth table100%2/2
A
A= $def{"code"}
Aresult
11
00
92
93 9 $self->{_step_definitions}->{ $def{regexp} } = $def{code};
94 }
95  
96 =head2 execute $step
97  
98 Executes the step. Finds the first definition to whose regexp the step title match,
99 then calls code of that definition passing to it three arguments
100  
101 =over
102  
103 =item $world
104 Test::Pcuke::World, the object, which is the same for any step inside a scenario
105  
106 =item $text
107 A multiline text associated with the step in *.feature file
108  
109 =item $table
110 A Test::Pcuke::Gherkin::Node::Table object for the table associated with the step in
111 the *.feature file
112  
113 =back
114  
115 Returns Test::Pcuke::Executor::Status object with the status of the execution, which
116 is 'undef' if no suitable definition is found.
117  
118 =cut
119  
120 3 ▶sub execute {
Subroutine execute: called CC=4 SCAR=13.9
Pod documented
121 3 my ($self, $step) = @_;
122 3 my $title = $step->title;
123
124 3 my ($status, $exception) = ('undef', undef);
125
126 3 foreach( keys %{ $self->{_step_definitions} } ) {
127 3 ▶ if ( $title =~ $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ /$_/) 2 1 3
128 2 $exception = $self->_try_definition($_, $step);
129 2 ▶ $status = $exception ? 'fail' : 'pass';
Branch: 1 branch
BranchTrueFalseTotal
$exception ? : 1 1 2
130 2 last;
131 }
132 }
133
134 3 return Test::Pcuke::Executor::Status->new($status, $exception);
135 }
136  
137 2 ▶sub _try_definition {
Subroutine _try_definition: called CC=3 SCAR=11.0
138 2 my ($self, $key, $step) = @_;
139
140 2 my $exception;
141
142 2 local $@;
143 2 eval {
144 2 $self->{_step_definitions}->{$key}->($self->world, $step->text, $step->table);
145 };
146
147 2 $exception = $@;
148
149 2 ▶ if ( my $class = ref $exception ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = ref $exception) 1 1 2
150 1 ▶ die $exception
Branch: 1 branch
BranchTrueFalseTotal
if $class ne "Test::Pcuke::Executor::StepFailure" 0 1 1
151 if $class ne 'Test::Pcuke::Executor::StepFailure';
152 }
153
154 2 return $exception;
155 }
156  
157 =head2 reset_world
158  
159 This method resets the $world object that is passed to step definition
160  
161 =cut
162 0 ▶sub reset_world { $_[0]->set_world }
Subroutine reset_world: not called CC=1 SCAR=6.9
Pod documented
163  
164 =head2 set_world $world?
165
166 Set the new world
167  
168 =cut
169  
170 5 ▶sub set_world {
Subroutine set_world: called CC=2 SCAR=7.2
Pod documented
171 5 my ($self, $new_world) = @_;
172
173 5 ▶ $self->{_world} = $new_world ?
Branch: 1 branch
BranchTrueFalseTotal
$new_world ? : 0 5 5
174 $new_world
175 : Test::Pcuke::World->new;
176 }
177  
178 =head2 world
179  
180 Returns the current world object that is passed to step definitions
181  
182 =cut
183  
184 2 ▶sub world { $_[0]->{_world} }
Subroutine world: called CC=1 SCAR=0.0
Pod documented
185  
186  
187 0 ▶sub _nop { }
Subroutine _nop: not called CC=1 SCAR=0.0
188  
189 4 1; # End of Test::Pcuke::StepRunner
190 __END__
« Pcuke/Expectation.pm ↑ Summary Pcuke/World.pm »