| 1 | package Test::BDD::Cucumber::Definitions; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use DDP ( show_unicode => 1 ); | ||
| 8 | use Exporter qw(import); | ||
| 9 | use Moose::Util::TypeConstraints; | ||
| 10 | use Params::ValidationCompiler qw(validation_for); | ||
| 11 | use Test::BDD::Cucumber::StepFile qw(Given When Then); | ||
| 12 | use Test::More; | ||
| 13 | use Try::Tiny; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Test::BDD::Cucumber::Definitions - a collection of step definitions for Test | ||
| 18 | Driven Development | ||
| 19 | |||
| 20 | =head1 VERSION | ||
| 21 | |||
| 22 | Version 0.41 | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | our $VERSION = '0.41'; | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | In file B<features/step_definitions/tbcd_steps.pl>: | ||
| 31 | |||
| 32 | #!/usr/bin/perl | ||
| 33 | |||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | |||
| 37 | use Test::BDD::Cucumber::Definitions::TBCD::In; | ||
| 38 | |||
| 39 | In file B<features/site.feature>: | ||
| 40 | |||
| 41 | Feature: Site | ||
| 42 | Site tests | ||
| 43 | |||
| 44 | Scenario: Loading the page | ||
| 45 | When http request "GET" send "http://metacpan.org" | ||
| 46 | Then http response code eq "200" | ||
| 47 | |||
| 48 | ... and, finally, in the terminal: | ||
| 49 | |||
| 50 | $ pherkin | ||
| 51 | |||
| 52 | Site | ||
| 53 | Site tests | ||
| 54 | |||
| 55 | Scenario: Loading the page | ||
| 56 | When http request "GET" send "http://metacpan.org" | ||
| 57 | Then http response code eq "200" | ||
| 58 | |||
| 59 | |||
| 60 | =head1 EXPORT | ||
| 61 | |||
| 62 | The module exports functions C<S>, C<C>, C<Given>, C<When> and C<Then>. | ||
| 63 | These functions are identical to the same functions from the module | ||
| 64 | L<Test::BDD::Cucumber>. | ||
| 65 | |||
| 66 | Additionally, the module exports several functions for parameter validation. | ||
| 67 | These functions are exported by the C<:validator> tag. | ||
| 68 | |||
| 69 | By default, no functions are exported. All functions must be imported | ||
| 70 | explicitly. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | our @EXPORT_OK = qw( | ||
| 75 | S C Given When Then | ||
| 76 | validator_i | ||
| 77 | validator_n | ||
| 78 | validator_s | ||
| 79 | validator_r | ||
| 80 | validator_ni | ||
| 81 | validator_ns | ||
| 82 | validator_nn | ||
| 83 | validator_nr | ||
| 84 | ); | ||
| 85 | |||
| 86 | our %EXPORT_TAGS = ( | ||
| 87 | validator => [ | ||
| 88 | qw( | ||
| 89 | validator_i | ||
| 90 | validator_n | ||
| 91 | validator_s | ||
| 92 | validator_r | ||
| 93 | validator_ni | ||
| 94 | validator_ns | ||
| 95 | validator_nn | ||
| 96 | validator_nr | ||
| 97 | ) | ||
| 98 | ] | ||
| 99 | ); | ||
| 100 | |||
| 101 | sub S { return Test::BDD::Cucumber::StepFile::S } | ||
| 102 | sub C { return Test::BDD::Cucumber::StepFile::C } | ||
| 103 | |||
| 104 | # Interpolation of variables (scenario and environment) | ||
| 105 | sub _interpolate { | ||
| 106 | my ($value) = @_; | ||
| 107 | |||
| 108 | my $orig = $value; | ||
| 109 | |||
| 110 | # Scenario variables | ||
| 111 | my $is = $value =~ s| S\{ (.+?) \} | | ||
| 112 | S->{Var}->scenario($1) // q{}; | ||
| 113 | |gxe; | ||
| 114 | |||
| 115 | # Environment variables | ||
| 116 | my $ie = $value =~ s/ \$\{ (.+?) \} / | ||
| 117 | $ENV{$1} || ''; | ||
| 118 | /gxe; | ||
| 119 | |||
| 120 | if ( $is || $ie ) { | ||
| 121 | diag( sprintf( q{Inteprolated value "%s" = %s}, $orig, np $value) ); | ||
| 122 | } | ||
| 123 | |||
| 124 | return $value; | ||
| 125 | } | ||
| 126 | |||
| 127 | # TbcdInt | ||
| 128 | subtype( | ||
| 129 | 'TbcdInt', | ||
| 130 | as 'Int', | ||
| 131 | message { | ||
| 132 | sprintf( '%s is not a valid TBCD Int', np $_); | ||
| 133 | } | ||
| 134 | ); | ||
| 135 | |||
| 136 | coerce( | ||
| 137 | 'TbcdInt', | ||
| 138 | from 'Str', | ||
| 139 | via { _interpolate $_ } | ||
| 140 | ); | ||
| 141 | |||
| 142 | # TbcdStr | ||
| 143 | subtype( | ||
| 144 | 'TbcdStr', | ||
| 145 | as 'Str', | ||
| 146 | message { | ||
| 147 | sprintf( '%s is not a valid TBCD Str', np $_); | ||
| 148 | } | ||
| 149 | ); | ||
| 150 | |||
| 151 | coerce( | ||
| 152 | 'TbcdStr', | ||
| 153 | from 'Str', | ||
| 154 | via { _interpolate $_} | ||
| 155 | ); | ||
| 156 | |||
| 157 | # TbcdNonEmptyStr | ||
| 158 | subtype( | ||
| 159 | 'TbcdNonEmptyStr', | ||
| 160 | as 'Str', | ||
| 161 | where { length($_) > 0 }, | ||
| 162 | message { | ||
| 163 | sprintf( '%s is not a valid TBCD NonEmptyStr', np $_); | ||
| 164 | } | ||
| 165 | ); | ||
| 166 | |||
| 167 | coerce( | ||
| 168 | 'TbcdNonEmptyStr', | ||
| 169 | from 'Str', | ||
| 170 | via { _interpolate $_} | ||
| 171 | ); | ||
| 172 | |||
| 173 | # TbcdRegexpRef | ||
| 174 | subtype( | ||
| 175 | 'TbcdRegexpRef', | ||
| 176 | as 'RegexpRef', | ||
| 177 | message { | ||
| 178 | sprintf( '%s is not a valid TBCD RegexpRef', np $_); | ||
| 179 | } | ||
| 180 | ); | ||
| 181 | |||
| 182 | coerce( | ||
| 183 | 'TbcdRegexpRef', | ||
| 184 | from 'Str', | ||
| 185 | via { | ||
| 186 | my $value = _interpolate $_; | ||
| 187 | |||
| 188 | try { | ||
| 189 | qr/$value/; ## no critic [RegularExpressions::RequireExtendedFormatting] | ||
| 190 | } | ||
| 191 | catch { | ||
| 192 | return $value; | ||
| 193 | }; | ||
| 194 | } | ||
| 195 | ); | ||
| 196 | |||
| 197 | my $validator_i = validation_for( | ||
| 198 | params => [ | ||
| 199 | |||
| 200 | # value integer | ||
| 201 | { type => find_type_constraint('TbcdInt') }, | ||
| 202 | ] | ||
| 203 | ); | ||
| 204 | |||
| 205 | sub validator_i { | ||
| 206 | return $validator_i; | ||
| 207 | } | ||
| 208 | |||
| 209 | my $validator_n = validation_for( | ||
| 210 | params => [ | ||
| 211 | |||
| 212 | # name | ||
| 213 | { type => find_type_constraint('TbcdNonEmptyStr') }, | ||
| 214 | ] | ||
| 215 | ); | ||
| 216 | |||
| 217 | sub validator_n { | ||
| 218 | return $validator_n; | ||
| 219 | } | ||
| 220 | |||
| 221 | my $validator_s = validation_for( | ||
| 222 | params => [ | ||
| 223 | |||
| 224 | # value string | ||
| 225 | { type => find_type_constraint('TbcdStr') }, | ||
| 226 | ] | ||
| 227 | ); | ||
| 228 | |||
| 229 | sub validator_s { | ||
| 230 | return $validator_s; | ||
| 231 | } | ||
| 232 | |||
| 233 | my $validator_r = validation_for( | ||
| 234 | params => [ | ||
| 235 | |||
| 236 | # value regexp | ||
| 237 | { type => find_type_constraint('TbcdRegexpRef') } | ||
| 238 | ] | ||
| 239 | ); | ||
| 240 | |||
| 241 | sub validator_r { | ||
| 242 | return $validator_r; | ||
| 243 | } | ||
| 244 | |||
| 245 | my $validator_ni = validation_for( | ||
| 246 | params => [ | ||
| 247 | |||
| 248 | # name | ||
| 249 | { type => find_type_constraint('TbcdNonEmptyStr') }, | ||
| 250 | |||
| 251 | # value int | ||
| 252 | { type => find_type_constraint('TbcdInt') }, | ||
| 253 | ] | ||
| 254 | ); | ||
| 255 | |||
| 256 | sub validator_ni { | ||
| 257 | return $validator_ni; | ||
| 258 | } | ||
| 259 | |||
| 260 | my $validator_ns = validation_for( | ||
| 261 | params => [ | ||
| 262 | |||
| 263 | # name | ||
| 264 | { type => find_type_constraint('TbcdNonEmptyStr') }, | ||
| 265 | |||
| 266 | # value string | ||
| 267 | { type => find_type_constraint('TbcdStr') } | ||
| 268 | ] | ||
| 269 | ); | ||
| 270 | |||
| 271 | sub validator_ns { | ||
| 272 | return $validator_ns; | ||
| 273 | } | ||
| 274 | |||
| 275 | my $validator_nn = validation_for( | ||
| 276 | params => [ | ||
| 277 | |||
| 278 | # value non empty string | ||
| 279 | { type => find_type_constraint('TbcdNonEmptyStr') }, | ||
| 280 | |||
| 281 | # value non empty string | ||
| 282 | { type => find_type_constraint('TbcdNonEmptyStr') }, | ||
| 283 | ] | ||
| 284 | ); | ||
| 285 | |||
| 286 | sub validator_nn { | ||
| 287 | return $validator_nn; | ||
| 288 | } | ||
| 289 | |||
| 290 | my $validator_nr = validation_for( | ||
| 291 | params => [ | ||
| 292 | |||
| 293 | # name | ||
| 294 | { type => find_type_constraint('TbcdNonEmptyStr') }, | ||
| 295 | |||
| 296 | # value regexp | ||
| 297 | { type => find_type_constraint('TbcdRegexpRef') } | ||
| 298 | ] | ||
| 299 | ); | ||
| 300 | |||
| 301 | sub validator_nr { | ||
| 302 | return $validator_nr; | ||
| 303 | } | ||
| 304 | |||
| 305 | =head1 AUTHOR | ||
| 306 | |||
| 307 | Mikhail Ivanov C<< <m.ivanych@gmail.com> >> | ||
| 308 | |||
| 309 | =head1 LICENSE AND COPYRIGHT | ||
| 310 | |||
| 311 | Copyright 2018 Mikhail Ivanov. | ||
| 312 | |||
| 313 | This is free software; you can redistribute it and/or modify it | ||
| 314 | under the same terms as the Perl 5 programming language system itself. | ||
| 315 | |||
| 316 | =cut | ||
| 317 | |||
| 318 | 1; |