0) Coding on Windows is a pain lol
0b) Install cucumber: gem install cucumber (+make available to path should be automatic on unixoid systems)
---
1) Create a features directory
2) Inside create a file called palindrome.feature
Code:
Feature: Detect Palindromes
As a user I want to be able to check if a given word or phrase is a palindrome.
Scenario Outline: Provide input to palindrome?
Given the input "<input>"
When palindrome? is run
Then the output should be "<output>"
Examples:
|input|output|
|A man, a plan, a canal -- Panama|true|
|Madam, I'm Adam!|true|
|Abracadabra|false|
3) Run "cucumber" from the parent directory of the features directory (i.e. homework1) you'll get a nice and helpfull warning
Code:
Feature: Detect Palindromes
As a user I want to be able to check if a given word or phrase is a palindrome.
Scenario Outline: Provide input to palindrome? # features\palindrome.feature:5
Given the input "<input>" # features\palindrome.feature:6
When palindrome? is run # features\palindrome.feature:7
Then the output should be "<output>" # features\palindrome.feature:8
Examples:
| input | output |
| A man, a plan, a canal -- Panama | true |
| Madam, I'm Adam! | true |
| Abracadabra | false |
3 scenarios (3 undefined)
9 steps (9 undefined)
0m0.021s
You can implement step definitions for undefined steps with these snippets:
Given /^the input "([^"]*)"$/ do |arg1|
pending # express the regexp above with the code you wish you had
end
When /^palindrome\? is run$/ do
pending # express the regexp above with the code you wish you had
end
Then /^the output should be "([^"]*)"$/ do |arg1|
pending # express the regexp above with the code you wish you had
end
If you want snippets in a different programming language,
just make sure a file with the appropriate file extension
exists where cucumber looks for step definitions.
4) All right..create the step_definitions directory and a file palindrome_steps.rb in it with the C+Ped code and run "cucumber" again
Code:
Feature: Detect Palindromes
As a user I want to be able to check if a given word or phrase is a palindrome.
Scenario Outline: Provide input to palindrome? # features\palindrome.feature:5
Given the input "<input>" # features/step_definitions/palindrome_steps.rb:1
When palindrome? is run # features/step_definitions/palindrome_steps.rb:5
Then the output should be "<output>" # features/step_definitions/palindrome_steps.rb:9
Examples:
| input | output |
| A man, a plan, a canal -- Panama | true |
TODO (Cucumber::Pending)
./features/step_definitions/palindrome_steps.rb:2:in `/^the input "([^"]*)"$/'
features\palindrome.feature:6:in `Given the input "<input>"'
| Madam, I'm Adam! | true |
TODO (Cucumber::Pending)
./features/step_definitions/palindrome_steps.rb:2:in `/^the input "([^"]*)"$/'
features\palindrome.feature:6:in `Given the input "<input>"'
| Abracadabra | false |
TODO (Cucumber::Pending)
./features/step_definitions/palindrome_steps.rb:2:in `/^the input "([^"]*)"$/'
features\palindrome.feature:6:in `Given the input "<input>"'
3 scenarios (3 pending)
9 steps (6 skipped, 3 pending)
0m0.010s
5) Improve the palindrome_steps file...final version
Code:
require_relative 'palindrome.rb'
Given /^the input "([^"]*)"$/ do |input|
@input = input
end
When /^palindrome\? is run$/ do
@output = palindrome?(@input).to_s
end
Then /^the output should be "([^"]*)"$/ do |expected_output|
raise('Wrong Output') unless @output == expected_output
end
And the corresponding palindrome.rb file, spoilered because it's part of the homework
Obviously you'll get there by getting closer to getting the tests to pass step by step (i.e. your first palindrome? could have simply returned true)
Code:
Feature: Detect Palindromes
As a user I want to be able to check if a given word or phrase is a palindrome.
Scenario Outline: Provide input to palindrome? # features\palindrome.feature:5
Given the input "<input>" # features/step_definitions/palindrome_steps.rb:3
When palindrome? is run # features/step_definitions/palindrome_steps.rb:7
Then the output should be "<output>" # features/step_definitions/palindrome_steps.rb:11
Examples:
| input | output |
| A man, a plan, a canal -- Panama | true |
| Madam, I'm Adam! | true |
| Abracadabra | false |
3 scenarios (3 passed)
9 steps (9 passed)
0m0.015s
GREAT SUCCESS!
(I'll answer questions as to how/why this works etc if you want)
tl;dr versin:
1) Create a feature file describing your feature and providing some scenarios (scenario outline I used is an advanced concept that lets you use a table). Special keywords: Feature, Scenario, Scenario Outline, Given, When, Then (and some others)
2) The steps are basically regexp matching to get you some variables to use
3) Implement code
Documentation:
http://cuke4ninja.com/toc.html
NOTE: Works with other languages as well, not just Ruby
Last edited by clowntable; 02-29-2012 at 03:33 PM.