Skip to content

Commit

Permalink
Feature: Pass regex matches through to the following subroutine (like…
Browse files Browse the repository at this point in the history
… Ruby's Cukes).
  • Loading branch information
prat0088 committed Feb 28, 2010
1 parent cd41b53 commit cc50e0e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
19 changes: 13 additions & 6 deletions lib/Test/Cukes.pm
Expand Up @@ -55,8 +55,8 @@ sub runtests {
for my $step_pattern (keys %$steps) {
my $cb = $steps->{$step_pattern}->{code};

if ($step =~ m/$step_pattern/) {
eval { $cb->(); };
if (my (@matches) = $step =~ m/$step_pattern/) {
eval { $cb->(@matches); };
Test::More::ok(!$@, $step_text);

if ($@) {
Expand Down Expand Up @@ -137,8 +137,9 @@ Write your test program like this:
Then it should pass
TEXT
Given qr/the test program is running/, sub {
assert "running";
Given qr/the (.+) program is (.+)/, sub {
my ($program_name, $running_or_failing) = @_;
assert "running program '$program_name'";
};
When qr/it reaches this step/, sub {
Expand Down Expand Up @@ -170,8 +171,10 @@ internally to print messages.
This module implements the Given-When-Then clause only in English. To
uses it in the test programs, feed the feature text into C<feature>
function, defines your step handlers, and then run all the tests by
calling C<runtests>. Each steps should use C<assert> instead of C<ok>
or C<is> to verify desired result.
calling C<runtests>. Step handlers may be defined in separate modules,
as long as those modules are included before C<runtests> is called.
Each step should use C<assert> instead of C<ok> or C<is> to verify
desired result.
If any assertion in the Given block failed, the the corresponding
C<When> and C<Then> blocks are skipped.
Expand All @@ -190,6 +193,10 @@ the documents from L<http://cukes.info>.
Kang-min Liu E<lt>gugod@gugod.orgE<gt>
=head1 CONTRIBUTORS
Tristan Pratt
=head1 SEE ALSO
The official Cucumber web-page, L<http://cukes.info/>.
Expand Down
14 changes: 10 additions & 4 deletions t/runtest-without-defined-steps.t
Expand Up @@ -9,13 +9,19 @@ Feature: foo
I want to bleh
Scenario: blehbleh
Given blah1
When blah2
Given I am a missing person
When blah2
Then blah3
FEATURE_TEXT

# This regex shouldn't match.
my $hit_given = 0;
When qr/I am a missing (animal|alien)/i => sub {
$hit_given = 1;
};

runtests;

is($hit_given, 0, "Correctly never ran the 'when' test");
ok(@Test::Cukes::missing_steps == 3);
pass; # two dummise
pass;
pass; # one dummy
27 changes: 18 additions & 9 deletions t/runtest.t
Expand Up @@ -8,29 +8,38 @@ Feature: foo
I want to bleh
Scenario: blehbleh
Given blah1
When blah2
Then blah3
Given I will say the word 'cake'
When it is my birthday
Then we will eat 28 cakes
FEATURE_TEXT

my @passed;
my @regex_matches;

Given qr/blah1/ => sub {
Given qr/I will say the word '(.+)'/ => sub {
push @passed, 1;
push @regex_matches, shift;

assert @passed == 1;
assert @passed == 1;
assert @regex_matches == 1
};

When qr/blah2/ => sub {
When qr/it is my birthday/ => sub {
push @passed, 2;
assert @passed == 2;
push @regex_matches, shift;

assert @passed == 2;
assert @regex_matches == 2
};

Then qr/blah3/ => sub {
Then qr/we will eat (\d+) (.+)/ => sub {
push @passed, 3;
assert @passed == 3;
push @regex_matches, shift, shift;

assert @passed == 3;
assert @regex_matches == 4;
assert( @{[ 1, 2, 3 ]} == @passed );
assert( @{[ 'cake', undef, 27, 'cakes' ]} == @regex_matches);
};

runtests;

0 comments on commit cc50e0e

Please sign in to comment.