From 912ab611587020aec97e7f79faf79784992f05b9 Mon Sep 17 00:00:00 2001 From: "moandji.ezana" Date: Thu, 2 Jul 2015 07:59:30 +0200 Subject: [PATCH] Test that keys are quoted when writing from an object and in table names --- .../com/moandjiezana/toml/MapValueWriter.java | 4 ++-- .../com/moandjiezana/toml/TomlWriterTest.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/moandjiezana/toml/MapValueWriter.java b/src/main/java/com/moandjiezana/toml/MapValueWriter.java index 2625c1a..4e6ec79 100644 --- a/src/main/java/com/moandjiezana/toml/MapValueWriter.java +++ b/src/main/java/com/moandjiezana/toml/MapValueWriter.java @@ -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 + "\""; } diff --git a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java index f5aa8f5..17ee96c 100644 --- a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java @@ -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 {