Skip to content

Commit

Permalink
#618 ReadFlowSequence.comment() implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Apr 13, 2024
1 parent 95f12af commit d465649
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
28 changes: 27 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/ReadFlowSequence.java
Expand Up @@ -141,7 +141,33 @@ public Collection<YamlNode> values() {

@Override
public Comment comment() {
return new BuiltComment(this, "");
boolean documentComment = this.previous.number() < 0;
//@checkstyle LineLength (50 lines)
return new ReadComment(
new Backwards(
new FirstCommentFound(
new Backwards(
new Skip(
this.all,
line -> {
final boolean skip;
if(documentComment) {
skip = line.number() >= this.folded.number();
} else {
skip = line.number() >= this.previous.number();
}
return skip;
},
line -> line.trimmed().startsWith("..."),
line -> line.trimmed().startsWith("%"),
line -> line.trimmed().startsWith("!!")
)
),
documentComment
)
),
this
);
}

/**
Expand Down
Expand Up @@ -44,7 +44,7 @@
public final class ReadFlowMappingTestCase {

/**
* YamlMapping in flow format can return the document comment.
* ReadFlowMapping can return the document comment.
*/
@Test
public void hasDocumentComment() {
Expand All @@ -70,7 +70,7 @@ public void hasDocumentComment() {
}

/**
* YamlMapping in flow format can return the comment referring to it.
* ReadFlowMapping can return the comment referring to it.
*/
@Test
public void hasOwnNodeComment() {
Expand Down
44 changes: 38 additions & 6 deletions src/test/java/com/amihaiemil/eoyaml/ReadFlowSequenceTestCase.java
Expand Up @@ -46,18 +46,50 @@
public final class ReadFlowSequenceTestCase {

/**
* Sequences in flow/json style have no comment.
* ReadFlowSequence can return the document comment.
*/
@Test
public void hasNoComment() {
public void hasDocumentComment() {
final YamlSequence seq = new ReadFlowSequence(
Mockito.mock(YamlLine.class),
Mockito.mock(YamlLine.class),
new AllYamlLines(new ArrayList<>())
new RtYamlLine("[{a: b}, {c: d}, {e: f}]", 2),
new RtYamlLine("---", 1),
new AllYamlLines(
Arrays.asList(
new RtYamlLine("# this is a flow sequence document", 0),
new RtYamlLine("---", 1),
new RtYamlLine("[{a: b}, {c: d}, {e: f}]", 2)
)
)
);
MatcherAssert.assertThat(
seq.comment().value(),
Matchers.equalTo("this is a flow sequence document")
);
MatcherAssert.assertThat(
seq.comment().yamlNode(),
Matchers.is(seq)
);
}

/**
* ReadFlowMapping can return the comment referring to it.
*/
@Test
public void hasOwnNodeComment() {
final YamlSequence seq = new ReadFlowSequence(
new RtYamlLine("[{a: b}, {c: d}, {e: f}]", 2),
new RtYamlLine("flow_seq:", 1),
new AllYamlLines(
Arrays.asList(
new RtYamlLine("# this comment about the 'flow' seq", 0),
new RtYamlLine("flow:", 1),
new RtYamlLine(" [{a: b}, {c: d}, {e: f}]", 2)
)
)
);
MatcherAssert.assertThat(
seq.comment().value(),
Matchers.isEmptyString()
Matchers.equalTo("this comment about the 'flow' seq")
);
MatcherAssert.assertThat(
seq.comment().yamlNode(),
Expand Down

0 comments on commit d465649

Please sign in to comment.