Repository: decorators-squad/eo-yaml
Pull request #468 (2021-06-07 10:52:42)

#463 print ScalarComment
PR for #463 

Adjusted printing logic to print ScalarComment properly.
Tests.


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
index b7fb2e4..4da5e33 100644
--- a/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
+++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
@@ -60,6 +60,7 @@ final class RtYamlPrinter implements YamlPrinter {
         try {
             if (node instanceof Scalar) {
                 this.writer.append("---").append(System.lineSeparator());
+                this.printPossibleComment(node, "");
                 this.printScalar((Scalar) node, 0);
                 this.writer.append(System.lineSeparator()).append("...");
             } else if (node instanceof YamlSequence) {
@@ -133,9 +134,7 @@ final class RtYamlPrinter implements YamlPrinter {
         while(keysIt.hasNext()) {
             final YamlNode key = keysIt.next();
             final YamlNode value = mapping.value(key);
-            if(!(value instanceof Scalar)) {
-                this.printPossibleComment(value, alignment.toString());
-            }
+            this.printPossibleComment(value, alignment.toString());
             this.writer.append(alignment);
             if(key instanceof Scalar) {
                 this.writer.append(
@@ -185,16 +184,13 @@ final class RtYamlPrinter implements YamlPrinter {
         final Iterator<YamlNode> valuesIt = sequence.values().iterator();
         while(valuesIt.hasNext()) {
             final YamlNode node = valuesIt.next();
+            this.printPossibleComment(node, alignment.toString());
+            this.writer
+                .append(alignment)
+                .append("-");
             if (node instanceof Scalar) {
-                this.writer
-                    .append(alignment)
-                    .append("-");
                 this.printNode(node, false, 0);
             } else  {
-                this.printPossibleComment(node, alignment.toString());
-                this.writer
-                    .append(alignment)
-                    .append("-");
                 this.printNode(node, true, indentation + 2);
             }
             if(valuesIt.hasNext()) {
@@ -245,14 +241,21 @@ final class RtYamlPrinter implements YamlPrinter {
                     this.indent(scalar.value(), indentation + 2)
                 );
         } else {
-            this.writer.append(
-                this.indent(
-                    new Escaped(scalar).value(),
-                    0
-                )
-            );
-            if(!scalar.comment().value().isEmpty()) {
-                this.writer.append(" # ").append(scalar.comment().value());
+            final Comment comment = scalar.comment();
+            if(comment instanceof ScalarComment) {
+                this.writer.append(
+                    this.indent(
+                        new Escaped(scalar).value(),
+                        0
+                    )
+                );
+                final ScalarComment scalarComment = (ScalarComment) comment;
+
+                if(!scalarComment.inline().value().isEmpty()) {
+                    this.writer.append(" # ").append(
+                        scalarComment.inline().value()
+                    );
+                }
             }
         }
     }
@@ -311,7 +314,12 @@ final class RtYamlPrinter implements YamlPrinter {
     ) throws IOException {
         boolean printed = false;
         if(node != null && node.comment() != null) {
-            Comment tmpComment = node.comment();
+            final Comment tmpComment;
+            if(node.comment() instanceof ScalarComment) {
+                tmpComment = ((ScalarComment) node.comment()).above();
+            } else {
+                tmpComment = node.comment();
+            }
             final String com = tmpComment.value();
             if (com.trim().length() != 0) {
                 String[] lines = com.split(System.lineSeparator());


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java b/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java
index ebc45ca..136b102 100644
--- a/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java
@@ -44,8 +44,6 @@ import java.io.IOException;
  * @author Mihai Andronache (amihaiemil@gmail.com)
  * @version $Id$
  * @since 4.2.0
- * @todo #457:60min Continue modifying the printing logic for scalar comments:
- *  The above and inline comments should be printed properly.
  */
 public final class YamlMappingCommentsPrintTest {
 
@@ -240,6 +238,86 @@ public final class YamlMappingCommentsPrintTest {
         );
     }
 
+    /**
+     * A read YamlMapping should print itself together with the
+     * read scalar .
+     * @throws Exception If something goes wrong.
+     */
+    @Test
+    public void printsReadYamlMappingWithScalarComments() throws Exception {
+        final YamlMapping read = Yaml.createYamlInput(
+            new File("src/test/resources/scalarCommentsInMapping.yml")
+        ).readYamlMapping();
+        System.out.println(read);
+        MatcherAssert.assertThat(
+            read.toString(),
+            Matchers.equalTo(
+                this.readExpected("scalarCommentsInMapping.yml")
+            )
+        );
+    }
+
+    /**
+     * We can print a built scalar with 2 comments.
+     */
+    @Test
+    public void printsBuiltScalarWithTwoComments() {
+        final Scalar scalar = Yaml.createYamlScalarBuilder()
+            .addLine("simple")
+            .buildPlainScalar("comment on top\nline2", "comment");
+        System.out.println(scalar);
+        MatcherAssert.assertThat(
+            scalar.toString(),
+            Matchers.equalTo(
+                "---" + System.lineSeparator()
+                + "# comment on top" + System.lineSeparator()
+                + "# line2" + System.lineSeparator()
+                + "simple # comment" + System.lineSeparator()
+                + "..."
+            )
+        );
+    }
+
+    /**
+     * We can print a built scalar an inline comment.
+     */
+    @Test
+    public void printsBuiltScalarWithInline() {
+        final Scalar scalar = Yaml.createYamlScalarBuilder()
+            .addLine("simple")
+            .buildPlainScalar("comment");
+        System.out.println(scalar);
+        MatcherAssert.assertThat(
+            scalar.toString(),
+            Matchers.equalTo(
+                "---" + System.lineSeparator()
+                    + "simple # comment" + System.lineSeparator()
+                    + "..."
+            )
+        );
+    }
+
+    /**
+     * We can print a built scalar with comment above.
+     */
+    @Test
+    public void printsBuiltScalarWithAboveComment() {
+        final Scalar scalar = Yaml.createYamlScalarBuilder()
+            .addLine("simple")
+            .buildPlainScalar("comment on top\nline2", "");
+        System.out.println(scalar);
+        MatcherAssert.assertThat(
+            scalar.toString(),
+            Matchers.equalTo(
+                "---" + System.lineSeparator()
+                    + "# comment on top" + System.lineSeparator()
+                    + "# line2" + System.lineSeparator()
+                    + "simple" + System.lineSeparator()
+                    + "..."
+            )
+        );
+    }
+
     /**
      * Read a test resource file's contents.
      * @param fileName File to read.
diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlSequenceCommentsPrintTest.java b/src/test/java/com/amihaiemil/eoyaml/YamlSequenceCommentsPrintTest.java
index d14e46f..162c467 100644
--- a/src/test/java/com/amihaiemil/eoyaml/YamlSequenceCommentsPrintTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/YamlSequenceCommentsPrintTest.java
@@ -206,6 +206,25 @@ public final class YamlSequenceCommentsPrintTest {
         );
     }
 
+    /**
+     * A read YamlSequence should print itself together with the
+     * read scalar comments.
+     * @throws Exception If something goes wrong.
+     */
+    @Test
+    public void printsReadYamlSequenceWithScalarComments() throws Exception {
+        final YamlSequence read = Yaml.createYamlInput(
+            new File("src/test/resources/scalarCommentsInSequence.yml")
+        ).readYamlSequence();
+        System.out.println(read);
+        MatcherAssert.assertThat(
+            read.toString(),
+            Matchers.equalTo(
+                this.readExpected("scalarCommentsInSequence.yml")
+            )
+        );
+    }
+
     /**
      * Read a test resource file's contents.
      * @param fileName File to read.