Skip to content

Commit

Permalink
YamlSequence keeps reading/building order
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Aug 14, 2018
1 parent 8d742d6 commit 90684cf
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 53 deletions.
5 changes: 2 additions & 3 deletions src/main/java/com/amihaiemil/camel/ReadYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ final class ReadYamlSequence extends AbstractYamlSequence {
@Override
public Collection<YamlNode> children() {
final List<YamlNode> kids = new LinkedList<>();
final AbstractYamlLines ordered = new OrderedYamlLines(this.lines);
for(final YamlLine line : ordered) {
for(final YamlLine line : this.lines) {
if("-".equals(line.trimmed())) {
kids.add(this.lines.nested(line.number()).toYamlNode(line));
} else {
Expand All @@ -49,7 +48,7 @@ public String toString() {

@Override
public String indent(final int indentation) {
return new OrderedYamlLines(this.lines).indent(indentation);
return this.lines.indent(indentation);
}

@Override
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/com/amihaiemil/camel/RtYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,23 @@ int count() {
String indent(final int indentation) {
final StringBuilder indented = new StringBuilder();
final Iterator<YamlLine> linesIt = this.lines.iterator();
final YamlLine first = linesIt.next();
if (first.indentation() == indentation) {
indented.append(first.toString()).append("\n");
while (linesIt.hasNext()) {
indented.append(linesIt.next().toString()).append("\n");
}
} else {
final int offset = indentation - first.indentation();
for (final YamlLine line : this.lines) {
int correct = line.indentation() + offset;
while (correct > 0) {
indented.append(" ");
correct--;
if(linesIt.hasNext()) {
final YamlLine first = linesIt.next();
if (first.indentation() == indentation) {
indented.append(first.toString()).append("\n");
while (linesIt.hasNext()) {
indented.append(linesIt.next().toString()).append("\n");
}
} else {
final int offset = indentation - first.indentation();
for (final YamlLine line : this.lines) {
int correct = line.indentation() + offset;
while (correct > 0) {
indented.append(" ");
correct--;
}
indented.append(line.trimmed()).append("\n");
}
indented.append(line.trimmed()).append("\n");
}
}
return indented.toString();
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/amihaiemil/camel/RtYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
package com.amihaiemil.camel;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -52,7 +51,6 @@ final class RtYamlSequence extends AbstractYamlSequence {
*/
RtYamlSequence(final Collection<YamlNode> elements) {
this.nodes.addAll(elements);
Collections.sort(this.nodes);
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/amihaiemil/camel/ReadYamlSequenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void returnsYamlMappingFromIndex(){
final YamlSequence sequence = new ReadYamlSequence(
new RtYamlLines(lines)
);
final YamlMapping alfa = sequence.yamlMapping(1);
final YamlMapping alfa = sequence.yamlMapping(2);
MatcherAssert.assertThat(alfa, Matchers.notNullValue());
MatcherAssert.assertThat(alfa, Matchers.instanceOf(YamlMapping.class));
MatcherAssert.assertThat(
Expand All @@ -85,7 +85,7 @@ public void returnsYamlSequenceFromIndex(){
final YamlSequence sequence = new ReadYamlSequence(
new RtYamlLines(lines)
);
final YamlSequence devops = sequence.yamlSequence(2);
final YamlSequence devops = sequence.yamlSequence(0);
MatcherAssert.assertThat(devops, Matchers.notNullValue());
MatcherAssert.assertThat(
devops, Matchers.instanceOf(YamlSequence.class)
Expand All @@ -106,8 +106,8 @@ public void returnsStringFromIndex(){
final YamlSequence devops = new ReadYamlSequence(
new RtYamlLines(lines)
);
MatcherAssert.assertThat(devops.string(0), Matchers.equalTo("0pdd"));
MatcherAssert.assertThat(devops.string(1), Matchers.equalTo("rultor"));
MatcherAssert.assertThat(devops.string(0), Matchers.equalTo("rultor"));
MatcherAssert.assertThat(devops.string(1), Matchers.equalTo("0pdd"));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/amihaiemil/camel/RtYamlInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public void readsMappingWithoutComments() throws Exception {
.add("developers",
Yaml.createYamlSequenceBuilder()
.add("rultor")
.add("sherif")
.add("salikjan")
.add("sherif")
.build()
).build();
YamlMapping read = new RtYamlInput(
Expand Down Expand Up @@ -62,7 +62,6 @@ public void readsMappingWithoutComments() throws Exception {
read.yamlSequence("developers").string(2),
Matchers.equalTo(expected.yamlSequence("developers").string(2))
);
System.out.println(read);
MatcherAssert.assertThat(
read, Matchers.equalTo(expected)
);
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/amihaiemil/camel/RtYamlMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ public void prettyPrintsSimpleYaml() throws Exception {
.add("developers",
Yaml.createYamlSequenceBuilder()
.add("rultor")
.add("sherif")
.add("salikjan")
.add("sherif")
.build()
).build();
String expected = this.readTestResource("simpleMapping.yml");
Expand All @@ -271,17 +271,17 @@ public void prettyPrintsComplexYaml() throws Exception {
YamlMapping yaml = Yaml.createYamlMappingBuilder()
.add(
Yaml.createYamlSequenceBuilder()
.add("Detroit Tigers")
.add("Chicago cubs")
.add("Detroit Tigers")
.build(),
Yaml.createYamlSequenceBuilder()
.add("2001-07-23")
.build()
)
.add(
Yaml.createYamlSequenceBuilder()
.add("New York Yankees")
.add("Atlanta Braves")
.add("New York Yankees")
.build(),
Yaml.createYamlSequenceBuilder()
.add("2001-07-02")
Expand Down
39 changes: 17 additions & 22 deletions src/test/java/com/amihaiemil/camel/RtYamlSequenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public void fetchesChildren() {
}

/**
* A Sequence is ordered.
* A Sequence maintains its order of reading.
*/
@Test
public void sequenceIsOrdered() {
public void sequenceKeepsOrder() {
List<YamlNode> nodes = new LinkedList<>();
Scalar first = new Scalar("test");
Scalar sec = new Scalar("mihai");
Expand All @@ -85,16 +85,16 @@ public void sequenceIsOrdered() {
RtYamlSequence seq = new RtYamlSequence(nodes);
Iterator<YamlNode> ordered = seq.children().iterator();
MatcherAssert.assertThat(
(Scalar) ordered.next(), Matchers.equalTo(fourth)
(Scalar) ordered.next(), Matchers.equalTo(first)
);
MatcherAssert.assertThat(
(Scalar) ordered.next(), Matchers.equalTo(third)
(Scalar) ordered.next(), Matchers.equalTo(sec)
);
MatcherAssert.assertThat(
(Scalar) ordered.next(), Matchers.equalTo(sec)
(Scalar) ordered.next(), Matchers.equalTo(third)
);
MatcherAssert.assertThat(
(Scalar) ordered.next(), Matchers.equalTo(first)
(Scalar) ordered.next(), Matchers.equalTo(fourth)
);
}

Expand All @@ -109,7 +109,7 @@ public void returnsYamlScalarAsString() {
nodes.add(new Scalar("mihai"));
YamlSequence seq = new RtYamlSequence(nodes);
MatcherAssert.assertThat(
seq.string(1), Matchers.equalTo("mihai")
seq.string(1), Matchers.equalTo("amber")
);
MatcherAssert.assertThat(
seq.yamlMapping(1), Matchers.nullValue()
Expand All @@ -127,10 +127,7 @@ public void returnsYamlMapping() {
nodes.add(new Scalar("mihai"));
YamlSequence seq = new RtYamlSequence(nodes);
MatcherAssert.assertThat(
seq.yamlMapping(2), Matchers.notNullValue()
);
MatcherAssert.assertThat(
seq.yamlMapping(1), Matchers.nullValue()
seq.yamlMapping(1), Matchers.notNullValue()
);
}

Expand Down Expand Up @@ -221,9 +218,9 @@ public void comparesToSequence() {
public void prettyPrintsSimpleYamlSequence() throws Exception {
YamlSequence seq = Yaml.createYamlSequenceBuilder()
.add("amihaiemil")
.add("sherif")
.add("salikjan")
.add("rultor")
.add("salikjan")
.add("sherif")
.build();
String expected = this.readTestResource("simpleSequence.yml");
MatcherAssert.assertThat(seq.toString(), Matchers.equalTo(expected));
Expand All @@ -247,22 +244,20 @@ public void printsEmptyYamlSequence() throws Exception {
public void prettyPrintsComplexYamlSequence() throws Exception {
YamlSequence seq = Yaml.createYamlSequenceBuilder()
.add("amihaiemil")
.add(
Yaml
.createYamlMappingBuilder()
.add("rule1", "test")
.add("rule2", "test2")
.add("rule3", "test3")
.build()
)
.add("salikjan")
.add(
Yaml.createYamlSequenceBuilder()
.add("element1")
.add("element2")
.add("element3")
.build()
)
).add(
Yaml.createYamlMappingBuilder()
.add("rule1", "test")
.add("rule2", "test2")
.add("rule3", "test3")
.build()
)
.build();
String expected = this.readTestResource("complexSequence.yml");
MatcherAssert.assertThat(seq.toString(), Matchers.equalTo(expected));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ public void representsCollectionOfStringsAndSimplePojos() {

YamlSequence yaml = new YamlCollectionDump(collection).represent();
MatcherAssert.assertThat(yaml.children().size(), Matchers.equalTo(2));
MatcherAssert.assertThat(yaml.string(0), Matchers.equalTo("objectA"));

YamlMapping yamlMapping = yaml.yamlMapping(1);

YamlMapping yamlMapping = yaml.yamlMapping(0);
MatcherAssert.assertThat(
yamlMapping.string("firstName"),
Matchers.equalTo("John")
Expand All @@ -77,5 +76,6 @@ public void representsCollectionOfStringsAndSimplePojos() {
yamlMapping.string("gpa"),
Matchers.equalTo("4.0")
);
MatcherAssert.assertThat(yaml.string(1), Matchers.equalTo("objectA"));
}
}

0 comments on commit 90684cf

Please sign in to comment.