Skip to content

Instantly share code, notes, and snippets.

@sma
Created November 16, 2009 15:53
Show Gist options
  • Save sma/236075 to your computer and use it in GitHub Desktop.
Save sma/236075 to your computer and use it in GitHub Desktop.
Cucumber steps in different programming languages
>>>> Gherkin <<<<
As a developer
In Order to find the best scripting solution
So that I can compare different implementation languages for Cucumber
Given I have 1h of time
When I write something about Cucumber
Then others should like that
>>>> Ruby <<<<
require 'rspec'
Before do
@usefulness = 0
end
Given "I have (\d+)h of time" do |time|
@time = time.to_i
end
When "I write something about (\w+)" do |topic|
@usefulness = @time * 20 if topic == 'cucumber'
end
Then "others should like that" do
@usefulness.should >= 20
end
>>>> Java <<<<
package com.coremedia.cucumber.test;
import cuke4duke.Given;
import cuke4duke.Then;
import cuke4duke.When;
import static org.junit.Assert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.BaseMatcher;
import static org.hamcrest.Matcher;
public class Steps {
static class GreaterOrEqualTo extends BaseMatcher {
private final int value;
GreaterOrEqualTo(int value) {
this.value = value;
}
public boolean matches(Object arg) {
return (Integer) arg >= value;
}
}
private static Matcher greaterOrEqualTo(int arg) {
return GreaterOrEqualTo(arg);
}
private int usefulness;
private int time;
@Given("^I have (\\d+)h of time")
public void iHaveTime(String time) {
this.time = Integer.parseInt(time);
}
@When("^I write something about (\\w+)")
public void iWriteSomethingAbout(String topic) {
if (topic.equals("cucumber")) {
usefulness = 20 * time;
}
}
@Then("^others should like that")
public void othersShouldLikeThat() {
assertThat(usefulness, is(greaterOrEqualTo(20)));
}
}
>>>> Scala <<<<
package com.coremedia.cucumber.test;
import cuke4duke.ScalaDsl
import org.scalatest.matchers.ShouldMatchers
class Steps extends ScalaDsl with ShouldMatchers {
var usefulness = 0
var time = 0
Given("^I have (\\d+)h of time") { time: String =>
this.time = Integer.parseInt(time)
}
When("^I write something about (\\w+)") { topic: String =>
if (topic == "cucumber") {
usefulness = 20 * time;
}
}
Then("^others should like that") {
usefulness should be >= 20
}
}
>>>> JavaScript <<<<
var time, usefulness = 0;
Given(/^I have (\\d+)h of time/, function(_time) {
time = parseInteger(_time, 10);
});
When(/^I write something about (\\w+)/, function(_topic) {
if (topic === "cucumber") usefulness = 20 * time;
});
Then(/^others should like that/, function() {
if (usefulness < 20) throw "Expected usefulness >= 20 but was " + usefulness;
});
>>>> Ioke <<<<
Before(usefulness = 0)
Given(#/^I have ({t}\\d+)h of time/,
time = t toInt)
When(#/^I write something about ({topic}\\w+)/,
if(topic == "cucumber", usefulness = 20 * time))
Then(#/^others should like that/,
usefulness should >= 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment