diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java index b7fb2e4f..4da5e331 100644 --- a/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java +++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java @@ -60,6 +60,7 @@ public void print(final YamlNode node) throws IOException { 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 @@ private void printMapping( 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 @@ private void printSequence( final Iterator 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 @@ private void printScalar( 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 @@ private boolean printPossibleComment( ) 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()); diff --git a/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java b/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java index ebc45cad..136b102c 100644 --- a/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java +++ b/src/test/java/com/amihaiemil/eoyaml/YamlMappingCommentsPrintTest.java @@ -44,8 +44,6 @@ * @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 void readsScalarComments() throws IOException { ); } + /** + * 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 d14e46f1..162c4679 100644 --- a/src/test/java/com/amihaiemil/eoyaml/YamlSequenceCommentsPrintTest.java +++ b/src/test/java/com/amihaiemil/eoyaml/YamlSequenceCommentsPrintTest.java @@ -206,6 +206,25 @@ public void readsScalarComments() throws IOException { ); } + /** + * 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.