Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
Added Cucumber "Background" support.
Browse files Browse the repository at this point in the history
  • Loading branch information
supernovus committed Jan 26, 2010
1 parent 7b4938f commit 1608e22
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 24 deletions.
12 changes: 10 additions & 2 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
Version 2.3.0
* Some bug fixes
* Reworked the story handler significantly
* Added Background support to the story handler
* Expanded test coverage
* Made the story tests quiet by default. Pass '-v' if you want
The story to be read out.

Version 2.2.0
* Made the times operator more like the .times method.
* Reorganized the files into directories such as 'lib', 'spec', etc.
* Added a story test to the pspec.t specification.
* Added a README file.

Version 2.1.0
* Implemented a .times method for Int objects, solution provided by Carl
Masak.
* Implemented a .times method for Int objects,
solution provided by Carl Masak.

Version 2.0.0
* Implemented minimal Cucumber-like "story" handling.
Expand Down
13 changes: 11 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ the one on the front page of the RSpec website.
After making one, I turned my attention to the example on the Cucumber
website, and added a simple story handler to the library.

The story handler currently handles simple stories, as well as backgrounds.
It does not handle scenario outlines yet (coming soon.)

In addition to the basic testing stuff, the PSpec library also includes some
utility functionality such as a 'times' operator and .times Int method.

Expand All @@ -26,7 +29,13 @@ If you want to run all of the specification tests, you can also use the

$ prove -v --perl perl6 -r ./spec/

Just note that the specs included with the library have intentionally
Oh, and now the story based tests are quiet by default. If you want
them to display their story (as TAP comments) then call the test
with the -v flag. For example:

$ perl6 ./spec/pspec.t -v

Just note that most of the specs included with the library have intentionally
failing tests to show how the testing framework displays them.

The only test that should pass 100% is the pspec.t which is the test
Expand All @@ -42,7 +51,7 @@ library soon, I promise!
For now, please read the original article that I wrote introducing the
PSpec library:

http://huri.net/tech/perl6-rspec
http://huri.net/tech/pspec

~ Credits ~

Expand Down
61 changes: 46 additions & 15 deletions lib/PSpec.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Int is also {
}
}

module PSpec:ver<2.2.0>:auth<http://huri.net/>;
module PSpec:ver<2.3.0>:auth<http://huri.net/>;

our $tests_run = 0;
our $failed_tests = 0;
Expand Down Expand Up @@ -172,28 +172,59 @@ END {
# Cucumber. It doesn't support backgrounds, or scenario outlines
# yet though, so don't even bother trying.

our $feature_name = '';
our $scenario_name = '';
our $feature_name = '';
our $scenario_name = '';

sub default-handler {
return -> $line {
sub lines-handler (@lines, @handlers) {
for @lines -> $line {
line-handler $line, @handlers;
}
}

sub line-handler ($line, @handlers) {
for @handlers -> $handle {
$handle($line);
}
}

## The story handler.
#
sub handle-story (@story, :$verbose=0, Code *@handlers) is export(:DEFAULT) {

my $in_background = 0;
my @background;

for @story -> $line {
if $verbose { diag $line; }
my $follow_dispatch = 1;

given $line {
when /:s Feature\: (.*)/ {
$feature_name = $0;
@background.splice; ## Kill existing backgrounds.
$feature_name = $0;
$follow_dispatch = 1;
}
when /:s Scenario\: (.*)/ {
$scenario_name = $0;
$scenario_name = $0;
$in_background = 0;
if @background {
$follow_dispatch = 0; ## No dispatch.
line-handler $line, @handlers; ## Run this now.
lines-handler @background, @handlers; ## Then run the rest.
}
}
when /:s Background\:/ {
$in_background = 1;
}
}
}
}

sub handle-story (@story, Code *@handlers is rw) is export(:DEFAULT) {
@handlers.unshift: default-handler();
for @story -> $line {
diag $line;
for @handlers -> $handler {
$handler($line);
if $in_background {
@background.push: $line;
$follow_dispatch = 0;
}

if $follow_dispatch {
line-handler $line, @handlers;
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions spec/calc.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,41 @@ use Calculator;

our $calculator = Calculator.new;

sub MAIN ( $verbose=0 ) {

handle-story(
lines("spec/stories/calculator.story"),
:verbose($verbose),
-> $line {
given $line {

## Every Scenario, we should start with a clear calculator.
when / Scenario\: / { $calculator.clear }
when / Scenario\: / {
#diag "====> Clearing Calculator";
$calculator.clear;
}

## A rule for entering the numbers.
when /:s have entered (.*?) into the calculator/ {
my $num = +$0;
$calculator.push: $num
$calculator.push: $num;
#diag "====> Entered $num in calculator";
}

## And one for pressing the operation buttons.
when /:s press (.*)/ {
#diag "====> Pressing $0 on calculator";
$calculator."$0";
}

## Finally, a way to get back the results.
when /:s the result should be (\d+) on the screen/ {
assert $calculator.total should-be +$0;
}

}
}
);

}

14 changes: 11 additions & 3 deletions spec/pspec.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ BEGIN { @*INC.unshift('lib'); }

use PSpec;

sub MAIN ( $verbose=0 ) {

describe "PSpec", "times", [
'operator works' => {
my $value = 0;
Expand All @@ -23,28 +25,34 @@ my $value;

handle-story(
lines("spec/stories/pspec.story"),
:verbose($verbose),
-> $line {
given $line {
## Every Scenario, we should start with an empty value.
when / Scenario\: / { $value = 0; }

## A rule for entering the numbers.
when /:s initial value of (\d+)/ {
$value = +$0;
}
## And one for pressing the operation buttons.

## One for incrementing.
when /:s increment (\d+) times/ {
+$0.Int times { $value++ }
}

## One for decrementing.
when /:s decrement (\d+) times/ {
+$0.Int times { $value-- }
}

## Finally, a way to get back the results.
when /:s value should be (\d+)/ {
assert $value should-be +$0;
}

}
}
);

}
## End of tests

28 changes: 28 additions & 0 deletions spec/stories/calculator.story
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,31 @@ Feature: Division
When I press divide
Then the result should be 5 on the screen

Feature: Common scenarios with a background
In order to show how backgrounds works
As a developer of this lovely library
I want to see a set of scenarios with a common background

Background:
Given I have entered 100 into the calculator

Scenario: Add 50
Given I have entered 50 into the calculator
When I press add
Then the result should be 150 on the screen

Scenario: Subtract 25
Given I have entered 25 into the calculator
When I press subtract
Then the result should be 75 on the screen

Scenario: Multiply by 5
Given I have entered 5 into the calculator
When I press multiply
Then the result should be 500 on the screen

Scenario: Divide by 2
Given I have entered 2 into the calculator
When I press divide
Then the result should be 50 on the screen

18 changes: 18 additions & 0 deletions spec/stories/pspec.story
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ Feature: Story Handling
And I increment 30 times
Then the value should be 70

Feature: Story Backgrounds
In order to provide the Background functionality from Cucumber
As a computer programmer
I want to be able to have a section that will be parsed for every scenario

Background:
Given an initial value of 100
When I decrement 50 times

Scenario: Add 25
When I increment 25 times
Then the value should be 75

Scenario: Subtract 25
When I decrement 25 times
Then the value should be 25


0 comments on commit 1608e22

Please sign in to comment.