Repository: decorators-squad/eo-yaml
Pull request #555 (2023-02-06 20:38:09)

fix reading of folded block scalars #542
PR for #542 

Keep track of reading the folded block scalar, analogous to the fix for #480 in the case of literal block scalars.


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalar.java b/src/main/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalar.java
index 98be4e7..15d15a9 100644
--- a/src/main/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalar.java
+++ b/src/main/java/com/amihaiemil/eoyaml/ReadFoldedBlockScalar.java
@@ -91,6 +91,17 @@ final class ReadFoldedBlockScalar extends BaseFoldedScalar {
             new Skip(
                 lines,
                 line -> line.number() <= previous.number(),
+                line -> {
+                    final YamlLine key = previous;
+                    final Skip.Line skipLine = (Skip.Line) line;
+                    if(skipLine.indentation() == key.indentation()) {
+                        // mark that we finished the block by storing the
+                        // block's key line
+                        skipLine.store(key);
+                    }
+                    // if the key is set then we can safely skip remaining lines
+                    return skipLine.getStored().equals(key);
+                },
                 line -> line.trimmed().endsWith(">"),
                 line -> line.trimmed().startsWith("---"),
                 line -> line.trimmed().startsWith("..."),


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
index ecf1a2e..bc8f312 100644
--- a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
@@ -37,6 +37,8 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.Set;
@@ -1303,6 +1305,135 @@ public final class RtYamlInputTest {
         );
     }
 
+    /**
+     * Unit test for issue 542.
+     * <a href="https://github.com/decorators-squad/eo-yaml/issues/542">#542</a>
+     * @throws IOException If something goes wrong.
+     */
+    @Test
+    public void shouldReadSequenceOfFoldedBlockScalars() throws IOException {
+        final String filename = "issue_542_seq_folded_blocks.yml";
+        final YamlMapping mapping = new RtYamlInput(
+            Files.newInputStream(Paths.get("src/test/resources/" + filename))
+        ).readYamlMapping();
+        MatcherAssert.assertThat(
+            mapping.yamlSequence("list").foldedBlockScalar(0),
+            Matchers.equalTo(
+                "    one" + System.lineSeparator()
+                + "    two" + System.lineSeparator())
+        );
+        MatcherAssert.assertThat(
+            mapping.yamlSequence("list").foldedBlockScalar(1),
+            Matchers.equalTo(
+                "    three" + System.lineSeparator()
+                + "    four" + System.lineSeparator())
+        );
+        MatcherAssert.assertThat(
+            mapping.yamlSequence("list").foldedBlockScalar(2),
+            Matchers.equalTo(
+                "    five" + System.lineSeparator()
+                + "    six" + System.lineSeparator()
+                + "    seven" + System.lineSeparator()
+            )
+        );
+        MatcherAssert.assertThat(
+            mapping.toString(),
+            Matchers.equalTo(
+                Yaml.createYamlMappingBuilder()
+                    .add(
+                        "list",
+                        Yaml.createYamlSequenceBuilder()
+                            .add(
+                                Yaml.createYamlScalarBuilder()
+                                    .addLine("one")
+                                    .addLine("two")
+                                    .buildFoldedBlockScalar()
+                            )
+                            .add(
+                                Yaml.createYamlScalarBuilder()
+                                    .addLine("three")
+                                    .addLine("four")
+                                    .buildFoldedBlockScalar()
+                            )
+                            .add(
+                                Yaml.createYamlScalarBuilder()
+                                    .addLine("five")
+                                    .addLine("six")
+                                    .addLine("seven")
+                                    .buildFoldedBlockScalar()
+                            )
+                        .build()
+                    ).build().toString()
+            )
+        );
+    }
+
+    /**
+     * Unit test for issue 542.
+     * <a href="https://github.com/decorators-squad/eo-yaml/issues/542">#542</a>
+     * @throws IOException If something goes wrong.
+     */
+    @Test
+    public void shouldReadMappingWithMixOfFoldedAndLiteralsScalars()
+        throws IOException {
+        final String filename = "issue_542_seq_folded_blocks_mix.yml";
+        final YamlMapping mapping = new RtYamlInput(
+            Files.newInputStream(Paths.get("src/test/resources/" + filename))
+        ).readYamlMapping();
+        MatcherAssert.assertThat(
+            mapping.toString(),
+            Matchers.equalTo(
+                Yaml.createYamlMappingBuilder()
+                    .add(
+                        "list",
+                        Yaml.createYamlSequenceBuilder()
+                            .add(
+                                Yaml.createYamlScalarBuilder()
+                                    .addLine("one")
+                                    .addLine("two")
+                                    .buildFoldedBlockScalar()
+                            )
+                            .add(
+                                Yaml.createYamlScalarBuilder()
+                                    .addLine("three")
+                                    .addLine("four")
+                                    .buildFoldedBlockScalar()
+                            )
+                            .add(
+                                Yaml.createYamlScalarBuilder()
+                                    .addLine("five")
+                                    .addLine("six")
+                                    .addLine("seven")
+                                    .buildFoldedBlockScalar()
+                            )
+                            .build()
+                    ).add(
+                        "other",
+                        Yaml.createYamlScalarBuilder()
+                            .addLine("one")
+                            .addLine("two")
+                            .buildFoldedBlockScalar()
+
+                    ).add(
+                        "another",
+                        Yaml.createYamlScalarBuilder()
+                            .addLine("three")
+                            .addLine("four")
+                            .buildFoldedBlockScalar()
+
+                    ).add(
+                        "anotherBlock",
+                        Yaml.createYamlScalarBuilder()
+                            .addLine("five")
+                            .addLine("six")
+                            .addLine("seven")
+                            .buildLiteralBlockScalar()
+
+                    ).build().toString()
+            )
+        );
+    }
+
     /**
      * Read a test resource file's contents.
      * @param fileName File to read.
diff --git a/src/test/resources/issue_542_seq_folded_blocks.yml b/src/test/resources/issue_542_seq_folded_blocks.yml
new file mode 100644
index 0000000..ed2f06d
--- /dev/null
+++ b/src/test/resources/issue_542_seq_folded_blocks.yml
@@ -0,0 +1,13 @@
+list:
+  - >
+    one
+
+    two
+  - >
+    three
+
+    four
+  - >
+    five
+    six
+    seven
\ No newline at end of file
diff --git a/src/test/resources/issue_542_seq_folded_blocks_mix.yml b/src/test/resources/issue_542_seq_folded_blocks_mix.yml
new file mode 100644
index 0000000..c727cba
--- /dev/null
+++ b/src/test/resources/issue_542_seq_folded_blocks_mix.yml
@@ -0,0 +1,21 @@
+list:
+  - >
+    one
+    two
+  - >
+    three
+    four
+  - >
+    five
+    six
+    seven
+other: >
+  one
+  two
+another: >
+  three
+  four
+anotherBlock: |
+  five
+  six
+  seven
\ No newline at end of file