forked from cucumber/cucumber-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuntimeTest.java
197 lines (170 loc) · 7.22 KB
/
RuntimeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package cucumber.runtime;
import cucumber.api.PendingException;
import cucumber.runtime.io.ClasspathResourceLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.model.CucumberFeature;
import gherkin.I18n;
import gherkin.formatter.JSONFormatter;
import gherkin.formatter.model.Step;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import static cucumber.runtime.TestHelper.feature;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
public class RuntimeTest {
private static final I18n ENGLISH = new I18n("en");
@Ignore
@Test
public void runs_feature_with_json_formatter() throws Exception {
CucumberFeature feature = feature("test.feature", "" +
"Feature: feature name\n" +
" Background: background name\n" +
" Given b\n" +
" Scenario: scenario name\n" +
" When s\n");
StringBuilder out = new StringBuilder();
JSONFormatter jsonFormatter = new JSONFormatter(out);
List<Backend> backends = asList(mock(Backend.class));
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
RuntimeOptions runtimeOptions = new RuntimeOptions(new Properties());
Runtime runtime = new Runtime(new ClasspathResourceLoader(classLoader), classLoader, backends, runtimeOptions);
feature.run(jsonFormatter, jsonFormatter, runtime);
jsonFormatter.done();
String expected = "" +
"[\n" +
" {\n" +
" \"id\": \"feature-name\",\n" +
" \"description\": \"\",\n" +
" \"name\": \"feature name\",\n" +
" \"keyword\": \"Feature\",\n" +
" \"line\": 1,\n" +
" \"elements\": [\n" +
" {\n" +
" \"description\": \"\",\n" +
" \"name\": \"background name\",\n" +
" \"keyword\": \"Background\",\n" +
" \"line\": 2,\n" +
" \"steps\": [\n" +
" {\n" +
" \"result\": {\n" +
" \"status\": \"undefined\"\n" +
" },\n" +
" \"name\": \"b\",\n" +
" \"keyword\": \"Given \",\n" +
" \"line\": 3,\n" +
" \"match\": {}\n" +
" }\n" +
" ],\n" +
" \"type\": \"background\"\n" +
" },\n" +
" {\n" +
" \"id\": \"feature-name;scenario-name\",\n" +
" \"description\": \"\",\n" +
" \"name\": \"scenario name\",\n" +
" \"keyword\": \"Scenario\",\n" +
" \"line\": 4,\n" +
" \"steps\": [\n" +
" {\n" +
" \"result\": {\n" +
" \"status\": \"undefined\"\n" +
" },\n" +
" \"name\": \"s\",\n" +
" \"keyword\": \"When \",\n" +
" \"line\": 5,\n" +
" \"match\": {}\n" +
" }\n" +
" ],\n" +
" \"type\": \"scenario\"\n" +
" }\n" +
" ],\n" +
" \"uri\": \"test.feature\"\n" +
" }\n" +
"]";
assertEquals(expected, out.toString());
}
@Test
public void strict_without_pending_steps_or_errors() {
Runtime runtime = createStrictRuntime();
assertEquals(0x0, runtime.exitStatus());
}
@Test
public void non_strict_without_pending_steps_or_errors() {
Runtime runtime = createNonStrictRuntime();
assertEquals(0x0, runtime.exitStatus());
}
@Test
public void non_strict_with_undefined_steps() {
Runtime runtime = createNonStrictRuntime();
runtime.undefinedStepsTracker.addUndefinedStep(new Step(null, "Given ", "A", 1, null, null), ENGLISH);
assertEquals(0x0, runtime.exitStatus());
}
@Test
public void strict_with_undefined_steps() {
Runtime runtime = createStrictRuntime();
runtime.undefinedStepsTracker.addUndefinedStep(new Step(null, "Given ", "A", 1, null, null), ENGLISH);
assertEquals(0x1, runtime.exitStatus());
}
@Test
public void strict_with_pending_steps_and_no_errors() {
Runtime runtime = createStrictRuntime();
runtime.addError(new PendingException());
assertEquals(0x1, runtime.exitStatus());
}
@Test
public void non_strict_with_pending_steps() {
Runtime runtime = createNonStrictRuntime();
runtime.addError(new PendingException());
assertEquals(0x0, runtime.exitStatus());
}
@Test
public void non_strict_with_failed_junit_assumption() {
Runtime runtime = createNonStrictRuntime();
runtime.addError(new AssumptionViolatedException("should be treated like pending"));
assertEquals(0x0, runtime.exitStatus());
}
@Test
public void non_strict_with_errors() {
Runtime runtime = createNonStrictRuntime();
runtime.addError(new RuntimeException());
assertEquals(0x1, runtime.exitStatus());
}
@Test
public void strict_with_errors() {
Runtime runtime = createStrictRuntime();
runtime.addError(new RuntimeException());
assertEquals(0x1, runtime.exitStatus());
}
@Test
public void should_throw_cucumer_exception_if_no_backends_are_found() throws Exception {
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
new Runtime(new ClasspathResourceLoader(classLoader), classLoader, Collections.<Backend>emptyList(),
new RuntimeOptions(new Properties()));
fail("A CucumberException should have been thrown");
} catch (CucumberException e) {
assertEquals("No backends were found. Please make sure you have a backend module on your CLASSPATH.", e.getMessage());
}
}
private Runtime createStrictRuntime() {
return createRuntime("-g", "anything", "--strict");
}
private Runtime createNonStrictRuntime() {
return createRuntime("-g", "anything");
}
private Runtime createRuntime(String... runtimeArgs) {
ResourceLoader resourceLoader = mock(ResourceLoader.class);
ClassLoader classLoader = mock(ClassLoader.class);
RuntimeOptions runtimeOptions = new RuntimeOptions(new Properties(), runtimeArgs);
Backend backend = mock(Backend.class);
Collection<Backend> backends = Arrays.asList(backend);
return new Runtime(resourceLoader, classLoader, backends, runtimeOptions);
}
}