Skip to content

Commit

Permalink
methods in ReadYamlMapping and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Feb 27, 2017
1 parent 05099b5 commit 391a70c
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 12 deletions.
4 changes: 3 additions & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@
<module name="SimplifyBooleanReturn"/>
<module name="StringLiteralEquality"/>
<module name="NestedForDepth"/>
<module name="NestedIfDepth"/>
<module name="NestedIfDepth">
<property name="max" value="3"/>
</module>
<module name="NestedTryDepth"/>
<module name="NoClone"/>
<module name="NoFinalizer"/>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/amihaiemil/camel/AbstractYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ abstract class AbstractYamlLines implements Iterable<YamlLine> {
*/
abstract AbstractYamlLines nested(final int after);

/**
* Number of lines.
* @return Integer.
*/
abstract int count();

/**
* Turn these lines into a YamlNode.
* @return YamlNode
Expand Down
83 changes: 75 additions & 8 deletions src/main/java/com/amihaiemil/camel/ReadYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
package com.amihaiemil.camel;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -61,7 +61,7 @@ public Collection<YamlNode> children() {
final List<YamlNode> kids = new LinkedList<>();
for (final YamlLine line : this.lines) {
final String trimmed = line.trimmed();
if("?".equals(trimmed)) {
if("?".equals(trimmed) || ":".equals(trimmed)) {
continue;
} else {
if(trimmed.endsWith(":")) {
Expand All @@ -84,7 +84,6 @@ public Collection<YamlNode> children() {
}
}
}
Collections.sort(kids);
return kids;
}

Expand All @@ -95,7 +94,7 @@ public YamlMapping yamlMapping(final String key) {

@Override
public YamlMapping yamlMapping(final YamlNode key) {
return null;
return (YamlMapping) this.nodeValue(key, true);
}

@Override
Expand All @@ -105,7 +104,7 @@ public YamlSequence yamlSequence(final String key) {

@Override
public YamlSequence yamlSequence(final YamlNode key) {
return null;
return (YamlSequence) this.nodeValue(key, false);
}

@Override
Expand All @@ -122,7 +121,24 @@ public String string(final String key) {

@Override
public String string(final YamlNode key) {
return null;
String value = null;
boolean found = false;
for (final YamlLine line : this.lines) {
final String trimmed = line.trimmed();
if("?".equals(trimmed)) {
YamlNode keyNode = this.lines.nested(line.number())
.toYamlNode();
if(keyNode.equals(key)) {
found = true;
continue;
}
}
if(found && trimmed.startsWith(":")) {
value = trimmed.substring(trimmed.indexOf(":") + 1).trim();
break;
}
}
return value;
}

@Override
Expand Down Expand Up @@ -160,9 +176,60 @@ private YamlNode nodeValue(final String key, final boolean map) {
return value;
}

/**
* The YamlNode value associated with a String key.
* @param key YamlNode key.
* @param map Is the value a map or a sequence?
* @return YamlNode.
*/
private YamlNode nodeValue(final YamlNode key, final boolean map) {
YamlNode value = null;
for (final YamlLine line : this.lines) {
final String trimmed = line.trimmed();
if("?".equals(trimmed)) {
AbstractYamlLines keyLines = this.lines.nested(line.number());
YamlNode keyNode = keyLines.toYamlNode();
if(keyNode.equals(key)) {
int colonLine = line.number() + keyLines.count() + 1;
if (map) {
value = new ReadYamlMapping(
this.lines.nested(colonLine)
);
} else {
value = new ReadYamlSequence(
this.lines.nested(colonLine)
);
}
}
}
}
return value;
}

@Override
Set<YamlNode> keys() {
// TODO Auto-generated method stub
return null;
final Set<YamlNode> keys = new HashSet<>();
for (final YamlLine line : this.lines) {
final String trimmed = line.trimmed();
if(":".equals(trimmed)) {
continue;
} else if ("?".equals(trimmed)) {
keys.add(this.lines.nested(line.number()).toYamlNode());
} else {
final String[] parts = trimmed.split(":");
if(parts.length < 2) {
throw new IllegalStateException(
"Expected ':' on line " + line.number()
);
} else {
keys.add(
new Scalar(
trimmed.substring(0, trimmed.indexOf(":")).trim()
)
);
}
}
}
return keys;
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/amihaiemil/camel/RtYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -45,13 +46,14 @@ public Iterator<YamlLine> iterator() {
sameIndentation.add(current);
}
}
Collections.sort(sameIndentation);
iterator = sameIndentation.iterator();
}
return iterator;
}

@Override
public AbstractYamlLines nested(final int after) {
AbstractYamlLines nested(final int after) {
final List<YamlLine> nestedLines = new ArrayList<YamlLine>();
YamlLine start = null;
for(final YamlLine line: this.lines) {
Expand All @@ -78,5 +80,10 @@ public String toString() {
return builder.toString();
}

@Override
int count() {
return this.lines.size();
}


}
53 changes: 52 additions & 1 deletion src/test/java/com/amihaiemil/camel/ReadYamlMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@ public void returnsYamlMappingWithStringKey(){
MatcherAssert.assertThat(
second, Matchers.instanceOf(YamlMapping.class)
);
MatcherAssert.assertThat(
second.string("fifth"), Matchers.equalTo("values")
);
}

/**
* ReadYamlMapping can return the YamlMapping mapped to a
* YamlMapping key.
*/
@Test
public void returnsYamlMappingWithYamlMappingKey(){
final YamlMapping key = new RtYamlMappingBuilder()
.add("complex1", "mapping1").add("complex2", "mapping2").build();
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("first: something", 0));
lines.add(new RtYamlLine("? ", 1));
lines.add(new RtYamlLine(" complex1: mapping1", 2));
lines.add(new RtYamlLine(" complex2: mapping2", 3));
lines.add(new RtYamlLine(": ", 4));
lines.add(new RtYamlLine(" map: value", 5));
lines.add(new RtYamlLine("second: something", 6));
final YamlMapping map = new ReadYamlMapping(new RtYamlLines(lines));
final YamlMapping value = map.yamlMapping(key);
MatcherAssert.assertThat(value, Matchers.notNullValue());
MatcherAssert.assertThat(
value, Matchers.instanceOf(YamlMapping.class)
);
MatcherAssert.assertThat(
value.string("map"), Matchers.equalTo("value")
);
}

/**
Expand All @@ -82,7 +112,28 @@ public void returnsYamlSequenceWithStringKey(){
second, Matchers.instanceOf(YamlSequence.class)
);
}


/**
* ReadYamlMapping can return the YamlMapping mapped to a
* YamlMapping key.
*/
@Test
public void returnsStringWithYamlMappingKey(){
final YamlMapping key = new RtYamlMappingBuilder()
.add("complex1", "mapping1").add("complex2", "mapping2").build();
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("first: something", 0));
lines.add(new RtYamlLine("? ", 1));
lines.add(new RtYamlLine(" complex1: mapping1", 2));
lines.add(new RtYamlLine(" complex2: mapping2", 3));
lines.add(new RtYamlLine(": value", 4));
lines.add(new RtYamlLine("second: something", 6));
final YamlMapping map = new ReadYamlMapping(new RtYamlLines(lines));
final String value = map.string(key);
MatcherAssert.assertThat(value, Matchers.notNullValue());
MatcherAssert.assertThat(value, Matchers.equalTo("value"));
}

/**
* ReadYamlMapping can return the String mapped to a
* String key.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/amihaiemil/camel/RtYamlLinesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public void returnsNestedLinesRight() {
AbstractYamlLines yamlLines = new RtYamlLines(lines);

Iterator<YamlLine> iterator = yamlLines.nested(0).iterator();
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(1));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(2));
MatcherAssert.assertThat(iterator.next().number(), Matchers.is(1));
MatcherAssert.assertThat(iterator.hasNext(), Matchers.is(false));

iterator = yamlLines.nested(1).iterator();
Expand Down

0 comments on commit 391a70c

Please sign in to comment.