Skip to content

Commit 462bc81

Browse files
committed
Merge branch 'inheritOptionsFromBaseClass' of github.com:klausbayrhammer/cucumber-jvm into inherit-options
2 parents d51236c + 5755aed commit 462bc81

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

junit/src/main/java/cucumber/runtime/junit/RuntimeOptionsFactory.java

+26-16
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,33 @@ public RuntimeOptionsFactory(Class clazz) {
1616
}
1717

1818
public RuntimeOptions create() {
19+
List<String> args = buildArgsFromOptions();
20+
21+
return new RuntimeOptions(System.getProperties(), args.toArray(new String[args.size()]));
22+
}
23+
24+
private List<String> buildArgsFromOptions() {
1925
List<String> args = new ArrayList<String>();
20-
Cucumber.Options options = getOptions(clazz);
21-
22-
addDryRun(options, args);
23-
addMonochrome(options, args);
24-
addGlue(options, args, clazz);
25-
addTags(options, args);
26-
addFormats(options, args);
27-
addFeatures(options, args, clazz);
28-
addStrict(options, args);
29-
addName(options, args);
30-
addDotCucumber(options, args);
31-
addSnippets(options, args);
32-
33-
RuntimeOptions runtimeOptions = new RuntimeOptions(System.getProperties(), args.toArray(new String[args.size()]));
34-
35-
return runtimeOptions;
26+
27+
for (Class classWithOptions = clazz; hasSuperClass(classWithOptions); classWithOptions = classWithOptions.getSuperclass()) {
28+
Cucumber.Options options = getOptions(classWithOptions);
29+
30+
addDryRun(options, args);
31+
addMonochrome(options, args);
32+
addGlue(options, args, classWithOptions);
33+
addTags(options, args);
34+
addFormats(options, args);
35+
addFeatures(options, args, classWithOptions);
36+
addStrict(options, args);
37+
addName(options, args);
38+
addDotCucumber(options, args);
39+
addSnippets(options, args);
40+
}
41+
return args;
42+
}
43+
44+
private boolean hasSuperClass(Class classWithOptions) {
45+
return classWithOptions != Object.class;
3646
}
3747

3848
private void addDotCucumber(Cucumber.Options options, List<String> args) {

junit/src/test/java/cucumber/runtime/junit/RuntimeOptionsFactoryTest.java

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package cucumber.runtime.junit;
22

3+
import cucumber.api.SnippetType;
34
import cucumber.api.junit.Cucumber;
45
import cucumber.runtime.RuntimeOptions;
5-
import cucumber.api.SnippetType;
6+
import gherkin.formatter.Formatter;
7+
import gherkin.formatter.JSONFormatter;
8+
import gherkin.formatter.PrettyFormatter;
69
import org.junit.Test;
710

811
import java.net.MalformedURLException;
@@ -94,6 +97,26 @@ public void finds_path_for_class_in_toplevel_package() {
9497
assertEquals("", packageName("TopLevelClass"));
9598
}
9699

100+
@Test
101+
public void inherit_formatter_from_baseclass() {
102+
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(SubClassWithFormatter.class);
103+
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
104+
105+
List<Formatter> formatters = runtimeOptions.getFormatters();
106+
assertEquals(2, formatters.size());
107+
assertTrue(formatters.get(0) instanceof PrettyFormatter);
108+
assertTrue(formatters.get(1) instanceof JSONFormatter);
109+
}
110+
111+
@Test
112+
public void override_monochrome_flag_from_baseclass() {
113+
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(SubClassWithMonoChromeTrue.class);
114+
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
115+
116+
assertFalse(runtimeOptions.isMonochrome());
117+
}
118+
119+
97120
@Cucumber.Options(snippets = SnippetType.CAMELCASE)
98121
static class Snippets {
99122
// empty
@@ -132,4 +155,24 @@ static class DotCucumberUrl {
132155
static class WithoutOptions {
133156
// empty
134157
}
158+
159+
@Cucumber.Options(format = "pretty")
160+
static class SubClassWithFormatter extends BaseClassWithFormatter {
161+
// empty
162+
}
163+
164+
@Cucumber.Options(format = "json:outFile")
165+
static class BaseClassWithFormatter {
166+
// empty
167+
}
168+
169+
@Cucumber.Options(monochrome = false)
170+
static class SubClassWithMonoChromeTrue extends BaseClassWithMonoChromeFalse {
171+
// empty
172+
}
173+
174+
@Cucumber.Options(monochrome = true)
175+
static class BaseClassWithMonoChromeFalse {
176+
// empty
177+
}
135178
}

0 commit comments

Comments
 (0)