Test that keys are quoted when writing from an object and in table names

This commit is contained in:
moandji.ezana 2015-07-02 07:59:30 +02:00
parent 9358162a58
commit 912ab61158
2 changed files with 17 additions and 2 deletions

View file

@ -11,7 +11,7 @@ import java.util.regex.Pattern;
class MapValueWriter implements ValueWriter {
static final ValueWriter MAP_VALUE_WRITER = new MapValueWriter();
private static final Pattern requiredQuotingPattern = Pattern.compile("^.*[^A-Za-z\\d_-].*$");
private static final Pattern REQUIRED_QUOTING_PATTERN = Pattern.compile("^.*[^A-Za-z\\d_-].*$");
@Override
public boolean canWrite(Object value) {
@ -70,7 +70,7 @@ class MapValueWriter implements ValueWriter {
private static String quoteKey(Object key) {
String stringKey = key.toString();
Matcher matcher = requiredQuotingPattern.matcher(stringKey);
Matcher matcher = REQUIRED_QUOTING_PATTERN.matcher(stringKey);
if (matcher.matches()) {
stringKey = "\"" + stringKey + "\"";
}

View file

@ -311,6 +311,21 @@ public class TomlWriterTest {
assertEquals(expected, new TomlWriter().write(aMap));
}
@Test
public void should_quote_keys_in_object() throws Exception {
class A$ {
Double µµ = 5.3;
}
class A {
int 5 = 5;
String français = "langue";
A$ a$ = new A$();
}
assertEquals("\"€5\" = 5\n\"français\" = \"langue\"\n\n[\"a$\"]\n\"µµ\" = 5.3\n", new TomlWriter().write(new A()));
}
@Test
public void should_handle_urls() throws Exception {
class WithUrl {