Skip to content

Commit 838fe1f

Browse files
committed
Refactored --snippet option. Closes #561, #302
1 parent d1391b2 commit 838fe1f

File tree

34 files changed

+163
-183
lines changed

34 files changed

+163
-183
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## [Git master](https://github.com/cucumber/cucumber-jvm/compare/v1.1.4...master)
22

3+
* [Java, Jython] New `--snippet [underscore|camelcase]` option for more control over snippet style. ([#561](https://github.com/cucumber/cucumber-jvm/pull/561), [302](https://github.com/cucumber/cucumber-jvm/pull/302) Márton Mészáros, Aslak Hellesøy)
34
* [Windows] Use uri instead of path in CucumberFeature ([#562](https://github.com/cucumber/cucumber-jvm/pull/562) Björn Rasmusson)
45

56
## [1.1.4](https://github.com/cucumber/cucumber-jvm/compare/v1.1.3...v1.1.4) (2013-08-11)

clojure/src/test/java/cucumber/runtime/clojure/ClojureSnippetTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ClojureSnippetTest {
1818
@Test
1919
public void generatesPlainSnippet() {
2020
Step step = new Step(NO_COMMENTS, "Given ", "I have 4 cukes in my \"big\" belly", 0, null, null);
21-
String snippet = new SnippetGenerator(new ClojureSnippet()).getSnippet(step);
21+
String snippet = new SnippetGenerator(new ClojureSnippet()).getSnippet(step, null);
2222
String expected = "" +
2323
"(Given #\"^I have (\\d+) cukes in my \\\"([^\\\"]*)\\\" belly$\" [arg1 arg2]\n" +
2424
" (comment Express the Regexp above with the code you wish you had )\n" +
@@ -30,7 +30,7 @@ public void generatesPlainSnippet() {
3030
public void generatesSnippetWithDataTable() {
3131
List<DataTableRow> dataTable = asList(new DataTableRow(NO_COMMENTS, asList("col1"), 1));
3232
Step step = new Step(NO_COMMENTS, "Given ", "I have:", 0, dataTable, null);
33-
String snippet = new SnippetGenerator(new ClojureSnippet()).getSnippet(step);
33+
String snippet = new SnippetGenerator(new ClojureSnippet()).getSnippet(step, null);
3434
String expected = "" +
3535
"(Given #\"^I have:$\" [arg1]\n" +
3636
" (comment Express the Regexp above with the code you wish you had )\n" +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cucumber.api;
2+
3+
import cucumber.runtime.CucumberException;
4+
import cucumber.runtime.snippets.CamelCaseFunctionNameSanitizer;
5+
import cucumber.runtime.snippets.FunctionNameSanitizer;
6+
import cucumber.runtime.snippets.UnderscoreFunctionNameSanitizer;
7+
8+
public enum SnippetType {
9+
UNDERSCORE("underscore", new UnderscoreFunctionNameSanitizer()),
10+
CAMELCASE("camelcase", new CamelCaseFunctionNameSanitizer());
11+
12+
private final String name;
13+
private final FunctionNameSanitizer functionNameSanitizer;
14+
15+
SnippetType(String name, FunctionNameSanitizer functionNameSanitizer) {
16+
this.name = name;
17+
this.functionNameSanitizer = functionNameSanitizer;
18+
}
19+
20+
public static SnippetType fromString(String name) {
21+
for (SnippetType snippetType : SnippetType.values()) {
22+
if (name.equalsIgnoreCase(snippetType.name)) {
23+
return snippetType;
24+
}
25+
}
26+
throw new CucumberException(String.format("Unrecognized SnippetType %s", name));
27+
}
28+
29+
public FunctionNameSanitizer getFunctionNameSanitizer() {
30+
return functionNameSanitizer;
31+
}
32+
}

core/src/main/java/cucumber/runtime/Backend.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cucumber.runtime;
22

3+
import cucumber.runtime.snippets.FunctionNameSanitizer;
34
import gherkin.formatter.model.Step;
45

56
import java.util.List;
@@ -27,5 +28,5 @@ public interface Backend {
2728
*/
2829
void disposeWorld();
2930

30-
String getSnippet(Step step);
31+
String getSnippet(Step step, FunctionNameSanitizer functionNameSanitizer);
3132
}

core/src/main/java/cucumber/runtime/Runtime.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@
1010
import gherkin.formatter.Argument;
1111
import gherkin.formatter.Formatter;
1212
import gherkin.formatter.Reporter;
13-
import gherkin.formatter.model.*;
13+
import gherkin.formatter.model.Comment;
14+
import gherkin.formatter.model.DataTableRow;
15+
import gherkin.formatter.model.DocString;
16+
import gherkin.formatter.model.Match;
17+
import gherkin.formatter.model.Result;
18+
import gherkin.formatter.model.Step;
19+
import gherkin.formatter.model.Tag;
1420

1521
import java.io.IOException;
1622
import java.io.PrintStream;
17-
import java.util.*;
23+
import java.util.ArrayList;
24+
import java.util.Arrays;
25+
import java.util.Collection;
26+
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.Set;
1829

1930
/**
2031
* This is the main entry point for running Cucumber features.
@@ -57,7 +68,7 @@ public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, Collectio
5768
}
5869

5970
public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, Collection<? extends Backend> backends,
60-
RuntimeOptions runtimeOptions, RuntimeGlue optionalGlue) {
71+
RuntimeOptions runtimeOptions, RuntimeGlue optionalGlue) {
6172
if (backends.isEmpty()) {
6273
throw new CucumberException("No backends were found. Please make sure you have a backend module on your CLASSPATH.");
6374
}
@@ -162,7 +173,7 @@ private boolean hasErrors() {
162173
}
163174

164175
public List<String> getSnippets() {
165-
return undefinedStepsTracker.getSnippets(backends, runtimeOptions.getSnippetType());
176+
return undefinedStepsTracker.getSnippets(backends, runtimeOptions.getSnippetType().getFunctionNameSanitizer());
166177
}
167178

168179
public Glue getGlue() {

core/src/main/java/cucumber/runtime/RuntimeOptions.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cucumber.runtime;
22

3+
import cucumber.api.SnippetType;
34
import cucumber.runtime.formatter.ColorAware;
45
import cucumber.runtime.formatter.FormatterFactory;
56
import cucumber.runtime.formatter.StrictAware;
67
import cucumber.runtime.io.ResourceLoader;
78
import cucumber.runtime.model.CucumberFeature;
9+
import cucumber.runtime.snippets.FunctionNameSanitizer;
810
import gherkin.formatter.Formatter;
911
import gherkin.formatter.Reporter;
1012
import gherkin.util.FixJava;
@@ -37,7 +39,7 @@ public class RuntimeOptions {
3739
private boolean dryRun;
3840
private boolean strict = false;
3941
private boolean monochrome = false;
40-
private SnippetType snippet = SnippetType.getDefault();
42+
private SnippetType snippetType = SnippetType.UNDERSCORE;
4143

4244
public RuntimeOptions(Properties properties, String... argv) {
4345
/* IMPORTANT! Make sure USAGE.txt is always uptodate if this class changes */
@@ -100,7 +102,7 @@ private void parse(List<String> args) {
100102
monochrome = !arg.startsWith("--no-");
101103
} else if (arg.equals("--snippets")) {
102104
String nextArg = args.remove(0);
103-
snippet = SnippetType.fromString(nextArg);
105+
snippetType = SnippetType.fromString(nextArg);
104106
} else if (arg.equals("--name") || arg.equals("-n")) {
105107
String nextArg = args.remove(0);
106108
Pattern patternFilter = Pattern.compile(nextArg);
@@ -208,6 +210,6 @@ public boolean isMonochrome() {
208210
}
209211

210212
public SnippetType getSnippetType() {
211-
return snippet;
213+
return snippetType;
212214
}
213215
}

core/src/main/java/cucumber/runtime/SnippetType.java

-28
This file was deleted.

core/src/main/java/cucumber/runtime/SnippetTypeAwareBackend.java

-7
This file was deleted.

core/src/main/java/cucumber/runtime/UndefinedStepsTracker.java

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cucumber.runtime;
22

3+
import cucumber.runtime.snippets.FunctionNameSanitizer;
34
import gherkin.I18n;
45
import gherkin.formatter.model.Step;
56

@@ -18,19 +19,17 @@ public void reset() {
1819
}
1920

2021
/**
21-
* @param backends what backends we want snippets for
22+
* @param backends what backends we want snippets for
23+
* @param functionNameSanitizer responsible for generating method name
2224
* @return a list of code snippets that the developer can use to implement undefined steps.
2325
* This should be displayed after a run.
2426
*/
25-
public List<String> getSnippets(Iterable<? extends Backend> backends, SnippetType type) {
27+
public List<String> getSnippets(Iterable<? extends Backend> backends, FunctionNameSanitizer functionNameSanitizer) {
2628
// TODO: Convert "And" and "But" to the Given/When/Then keyword above in the Gherkin source.
2729
List<String> snippets = new ArrayList<String>();
2830
for (Step step : undefinedSteps) {
2931
for (Backend backend : backends) {
30-
if(backend instanceof SnippetTypeAwareBackend) {
31-
((SnippetTypeAwareBackend) backend).setSnippetType(type);
32-
}
33-
String snippet = backend.getSnippet(step);
32+
String snippet = backend.getSnippet(step, functionNameSanitizer);
3433
if (snippet == null) {
3534
throw new NullPointerException("null snippet");
3635
}
@@ -42,15 +41,6 @@ public List<String> getSnippets(Iterable<? extends Backend> backends, SnippetTyp
4241
return snippets;
4342
}
4443

45-
/**
46-
* @param backends what backends we want snippets for
47-
* @return a list of code snippets that the developer can use to implement undefined steps.
48-
* This should be displayed after a run.
49-
*/
50-
public List<String> getSnippets(Iterable<? extends Backend> backends) {
51-
return getSnippets(backends, SnippetType.getDefault());
52-
}
53-
5444
public void storeStepKeyword(Step step, I18n i18n) {
5545
String keyword = step.getKeyword();
5646
if (isGivenWhenThenKeyword(keyword, i18n)) {

java/src/main/java/cucumber/runtime/java/CamelCaseFunctionNameSanitizer.java renamed to core/src/main/java/cucumber/runtime/snippets/CamelCaseFunctionNameSanitizer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
package cucumber.runtime.java;
2-
3-
import cucumber.runtime.snippets.FunctionNameSanitizer;
1+
package cucumber.runtime.snippets;
42

53
public class CamelCaseFunctionNameSanitizer implements FunctionNameSanitizer {
64

core/src/main/java/cucumber/runtime/snippets/SnippetGenerator.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cucumber.runtime.snippets;
22

33
import cucumber.api.DataTable;
4-
import cucumber.runtime.SnippetType;
54
import gherkin.I18n;
65
import gherkin.formatter.model.Step;
76

@@ -32,23 +31,16 @@ public class SnippetGenerator {
3231

3332
private final Snippet snippet;
3433

35-
private FunctionNameSanitizer sanitizer;
36-
3734
public SnippetGenerator(Snippet snippet) {
38-
this(snippet, new UnderscoreFunctionNameSanitizer());
39-
}
40-
41-
public SnippetGenerator(Snippet snippet, FunctionNameSanitizer sanitizer) {
4235
this.snippet = snippet;
43-
this.sanitizer = sanitizer;
4436
}
4537

46-
public String getSnippet(Step step) {
38+
public String getSnippet(Step step, FunctionNameSanitizer functionNameSanitizer) {
4739
return MessageFormat.format(
4840
snippet.template(),
4941
I18n.codeKeywordFor(step.getKeyword()),
5042
snippet.escapePattern(patternFor(step.getName())),
51-
functionName(step.getName()),
43+
functionName(step.getName(), functionNameSanitizer),
5244
snippet.arguments(argumentTypes(step)),
5345
REGEXP_HINT,
5446
step.getRows() == null ? "" : snippet.tableHint()
@@ -72,12 +64,15 @@ String patternFor(String stepName) {
7264
return "^" + pattern + "$";
7365
}
7466

75-
private String functionName(String name) {
67+
private String functionName(String name, FunctionNameSanitizer functionNameSanitizer) {
68+
if(functionNameSanitizer == null) {
69+
return null;
70+
}
7671
String functionName = name;
7772
for (ArgumentPattern argumentPattern : argumentPatterns()) {
7873
functionName = argumentPattern.replaceMatchesWithSpace(functionName);
7974
}
80-
functionName = sanitizer.sanitizeFunctionName(functionName);
75+
functionName = functionNameSanitizer.sanitizeFunctionName(functionName);
8176
return functionName;
8277
}
8378

core/src/main/resources/cucumber/runtime/USAGE.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Options:
1111
-d, --[no-]-dry-run Skip execution of glue code.
1212
-m, --[no-]-monochrome Don't colour terminal output.
1313
-s, --[no-]-strict Treat undefined and pending steps as errors.
14-
--snippets Snippet type: underscore, camelcase
14+
--snippets Snippet name: underscore, camelcase
1515
--dotcucumber PATH_OR_URL Where to write out runtime information. PATH_OR_URL can be a file system
1616
path or a URL.
1717
-v, --version Print version.

core/src/test/java/cucumber/runtime/RuntimeOptionsTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cucumber.runtime;
22

3+
import cucumber.api.SnippetType;
34
import org.junit.Test;
45

56
import cucumber.runtime.formatter.ColorAware;

0 commit comments

Comments
 (0)