Repository: decorators-squad/eo-yaml
Pull request #250 (2020-03-28 18:10:22)

#220 implemented and tested YamlSequence.literalBlockScalar()
PR for #220
Implemented and tested YamlSequence.literalBlockScalar(...)


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
index 68bd230..a231071 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
@@ -27,10 +27,7 @@
  */
 package com.amihaiemil.eoyaml;
 
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.*;
 
 /**
  * YamlSequence read from somewhere.
@@ -130,6 +127,23 @@ final class ReadYamlSequence extends BaseYamlSequence {
         return value;
     }
 
+    @Override
+    public Collection<String> literalBlockScalar(final int index) {
+        Collection<String> value = null;
+        int count = 0;
+        for (final YamlNode node : this.values()) {
+            if(count == index && (node instanceof ReadLiteralBlockScalar)) {
+                value = Arrays.asList(
+                    ((ReadLiteralBlockScalar) node)
+                        .value().split(System.lineSeparator())
+                );
+                break;
+            }
+            count++;
+        }
+        return value;
+    }
+
     @Override
     @SuppressWarnings("unused")
     public int size() {
diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java
index f3aca93..a71e50d 100644
--- a/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java
@@ -27,10 +27,7 @@
  */
 package com.amihaiemil.eoyaml;
 
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.*;
 
 /**
  * YAML sequence implementation (rt means runtime).
@@ -103,6 +100,22 @@ final class RtYamlSequence extends BaseYamlSequence {
         return found;
     }
 
+    @Override
+    public Collection<String> literalBlockScalar(final int index) {
+        final YamlNode value = this.nodes.get(index);
+        final Collection<String> found;
+        if (value instanceof RtYamlScalarBuilder.BuiltLiteralBlockScalar) {
+            found = Arrays.asList(
+                ((RtYamlScalarBuilder.BuiltLiteralBlockScalar) value)
+                    .value()
+                    .split(System.lineSeparator())
+            );
+        } else {
+            found = null;
+        }
+        return found;
+    }
+
     @Override
     public Collection<YamlNode> values() {
         final List<YamlNode> children = new LinkedList<>();
diff --git a/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java
index 2a2f637..a7d6fc2 100644
--- a/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java
@@ -101,14 +101,14 @@ public final class StrictYamlSequence extends BaseYamlSequence {
         String found = this.decorated.string(index);
         if (found == null) {
             throw new YamlNodeNotFoundException(
-                "No String found for index " + index
+                "No String found at index " + index
             );
         }
         return found;
     }
 
     /**
-     * Get the folded bloc String from the given index.
+     * Get the folded block String from the given index.
      * @param index Integer index.
      * @return String
      */
@@ -117,7 +117,23 @@ public final class StrictYamlSequence extends BaseYamlSequence {
         String found = this.decorated.foldedBlockScalar(index);
         if (found == null) {
             throw new YamlNodeNotFoundException(
-                    "No String found for index " + index
+                "No folded block scalar found at index " + index
+            );
+        }
+        return found;
+    }
+
+    /**
+     * Get the literal block Strings from the given index.
+     * @param index Integer index.
+     * @return Collection of String lines of the literal.
+     */
+    @Override
+    public Collection<String> literalBlockScalar(final int index) {
+        Collection<String> found = this.decorated.literalBlockScalar(index);
+        if (found == null) {
+            throw new YamlNodeNotFoundException(
+                "No String found at index " + index
             );
         }
         return found;
diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
index 8701adb..8486ad8 100644
--- a/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
@@ -83,6 +83,13 @@ public interface YamlSequence extends YamlNode, Iterable<YamlNode> {
      */
     String foldedBlockScalar(final int index);
 
+    /**
+     * Get the literal block scalar from the given index.
+     * @param index Integer index.
+     * @return The folded block scalar as String.
+     */
+    Collection<String> literalBlockScalar(final int index);
+
     /**
      * Returns this YamlSequence's children Iterator.<br><br>
      * It is equivalent to YamlSequence.values().iterator().


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java
index 829634d..2c789f8 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java
@@ -28,6 +28,8 @@
 package com.amihaiemil.eoyaml;
 
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 
 import org.hamcrest.MatcherAssert;
@@ -138,6 +140,42 @@ public final class ReadYamlSequenceTest {
         );
     }
 
+    /**
+     * ReadYamlSequence can return the folded block scalar as a collection of
+     * string lines from a given index.
+     */
+    @Test
+    public void returnsLiteralBlockScalarFromIndex(){
+        final List<YamlLine> lines = new ArrayList<>();
+        lines.add(new RtYamlLine("- rultor", 0));
+        lines.add(new RtYamlLine("- 0pdd", 1));
+        lines.add(new RtYamlLine("- |", 2));
+        lines.add(new RtYamlLine("  first", 3));
+        lines.add(new RtYamlLine("  second", 4));
+        lines.add(new RtYamlLine("- another", 5));
+        final YamlSequence sequence = new ReadYamlSequence(
+            new AllYamlLines(lines)
+        );
+        System.out.println(sequence);
+        final Collection<String> literalLines = sequence.literalBlockScalar(2);
+        MatcherAssert.assertThat(
+            literalLines.size(),
+            Matchers.is(2)
+        );
+        final Iterator<String> linesIt = literalLines.iterator();
+        MatcherAssert.assertThat(
+            linesIt.next(),
+            Matchers.equalTo("first")
+        );
+        MatcherAssert.assertThat(
+            linesIt.next(),
+            Matchers.equalTo("second")
+        );
+        MatcherAssert.assertThat(
+            sequence.yamlMapping(2), Matchers.nullValue()
+        );
+    }
+
     /**
      * ReadYamlSequence can return its size.
      */
diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java
index af42d0c..30bfd9a 100644
--- a/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java
@@ -143,6 +143,41 @@ public final class RtYamlSequenceTest {
         );
     }
 
+    /**
+     * RtYamlSequence can return a literal block Scalar as a
+     * Collection of string lines.
+     */
+    @Test
+    public void returnsLiteralBlockScalar() {
+        final List<YamlNode> nodes = new LinkedList<>();
+        nodes.add(new PlainStringScalar("test"));
+        nodes.add(
+            new RtYamlScalarBuilder.BuiltLiteralBlockScalar(
+                Arrays.asList("first", "second")
+            )
+        );
+        nodes.add(new PlainStringScalar("mihai"));
+        final YamlSequence seq = new RtYamlSequence(nodes);
+        final Collection<String> literalLines = seq.literalBlockScalar(1);
+        MatcherAssert.assertThat(
+            literalLines.size(),
+            Matchers.is(2)
+        );
+        final Iterator<String> linesIt = literalLines.iterator();
+        MatcherAssert.assertThat(
+            linesIt.next(),
+            Matchers.equalTo("first")
+        );
+        MatcherAssert.assertThat(
+            linesIt.next(),
+            Matchers.equalTo("second")
+        );
+
+        MatcherAssert.assertThat(
+            seq.yamlMapping(1), Matchers.nullValue()
+        );
+    }
+
     /**
      * RtYamlSequence can return a YamlMapping.
      */
diff --git a/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java b/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java
index 84e650e..68cda71 100644
--- a/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java
@@ -32,6 +32,7 @@ import org.hamcrest.Matchers;
 import org.junit.Test;
 import org.mockito.Mockito;
 
+import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -125,6 +126,18 @@ public final class StrictYamlSequenceTest {
         strict.foldedBlockScalar(1);
     }
 
+    /**
+     * StringYamlSequence can throw YamlNodeNotFoundException
+     * when the demanded literal block scalar is not found.
+     */
+    @Test (expected = YamlNodeNotFoundException.class)
+    public void exceptionOnNullLiteralBlockScalarString() {
+        YamlSequence origin = Mockito.mock(YamlSequence.class);
+        Mockito.when(origin.literalBlockScalar(1)).thenReturn(null);
+        YamlSequence strict = new StrictYamlSequence(origin);
+        strict.literalBlockScalar(1);
+    }
+
     /**
      * StrictYamlSequence can return the number of elements in a sequence.
      */
@@ -198,4 +211,20 @@ public final class StrictYamlSequenceTest {
         );
     }
 
+    /**
+     * StringYamlSequence can fetch a literal block scalar
+     * as String based in its index.
+     */
+    @Test
+    public void returnsLiteralBlockScalar() {
+        YamlSequence origin = Mockito.mock(YamlSequence.class);
+        Collection<String> lines = Mockito.mock(Collection.class);
+        Mockito.when(origin.literalBlockScalar(1)).thenReturn(lines);
+        YamlSequence strict = new StrictYamlSequence(origin);
+        MatcherAssert.assertThat(
+            strict.literalBlockScalar(1),
+            Matchers.is(lines)
+        );
+    }
+
 }