mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-29 11:42:15 +00:00
Test that keys are quoted when writing from an object and in table names
This commit is contained in:
parent
9358162a58
commit
912ab61158
2 changed files with 17 additions and 2 deletions
|
@ -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 + "\"";
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue