Repository: decorators-squad/eo-yaml
Pull request #557 (2023-03-05 14:02:21)

fix for #556
PR for fixing unit test added in #556 


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/SameIndentationLevel.java b/src/main/java/com/amihaiemil/eoyaml/SameIndentationLevel.java
index 916cb3f..e8862ef 100644
--- a/src/main/java/com/amihaiemil/eoyaml/SameIndentationLevel.java
+++ b/src/main/java/com/amihaiemil/eoyaml/SameIndentationLevel.java
@@ -81,7 +81,7 @@ final class SameIndentationLevel implements YamlLines {
             while (iterator.hasNext()) {
                 YamlLine current = iterator.next();
                 if (firstIsDashMap
-                        && this.mapping(current)
+                        && this.isMappingEntry(current)
                         && current.indentation() == firstIndentation + 2) {
                     sameIndentation.add(current);
                 } else if(current.indentation() == firstIndentation) {
@@ -123,11 +123,11 @@ final class SameIndentationLevel implements YamlLines {
      * @param dashLine Line.
      * @return True of false.
      */
-    private boolean mapping(final YamlLine dashLine) {
+    private boolean isMappingEntry(final YamlLine dashLine) {
         final String trimmed = dashLine.trimmed();
         final boolean escapedScalar = trimmed.matches("^\\s*\".*\"$")
                 || trimmed.matches("^\\s*'.*'$");
-        return trimmed.matches("^.*:\\s.+$") && !escapedScalar;
+        return trimmed.matches("^.*:(\\s.*)?$") && !escapedScalar;
     }
 
 }


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
index 15c89ef..c658fbf 100644
--- a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
@@ -1436,46 +1436,66 @@ public final class RtYamlInputTest {
 
     /**
      * Unit test for issue 556.
-     * <a href="https://github.com/decorators-squad/eo-yaml/issues/556">#542</a>
+     * <a href="https://github.com/decorators-squad/eo-yaml/issues/556">#556</a>
      * @throws IOException If something goes wrong.
      */
     @Test
-    public void shouldIterateKeys_issue_556() throws IOException {
+    public void shouldIterateKeysOfFoldedMappingInSequence()
+        throws IOException {
         final String filename = "issue_556_seq_folded_blocks.yml";
         final YamlMapping mapping = new RtYamlInput(
-                Files.newInputStream(Paths.get("src/test/resources/" + filename))
+            Files.newInputStream(Paths.get("src/test/resources/" + filename))
         ).readYamlMapping();
+        System.out.println(mapping);
 
         YamlSequence states = mapping.value("states").asSequence();
         MatcherAssert.assertThat(states.size(), Matchers.equalTo(1));
         YamlMapping state = states.iterator().next().asMapping();
 
-        MatcherAssert.assertThat(state.string("name"), Matchers.equalTo("ConsumeReading"));
-        MatcherAssert.assertThat(state.string("type"), Matchers.equalTo("event"));
-        MatcherAssert.assertThat(state.string("end"), Matchers.equalTo("true"));
+        MatcherAssert.assertThat(
+            state.string("name"), Matchers.equalTo("ConsumeReading")
+        );
+        MatcherAssert.assertThat(
+            state.string("type"), Matchers.equalTo("event")
+        );
+        MatcherAssert.assertThat(
+            state.string("end"), Matchers.equalTo("true")
+        );
         YamlSequence onEvents = state.value("onEvents").asSequence();
         MatcherAssert.assertThat(onEvents.size(), Matchers.equalTo(1));
         YamlMapping event = onEvents.iterator().next().asMapping();
 
-        YamlMapping eventDataFilter = event.value("eventDataFilter").asMapping();
         MatcherAssert.assertThat(event.keys().size(), Matchers.equalTo(3));
 
-        MatcherAssert.assertThat(eventDataFilter.string("toStateData"), Matchers.equalTo("${ .readings }"));
+        YamlMapping eventDataFilter = event.value("eventDataFilter")
+            .asMapping();
+        MatcherAssert.assertThat(
+            eventDataFilter.string("toStateData"),
+            Matchers.equalTo("${ .readings }")
+        );
         YamlSequence actions = event.value("actions").asSequence();
         MatcherAssert.assertThat(actions.size(), Matchers.equalTo(1));
         YamlMapping action = actions.iterator().next().asMapping();
         YamlMapping functionRef = action.value("functionRef").asMapping();
-        MatcherAssert.assertThat(functionRef.string("refName"), Matchers.equalTo("LogReading"));
+        MatcherAssert.assertThat(
+            functionRef.string("refName"),
+            Matchers.equalTo("LogReading")
+        );
 
         YamlSequence eventRefs = event.value("eventRefs").asSequence();
         MatcherAssert.assertThat(eventRefs.size(), Matchers.equalTo(2));
 
-        String eventRef1 = eventRefs.iterator().next().asScalar().value();
-        MatcherAssert.assertThat(eventRef1, Matchers.equalTo("TemperatureEvent"));
-
-        String eventRef2 = eventRefs.iterator().next().asScalar().value();
-        MatcherAssert.assertThat(eventRef2, Matchers.equalTo("HumidityEvent"));
+        String firstEventRef = eventRefs.string(0);
+        MatcherAssert.assertThat(
+            firstEventRef,
+            Matchers.equalTo("TemperatureEvent")
+        );
 
+        String secondEventRef = eventRefs.string(1);
+        MatcherAssert.assertThat(
+            secondEventRef,
+            Matchers.equalTo("HumidityEvent")
+        );
     }
 
     /**
diff --git a/src/test/resources/issue_556_seq_folded_blocks.yml b/src/test/resources/issue_556_seq_folded_blocks.yml
index 47822e0..ee40a41 100644
--- a/src/test/resources/issue_556_seq_folded_blocks.yml
+++ b/src/test/resources/issue_556_seq_folded_blocks.yml
@@ -15,5 +15,4 @@ states:
               refName: LogReading
         eventRefs:
           - TemperatureEvent
-          - HumidityEvent
-
+          - HumidityEvent
\ No newline at end of file