p5-cucumber icon indicating copy to clipboard operation
p5-cucumber copied to clipboard

Cucumber for Perl 5

h1. Perl 5 Cucumber

This is a minimal implementation of Cucumber in Perl5

To learn about Cucumber take a look at: http://wiki.gihub.com/aslakhellesoy/cucumber

h2. Example:

h3. Story

Feature: Dealing with mushrooms
  In order to test the effect of evil poisonous mushrooms
  As an evil scientist
  I want to test effects of eating mushrooms on little children

  Scenario: Mushrooms are bad for you, they kill boys
    Given a live boy in a forest
    When he ate a mushroom
    Then he was a dead boy in a forest

  Scenario: Mushrooms are bad for girls too
    Given a live girl in a forest
    When she ate a mushroom
    Then she was a dead girl in a forest

h3. Code


Given qr/(.*) in (.*)/, sub {
  my ($description,$location) = @_;
  $state{human} = $description;
  $state{location} = $location;
};

When qr/s?he ate (.*)/, sub {
  my $item = shift;
  if ($item eq 'a mushroom') {
    $state{human} =~ s/live/dead/;
  }
};

Then qr/s?he was (.*) in (.*)/, sub {
  my ($description,$location) = @_;
  is($state{human},$description,$description);
  is($state{location},$location,$location);
};

h3. Execution output

$ perl p5-cucumber.pl
Feature: Dealing with mushrooms
  In order to test the effect of evil poisonous mushrooms
  As an evil scientist
  I want to test effects of eating mushrooms on little children

  Scenario: Mushrooms are bad for you, they kill boys
    Given a live boy in a forest
    When he ate a mushroom
    Then he was a dead boy in a forest
ok 1 - a dead boy
ok 2 - a forest

  Scenario: Mushrooms are bad for girls too
    Given a live girl in a forest
    When she ate a mushroom
    Then she was a dead girl in the sea
ok 3 - a dead girl
not ok 4 - the sea
#   Failed test 'the sea'
#   at ./p5-cucumber.pl line 82.
#          got: 'a forest'
#     expected: 'the sea'
1..4
# Looks like you failed 1 test of 4.

(exitcode is 1 for failure)

And when we fix the story:

$ perl p5-cucumber.pl
Feature: Dealing with mushrooms
  In order to test the effect of evil poisonous mushrooms
  As an evil scientist
  I want to test effects of eating mushrooms on little children

  Scenario: Mushrooms are bad for you, they kill boys
    Given a live boy in a forest
    When he ate a mushroom
    Then he was a dead boy in a forest
ok 1 - a dead boy
ok 2 - a forest

  Scenario: Mushrooms are bad for girls too
    Given a live girl in a forest
    When she ate a mushroom
    Then she was a dead girl in a forest
ok 3 - a dead girl
ok 4 - a forest
1..4

(exitcode is 0 for success)

h3. Thanks

Great thank you for the perl-il and perl-qa mailing lists for helping me with some high level perl concepts that make this code so beautiful like it is.

Another huge thank you to the people in #perl @freenode.net who helped me with sticky dereferencing and scalar/list problems.