Skip to content

Add error check to scenario outline, add java snippet escaping for + and . #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cucumber.runtime.model;

import cucumber.runtime.CucumberException;
import cucumber.runtime.Runtime;
import gherkin.formatter.Formatter;
import gherkin.formatter.Reporter;
Expand Down Expand Up @@ -103,13 +104,17 @@ private static DocString docStringWithTokensReplaced(DocString docString, List<S
}

private static String replaceTokens(Set<Integer> matchedColumns, List<String> headerCells, List<String> exampleCells, String text) {
for (int i = 0; i < headerCells.size(); i++) {
String headerCell = headerCells.get(i);
String value = exampleCells.get(i);
for (int col = 0; col < headerCells.size(); col++) {
String headerCell = headerCells.get(col);
String value = exampleCells.get(col);
String token = "<" + headerCell + ">";

if (text.contains(token)) {
text = text.replace(token, value);
matchedColumns.add(i);
if (text.isEmpty()) {
throw new CucumberException("Step generated from scenario outline '" + token + "' is empty");
}
matchedColumns.add(col);
}
}
return text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class SnippetGenerator {
Pattern.compile("\\]"),
Pattern.compile("\\?"),
Pattern.compile("\\*"),
Pattern.compile("\\+"),
Pattern.compile("\\."),
Pattern.compile("\\^"),};

private static final String REGEXP_HINT = "Express the Regexp above with the code you wish you had";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public String sanitizeFunctionName(String functionName) {
StringBuilder sanitized = new StringBuilder();

String trimmedFunctionName = functionName.trim();
if (trimmedFunctionName.isEmpty()) {
throw new IllegalArgumentException("Cannot have empty function name");
}

sanitized.append(Character.isJavaIdentifierStart(trimmedFunctionName.charAt(0)) ? trimmedFunctionName.charAt(0) : SUBST);
for (int i = 1; i < trimmedFunctionName.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cucumber.runtime.snippets;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class UnderscoreFunctionNameSanitizerTest {

private UnderscoreFunctionNameSanitizer sanitizer = new UnderscoreFunctionNameSanitizer();

@Test
public void testSanitizeFunctionName() {
assertEquals("_test_function_123", sanitizer.sanitizeFunctionName(".test function 123 "));
}

@Test(expected = IllegalArgumentException.class)
public void testSanitizeEmptyFunctionName() {
sanitizer.sanitizeFunctionName("");
}

}
4 changes: 2 additions & 2 deletions java/src/test/java/cucumber/runtime/java/JavaSnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ public void generatesSnippetWithEscapedQuestionMarks() {
@Test
public void generatesSnippetWithLotsOfEscapes() {
String expected = "" +
"@Given(\"^\\\\^\\\\(\\\\[a-z\\\\]\\\\*\\\\)\\\\?\\\\$$\")\n" +
"@Given(\"^\\\\^\\\\(\\\\[a-z\\\\]\\\\*\\\\)\\\\?\\\\.\\\\+\\\\$$\")\n" +
"public void _a_z_$() throws Throwable {\n" +
" // Express the Regexp above with the code you wish you had\n" +
" throw new PendingException();\n" +
"}\n";
assertEquals(expected, snippetFor("^([a-z]*)?$"));
assertEquals(expected, snippetFor("^([a-z]*)?.+$"));
}

@Test
Expand Down