Skip to content

Commit

Permalink
added cuke4duke groovy example
Browse files Browse the repository at this point in the history
  • Loading branch information
hauner committed Nov 6, 2011
1 parent 79a8884 commit 1140722
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/groovy-calculator/README.md
@@ -0,0 +1,8 @@
## Groovy Example

copied from cuke4duke groovy example


## Running Example

mvn test
40 changes: 40 additions & 0 deletions examples/groovy-calculator/pom.xml
@@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>groovy-calculator</artifactId>
<packaging>jar</packaging>
<name>Examples: Groovy Calculator</name>

<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-groovy</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
16 changes: 16 additions & 0 deletions examples/groovy-calculator/src/main/java/calc/Calculator.java
@@ -0,0 +1,16 @@
package calc;

import java.util.ArrayList;
import java.util.List;

public class Calculator {
List<Double> stack = new ArrayList<Double>();

public void push(double arg) {
stack.add(arg);
}

public double divide() {
return stack.get(0) / stack.get(1);
}
}
12 changes: 12 additions & 0 deletions examples/groovy-calculator/src/test/java/calc/DivisionTest.java
@@ -0,0 +1,12 @@
package calc;

import cucumber.junit.Cucumber;
import cucumber.junit.Feature;
import org.junit.runner.RunWith;
import org.junit.Test;
import static org.junit.Assert.*;

@RunWith(Cucumber.class)
@Feature(value = "division.feature")
public class DivisionTest {
}
17 changes: 17 additions & 0 deletions examples/groovy-calculator/src/test/resources/division.feature
@@ -0,0 +1,17 @@
# language: en
Feature: Division
In order to avoid silly mistakes
Cashiers must be able to calculate a fraction

@important
Scenario: Regular numbers
Given I have entered 3 into the calculator
And I have entered 2 into the calculator
When I press divide
Then the stored result should be 1.5

Scenario: More numbers
Given I have entered 6 into the calculator
And I have entered 3 into the calculator
When I press divide
Then the stored result should be 2.0
@@ -0,0 +1,44 @@
import calc.Calculator

this.metaClass.mixin(cucumber.runtime.groovy.Hooks)
this.metaClass.mixin(cucumber.runtime.groovy.EN)


class CustomWorld {
String customMethod() {
"foo"
}
}

World {
new CustomWorld()
}

Before() {
assert "foo" == customMethod()
calc = new Calculator()
}

Before("@notused") {
throw new RuntimeException("Never happens")
}

Before("@notused,@important", "@alsonotused") {
throw new RuntimeException("Never happens")
}

Given(~"I have entered (\\d+) into (.*) calculator") { int number, String ignore ->
calc.push number
}

Given(~"(\\d+) into the") { ->
throw new RuntimeException("should never get here since we're running with --guess")
}

When(~"I press (\\w+)") { String opname ->
result = calc."$opname"()
}

Then(~"the stored result should be (.*)") { double expected ->
assert expected == result
}
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -48,6 +48,11 @@
<artifactId>cucumber-java</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-groovy</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
Expand Down

0 comments on commit 1140722

Please sign in to comment.