Pcuke/StepDefinition.pm

filter: Statementstmt 68.5% 24 / 35 Branchbran n/a Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 7 / 14 Podpod 100% 7 / 7 totaltotal 62.3% 38 / 61 CC 2 SCAR 7.3
CC
2
Cov
63%
add_step
6.0
Given
2.0
When
2.0
CRAP 2.1
« Pcuke/World.pm ↑ Summary Pcuke/Executor/StepFailure.pm »
1 package Test::Pcuke::StepDefinition;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Test::Pcuke::Executor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Test::Pcuke::Executor::StepFailure;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Test::Pcuke::Expectation;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our @EXPORT = qw{Given When Then And But expect};
15  
16 1 our $_executor;
17  
18 =head1 NAME
19  
20 Test::Pcuke::StepDefinition - Provides the commands for steps' definitions
21  
22 =head1 SYNOPSIS
23  
24 This module is used to define steps. If you are L<pcuke> user
25 then you probably want save your step definitions to
26 <your project>/features/step_definitions/steps.pm. Content
27 of that file could look like this:
28  
29 package steps;
30 use utf8; # for i18n-zed regexps
31 use warnings;
32 use strict;
33
34 use Test::Pcuke::StepDefinition;
35
36 Given qr{^a calculator instance$} => sub {
37 my ($world, $text, $table) = @_;
38 $world->{_calculator} = Local::Adder->new;
39 };
40
41 Given qr{^I have entered "([^"]+)" into the calculator} => sub {
42 my ($world, $text, $table) = @_;
43 push @{ $world->{_arguments} }, $1;
44 };
45  
46 When qr{^I press "([^"]+)"$} => sub {
47 my ($world, $text, $table) = @_;
48 my ($a1, $a2) = @{ $world->{_arguments} };
49 $world->{_result} = $world->{_calculator}->add( $a1, $a2 );
50 };
51  
52 Then qr{^the result should be "([^"]+)" on the screen$} => sub {
53 my ($world, $text, $table) = @_;
54 expect( $world->{_result} )->equals($1);
55 };
56
57 1; # Do not forget this number. pcuke uses require
58  
59 Note that if you can use native language in *.feature file
60 you can't currently use native language for Given, When etc.
61  
62  
63 =head1 EXPORT
64  
65 Given, When, Then, And, But
66  
67 =head1 SUBROUTINES
68  
69 =head2 Given $regexp $coderef
70  
71 Synonym of the add_step.
72  
73 =cut
74  
75 0 ▶sub Given ($$) {
Subroutine Given: not called CC=1 SCAR=6.9
Pod documented
76 0 my ($regexp, $coderef) = @_;
77 0 add_step('GIVEN', $regexp, $coderef);
78 }
79  
80 =head2 When $regexp $coderef
81  
82 Synonym of the add_step.
83  
84 =cut
85  
86 0 ▶sub When ($$) {
Subroutine When: not called CC=1 SCAR=6.9
Pod documented
87 0 add_step('WHEN',@_);
88 }
89  
90 =head2 Then $regexp $coderef
91  
92 Synonym of the add_step.
93  
94 =cut
95  
96 0 ▶sub Then ($$) {
Subroutine Then: not called CC=1 SCAR=6.9
Pod documented
97 0 add_step('THEN',@_)
98 }
99  
100 =head2 And $regexp $coderef
101  
102 Synonym of the add_step.
103  
104 =cut
105  
106 0 ▶sub And ($$) {
Subroutine And: not called CC=1 SCAR=6.9
Pod documented
107 0 add_step('AND',@_)
108 }
109  
110 =head2 But $regexp $coderef
111  
112 Synonym of the add_step.
113  
114 =cut
115  
116 0 ▶sub But ($$) {
Subroutine But: not called CC=1 SCAR=6.9
Pod documented
117 0 add_step('BUT',@_)
118 }
119  
120 =head2 add_step $regexp, $coderef
121  
122 Add a definition for a step whose title match $regexp.
123  
124 See L<Test::Pcuke::Executor>->add_definition().
125  
126 =cut
127  
128 0 ▶sub add_step {
Subroutine add_step: not called CC=2 SCAR=17.9
Pod documented
129 0 my ($type, $regexp, $coderef) = @_;
130
131 0 ▶ $_executor ||= Test::Pcuke::Executor->new();
Condition0%0/3
$Test::Pcuke::StepDefinition::_executor ||= "Test::Pcuke::Executor"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Test::Pcuke::StepDefinition::_executor ||= "Test::Pcuke::Executor"->new
$Test::Pcuke::StepDefinition::_executor "Test::Pcuke::Executor"->new
Truth table0%0/3
A ||= B
A= $Test::Pcuke::StepDefinition::_executorB= "Test::Pcuke::Executor"->new
ABresult
1X1
011
000
132 0 $_executor->add_definition(
133 step_type => $type,
134 regexp => $regexp,
135 code => $coderef,
136 );
137 }
138  
139 =head2 expect $object
140  
141 Return L<Test::Pcuke::Expectation> object. Use it for tests in step definitions
142  
143 =cut
144  
145 0 ▶sub expect {
Subroutine expect: not called CC=1 SCAR=6.9
Pod documented
146 0 my ($object) = @_;
147 0 return Test::Pcuke::Expectation->new($object, {throw => 'Test::Pcuke::Executor::StepFailure'});
148 }
149  
150 1 1; # End of Test::Pcuke::StepDefinition
151 __END__
« Pcuke/World.pm ↑ Summary Pcuke/Executor/StepFailure.pm »