Repository: decorators-squad/eo-yaml
Pull request #215 (2020-03-23 17:14:26)

#212 fixed empty stream case and added more unit tests
PR for #212 

* fixed reading of empty YamlStream
* added unit tests also for the cases of empty mapping and sequence


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadYamlStream.java b/src/main/java/com/amihaiemil/eoyaml/ReadYamlStream.java
index c439c38..6b08bb6 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadYamlStream.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadYamlStream.java
@@ -58,11 +58,10 @@ final class ReadYamlStream extends ComparableYamlStream {
     public Collection<YamlNode> values() {
         final List<YamlNode> values = new ArrayList<>();
         for(final YamlLine startDoc : this.lines) {
-            values.add(
-                this.lines
-                    .nested(startDoc.number())
-                    .toYamlNode(startDoc)
-            );
+            final YamlLines document = this.lines.nested(startDoc.number());
+            if(!document.lines().isEmpty()) {
+                values.add(document.toYamlNode(startDoc));
+            }
         }
         return values;
 


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadYamlStreamTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadYamlStreamTest.java
index 27f1a2f..0b107c8 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadYamlStreamTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadYamlStreamTest.java
@@ -29,7 +29,6 @@ package com.amihaiemil.eoyaml;
 
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
-import org.junit.Ignore;
 import org.junit.Test;
 
 import java.util.ArrayList;
@@ -57,16 +56,8 @@ public final class ReadYamlStreamTest {
     /**
      * If an empty Yaml stream is provided,
      * then ReadYamlStream should be empty.
-     * @todo #90:30min Fix this test case. The problem is that,
-     *  by the time .toYamlNode(...) is called, the YamlLines
-     *  iterable is actually empty and iterator.next() throws an
-     *  exception. A more general solution should be provided, we
-     *  should actually have an EmptyYamlNode implementation, or
-     *  something else, to represent the case of a YamlNode with no
-     *  lines.
      */
     @Test
-    @Ignore
     public void worksWithEmptyStream() {
         final List<YamlLine> lines = new ArrayList<>();
         lines.add(new RtYamlLine("---", 0));
diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
index a1aea7f..1e9c88d 100644
--- a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
@@ -366,6 +366,66 @@ public final class RtYamlInputTest {
         );
     }
 
+    /**
+     * RtYamlInput can read an empty mapping.
+     * @throws Exception If something goes wrong.
+     */
+    @Test
+    public void readsEmptyMapping() throws Exception {
+        final String newLine = System.lineSeparator();
+        YamlMapping read = Yaml.createYamlInput(
+            "---" + newLine + "..."
+        ).readYamlMapping();
+        MatcherAssert.assertThat(read.values(), Matchers.emptyIterable());
+        MatcherAssert.assertThat(read.keys(), Matchers.emptyIterable());
+        MatcherAssert.assertThat(read.toString(), Matchers.isEmptyString());
+
+        read = Yaml.createYamlInput("").readYamlMapping();
+        MatcherAssert.assertThat(read.values(), Matchers.emptyIterable());
+        MatcherAssert.assertThat(read.keys(), Matchers.emptyIterable());
+        MatcherAssert.assertThat(read.toString(), Matchers.isEmptyString());
+    }
+
+    /**
+     * RtYamlInput can read an empty sequence.
+     * @throws Exception If something goes wrong.
+     */
+    @Test
+    public void readsEmptySequence() throws Exception {
+        final String newLine = System.lineSeparator();
+        YamlSequence read = Yaml.createYamlInput(
+            "---" + newLine + "..."
+        ).readYamlSequence();
+        MatcherAssert.assertThat(read.values(), Matchers.emptyIterable());
+        MatcherAssert.assertThat(read.toString(), Matchers.isEmptyString());
+
+        read = Yaml.createYamlInput("").readYamlSequence();
+        MatcherAssert.assertThat(read.values(), Matchers.emptyIterable());
+        MatcherAssert.assertThat(read.toString(), Matchers.isEmptyString());
+    }
+
+    /**
+     * RtYamlInput can read an empty stream.
+     * @throws Exception If something goes wrong.
+     */
+    @Test
+    public void readsEmptyStream() throws Exception {
+        final String newLine = System.lineSeparator();
+        YamlStream read = Yaml.createYamlInput(
+            "---" + newLine + "..."
+        ).readYamlStream();
+        MatcherAssert.assertThat(read.values(), Matchers.emptyIterable());
+        MatcherAssert.assertThat(
+            read.toString(), Matchers.equalTo("---" + newLine + "...")
+        );
+
+        read = Yaml.createYamlInput("").readYamlStream();
+        MatcherAssert.assertThat(read.values(), Matchers.emptyIterable());
+        MatcherAssert.assertThat(
+            read.toString(), Matchers.equalTo("---" + newLine + "...")
+        );
+    }
+
     /**
      * Read a test resource file's contents.
      * @param fileName File to read.