Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Sep 22, 2020
2 parents 246d11e + a696d39 commit 6787936
Show file tree
Hide file tree
Showing 18 changed files with 304 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ private YamlNode mappingSequenceOrPlainScalar(
if (first.trimmed().startsWith("-")){
node = new ReadYamlSequence(prev, this, guessIndentation);
} else if(first.trimmed().contains(":")) {
node = new ReadYamlMapping(prev, this, guessIndentation);
node = new ReadYamlMapping(prev.number(), prev, this,
guessIndentation);
} else if(this.original().size() == 1) {
node = new ReadPlainScalar(this, first);
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/FirstCommentFound.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ private Iterator<YamlLine> nodeComment() {
final List<YamlLine> comment = new ArrayList<>();
while (iterator.hasNext()) {
final YamlLine line = iterator.next();
if(!"---".equals(line.trimmed()) && !line.comment().isEmpty()) {
boolean hasComment = !line.comment().isEmpty();
boolean notYamlStart = !"---".equals(line.trimmed());
if(notYamlStart && hasComment) {
if(line.trimmed().startsWith("#")) {
comment.add(line);
}
Expand Down
33 changes: 22 additions & 11 deletions src/main/java/com/amihaiemil/eoyaml/ReadComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,24 @@
final class ReadComment implements Comment {

/**
* Lines of this comment.
* Node to which this comment refers.
*/
private final YamlLines lines;
private final YamlNode node;

/**
* Node to which this comment refers.
* Calculated comment.
*/
private final YamlNode node;
private final String comment;


/**
* Constructor.
* @param lines Lines of this comment.
* @param node Node to which it refers.
*/
ReadComment(final YamlLines lines, final YamlNode node) {
this.lines = lines;
this.node = node;
this.comment = calculateComments(lines).toString().trim();
}

@Override
Expand All @@ -62,12 +63,22 @@ public YamlNode yamlNode() {

@Override
public String value() {
final StringBuilder comment = new StringBuilder();
for(final YamlLine line : this.lines) {
comment
.append(line.comment().trim())
.append(System.lineSeparator());
return this.comment;
}

/**
* Pre-compute the comment value.
*
* @param lines The lines to parse into comments.
* @return Comments.
*/
private StringBuilder calculateComments(final YamlLines lines) {
final StringBuilder tmpComment = new StringBuilder();
for(final YamlLine line : lines) {
tmpComment
.append(line.comment().trim())
.append(System.lineSeparator());
}
return comment.toString().trim();
return tmpComment;
}
}
26 changes: 15 additions & 11 deletions src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
package com.amihaiemil.eoyaml;

import static com.amihaiemil.eoyaml.YamlLine.UNKNOWN_LINE_NUMBER;

import com.amihaiemil.eoyaml.exceptions.YamlReadingException;
import java.util.*;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -75,6 +77,11 @@ final class ReadYamlMapping extends BaseYamlMapping {
*/
private final boolean guessIndentation;

/**
* Where to stop looking for comments.
*/
private final int commentStop;

/**
* Ctor.
* @param lines Given lines.
Expand All @@ -93,30 +100,26 @@ final class ReadYamlMapping extends BaseYamlMapping {
final AllYamlLines lines,
final boolean guessIndentation
) {
this(new YamlLine.NullYamlLine(), lines, guessIndentation);
}

/**
* Ctor.
* @param previous Line just before the start of this mapping.
* @param lines Given lines.
*/
ReadYamlMapping(final YamlLine previous, final AllYamlLines lines) {
this(previous, lines, Boolean.FALSE);
this(UNKNOWN_LINE_NUMBER, new YamlLine.NullYamlLine(), lines,
guessIndentation);
}

/**
* Ctor.
* @checkstyle ParameterNumber (100 lines)
* @param commentStop Where to
* @param previous Line just before the start of this mapping.
* @param lines Given lines.
* @param guessIndentation If true, we will try to guess the correct
* indentation of misplaced lines.
*/
ReadYamlMapping(
final int commentStop,
final YamlLine previous,
final AllYamlLines lines,
final boolean guessIndentation
) {
this.commentStop = commentStop;
this.previous = previous;
this.all = lines;
this.significant = new SameIndentationLevel(
Expand Down Expand Up @@ -205,7 +208,7 @@ public Comment comment() {
skip = false;
}
} else {
skip = line.number() >= this.previous.number();
skip = line.number() >= commentStop;
}
return skip;
},
Expand Down Expand Up @@ -247,6 +250,7 @@ private YamlNode valueOfStringKey(final String key) {
);
} else if (trimmed.matches(tryKey + ":[ ]*\\{}")) {
value = new EmptyYamlMapping(new ReadYamlMapping(
line.number(),
this.all.line(line.number()),
this.all,
this.guessIndentation
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/ReadYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public Collection<YamlNode> values() {
);
} else if (trimmed.matches("^-[ ]*\\{}")) {
kids.add(new EmptyYamlMapping(new ReadYamlMapping(
line.number(),
this.all.line(line.number()),
this.all,
this.guessIndentation
Expand All @@ -167,7 +168,8 @@ public Collection<YamlNode> values() {
if(this.mappingStartsAtDash(line)) {
kids.add(
new ReadYamlMapping(
new RtYamlLine("", line.number()-1),
line.number() + 1,
this.all.line(line.number() - 1),
this.all,
this.guessIndentation
)
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ private boolean printPossibleComment(
final String alignment
) throws IOException {
boolean printed = false;
if(node != null) {
final String com = node.comment().value();
if(node != null && node.comment() != null) {
Comment tmpComment = node.comment();
final String com = tmpComment.value();
if (com.trim().length() != 0) {
String[] lines = com.split(System.lineSeparator());
for (final String line : lines) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/YamlLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
* @since 1.0.0
*/
interface YamlLine extends Comparable<YamlLine> {
/**
* Indicates we don't know or don't have a line number for the given
* comment (may not have come from a file).
*/
int UNKNOWN_LINE_NUMBER = -1;

/**
* The line's trimmed contents with comments, aliases etc removed.
Expand Down Expand Up @@ -84,7 +89,7 @@ public String comment() {

@Override
public int number() {
return -1;
return UNKNOWN_LINE_NUMBER;
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/amihaiemil/eoyaml/FirstCommentFoundTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,20 @@ public void noFirstComment() {
);
MatcherAssert.assertThat(comment, Matchers.emptyIterable());
}

/**
* {@link FirstCommentFound} returns the comment when called multiple
* times - check we re-parse correctly.
*/
@Test
public void findCommentsMultipleTimes() {
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("# two", 0));
lines.add(new RtYamlLine("three: three", 1));
final YamlLines comment = new FirstCommentFound(
new AllYamlLines(lines)
);
MatcherAssert.assertThat(comment, Matchers.iterableWithSize(1));
MatcherAssert.assertThat(comment, Matchers.iterableWithSize(1));
}
}
55 changes: 39 additions & 16 deletions src/test/java/com/amihaiemil/eoyaml/ReadCommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ public void returnsYamlNode() {
*/
@Test
public void returnsValueFromEmptyLines() {
Comment readComment = new ReadComment(
new AllYamlLines(new ArrayList<>()),
Mockito.mock(YamlNode.class)
);
MatcherAssert.assertThat(
new ReadComment(
new AllYamlLines(new ArrayList<>()),
Mockito.mock(YamlNode.class)
).value(),
readComment.value(),
Matchers.isEmptyString()
);
}
Expand All @@ -78,11 +79,12 @@ public void returnsValueFromEmptyLines() {
public void returnsValueFromSingleLine() {
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("# comment line", 0));
Comment readComment = new ReadComment(
new AllYamlLines(lines),
Mockito.mock(YamlNode.class)
);
MatcherAssert.assertThat(
new ReadComment(
new AllYamlLines(lines),
Mockito.mock(YamlNode.class)
).value(),
readComment.value(),
Matchers.equalTo("comment line")
);
}
Expand All @@ -101,11 +103,12 @@ public void returnsMultiLineComment() {
.append("comment").append(System.lineSeparator())
.append("on multiple").append(System.lineSeparator())
.append("lines");
Comment readComment = new ReadComment(
new AllYamlLines(lines),
Mockito.mock(YamlNode.class)
);
MatcherAssert.assertThat(
new ReadComment(
new AllYamlLines(lines),
Mockito.mock(YamlNode.class)
).value(),
readComment.value(),
Matchers.equalTo(expected.toString())
);
}
Expand All @@ -129,11 +132,31 @@ public void returnsMultiLineCommentWithEmptyLines() {
.append("on multiple").append(System.lineSeparator())
.append(System.lineSeparator())
.append("lines");
Comment readComment = new ReadComment(
new AllYamlLines(lines),
Mockito.mock(YamlNode.class)
);
MatcherAssert.assertThat(
new ReadComment(
new AllYamlLines(lines),
Mockito.mock(YamlNode.class)
).value(),
readComment.value(),
Matchers.equalTo(expected.toString())
);
}

/**
* ReadComment can return a comment starting on a different line.
*/
@Test
public void singleLineCommentStartingOnTheSecondLine() {
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("# comment", 1));
final StringBuilder expected = new StringBuilder();
expected.append("comment");
Comment readComment = new ReadComment(
new AllYamlLines(lines),
Mockito.mock(YamlNode.class)
);
MatcherAssert.assertThat(
readComment.value(),
Matchers.equalTo(expected.toString())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ public void reflectsValuess() {
);
}

/**
* Use the wrong object to construct a sequence - non-collection or array.
*/
@Test(expected = IllegalArgumentException.class)
public void throwsExceptionWhenWrongObject() {
new ReflectedYamlMapping(new String[] {"wrong"});
}

/**
* Simple student pojo for test.
* @checkstyle JavadocVariable (100 lines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,12 @@ public void reflectsIntegerCollection() {
Matchers.equalTo("3")
);
}

/**
* Use the wrong object to construct a sequence - non-collection or array.
*/
@Test(expected = IllegalArgumentException.class)
public void throwsExceptionWhenWrongObject() {
new ReflectedYamlSequence("wrong");
}
}

0 comments on commit 6787936

Please sign in to comment.