Skip to content

Commit

Permalink
Replaced cucumber.options split on spaces with regex
Browse files Browse the repository at this point in the history
added a private method (cucumberOptionsSplit to RuntimeOptions to split
on spaces or strings encased in single quotes.
  • Loading branch information
wjpowell committed Nov 19, 2012
1 parent b1d3f48 commit ecdbd77
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
18 changes: 16 additions & 2 deletions core/src/main/java/cucumber/runtime/RuntimeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static cucumber.runtime.model.CucumberFeature.load;
Expand All @@ -40,7 +41,7 @@ public RuntimeOptions(Properties properties, String... argv) {

parse(new ArrayList<String>(asList(argv)));
if (properties.containsKey("cucumber.options")) {
parse(new ArrayList<String>(asList(properties.getProperty("cucumber.options").split(" "))));
parse(cucumberOptionsSplit(properties.getProperty("cucumber.options")));
}

if (formatters.isEmpty()) {
Expand All @@ -54,7 +55,20 @@ public RuntimeOptions(Properties properties, String... argv) {
}
}

private void parse(List<String> args) {
private List<String> cucumberOptionsSplit(String property) {
List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("[^\\s']+|'([^']*)'");
Matcher regexMatcher = regex.matcher(property);
while (regexMatcher.find()) {
if (regexMatcher.group(1) != null)
matchList.add(regexMatcher.group(1));
else
matchList.add(regexMatcher.group());
}
return matchList;
}

private void parse(List<String> args) {
List<Object> parsedFilters = new ArrayList<Object>();
while (!args.isEmpty()) {
String arg = args.remove(0);
Expand Down
34 changes: 33 additions & 1 deletion core/src/test/java/cucumber/runtime/RuntimeOptionsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cucumber.runtime;

import net.sourceforge.cobertura.ant.Regex;

import org.junit.Test;

import java.io.File;
Expand Down Expand Up @@ -71,7 +73,37 @@ public void name() {
Pattern actualPattern = (Pattern) options.filters.iterator().next();
assertEquals(someName, actualPattern.pattern());
}


@Test
public void name_with_spaces() {
String someName = "some Name";
RuntimeOptions options = new RuntimeOptions(new Properties(), "--name", someName);
Pattern actualPattern = (Pattern) options.filters.iterator().next();
assertEquals(someName, actualPattern.pattern());
}

@Test
public void ensure_name_with_spaces_works_with_cucumber_options() {
String someName = "some Name";
Properties properties = new Properties();
properties.setProperty("cucumber.options", "--name '" + someName + "'");
RuntimeOptions options = new RuntimeOptions(properties);
Pattern actualPattern = (Pattern) options.filters.iterator().next();
assertEquals(someName, actualPattern.pattern());
}

@Test
public void ensure_multiple_cucumber_options_with_spaces_parse_correctly() {
String someName = "some Name";
String somePath = "some file\\path";
Properties properties = new Properties();
properties.setProperty("cucumber.options", "--name '" + someName + "'" + " --dotcucumber '" + somePath + "'");
RuntimeOptions options = new RuntimeOptions(properties);
Pattern actualPattern = (Pattern) options.filters.iterator().next();
assertEquals(someName, actualPattern.pattern());
assertEquals(new File(somePath), options.dotCucumber);
}

@Test
public void name_short() {
String someName = "someName";
Expand Down

0 comments on commit ecdbd77

Please sign in to comment.