Repository: decorators-squad/eo-yaml
Pull request #157 (2020-03-06 12:05:25)

#156 YamlSequence is now Iterable
PR for #156 
YamlSequence can now be iterated. Also added unit tests.


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
index 8ceac3f..6b2e5d3 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
@@ -1,6 +1,7 @@
 package com.amihaiemil.eoyaml;
 
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -100,4 +101,9 @@ final class ReadYamlSequence extends AbstractYamlSequence {
         return size;
     }
 
+    @Override
+    public Iterator<YamlNode> iterator() {
+        return this.children().iterator();
+    }
+
 }
diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java
index c08421d..975630b 100644
--- a/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlSequence.java
@@ -28,6 +28,7 @@
 package com.amihaiemil.eoyaml;
 
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -131,4 +132,10 @@ final class RtYamlSequence extends AbstractYamlSequence {
     public int size() {
         return this.nodes.size();
     }
+
+    @Override
+    public Iterator<YamlNode> iterator() {
+        return this.nodes.iterator();
+    }
+
 }
diff --git a/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java
index 2324c64..bea5319 100644
--- a/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/StrictYamlSequence.java
@@ -28,6 +28,7 @@
 package com.amihaiemil.eoyaml;
 
 import java.util.Collection;
+import java.util.Iterator;
 
 /**
  * Decorator for a {@link YamlSequence} which throws YamlNodeNotFoundException
@@ -120,4 +121,9 @@ public final class StrictYamlSequence extends AbstractYamlSequence {
     public int size() {
         return this.decorated.size();
     }
+
+    @Override
+    public Iterator<YamlNode> iterator() {
+        return this.decorated.iterator();
+    }
 }
diff --git a/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java b/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
index f876ab7..459d324 100644
--- a/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
+++ b/src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
@@ -1,12 +1,14 @@
 package com.amihaiemil.eoyaml;
 
+import java.util.Iterator;
+
 /**
  * A Yaml sequence.
  * @author Mihai Andronache (amihaiemil@gmail.com)
  * @version $Id$
  * @since 1.0.0
  */
-public interface YamlSequence extends YamlNode {
+public interface YamlSequence extends YamlNode, Iterable<YamlNode> {
 
     /**
      * The number of Yaml elements (scalars, mappings and sequences) found in
@@ -35,4 +37,11 @@ public interface YamlSequence extends YamlNode {
      * @return String
      */
     String string(final int index);
+    
+    /**
+     * Returns this YamlSequence's children Iterator.<br><br>
+     * It is equivalent to YamlSequence.children().iterator().
+     * @return Iterator of YamlNode.
+     */
+    Iterator<YamlNode> iterator();
 }


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java b/src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java
index 3c82b27..88b0b31 100644
--- a/src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/ReadYamlSequenceTest.java
@@ -129,6 +129,26 @@ public final class ReadYamlSequenceTest {
         MatcherAssert.assertThat(sequence.size(), Matchers.is(3));
     }
     
+    /**
+     * ReadYamlSequence can be iterated.
+     */
+    @Test
+    public void readSequenceIsIterable(){
+        final List<YamlLine> lines = new ArrayList<>();
+        lines.add(new RtYamlLine("- ", 0));
+        lines.add(new RtYamlLine("  beta: somethingElse", 1));
+        lines.add(new RtYamlLine("- scalar", 2));
+        lines.add(new RtYamlLine("- ", 3));
+        lines.add(new RtYamlLine("  alfa: ", 4));
+        lines.add(new RtYamlLine("    fourth: some", 5));
+        lines.add(new RtYamlLine("    key: value", 6));
+        final YamlSequence seq = new ReadYamlSequence(
+            new RtYamlLines(lines)
+        );
+        MatcherAssert.assertThat(seq, Matchers.not(Matchers.emptyIterable()));
+        MatcherAssert.assertThat(seq, Matchers.iterableWithSize(3));
+    }
+    
     /**
      * An empty ReadYamlSequence can be printed.
      * @throws Exception if something goes wrong
diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java
index 2656a17..51a27f9 100644
--- a/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java
@@ -68,6 +68,20 @@ public final class RtYamlSequenceTest {
         );        
     }
     
+    /**
+     * Sequence can be iterated.
+     */
+    @Test
+    public void sequenceIsIterable() {
+        List<YamlNode> nodes = new LinkedList<>();
+        nodes.add(Mockito.mock(YamlNode.class));
+        nodes.add(Mockito.mock(YamlNode.class));
+        nodes.add(Mockito.mock(YamlNode.class));
+        YamlSequence seq = new RtYamlSequence(nodes);
+        MatcherAssert.assertThat(seq, Matchers.not(Matchers.emptyIterable()));
+        MatcherAssert.assertThat(seq, Matchers.iterableWithSize(3));
+    }
+    
     /**
      * A Sequence maintains its order of reading.
      */
diff --git a/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java b/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java
index 1db62f9..123ca5e 100644
--- a/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/StrictYamlSequenceTest.java
@@ -60,6 +60,22 @@ public final class StrictYamlSequenceTest {
             Matchers.equalTo(3)
         );
     }
+    
+    /**
+     * StrictYamlSequence can be iterated.
+     */
+    @Test
+    public void strictSequenceIsIterable() {
+        List<YamlNode> elements = new LinkedList<>();
+        elements.add(new Scalar("key1"));
+        elements.add(new Scalar("key2"));
+        elements.add(new Scalar("key3"));
+        YamlSequence seq = new StrictYamlSequence(
+            new RtYamlSequence(elements)
+        );
+        MatcherAssert.assertThat(seq, Matchers.not(Matchers.emptyIterable()));
+        MatcherAssert.assertThat(seq, Matchers.iterableWithSize(3));
+    }
 
     /**
      * StringYamlSequence can throw YamlNodeNotFoundException