Skip to content

Commit

Permalink
Add method YamlSequence.yamlNode
Browse files Browse the repository at this point in the history
This returns the YamlNode at position `index` without casting it to a
subtype.
  • Loading branch information
Rudolf Schlatte committed Dec 6, 2023
1 parent 2142ea0 commit b677bf4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
Expand Up @@ -67,6 +67,23 @@ default int size() {
return this.values().size();
}

/**
* Get the Yaml node from the given index.
* @param index Integer index.
* @return Yaml node.
*/
default YamlNode yamlNode(final int index) {
YamlNode result = null;
int count = 0;
for (final YamlNode node : this.values()) {
if (count == index) {
result = node;
}
count = count + 1;
}
return result;
}

/**
* Get the Yaml mapping from the given index.
* @param index Integer index.
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/amihaiemil/eoyaml/RtYamlSequenceTest.java
Expand Up @@ -178,6 +178,21 @@ public void returnsLiteralBlockScalar() {
);
}

/**
* RtYamlSequence can return a YamlNode.
*/
@Test
public void returnsYamlNode() {
List<YamlNode> nodes = new LinkedList<>();
nodes.add(new PlainStringScalar("test"));
nodes.add(Mockito.mock(YamlMapping.class));
nodes.add(new PlainStringScalar("mihai"));
YamlSequence seq = new RtYamlSequence(nodes);
MatcherAssert.assertThat(
seq.yamlNode(1), Matchers.notNullValue()
);
}

/**
* RtYamlSequence can return a YamlMapping.
*/
Expand Down

0 comments on commit b677bf4

Please sign in to comment.