Skip to content

Commit

Permalink
Fix for Issue #7: Support empty strings in table data
Browse files Browse the repository at this point in the history
  • Loading branch information
terzerm committed Jun 6, 2017
1 parent 17229de commit 26bcd09
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
25 changes: 8 additions & 17 deletions src/main/java/org/tools4j/spockito/Converters.java
Expand Up @@ -180,10 +180,7 @@ private List<Object> toList(final ActualType elementType, final String value) {
if (plainValue.trim().isEmpty()) {
return Collections.emptyList();
}
String[] parts = Strings.UNESCAPED_COMMA.split(plainValue);
if (parts.length == 1) {
parts = Strings.UNESCAPED_SEMICOLON.split(plainValue);
}
final String[] parts = parseListValues(plainValue);
final List<Object> list = new ArrayList<>(parts.length);
for (int i = 0; i < parts.length; i++) {
list.add(elementConverter.convert(elementType.rawType, elementType.genericType, parts[i].trim()));
Expand Down Expand Up @@ -285,10 +282,7 @@ private Map<Object, Object> toMap(final ActualType keyType, final ActualType val
if (plainValue.trim().isEmpty()) {
return Collections.emptyMap();
}
String[] parts = Strings.UNESCAPED_COMMA.split(plainValue);
if (parts.length == 1) {
parts = Strings.UNESCAPED_SEMICOLON.split(plainValue);
}
final String[] parts = parseListValues(plainValue);
final Map<Object, Object> map = new LinkedHashMap<>();
for (int i = 0; i < parts.length; i++) {
final String[] keyAndValue = parseKeyValue(parts[i].trim());
Expand Down Expand Up @@ -352,10 +346,7 @@ private <T> T newInstance(final Class<T> type, final String value) {

private void injectValues(final Object instance, final Map<String, Accessor> accessorByName, final String value) {
final String plainValue = Strings.removeStartAndEndChars(value, '{', '}');
String[] parts = Strings.UNESCAPED_COMMA.split(plainValue);
if (parts.length == 1) {
parts = Strings.UNESCAPED_SEMICOLON.split(plainValue);
}
final String[] parts = parseListValues(plainValue);
final Map<String, String> valueByName = new LinkedHashMap<>();
for (int i = 0; i < parts.length; i++) {
final String[] nameAndValue = parseKeyValue(parts[i].trim());
Expand Down Expand Up @@ -472,11 +463,11 @@ private static String normalizeFieldName(final String name) {
}

private static final String[] parseKeyValue(final String pair) {
final String[] split = Strings.UNESCAPED_EQUAL.split(pair);
if (split.length == 1) {
return Strings.UNESCAPED_COLON.split(pair);
}
return split;
return Strings.split(pair, Strings.UNESCAPED_EQUAL, Strings.UNESCAPED_COLON);
}

private static final String[] parseListValues(final String pair) {
return Strings.split(pair, Strings.UNESCAPED_COMMA, Strings.UNESCAPED_SEMICOLON);
}

private static class ActualType {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/tools4j/spockito/Strings.java
Expand Up @@ -23,6 +23,7 @@
*/
package org.tools4j.spockito;

import java.util.Arrays;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -92,6 +93,31 @@ static boolean isCharAnyOf(final char ch, final char... chars) {
return false;
}

static String[] split(final String s, final Pattern delimRegex1, final Pattern delimRegex2) {
String[] parts = delimRegex1.split(s);
if (parts.length == 1 && s.equals(parts[0])) {
//delimiter was not contained in string, try second delimiter
parts = delimRegex2.split(s);
if (parts.length == 1 && s.equals(parts[0])) {
//delimiter was not contained in string, we're done
return parts;
}
}
if (!s.startsWith(parts[0])) {
//must be delimiter at the start of the string, meaning we have an empty string at the start
final String[] newParts = new String[parts.length + 1];
System.arraycopy(parts, 0, newParts, 1, parts.length);
newParts[0] = "";
parts = newParts;
}
if (!s.endsWith(parts[parts.length - 1])) {
//must be delimiter at the end of the string, meaning we have an empty string at the end
parts = Arrays.copyOf(parts, parts.length + 1);
parts[parts.length - 1] = "";
}
return parts;
}

private Strings() {
throw new RuntimeException("No Strings for you!");
}
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/org/tools4j/spockito/TableTest.java
Expand Up @@ -36,6 +36,7 @@ public class TableTest {
static final class Row {
int index;
List<Integer> listOfInteger;
String emptyString;
}


Expand All @@ -44,16 +45,17 @@ static final class Row {
public void parse() {
//when
final Row[] rows = Table.parse(Row.class, new String[] {
"| index | listOfInteger |",
"| 0 | [1;2;3;4;5;6] |",
"| 1 | [9;8;7;6;5;4] |"
"| index | listOfInteger | emptyString |",
"| 0 | [1;2;3;4;5;6] | |",
"| 1 | [9;8;7;6;5;4] | '' |"
});

//then
Assert.assertEquals("unexpected row count", 2, rows.length);
for (int row = 0; row < rows.length; row++) {
Assert.assertEquals("unexpected index", row, rows[row].index);
Assert.assertEquals("unexpected list length", 6, rows[row].listOfInteger.size());
Assert.assertEquals("string should be empty", "", rows[row].emptyString);
}
}
}
6 changes: 6 additions & 0 deletions src/test/java/org/tools4j/spockito/ValueConverterTest.java
Expand Up @@ -92,6 +92,8 @@ public void convertString() {
assertEquals("Expected string with quotes", "'hello world'", converter.convert(String.class, String.class, "''hello world''"));
assertEquals("Expected string with left quotes", "'hello world", converter.convert(String.class, String.class, "'hello world"));
assertEquals("Expected string with right quotes", "hello world'", converter.convert(String.class, String.class, "hello world'"));
assertEquals("Expected empty string", "", converter.convert(String.class, String.class, ""));
assertEquals("Expected empty string", "", converter.convert(String.class, String.class, "''"));
}

@Test
Expand All @@ -103,6 +105,8 @@ public void convertStringBuilder() {
assertEquals("Expected string with quotes", "'hello world'", converter.convert(StringBuilder.class, StringBuilder.class, "''hello world''").toString());
assertEquals("Expected string with left quotes", "'hello world", converter.convert(StringBuilder.class, StringBuilder.class, "'hello world").toString());
assertEquals("Expected string with right quotes", "hello world'", converter.convert(StringBuilder.class, StringBuilder.class, "hello world'").toString());
assertEquals("Expected empty string", "", converter.convert(StringBuilder.class, StringBuilder.class, "").toString());
assertEquals("Expected empty string", "", converter.convert(StringBuilder.class, StringBuilder.class, "''").toString());
}

@Test
Expand All @@ -114,6 +118,8 @@ public void convertStringBuffer() {
assertEquals("Expected string with quotes", "'hello world'", converter.convert(StringBuffer.class, StringBuffer.class, "''hello world''").toString());
assertEquals("Expected string with left quotes", "'hello world", converter.convert(StringBuffer.class, StringBuffer.class, "'hello world").toString());
assertEquals("Expected string with right quotes", "hello world'", converter.convert(StringBuffer.class, StringBuffer.class, "hello world'").toString());
assertEquals("Expected empty string", "", converter.convert(StringBuffer.class, StringBuffer.class, "").toString());
assertEquals("Expected empty string", "", converter.convert(StringBuffer.class, StringBuffer.class, "''").toString());
}

@Test
Expand Down

0 comments on commit 26bcd09

Please sign in to comment.