|
1 | | -package calc |
2 | | - |
3 | | -import io.cucumber.groovy.EN |
4 | | -import io.cucumber.groovy.Hooks |
5 | | - |
6 | | -// Add functions to register hooks and steps to this script. |
7 | | -this.metaClass.mixin(Hooks) |
8 | | -this.metaClass.mixin(EN) |
9 | | - |
10 | | -// Define a world that represents the test environment. |
11 | | -// Hooks can set up and tear down the environment and steps |
12 | | -// can change its state, e.g. store values used by later steps. |
13 | | -class CustomWorld { |
14 | | - def result |
15 | | - |
16 | | - String customMethod() { |
17 | | - "foo" |
18 | | - } |
19 | | -} |
20 | | - |
21 | | -// Create a fresh new world object as the test environment for each scenario. |
22 | | -// Hooks and steps will belong to this object so can access its properties |
23 | | -// and methods directly. |
24 | | -World { |
25 | | - new CustomWorld() |
26 | | -} |
27 | | - |
28 | | -// This closure gets run before each scenario |
29 | | -// and has direct access to the new world object |
30 | | -// but can also make use of script variables. |
31 | | -Before() { |
32 | | - assert "foo" == customMethod() |
33 | | - calc = new Calculator() // belongs to this script |
34 | | -} |
35 | | - |
36 | | -// Register another that also gets run before each scenario tagged with @notused. |
37 | | -Before("@notused") { |
38 | | - throw new RuntimeException("Never happens") |
39 | | -} |
40 | | - |
41 | | -// Register another that also gets run before each scenario tagged with |
42 | | -// (@notused or @important) and @alsonotused. |
43 | | -Before("(@notused or @important) and @alsonotused") { |
44 | | - throw new RuntimeException("Never happens") |
45 | | -} |
46 | | - |
47 | | -// Register step definition using Groovy syntax for regex patterns. |
48 | | -// If you use slashes to quote your regexes, you don't have to escape backslashes. |
49 | | -// Any Given/When/Then function can be used, the name is just to indicate the kind of step. |
50 | | -Given(~/^I have entered (\d+) into .* calculator$/) { int number -> |
51 | | - calc.push number |
52 | | -} |
53 | | - |
54 | | -// Remember to still include "->" if there are no parameters. |
55 | | -Given(~/\d+ into the/) {-> |
56 | | - throw new RuntimeException("should never get here since we're running with --guess") |
57 | | -} |
58 | | - |
59 | | -// This step calls a Calculator function specified in the step |
60 | | -// and saves the result in the current world object. |
61 | | -When(~/^I press (\w+)$/) { String opname -> |
62 | | - result = calc."$opname"() |
63 | | -} |
64 | | - |
65 | | -// Use the world object to get any result from a previous step. |
66 | | -// The expected value in the step is converted to the required type. |
67 | | -Then(/the stored result should be {}/) { double expected -> |
68 | | - assert expected == result |
69 | | -} |
| 1 | +package calc |
| 2 | + |
| 3 | +import io.cucumber.groovy.EN |
| 4 | +import io.cucumber.groovy.Hooks |
| 5 | + |
| 6 | +// Add functions to register hooks and steps to this script. |
| 7 | +this.metaClass.mixin(Hooks) |
| 8 | +this.metaClass.mixin(EN) |
| 9 | + |
| 10 | +// Define a world that represents the test environment. |
| 11 | +// Hooks can set up and tear down the environment and steps |
| 12 | +// can change its state, e.g. store values used by later steps. |
| 13 | +class CustomWorld { |
| 14 | + def result |
| 15 | + |
| 16 | + String customMethod() { |
| 17 | + "foo" |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// Create a fresh new world object as the test environment for each scenario. |
| 22 | +// Hooks and steps will belong to this object so can access its properties |
| 23 | +// and methods directly. |
| 24 | +World { |
| 25 | + new CustomWorld() |
| 26 | +} |
| 27 | + |
| 28 | +// This closure gets run before each scenario |
| 29 | +// and has direct access to the new world object |
| 30 | +// but can also make use of script variables. |
| 31 | +Before() { |
| 32 | + assert "foo" == customMethod() |
| 33 | + calc = new Calculator() // belongs to this script |
| 34 | +} |
| 35 | + |
| 36 | +// Register another that also gets run before each scenario tagged with @notused. |
| 37 | +Before("@notused") { |
| 38 | + throw new RuntimeException("Never happens") |
| 39 | +} |
| 40 | + |
| 41 | +// Register another that also gets run before each scenario tagged with |
| 42 | +// (@notused or @important) and @alsonotused. |
| 43 | +Before("(@notused or @important) and @alsonotused") { |
| 44 | + throw new RuntimeException("Never happens") |
| 45 | +} |
| 46 | + |
| 47 | +// Register step definition using Groovy syntax for regex patterns. |
| 48 | +// If you use slashes to quote your regexes, you don't have to escape backslashes. |
| 49 | +// Any Given/When/Then function can be used, the name is just to indicate the kind of step. |
| 50 | +Given(~/^I have entered (\d+) into .* calculator$/) { int number -> |
| 51 | + calc.push number |
| 52 | +} |
| 53 | + |
| 54 | +// Remember to still include "->" if there are no parameters. |
| 55 | +Given(~/\d+ into the/) {-> |
| 56 | + throw new RuntimeException("should never get here since we're running with --guess") |
| 57 | +} |
| 58 | + |
| 59 | +// This step calls a Calculator function specified in the step |
| 60 | +// and saves the result in the current world object. |
| 61 | +When(~/^I press (\w+)$/) { String opname -> |
| 62 | + result = calc."$opname"() |
| 63 | +} |
| 64 | + |
| 65 | +// Use the world object to get any result from a previous step. |
| 66 | +// The expected value in the step is converted to the required type. |
| 67 | +Then(/the stored result should be {}/) { double expected -> |
| 68 | + assert expected == result |
| 69 | +} |
0 commit comments