1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
package Test::BDD::Cucumber::StepContext;
$Test::BDD::Cucumber::StepContext::VERSION = '0.75';
use Moo;
use Types::Standard qw( Bool Str HashRef ArrayRef InstanceOf );
use List::Util qw( first );
=head1 NAME
Test::BDD::Cucumber::StepContext - Data made available to step definitions
=head1 VERSION
version 0.75
=head1 DESCRIPTION
The coderefs in Step Definitions have a single argument passed to them, a
C<Test::BDD::Cucumber::StepContext> object. This is an attribute-only class,
populated by L<Test::BDD::Cucumber::Executor>.
When steps are run normally, C<C()> is set directly before execution to return
the context; this allows you to do:
sub { return C->columns }
instead of:
sub { my $c = shift; return $c->columns; }
=head1 ATTRIBUTES
=head2 columns
If the step-specific data supplied is a table, the this attribute will contain
the column names in the order they appeared.
=cut
has 'columns' => ( is => 'ro', isa => ArrayRef );
=head2 _data
Step-specific data. Will either be a text string in the case of a """ string, or
an arrayref of hashrefs if the step had an associated table.
See the C<data> method below.
=cut
has '_data' =>
( is => 'ro', isa => Str|ArrayRef, init_arg => 'data', default => '' );
=head2 stash
A hash of hashes, containing two keys, C<feature>, C<scenario>.
The stash allows you to persist data across features or scenarios.
The scenario-level stash is also available to steps by calling C<S()>, making
the following two lines of code equivalent:
sub { my $context = shift; my $stash = $context->stash->{'scenario'}; $stash->{'count'} = 1 }
sub { S->{'count'} = 1 }
=cut
has 'stash' => ( is => 'ro', required => 1, isa => HashRef );
=head2 feature
=head2 scenario
=head2 step
Links to the L<Test::BDD::Cucumber::Model::Feature>,
L<Test::BDD::Cucumber::Model::Scenario>, and L<Test::BDD::Cucumber::Model::Step>
objects respectively.
=cut
has 'feature' => (
is => 'ro',
required => 1,
isa => InstanceOf['Test::BDD::Cucumber::Model::Feature']
);
has 'scenario' => (
is => 'ro',
required => 1,
isa => InstanceOf['Test::BDD::Cucumber::Model::Scenario']
);
has 'step' =>
( is => 'ro', required => 0, isa => InstanceOf['Test::BDD::Cucumber::Model::Step'] );
=head2 verb
The lower-cased verb a Step Definition was called with.
=cut
has 'verb' => ( is => 'ro', required => 1, isa => Str );
=head2 text
The text of the step, minus the verb. Placeholders will have already been
multiplied out at this point.
=cut
has 'text' => ( is => 'ro', required => 1, isa => Str, default => '' );
=head2 harness
The L<Test::BDD::Cucumber::Harness> harness being used by the executor.
=cut
has 'harness' =>
( is => 'ro', required => 1, isa => InstanceOf['Test::BDD::Cucumber::Harness'] );
=head2 executor
Weak reference to the L<Test::BDD::Cucumber::Executor> being used - this allows
for step redispatch.
=cut
has 'executor' => (
is => 'ro',
required => 1,
isa => InstanceOf['Test::BDD::Cucumber::Executor'],
weak_ref => 1
);
=head2 matches
Any matches caught by the Step Definition's regex. These are also available as
C<$1>, C<$2> etc as appropriate.
=cut
has '_matches' => (
is => 'rw',
isa => ArrayRef,
init_arg => 'matches',
default => sub { [] }
);
has 'transformers' =>
( is => 'ro', isa => ArrayRef, predicate => 'has_transformers', );
has '_transformed_matches' => (
is => 'ro',
isa => ArrayRef,
lazy => 1,
builder => '_build_transformed_matches',
clearer => '_clear_transformed_matches',
);
has '_transformed_data' => (
is => 'ro',
isa => Str|ArrayRef,
lazy => 1,
builder => '_build_transformed_data',
clearer => '_clear_transformed_data',
);
=head2 is_hook
The harness processing the output can decide whether to shop information for
this step which is actually an internal hook, i.e. a Before or After step
=cut
has 'is_hook' =>
( is => 'ro', isa => Bool, lazy => 1, builder => '_build_is_hook' );
=head2 parent
If a step redispatches to another step, the child step will have a link back to
its parent step here; otherwise undef. See L</Redispatching>.
=cut
has 'parent' => ( is => 'ro', isa => InstanceOf['Test::BDD::Cucumber::StepContext'] );
=head1 METHODS
=head2 background
Boolean for "is this step being run as part of the background section?".
Currently implemented by asking the linked Scenario object...
=cut
sub background { my $self = shift; return $self->scenario->background }
=head2 data
See the C<_data> attribute above.
Calling this method will return either the """ string, or a possibly Transform-ed
set of table data.
=cut
sub data {
my $self = shift;
if (@_) {
$self->_data(@_);
$self->_clear_transformed_data;
return;
}
return $self->_transformed_data;
}
=head2 matches
See the C<_matches> attribute above.
Call this method will return the possibly Transform-ed matches .
=cut
sub matches {
my $self = shift;
if (@_) {
$self->_matches(@_);
$self->_clear_transformed_matches;
return;
}
return $self->_transformed_matches;
}
=head2 transform
Used internally to transform data and placeholders, but it can also be called
from within your Given/When/Then code.
=cut
sub transform {
my $self = shift;
my $value = shift;
defined $value or return $value;
TRANSFORM:
for my $transformer ( @{ $self->transformers } ) {
# turn off this warning so undef can be set in the following regex
no warnings 'uninitialized';
# uses the same magic as other steps
# and puts any matches into $1, $2, etc.
# and calls the Transform step
# also, if the transformer code ref returns undef, this will be coerced
# into an empty string, so need to mark it as something else
# and then turn it into proper undef
if (
$value =~ s/$transformer->[0]/
my $value = $transformer->[2]->( $self );
defined $value ? $value : '__UNDEF__'
/e
)
{
# if we matched then stop processing this match
return $value eq '__UNDEF__' ? undef : $value;
}
}
# if we're here, the value will be returned unchanged
return $value;
}
=head1 Redispatching
Sometimes you want to call one step from another step. You can do this via the
I<StepContext>, using the C<dispatch()> method. For example:
Given qr/I have entered (\d+)/, sub {
C->dispatch( 'Given', "I have pressed $1");
C->dispatch( 'Given', "I have pressed enter", { some => 'data' } );
};
You redispatch step will have its own, new step context with almost everything
copied from the parent step context. However, specifically not copied are:
C<columns>, C<data>, the C<step> object, and of course the C<verb> and the
C<text>.
If you want to pass data to your child step, you should IDEALLY do it via the
text of the step itself, or failing that, through the scenario-level stash.
Otherwise it'd make more sense just to be calling some subroutine... But you
B<can> pass in a third argument - a hashref which will be used as C<data>.
If the step you dispatch to doesn't pass for any reason (can't be found, dies,
fails, whatever), it'll throw an exception. This will get caught by the parent
step, which will then fail, and show debugging output.
B<You must use the English names for the step verb, because we have no access to
the parser. Also, remember to quote them as if you're in a step file, there may
be a subroutine defined with the same name.>
=head2 dispatch
C->dispatch( 'Then', "the page has loaded successfully");
See the paragraphs immediately above this
=cut
sub dispatch {
my ( $self, $verb, $text, $data ) = @_;
my $step = Test::BDD::Cucumber::Model::Step->new(
{
text => $text,
verb => $verb,
line => Test::BDD::Cucumber::Model::Line->new(
{
number => $self->step->line->number,
raw_content => "[Redispatched step: $verb $text]",
document => $self->step->line->document,
}
),
}
);
my $columns;
if ($data) {
if ( ref $data eq 'HASH' ) {
$columns = [ sort keys %$data ];
}
}
my $new_context = $self->new(
{
executor => $self->executor,
( $data ? ( data => $data ) : () ),
( $columns ? ( columns => $columns ) : () ),
stash => {
feature => $self->stash->{'feature'},
scenario => $self->stash->{'scenario'},
step => {},
},
feature => $self->feature,
scenario => $self->scenario,
harness => $self->harness,
transformers => $self->transformers,
step => $step,
verb => lc($verb),
text => $text,
}
);
my $result = $self->executor->find_and_dispatch( $new_context, 0, 1 );
# If it didn't pass, short-circuit the rest
unless ( $result->result eq 'passing' ) {
my $error = "Redispatched step didn't pass:\n";
$error .= "\tStatus: " . $result->result . "\n";
$error .= "\tOutput: " . $result->output . "\n";
$error .= "Failure to redispatch a step causes the parent to fail\n";
die $error;
}
return $result;
}
# the builder for the is_hook attribute
sub _build_is_hook {
my $self = shift;
return ( $self->verb eq 'before' or $self->verb eq 'after' ) ? 1 : 0;
}
# the builder for the _transformed_matches attribute
sub _build_transformed_matches {
my $self = shift;
my @transformed_matches = @{ $self->_matches };
# this stops it recursing forever...
# and only Transform if there are any to process
if ( $self->verb ne 'transform'
and $self->has_transformers )
{
@transformed_matches = map {
my $match = $_;
$match = $self->transform($match);
} @transformed_matches;
}
return \@transformed_matches;
}
# the builder for the _transformed_data attribute
sub _build_transformed_data {
my $self = shift;
my $transformed_data = $self->_data;
# again stop recursing
# only transform table data
# and only Transform if there are any to process
if ( $self->verb ne 'transform'
and ref $transformed_data
and $self->has_transformers )
{
# build the string that a Transform is looking for
# table:column1,column2,column3
my $table_text = 'table:' . join( ',', @{ $self->columns } );
if ( my $transformer =
first { $table_text =~ $_->[0] } @{ $self->transformers } )
{
# call the Transform step
$transformer->[2]->( $self, $transformed_data );
}
}
return $transformed_data;
}
=head1 AUTHOR
Peter Sergeant C<pete@clueball.com>
=head1 LICENSE
Copyright 2019-2020, Erik Huelsmann
Copyright 2011-2019, Peter Sergeant; Licensed under the same terms as Perl
=cut
1;
|