Repository: decorators-squad/eo-yaml
Pull request #392 (2020-07-17 15:36:13)

#388 Implementing ReadFlowMapping
PR for #388 

* Scaffolding


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java b/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java
new file mode 100644
index 0000000..8891894
--- /dev/null
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadFlowMapping.java
@@ -0,0 +1,92 @@
+/**
+ * Copyright (c) 2016-2020, Mihai Emil Andronache
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ *  list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright notice,
+ *  this list of conditions and the following disclaimer in the documentation
+ *  and/or other materials provided with the distribution.
+ * Neither the name of the copyright holder nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+package com.amihaiemil.eoyaml;
+
+import java.util.Set;
+
+/**
+ * A read YamlMapping in flow format, where elements are
+ * between curly brackets, separated by a comma.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 6.0.0
+ */
+final class ReadFlowMapping extends BaseYamlMapping {
+
+    /**
+     * This flow mapping folded on a single line.
+     */
+    private final YamlLine folded;
+
+    /**
+     * Ctor.
+     * @param start Line where this flow sequence starts.
+     * @param lines All lines of the YAML document.
+     */
+    ReadFlowMapping(final YamlLine start, final AllYamlLines lines) {
+        this(
+            new FoldedFlowLines(
+                new Skip(
+                    lines,
+                    line -> line.number() < start.number(),
+                    line -> line.trimmed().startsWith("#"),
+                    line -> line.trimmed().startsWith("---"),
+                    line -> line.trimmed().startsWith("..."),
+                    line -> line.trimmed().startsWith("%"),
+                    line -> line.trimmed().startsWith("!!")
+                ),
+                '{',
+                '}'
+            ).iterator().next()
+        );
+    }
+
+    /**
+     * Constructor.
+     * @param folded All the YAML lines of this flow mapping,
+     *  folded into a single one.
+     */
+    ReadFlowMapping(final YamlLine folded) {
+        this.folded = folded;
+    }
+
+    @Override
+    public Set<YamlNode> keys() {
+        return null;
+    }
+
+    @Override
+    public YamlNode value(final YamlNode key) {
+        return null;
+    }
+
+    @Override
+    public Comment comment() {
+        return new BuiltComment(this, "");
+    }
+}


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java b/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java
new file mode 100644
index 0000000..c81a750
--- /dev/null
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadFlowMappingTestCase.java
@@ -0,0 +1,71 @@
+/**
+ * Copyright (c) 2016-2020, Mihai Emil Andronache
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ *  list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright notice,
+ *  this list of conditions and the following disclaimer in the documentation
+ *  and/or other materials provided with the distribution.
+ * Neither the name of the copyright holder nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+package com.amihaiemil.eoyaml;
+
+import org.hamcrest.MatcherAssert;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * Unit tests for {@link ReadFlowMapping}.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 6.0.0
+ */
+public final class ReadFlowMappingTestCase {
+
+    /**
+     * YamlMapping in flow format has no comment.
+     */
+    @Test
+    public void hasNoComment() {
+        final YamlMapping map = new ReadFlowMapping(
+            Mockito.mock(YamlLine.class)
+        );
+        MatcherAssert.assertThat(
+            map.comment().value(),
+            Matchers.isEmptyString()
+        );
+        MatcherAssert.assertThat(
+            map.comment().yamlNode(),
+            Matchers.is(map)
+        );
+    }
+
+    /**
+     * ReadFlowMapping can return its keys.
+     */
+    @Test
+    public void returnsKeys() {
+        final YamlMapping map = new ReadFlowMapping(
+            new RtYamlLine("{a:b, [s]:s, {s:d,f:r}:e}", 0)
+        );
+    }
+
+}