Permalink
Browse files

import Test-Cukes 0.02 from CPAN

git-cpan-module:   Test-Cukes
git-cpan-version:  0.02
git-cpan-authorid: GUGOD
git-cpan-file:     authors/id/G/GU/GUGOD/Test-Cukes-0.02.tar.gz
  • Loading branch information...
1 parent c3b6d95 commit 5e38ef0c2608dce795341964f236561d4b0422a4 @gugod committed with schwern Jun 4, 2009
Showing with 62 additions and 27 deletions.
  1. +4 −1 Changes
  2. +2 −1 META.yml
  3. +3 −2 Makefile.PL
  4. +47 −18 lib/Test/Cukes.pm
  5. +6 −5 t/runtest.t
View
@@ -1,6 +1,9 @@
Revision history for Perl extension Test::Cukes
+0.02:
+- Count each block as one assertion-based test
+- If pre-condition steps fail, then its successing steps will be skipped.
+
0.01:
- initial version
- basic implementation.
-
View
@@ -22,7 +22,8 @@ no_index:
- xt
requires:
Any::Moose: 0.07
+ Carp::Assert: 0.20
Exporter::Lite: 0.02
resources:
license: http://opensource.org/licenses/mit-license.php
-version: 0.01
+version: 0.02
View
@@ -3,8 +3,9 @@ name 'Test-Cukes';
all_from 'lib/Test/Cukes.pm';
requires
- "Any::Moose" => "0.07",
- "Exporter::Lite" => "0.02";
+ "Carp::Assert" => "0.20",
+ "Any::Moose" => "0.07",
+ "Exporter::Lite" => "0.02";
tests 't/*.t';
author_tests 'xt';
View
@@ -4,9 +4,10 @@ use warnings;
use Exporter::Lite;
use Test::More;
use Test::Cukes::Feature;
+use Carp::Assert;
-our $VERSION = "0.01";
-our @EXPORT = qw(feature runtests Given When Then);
+our $VERSION = "0.02";
+our @EXPORT = qw(feature runtests Given When Then assert affirm should shouldnt);
my $steps = {};
my $feature = {};
@@ -20,20 +21,39 @@ sub feature {
sub runtests {
my $caller = caller;
+ my $total_tests = 0;
+ for my $scenario (@{$feature->{$caller}->scenarios}) {
+ $total_tests += @{$scenario->steps};
+ }
+
+ Test::More::plan(tests => $total_tests);
+
+ my $skip = 0;
+ my $skip_reason = "";
for my $scenario (@{$feature->{$caller}->scenarios}) {
my %steps = %{$steps->{$caller}};
for my $step_text (@{$scenario->steps}) {
- Test::More::note( $step_text );
-
my (undef, $step) = split " ", $step_text, 2;
+
+ SKIP:
while (my ($step_pattern, $cb) = each %steps) {
+ Test::More::skip($step, 1) if $skip;
+
if ($step =~ m/$step_pattern/) {
- $cb->();
+ eval { $cb->(); };
+ Test::More::ok(!$@, $step_text);
+
+ if ($@) {
+ $skip = 1;
+ $skip_reason = "Failed: $step_text";
+ }
next;
}
}
}
}
+
+ return 0;
}
sub _add_step {
@@ -74,30 +94,26 @@ Write your test program like this:
TEXT
Given qr/the test program is running/, sub {
- pass("running");
+ assert "running";
}
When qr/it reaches this step/, sub {
- pass("reaches");
+ assert "reaches";
}
Then qr/it should pass/, sub {
- pass("passes");
+ assert "passes";
}
- plan tests => 3;
runtests;
When it runs, it looks like this:
> perl test.pl
1..3
- # Given the test program is running
- ok 1 - running
- # When it reaches this step
- ok 2 - reaches
- # Then it should pass
- ok 3 - passes
+ ok 1 - Given the test program is running
+ ok 2 - When it reaches this step
+ ok 3 - Then it should pass
=head1 DESCRIPTION
@@ -108,9 +124,20 @@ family of C<Test::*> modules. It uses L<Test::More::note> function
internally to print messages.
This module implements the Given-When-Then clause only in English. To
-uses it in the test programs, you feed your feature text into
-C<feature> function, defines your step handlers, and then run all the
-tests by calling C<runtests>.
+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.
+
+If any assertion in the Given block failed, the the corresponding
+C<When> and C<Then> blocks are skipped.
+
+You don't need to specify the number of tests with C<plan>. Each step
+block itself is simply one test. If the block died, it's then
+considered failed. Otherwise it's considered as passing.
+
+Test::Cukes re-exports C<assert> function from C<Carp::Assert> for you
+to use in the step block.
For more info about how to define feature and scenarios, please read
the documents from L<http://cukes.info>.
@@ -126,6 +153,8 @@ The official Cucumber web-page, L<http://cukes.info/>.
cucumber.pl, L<http://search.cpan.org/dist/cucumber/>, another Perl
implementation of Cucumber tool.
+L<Carp::Assert>
+
=head1 LICENSE AND COPYRIGHT
Copyright (c) 2009, Kang-min Liu C<< <gugod@gugod.org> >>.
View
@@ -1,6 +1,5 @@
#!/usr/bin/env perl -w
use strict;
-use Test::More qw(no_plan);
use Test::Cukes;
feature(<<FEATURE_TEXT);
@@ -17,19 +16,21 @@ FEATURE_TEXT
my @passed;
Given qr/blah1/ => sub {
- pass("blah 1");
push @passed, 1;
+
+ assert @passed == 1;
};
When qr/blah2/ => sub {
- pass("blah 2");
push @passed, 2;
+ assert @passed == 2;
};
Then qr/blah3/ => sub {
- pass("blah 3");
push @passed, 3;
+ assert @passed == 3;
+
+ assert( @{[ 1, 2, 3 ]} == @passed );
};
runtests;
-is_deeply(\@passed, [1,2,3], "All step handlers are invoked.");

0 comments on commit 5e38ef0

Please sign in to comment.