Gherkin/Node/Step.pm

filter: Statementstmt 100% 69 / 69 Branchbran 90.0% 18 / 20 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 24 / 24 Podpod 93.7% 15 / 16 totaltotal 96.4% 137 / 142 CC 15 SCAR 9.9
CC
15
Cov
97%
_make_result_object
7.0
title
4.0
type
2.1
CRAP 2.7
« Gherkin/Node/Scenario.pm ↑ Summary Gherkin/I18n.pm »
1 package Test::Pcuke::Gherkin::Node::Step;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base 'Test::Pcuke::Gherkin::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Test::Pcuke::Gherkin::Executor::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Test::Pcuke::Gherkin::Node::Step - Step of the scenario of the feature
14  
15 =head1 SYNOPSIS
16  
17 This module is used internally by L<Test::Pcuke::Gherkin>
18
19 =head1 METHODS
20  
21 =head2 new $conf
22  
23 Creates new step. $conf is a hashref that may contain
24 the settings for the object. Without $conf the corresponding properties are
25 unset. The keys of hashref are
26  
27 =over
28  
29 =item type
30 Type of the step: 'Given', 'when', 'then', 'and', 'but', '*'
31
32 =item title
33 Title of the step (probably without the type)
34  
35 =item text
36 Multiline text associated with the step
37  
38 =item table
39 Table associated with the step
40  
41 =item executor
42 Executor object, see L<Test::Pcuke::Gherkin>
43  
44 =back
45  
46 =cut
47  
48 26 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
49 26 my ($self, $args) = @_;
50
51 26 my $new_step = $self->SUPER::new(
52 immutables => [qw{type title text table executor}],
53 args => $args,
54 );
55
56 26 return $new_step;
57
58 }
59  
60 =head2 type
61  
62 Returns the type of the step
63  
64 =cut
65  
66 2 ▶sub type { $_[0]->_get_immutable('type') || q{}; }
Subroutine type: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_get_immutable("type") or "" 2 0 2
67  
68 =head2 set_type $type
69  
70 Sets the type of the step. Type is an immutable property
71 and may be set only once. Trying to set it more than once
72 is a fatal error
73  
74 =cut
75  
76 3 ▶sub set_type {
Subroutine set_type: called CC=1 SCAR=0.0
Pod documented
77 3 my ($self, $type) = @_;
78 3 $self->_set_immutable('type', $self->_normalize_type($type));
79 }
80  
81 # TODO I18n ????
82 3 ▶sub _normalize_type {
Subroutine _normalize_type: called CC=2 SCAR=6.9
83 3 my ($self, $type) = @_;
84 3 $type = uc $type;
85 confess q{The type of the step must be one of 'GIVEN',WHEN','THEN','AND', 'BUT', '*'}
86 3 ▶ unless grep { $_ eq $type } qw{GIVEN WHEN THEN AND BUT *};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $type;} "GIVEN", "WHEN", "THEN", "AND", "BUT", "*" 1 2 3
87 2 return $type;
88 }
89  
90  
91 =head2 title
92  
93 Returns the title of the step. If the step is parametrized,
94 i.e. the original title contails placeholders in angle brackets
95 (Scenario Outline steps), they are replaced with the step parameters
96 if those are set.
97  
98 =cut
99  
100 4 ▶sub title {
Subroutine title: called CC=4 SCAR=13.9
Pod documented
101 4 my $self = shift;
102 4 my $title = $self->_get_immutable('title');
103
104 4 ▶ return q{} unless $title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 1 3 4
105
106 3 my $params = $self->_get_property('_step_parameters');
107
108 3 ▶ return $title unless $params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params 2 1 3
109
110 1 foreach my $p (keys %$params) {
111 1 my $v = $params->{$p};
112 1 $title =~ s/<$p>/"$v"/g;
113 }
114
115 1 return $title;
116 }
117  
118 =head2 set_title $title
119  
120 Sets the title. Title is an immutable property.
121  
122 =cut
123  
124 2 ▶sub set_title { $_[0]->_set_immutable('title', $_[1]); }
Subroutine set_title: called CC=1 SCAR=0.0
Pod documented
125  
126 =head2 set_text
127  
128 Sets the text. Text is an immutable property
129  
130 =cut
131  
132 1 ▶sub set_text {
Subroutine set_text: called CC=1 SCAR=0.0
Pod documented
133 1 my ($self, $text) = @_;
134 1 $self->_set_immutable('text', $text);
135 }
136  
137 =head2
138  
139 Returns the text associated with the step.
140  
141 =cut
142  
143 4 ▶sub text { $_[0]->_get_immutable('text') }
Subroutine text: called CC=1 SCAR=0.0
Pod documented
144  
145 =head2
146  
147 Returns the table associated with the step
148  
149 =cut
150  
151 3 ▶sub table { $_[0]->_get_immutable('table') }
Subroutine table: called CC=1 SCAR=0.0
Pod documented
152  
153 =head2 set_table
154  
155 Returns the tyable associated with the text
156  
157 =cut
158  
159 3 ▶sub set_table {
Subroutine set_table: called CC=1 SCAR=0.0
Pod documented
160 3 my($self, $table) = @_;
161 3 $self->_set_immutable('table', $table );
162 }
163  
164 =head2 set_params $hash
165  
166 Sets the step parameters which are used to replace the
167 placeholders in the title if any. For example,
168 if the title is 'm = <m>' the hash is {m => 'MMM'}
169 then the title() returns 'm = "MMM"'
170  
171 Note: this method is used in execute() and probably
172 should be private. Don't use it %-)
173  
174 =cut
175  
176 2 ▶sub set_params {
Subroutine set_params: called CC=2 SCAR=7.1
Pod documented
177 2 my ($self, $hash) = @_;
178 2 ▶ confess "parameters must be a hash!"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 2 2
179 unless ref $hash eq 'HASH';
180
181 2 $self->_set_property('_step_parameters', $hash );
182 }
183  
184 =head2 ubset_params
185  
186 Unsets params that are set by set_params.
187  
188 Must be private, don't use
189  
190 =cut
191  
192 1 ▶sub unset_params {
Subroutine unset_params: called CC=1 SCAR=0.0
Pod undocumented
193 1 my ($self) = @_;
194 1 $self->_set_property('_step_parameters', undef);
195 }
196  
197 =head2 param_names
198  
199 returns the names of the placeholders in the title
200 If title is ' <m> = <n>' then returns ['m', 'n']
201 or ['n', 'm']
202  
203 Must be private?
204  
205 =cut
206  
207 2 ▶sub param_names {
Subroutine param_names: called CC=1 SCAR=0.0
Pod documented
208 2 my $self = shift;
209 2 my %params = map { $_ => 1 } ( $self->_get_immutable('title') =~ /<([^>]+)>/g );
210 2 return [ keys %params ];
211 }
212  
213 =head2 executor
214  
215 Returns the executor object. See L<Test::Pcuke::Gherkin>
216  
217 =cut
218  
219 13 ▶sub executor { $_[0]->_get_immutable('executor') }
Subroutine executor: called CC=1 SCAR=0.0
Pod documented
220  
221 =head2 execute
222  
223 Executes the step.
224  
225 =cut
226  
227 12 ▶sub execute {
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
228 12 my ($self) = @_;
229
230 12 my $result = $self->executor->execute( $self );
231
232 12 $self->_set_result($result);
233  
234 }
235  
236 12 ▶sub _set_result {
Subroutine _set_result: called CC=2 SCAR=6.9
237 12 my ($self, $result) = @_;
238
239 12 ▶ $result = $self->_make_result_object($result)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result 11 1 12
240 unless ref $result;
241
242 12 $self->_set_property('_result', $result);
243 }
244  
245 11 ▶sub _make_result_object {
Subroutine _make_result_object: called CC=7 SCAR=19.5
246 11 my ($self, $result) = @_;
247
248 11 my $normalized = 'undef';
249
250 11 ▶ if ( defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) 10 1 11
251 10 ▶ $normalized = 'pass'
Branch: 1 branch
BranchTrueFalseTotal
if $result =~ /pass/ or $result 8 2 10
Condition100%3/3
$result =~ /pass/ or $result
l!l&&r!l&&!r
262
MC/DC100%2/2
$result =~ /pass/ or $result
$result =~ /pass/ $result
Truth table100%3/3
A or B
A= $result =~ /pass/B= $result
ABresult
1X1
011
000
252 if $result =~ /pass/ || $result;
253 10 ▶ $normalized = 'fail'
Branch: 1 branch
BranchTrueFalseTotal
if $result =~ /fail/ or not $result 4 6 10
Condition100%3/3
$result =~ /fail/ or not $result
l!l&&r!l&&!r
226
MC/DC100%2/2
$result =~ /fail/ or not $result
$result =~ /fail/ not $result
Truth table100%3/3
A or not(B)
A= $result =~ /fail/B= not $result
ABresult
1X1
011
000
254 if $result =~ /fail/ || ! $result;
255 10 ▶ $normalized = 'undef'
Branch: 1 branch
BranchTrueFalseTotal
if $result =~ /undef/ 2 8 10
256 if $result =~ /undef/;
257 }
258
259 11 return Test::Pcuke::Gherkin::Executor::Status->new($normalized);
260 }
261  
262 =head2 result
263  
264 Returns a result object. If the executor returns a string, then
265 L<Test::Pcuke::Gherkin::Executor::Status> object is created
266 with the normalized status which is one of 'pass', 'undef', or 'fail'
267  
268 =cut
269  
270 12 ▶sub result { $_[0]->_get_property('_result') }
Subroutine result: called CC=1 SCAR=0.0
Pod documented
271  
272 =head2 status
273  
274 Returns the status of the sexecuted step. The result object that is returned
275 by executor->execute is asked for that.
276
277 =cut
278  
279 12 ▶sub status {
Subroutine status: called CC=2 SCAR=7.2
Pod documented
280 12 my ($self) = @_;
281 12 ▶ return $self->result->status || 'undef';
Condition50%1/2
$self->result->status || 'undef'
l!l
120
MC/DC0%0/1
$self->result->status || 'undef'
$self->result->status
Truth table50%1/2
A
A= $self->result->status
Aresult
11
00
282 }
283  
284  
285 3 1; # End of Test::Pcuke::Gherkin::Node::Step
286 __END__
« Gherkin/Node/Scenario.pm ↑ Summary Gherkin/I18n.pm »