Commits

Steve Lancashire committed e397eab

cucumber demo resources

Comments (0)

Files changed (8)

examples/java-calculator/pom.xml

+<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>java-calculator</artifactId>
+    <packaging>jar</packaging>
+    <name>Examples: Java Calculator</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>info.cukes</groupId>
+            <artifactId>cucumber-picocontainer</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>info.cukes</groupId>
+            <artifactId>cucumber-junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.picocontainer</groupId>
+            <artifactId>picocontainer</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>

examples/java-calculator/src/main/java/cucumber/examples/java/calculator/RpnCalculator.java

+package cucumber.examples.java.calculator;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static java.util.Arrays.asList;
+
+public class RpnCalculator {
+    private final List<Number> stack = new ArrayList<Number>();
+    private static final List<String> OPS = asList("-", "+", "*", "/");
+
+    public void push(Object arg) {
+        if (OPS.contains(arg)) {
+            Number y = stack.remove(stack.size() - 1);
+            Number x = stack.isEmpty() ? 0 : stack.remove(stack.size() - 1);
+            Double val = null;
+            if (arg.equals("-")) {
+                val = x.doubleValue() - y.doubleValue();
+            } else if (arg.equals("+")) {
+                val = x.doubleValue() + y.doubleValue();
+            } else if (arg.equals("*")) {
+                val = x.doubleValue() * y.doubleValue();
+            } else if (arg.equals("/")) {
+                val = x.doubleValue() / y.doubleValue();
+            }
+            push(val);
+        } else {
+            stack.add((Number) arg);
+        }
+    }
+
+    public void PI() {
+        push(Math.PI);
+    }
+
+    public Number value() {
+        return stack.get(stack.size() - 1);
+    }
+}

examples/java-calculator/src/test/java/cucumber/examples/java/calculator/RpnCalculatorStepdefs.java

+package cucumber.examples.java.calculator;
+
+import cucumber.annotation.After;
+import cucumber.annotation.Before;
+import cucumber.annotation.en.Given;
+import cucumber.annotation.en.Then;
+import cucumber.annotation.en.When;
+
+import static junit.framework.Assert.assertEquals;
+
+public class RpnCalculatorStepdefs {
+    private RpnCalculator calc;
+
+    @Given("^a calculator I just turned on$")
+    public void a_calculator_I_just_turned_on() {
+        calc = new RpnCalculator();
+    }
+
+    @When("^I add (\\d+) and (\\d+)$")
+    public void adding(int arg1, int arg2) {
+        calc.push(arg1);
+        calc.push(arg2);
+        calc.push("+");
+    }
+
+    @Then("^the result is (\\d+)$")
+    public void the_result_is(double expected) {
+        assertEquals(expected, calc.value());
+    }
+
+    @Before({"~@foo"})
+    public void before() {
+        System.out.println("Runs before scenarios *not* tagged with @foo");
+    }
+
+    @After
+    public void after() {
+
+    }
+}

examples/java-calculator/src/test/java/cucumber/examples/java/calculator/ShoppingStepdefs.java

+package cucumber.examples.java.calculator;
+
+import cucumber.annotation.en.Given;
+import cucumber.annotation.en.Then;
+import cucumber.annotation.en.When;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public class ShoppingStepdefs {
+    private RpnCalculator calc = new RpnCalculator();
+
+    @Given("^the following groceries:$")
+    public void the_following_groceries(List<Grocery> groceries) {
+        for (Grocery grocery : groceries) {
+            calc.push(grocery.price);
+            calc.push("+");
+        }
+    }
+
+    @When("^I pay (\\d+)$")
+    public void i_pay(int amount) {
+        calc.push(amount);
+        calc.push("-");
+    }
+
+    @Then("^my change should be (\\d+)$")
+    public void my_change_should_be_(int change) {
+        assertEquals(-calc.value().intValue(), change);
+    }
+
+    public static class Grocery {
+        public String name;
+        public int price;
+    }
+}

examples/java-calculator/src/test/java/cucumber/examples/java/calculator/shopping_Test.java

+package cucumber.examples.java.calculator;
+
+import cucumber.junit.Cucumber;
+import cucumber.junit.Feature;
+import org.junit.runner.RunWith;
+
+@RunWith(Cucumber.class)
+@Feature(value = "shopping.feature")
+public class shopping_Test {
+}

examples/java-calculator/src/test/java/cucumber/examples/java/scan/basic_arithmetic_with_scan_Test.java

+package cucumber.examples.java.scan;
+
+import cucumber.junit.Cucumber;
+import cucumber.junit.Feature;
+import org.junit.runner.RunWith;
+
+/**
+ * This test class tells JUnit to run a particular feature with Cucumber.
+ * <p/>
+ * Step definitions will be scanned underneath both this class' package, and
+ * underneath cucumber.examples.java.calculator.
+ */
+@RunWith(Cucumber.class)
+@Feature(value = "basic_arithmetic.feature", packages = {"cucumber.examples.java.calculator"})
+public class basic_arithmetic_with_scan_Test {
+}

examples/java-calculator/src/test/resources/basic_arithmetic.feature

+@foo
+Feature: Basic Arithmetic
+  Background: A Calculator
+    Given a calculator I just turned on
+
+  Scenario: Addition
+    # Try to change one of the values below to provoke a failure
+    When I add 4 and 5
+    Then the result is 9
+
+  Scenario: Another Addition
+    # Try to change one of the values below to provoke a failure
+    When I add 4 and 7
+    Then the result is 11
+
+  Scenario Outline: Many additions
+     When I add <a> and <b>
+     Then the result is <c>
+     
+     Examples: Single digits
+       | a | b | c |
+       | 1 | 2 | 3 |
+       | 2 | 3 | 5 |
+
+     Examples: Double digits
+       |  a |  b |  c |
+       | 10 | 20 | 31 |
+       | 20 | 30 | 50 |

examples/java-calculator/src/test/resources/shopping.feature

+Feature: Shopping
+  Scenario: Give correct change
+    Given the following groceries:
+      | name  | price |
+      | milk  |     9 |
+      | bread |     7 |
+      | soap  |     5 |
+    When I pay 25
+    Then my change should be 4