Skip to content

Show explicit error message when field name missed in table header #1014

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
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,7 @@
package cucumber.runtime.table;

import cucumber.runtime.CucumberException;

import java.util.regex.Pattern;

public class CamelCaseStringConverter implements StringConverter {
Expand Down Expand Up @@ -34,6 +36,9 @@ private String capitalize(String string) {
}

private String uncapitalize(String string) {
if (string.isEmpty()) {
throw new CucumberException("Field name cannot be empty. Please check the table header.");
}
return new StringBuilder(string.length()).append(Character.toLowerCase(string.charAt(0))).append(string.substring(1)).toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package cucumber.runtime.table;

import cucumber.runtime.CucumberException;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class CamelCaseStringConverterTest {

private final CamelCaseStringConverter mapper = new CamelCaseStringConverter();

@Test
public void testTransformToJavaPropertyName() {
CamelCaseStringConverter mapper = new CamelCaseStringConverter();

assertEquals("Transformed Name", "userName", mapper.map("User Name"));
assertEquals("Transformed Name", "birthDate", mapper.map(" Birth Date\t"));
assertEquals("Transformed Name", "email", mapper.map("email"));
}

@Test(expected = CucumberException.class)
public void testEmptyInputShouldBeRejected() {
assertEquals("", mapper.map(" "));
}

}
15 changes: 15 additions & 0 deletions core/src/test/java/cucumber/runtime/table/ToDataTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public void gives_a_nice_error_message_when_field_is_missing() {
}
}

@Test
public void gives_explicit_error_message_on_field_name_missing_in_header() {
try {
tc.toList(TableParser.parse("" +
"| name | |\n" +
"| Sid Vicious | 10/05/1957 |\n" +
"| Frank Zappa | 21/12/1940 |\n" +
"", PARAMETER_INFO),
UserPojo.class);
fail();
} catch (CucumberException e) {
assertEquals("Field name cannot be empty. Please check the table header.", e.getMessage());
}
}

@Test
public void gives_a_nice_error_message_when_primitive_field_is_null() {
try {
Expand Down