Repository: decorators-squad/eo-yaml
Pull request #249 (2020-03-28 17:42:54)

Implemented and tested YamlSequence.foldedBlockScalar(...)
PR for #220 
Implemented and tested ``YamlSequence.foldedBlockScalar(...)``


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
index 84eec68..68bd230 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
@@ -104,15 +104,30 @@ final class ReadYamlSequence extends BaseYamlSequence {
 
     @Override
     public String string(final int index) {
-        String scalar = null;
+        String value = null;
         int count = 0;
         for (final YamlNode node : this.values()) {
-            if (count == index && node instanceof Scalar) {
-                scalar = ((Scalar) node).value();
+            if(count == index && (node instanceof ReadPlainScalarValue)) {
+                value = ((Scalar) node).value();
+                break;
             }
-            count = count + 1;
+            count++;
+        }
+        return value;
+    }
+
+    @Override
+    public String foldedBlockScalar(final int index) {
+        String value = null;
+        int count = 0;
+        for (final YamlNode node : this.values()) {
+            if(count == index && (node instanceof ReadFoldedBlockScalar)) {
+                value = ((Scalar) node).value();
+                break;
+            }
+            count++;
         }
-        return scalar;
+        return value;
     }
 
     @Override
diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java
index 1db1906..f3aca93 100644
--- a/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java
@@ -82,8 +82,21 @@ final class RtYamlSequence extends BaseYamlSequence {
     public String string(final int index) {
         final YamlNode value = this.nodes.get(index);
         final String found;
-        if (value instanceof Scalar) {
-            found = ((Scalar) value).value();
+        if (value instanceof PlainStringScalar) {
+            found = ((PlainStringScalar) value).value();
+        } else {
+            found = null;
+        }
+        return found;
+    }
+
+    @Override
+    public String foldedBlockScalar(final int index) {
+        final YamlNode value = this.nodes.get(index);
+        final String found;
+        if (value instanceof RtYamlScalarBuilder.BuiltFoldedBlockScalar) {
+            found = ((RtYamlScalarBuilder.BuiltFoldedBlockScalar) value)
+                .value();
         } else {
             found = null;
         }
diff --git a/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java
index e9dca14..2a2f637 100644
--- a/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java
@@ -107,6 +107,22 @@ public final class StrictYamlSequence extends BaseYamlSequence {
         return found;
     }
 
+    /**
+     * Get the folded bloc String from the given index.
+     * @param index Integer index.
+     * @return String
+     */
+    @Override
+    public String foldedBlockScalar(final int index) {
+        String found = this.decorated.foldedBlockScalar(index);
+        if (found == null) {
+            throw new YamlNodeNotFoundException(
+                    "No String found for index " + index
+            );
+        }
+        return found;
+    }
+
     @Override
     public int size() {
         return this.decorated.size();
diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
index fc0ca06..8701adb 100644
--- a/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
@@ -76,6 +76,13 @@ public interface YamlSequence extends YamlNode, Iterable<YamlNode> {
      */
     String string(final int index);
 
+    /**
+     * Get the folded block scalar from the given index.
+     * @param index Integer index.
+     * @return The folded block scalar as String.
+     */
+    String foldedBlockScalar(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 8d3e6c2..829634d 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java
@@ -32,6 +32,7 @@ import java.util.List;
 
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
+import org.junit.Ignore;
 import org.junit.Test;
 
 /**
@@ -96,7 +97,7 @@ public final class ReadYamlSequenceTest {
     }
 
     /**
-     * ReadYamlSequence can return the YamlMapping from a given index.
+     * ReadYamlSequence can return the plain scalar string from a given index.
      * Note that a YamlSequence is ordered, so the index might differ from
      * the original found at read time.
      */
@@ -113,6 +114,30 @@ public final class ReadYamlSequenceTest {
         MatcherAssert.assertThat(devops.string(1), Matchers.equalTo("0pdd"));
     }
 
+    /**
+     * ReadYamlSequence can return the folded block scalar as a string,
+     * from a given index.
+     */
+    @Test
+    @Ignore
+    public void returnsFoldedBlockScalarStringFromIndex(){
+        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);
+        MatcherAssert.assertThat(
+            sequence.foldedBlockScalar(2),
+            Matchers.equalTo("first second")
+        );
+    }
+
     /**
      * 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 e14b8fb..af42d0c 100644
--- a/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java
@@ -31,10 +31,8 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.*;
+
 import org.apache.commons.io.IOUtils;
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
@@ -98,32 +96,47 @@ public final class RtYamlSequenceTest {
         nodes.add(fourth);
         RtYamlSequence seq = new RtYamlSequence(nodes);
         Iterator<YamlNode> ordered = seq.values().iterator();
+        MatcherAssert.assertThat(ordered.next(), Matchers.equalTo(first));
+        MatcherAssert.assertThat(ordered.next(), Matchers.equalTo(sec));
+        MatcherAssert.assertThat(ordered.next(), Matchers.equalTo(third));
+        MatcherAssert.assertThat(ordered.next(), Matchers.equalTo(fourth));
+    }
+
+    /**
+     * RtYamlSequence can return a Plain Scalar as a string.
+     */
+    @Test
+    public void returnsYamlScalarAsString() {
+        List<YamlNode> nodes = new LinkedList<>();
+        nodes.add(new PlainStringScalar("test"));
+        nodes.add(new PlainStringScalar("amber"));
+        nodes.add(new PlainStringScalar("mihai"));
+        YamlSequence seq = new RtYamlSequence(nodes);
         MatcherAssert.assertThat(
-            (PlainStringScalar) ordered.next(), Matchers.equalTo(first)
-        );
-        MatcherAssert.assertThat(
-            (PlainStringScalar) ordered.next(), Matchers.equalTo(sec)
-        );
-        MatcherAssert.assertThat(
-            (PlainStringScalar) ordered.next(), Matchers.equalTo(third)
+            seq.string(1), Matchers.equalTo("amber")
         );
         MatcherAssert.assertThat(
-            (PlainStringScalar) ordered.next(), Matchers.equalTo(fourth)
+            seq.yamlMapping(1), Matchers.nullValue()
         );
     }
 
     /**
-     * RtYamlSequence can return a Scalar as a string.
+     * RtYamlSequence can return a folded block Scalar as a string.
      */
     @Test
-    public void returnsYamlScalarAsString() {
+    public void returnsFoldedBlockScalarAsString() {
         List<YamlNode> nodes = new LinkedList<>();
         nodes.add(new PlainStringScalar("test"));
-        nodes.add(new PlainStringScalar("amber"));
+        nodes.add(
+            new RtYamlScalarBuilder.BuiltFoldedBlockScalar(
+                Arrays.asList("first", "second")
+            )
+        );
         nodes.add(new PlainStringScalar("mihai"));
         YamlSequence seq = new RtYamlSequence(nodes);
         MatcherAssert.assertThat(
-            seq.string(1), Matchers.equalTo("amber")
+            seq.foldedBlockScalar(1),
+            Matchers.equalTo("first second")
         );
         MatcherAssert.assertThat(
             seq.yamlMapping(1), Matchers.nullValue()
diff --git a/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java b/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java
index ce64460..84e650e 100644
--- a/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java
@@ -113,6 +113,18 @@ public final class StrictYamlSequenceTest {
         strict.string(1);
     }
 
+    /**
+     * StringYamlSequence can throw YamlNodeNotFoundException
+     * when the demanded folded block scalar is not found.
+     */
+    @Test (expected = YamlNodeNotFoundException.class)
+    public void exceptionOnNullFoldedBlockScalarString() {
+        YamlSequence origin = Mockito.mock(YamlSequence.class);
+        Mockito.when(origin.foldedBlockScalar(1)).thenReturn(null);
+        YamlSequence strict = new StrictYamlSequence(origin);
+        strict.foldedBlockScalar(1);
+    }
+
     /**
      * StrictYamlSequence can return the number of elements in a sequence.
      */
@@ -172,4 +184,18 @@ public final class StrictYamlSequenceTest {
         );
     }
 
+    /**
+     * StringYamlSequence can fetch a folded block scalar
+     * as String based in its index.
+     */
+    @Test
+    public void returnsFoldedBlockScalarString() {
+        YamlSequence origin = Mockito.mock(YamlSequence.class);
+        Mockito.when(origin.foldedBlockScalar(1)).thenReturn("found");
+        YamlSequence strict = new StrictYamlSequence(origin);
+        MatcherAssert.assertThat(
+            strict.foldedBlockScalar(1), Matchers.equalTo("found")
+        );
+    }
+
 }