| 1 | 22 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Test::BDD::Cucumber::StepFile 0.87; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Test::BDD::Cucumber::StepFile - Functions for creating and loading Step Definitions | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | version 0.87 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 22 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 22 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 22 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 22 | ▶ | use Scalar::Util qw/reftype/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 22 | ▶ | use Test::BDD::Cucumber::I18n qw(languages langdef keyword_to_subname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 22 | require Exporter; | |||||||||||||||||||||||||||
| 25 | 22 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 26 | 22 | our @EXPORT = qw(Step Transform Before After C S); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 22 | our @definitions; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Provides the Given/When/Then functions, and a method for loading Step Definition | ||||||||||||||||||||||||||||
| 33 | files and returning the steps. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | Defining steps: | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | #!perl | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | use strict; use warnings; use Test::More; | ||||||||||||||||||||||||||||
| 42 | # or: use strict; use warnings; use Test2::V0; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | use Test::BDD::Cucumber::StepFile; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Given 'something', sub { print "YEAH!" } | ||||||||||||||||||||||||||||
| 47 | When qr/smooooth (\d+)/, sub { print "YEEEHAH $1" } | ||||||||||||||||||||||||||||
| 48 | Then qr/something (else)/, sub { S->{'match'} = $1 } | ||||||||||||||||||||||||||||
| 49 | Step qr/die now/, sub { die "now" } | ||||||||||||||||||||||||||||
| 50 | Transform qr/^(\d+)$/, sub { int $1 } | ||||||||||||||||||||||||||||
| 51 | Before sub { setup_db() } | ||||||||||||||||||||||||||||
| 52 | After sub { teardown() } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Loading steps, in a different file: | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | use Test::BDD::Cucumber::StepFile; | ||||||||||||||||||||||||||||
| 57 | my @steps = Test::BDD::Cucumber::StepFile->load('filename_steps.pl'); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head2 Given | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 When | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head2 Then | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 Step | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head2 Transform | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head2 Before | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 After | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Accept a regular expression or string, and a coderef. Some cute tricks ensure | ||||||||||||||||||||||||||||
| 76 | that when you call the C<load()> method on a file with these statements in, | ||||||||||||||||||||||||||||
| 77 | these are returned to it... | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 140 | ▶ | sub _ensure_meta { | ||||||||||||||||||||||||||
|
Subroutine
_ensure_meta: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 82 | 140 | my ($p, $f, $l) = caller(1); | |||||||||||||||||||||||||||
| 83 | 140 | ▶ | if (ref $_[1] and reftype $_[1] eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_[1] and builtin::reftype($_[1]) eq "HASH"
MC/DC50%1/2
ref $_[1] and builtin::reftype($_[1]) eq "HASH"
ref $_[1]
builtin::reftype($_[1]) eq "HASH"
Truth table66%2/3
A and B
A= ref $_[1]B= builtin::reftype($_[1]) eq "HASH"
| |||||||||||||||||||||||||||||
| 84 | 1 | $_[1]->{source} = $f; | |||||||||||||||||||||||||||
| 85 | 1 | $_[1]->{line} = $l; | |||||||||||||||||||||||||||
| 86 | 1 | return @_; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | else { | ||||||||||||||||||||||||||||
| 89 | 139 | return ($_[0], { source => $f, line => $l }, $_[1]); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Mapped to Given, When, and Then as part of the i18n mapping below | ||||||||||||||||||||||||||||
| 94 | 54 | ▶ | sub _Given { push( @definitions, [ Given => _ensure_meta(@_) ] ) } | ||||||||||||||||||||||||||
|
Subroutine
_Given: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 43 | ▶ | sub _When { push( @definitions, [ When => _ensure_meta(@_) ] ) } | ||||||||||||||||||||||||||
|
Subroutine
_When: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 27 | ▶ | sub _Then { push( @definitions, [ Then => _ensure_meta(@_) ] ) } | ||||||||||||||||||||||||||
|
Subroutine
_Then: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub Step { push( @definitions, [ Step => _ensure_meta(@_) ] ) } | ||||||||||||||||||||||||||
|
Subroutine
Step: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 8 | ▶ | sub Transform { push( @definitions, [ Transform => _ensure_meta(@_) ] ) } | ||||||||||||||||||||||||||
|
Subroutine
Transform: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 4 | ▶ | sub Before { push( @definitions, [ Before => _ensure_meta(qr//, @_) ] ) } | ||||||||||||||||||||||||||
|
Subroutine
Before: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 4 | ▶ | sub After { push( @definitions, [ After => _ensure_meta(qr//, @_) ] ) } | ||||||||||||||||||||||||||
|
Subroutine
After: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 22 | my @SUBS; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 22 | for my $language ( languages() ) { | |||||||||||||||||||||||||||
| 107 | 1694 | my $langdef = langdef($language); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1694 | _alias_function( $langdef->{given}, \&_Given ); | |||||||||||||||||||||||||||
| 110 | 1694 | _alias_function( $langdef->{when}, \&_When ); | |||||||||||||||||||||||||||
| 111 | 1694 | _alias_function( $langdef->{then}, \&_Then ); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Hm ... in cucumber, all step defining keywords are the same. | ||||||||||||||||||||||||||||
| 114 | # Here, the parser replaces 'and' and 'but' with the last verb. Tricky ... | ||||||||||||||||||||||||||||
| 115 | # _alias_function( $langdef->{and}, \&And); | ||||||||||||||||||||||||||||
| 116 | # _alias_function( $langdef->{but}, \&But); | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 22 | push @EXPORT, @SUBS; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 5082 | ▶ | sub _alias_function { | ||||||||||||||||||||||||||
|
Subroutine
_alias_function: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 122 | 5082 | my ( $keywords, $f ) = @_; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 5082 | my @keywords = split( '\|', $keywords ); | |||||||||||||||||||||||||||
| 125 | 5082 | for my $word (@keywords) { | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # asterisks won't be aliased to any sub | ||||||||||||||||||||||||||||
| 128 | 8646 | ▶ | next if $word eq '*'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 8646 | my $subname = keyword_to_subname($word); | |||||||||||||||||||||||||||
| 131 | 8646 | ▶ | next unless length $subname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||
| 134 | 22 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 22 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 136 | 22 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 8580 | *$subname = $f; | |||||||||||||||||||||||||||
| 139 | 8580 | push @SUBS, $subname; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head2 C | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head2 S | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Return the context and the Scenario stash, respectively, B<but only when called | ||||||||||||||||||||||||||||
| 149 | inside a step definition>. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # We need an extra level of indirection when we want to support step functions | ||||||||||||||||||||||||||||
| 154 | # loaded into their own packages (which we do, for cleanliness); the exporter | ||||||||||||||||||||||||||||
| 155 | # binds the subs declared below to S and C symbols in the imported-into package | ||||||||||||||||||||||||||||
| 156 | # That prevents us from binding a different function to these symbols at | ||||||||||||||||||||||||||||
| 157 | # execution time. | ||||||||||||||||||||||||||||
| 158 | # We *can* bind the _S and _C functions declared below. | ||||||||||||||||||||||||||||
| 159 | 1067 | ▶ | sub S { _S() } | ||||||||||||||||||||||||||
|
Subroutine
S: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 536 | ▶ | sub C { _C() } | ||||||||||||||||||||||||||
|
Subroutine
C: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub _S { croak "You can only call `S` inside a step definition" } | ||||||||||||||||||||||||||
|
Subroutine
_S: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub _C { croak "You can only call `C` inside a step definition" } | ||||||||||||||||||||||||||
|
Subroutine
_C: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 load | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Loads a file containing step definitions, and returns a list of the steps | ||||||||||||||||||||||||||||
| 168 | defined in it, of the form: | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | ( | ||||||||||||||||||||||||||||
| 171 | [ 'Given', qr/abc/, sub { etc } ], | ||||||||||||||||||||||||||||
| 172 | [ 'Step', 'asdf', sub { etc } ] | ||||||||||||||||||||||||||||
| 173 | ) | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 26 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 26 | my ( $class, $filename ) = @_; | |||||||||||||||||||||||||||
| 179 | { | ||||||||||||||||||||||||||||
| 180 | 26 | local @definitions; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # Debian Jessie with security patches requires an absolute path | ||||||||||||||||||||||||||||
| 183 | 26 | do File::Spec->rel2abs($filename); | |||||||||||||||||||||||||||
| 184 | 26 | ▶ | die "Step file [$filename] failed to load: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 26 | return @definitions; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Peter Sergeant C<pete@clueball.com> | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Copyright 2019-2023, Erik Huelsmann | ||||||||||||||||||||||||||||
| 197 | Copyright 2011-2019, Peter Sergeant; Licensed under the same terms as Perl | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 22 | 1; | |||||||||||||||||||||||||||