Skip to content
Browse files

Feature: Pass regex matches through to the following subroutine (like…

… Ruby's Cukes).
  • Loading branch information...
1 parent cd41b53 commit cc50e0ede49b89eb7189383488189698b9c05cf5 @prat0088 prat0088 committed Feb 28, 2010
Showing with 41 additions and 19 deletions.
  1. +13 −6 lib/Test/Cukes.pm
  2. +10 −4 t/runtest-without-defined-steps.t
  3. +18 −9 t/runtest.t
View
19 lib/Test/Cukes.pm
@@ -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 ($@) {
@@ -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 {
@@ -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.
@@ -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/>.
View
14 t/runtest-without-defined-steps.t
@@ -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
View
27 t/runtest.t
@@ -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.
Something went wrong with that request. Please try again.