Skip to content

Commit

Permalink
YamlSequence.size() and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Mar 9, 2017
1 parent d1d9a27 commit 5f92c94
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/java/com/amihaiemil/camel/ReadYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,13 @@ public String string(final int index) {
return null;
}

@Override
public int size() {
int size = 0;
for(final YamlLine line : this.lines) {
size = size + 1;
}
return size;
}

}
5 changes: 5 additions & 0 deletions src/main/java/com/amihaiemil/camel/RtYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,9 @@ public String indent(final int indentation) {
printed = printed.substring(0, printed.length() - 1);
return printed;
}

@Override
public int size() {
return this.nodes.size();
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/amihaiemil/camel/StrictYamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@ public String toString() {
public String indent(final int indentation) {
return this.decorated.indent(indentation);
}

@Override
public int size() {
return this.decorated.size();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/amihaiemil/camel/YamlSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
*/
public interface YamlSequence extends YamlNode {

/**
* The number of Yaml elements (scalars, mappings and sequences) found in
* this sequence.
* @return Integer.
*/
int size();

/**
* Get the Yaml mapping from the given index.
* @param index Integer index.
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/amihaiemil/camel/ReadYamlSequenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,23 @@ public void returnsYamlMappingFromIndex(){
alfa.yamlMapping("alfa").string("key"), Matchers.equalTo("value")
);
}

/**
* ReadYamlSequence can return its size.
*/
@Test
public void returnsSize(){
final List<YamlLine> lines = new ArrayList<>();
lines.add(new RtYamlLine("- ", 0));
lines.add(new RtYamlLine(" beta: somethingElse", 1));
lines.add(new RtYamlLine("- scalar", 2));
lines.add(new RtYamlLine("- ", 3));
lines.add(new RtYamlLine(" alfa: ", 4));
lines.add(new RtYamlLine(" fourth: some", 5));
lines.add(new RtYamlLine(" key: value", 6));
final YamlSequence sequence = new ReadYamlSequence(
new RtYamlLines(lines)
);
MatcherAssert.assertThat(sequence.size(), Matchers.is(3));
}
}
13 changes: 13 additions & 0 deletions src/test/java/com/amihaiemil/camel/RtYamlSequenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,19 @@ public void prettyPrintsComplexYamlSequence() throws Exception {
MatcherAssert.assertThat(seq.toString(), Matchers.equalTo(expected));
}

/**
* RtYamlSequence can return its size.
*/
@Test
public void returnsSize(){
List<YamlNode> nodes = new LinkedList<>();
nodes.add(new Scalar("test"));
nodes.add(Mockito.mock(YamlMapping.class));
nodes.add(new Scalar("mihai"));
YamlSequence seq = new RtYamlSequence(nodes);
MatcherAssert.assertThat(seq.size(), Matchers.is(3));
}

/**
* 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 @@ -110,7 +110,7 @@ public void returnsSize() {
);

MatcherAssert.assertThat(
sequence.children().size(),
sequence.size(),
Matchers.equalTo(2)
);
}
Expand Down

0 comments on commit 5f92c94

Please sign in to comment.