Skip to content

Commit

Permalink
Merge pull request #468 from decorators-squad/463
Browse files Browse the repository at this point in the history
#463 print ScalarComment
  • Loading branch information
amihaiemil authored Jun 7, 2021
2 parents acc4ba1 + b190a54 commit ef6d216
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 21 deletions.
46 changes: 27 additions & 19 deletions src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -185,16 +184,13 @@ private void printSequence(
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()) {
Expand Down Expand Up @@ -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()
);
}
}
}
}
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
* @author Mihai Andronache ([email protected])
* @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 {

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ef6d216

Please sign in to comment.