Repository: decorators-squad/eo-yaml
Pull request #429 (2021-02-06 23:06:17)

428 - parsing quoted keys
#428 

SEQUENCE_OR_MAP regex: added non-capturing group around the 3 options of single-quoted, double-quoted and unquoted key; removed optional leading spaces as it's only used with trimmed() input.

Added 2 sample files: minimal case; and more test.

Changed AllYamlLinesTest to parameterize `guessIndentation` modes for all tests.


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java b/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
index d46f5d1..0030aeb 100644
--- a/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
+++ b/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
@@ -68,12 +68,12 @@ final class AllYamlLines implements YamlLines {
      *            and any other characters.
      */
     private static final Pattern SEQUENCE_OR_MAP = Pattern.compile("^("
-            + "([ ]*[\\-](|[ ]+.*))|"
-            + "([ ]*"
+            + "([\\-](|[ ]+.*))|"
+            + "((?:"
                 + "('(?:[^'\\\\]|\\\\.)*')|"
                   + "(\"(?:[^\"\\\\]|\\\\.)*\")|"
                   + "([^\"']*)"
-                + ":(|[ ].*))"
+                + "):(|[ ].*))"
             + ")$");
 
     /**


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
index ec1c3fa..bca22ae 100644
--- a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
@@ -709,6 +709,71 @@ public final class RtYamlInputTest {
         MatcherAssert.assertThat(java.compareTo(java), Matchers.is(0));
     }
 
+    /**
+     * Do a round-trip test on the minimal quoted-key sample file.
+     *
+     * @throws IOException When there's a problem reading the sample files.
+     */
+    @Test
+    public void quotedKeysMinTest() throws IOException {
+        final String filename = "quotedKeysMin.yml";
+        final String fileContents = readTestResource(filename).trim();
+
+        final YamlMapping read = new RtYamlInput(
+                new FileInputStream("src/test/resources/" + filename)
+        ).readYamlMapping();
+
+        MatcherAssert.assertThat(read.type(), Matchers.equalTo(Node.MAPPING));
+        MatcherAssert.assertThat(
+                read.asMapping().keys().size(),
+                Matchers.equalTo(1));
+
+        final YamlNode topLevelMapping = read.asMapping().value("a_mapping");
+        MatcherAssert.assertThat(
+                topLevelMapping.type(),
+                Matchers.equalTo(Node.MAPPING));
+        MatcherAssert.assertThat(
+                topLevelMapping.asMapping().keys().size(),
+                Matchers.equalTo(1));
+
+        final String pretty = read.toString().trim();
+
+        MatcherAssert.assertThat(pretty, Matchers.equalTo(fileContents));
+    }
+
+
+    /**
+     * Do a round-trip test on the maximal quoted-key sample file.
+     *
+     * @throws IOException When there's a problem reading the sample files.
+     */
+    @Test
+    public void quotedKeysMaxTest() throws IOException {
+        final String filename = "quotedKeysMax.yml";
+        final String fileContents = readTestResource(filename).trim();
+
+        final YamlMapping read = new RtYamlInput(
+                new FileInputStream("src/test/resources/" + filename)
+        ).readYamlMapping();
+
+        MatcherAssert.assertThat(read.type(), Matchers.equalTo(Node.MAPPING));
+        MatcherAssert.assertThat(
+                read.asMapping().keys().size(),
+                Matchers.equalTo(1));
+
+        final YamlNode topLevelMapping = read.asMapping().value("a_mapping");
+        MatcherAssert.assertThat(
+                topLevelMapping.type(),
+                Matchers.equalTo(Node.MAPPING));
+        MatcherAssert.assertThat(
+                topLevelMapping.asMapping().keys().size(),
+                Matchers.equalTo(6));
+
+        final String pretty = read.toString().trim();
+
+        MatcherAssert.assertThat(pretty, Matchers.equalTo(fileContents));
+    }
+
     /**
      * Read a test resource file's contents.
      * @param fileName File to read.
diff --git a/src/test/resources/quotedKeysMax.yml b/src/test/resources/quotedKeysMax.yml
new file mode 100644
index 0000000..5899fe6
--- /dev/null
+++ b/src/test/resources/quotedKeysMax.yml
@@ -0,0 +1,10 @@
+a_mapping:
+  "of-quoted": val0
+  'also-single-quoted': val1
+  and_unquoted: val2
+  "to-introduce-leading-spaces":
+    key0: 1.0
+  'and-single-quotes':
+    key1: 2.0
+  and_unquoted2:
+    key2: 3.0
diff --git a/src/test/resources/quotedKeysMin.yml b/src/test/resources/quotedKeysMin.yml
new file mode 100644
index 0000000..c516479
--- /dev/null
+++ b/src/test/resources/quotedKeysMin.yml
@@ -0,0 +1,2 @@
+a_mapping:
+  "quoted-key": val0