added test case and bug fix for (next|previous)(Elements|Siblings) methods

This commit is contained in:
James Strachan
2012-05-22 18:51:44 +01:00
parent 88c4778055
commit a81e40fae2
2 changed files with 59 additions and 6 deletions
+9 -6
View File
@@ -202,23 +202,26 @@ fun Node.nextSiblings() : Iterator<Node> = NextSiblingIterator(this)
class NextSiblingIterator(var node: Node) : AbstractIterator<Node>() {
override fun computeNext(): Unit {
val next = node.getNextSibling()
if (next != null) {
setNext(next)
val nextValue = node.getNextSibling()
if (nextValue != null) {
setNext(nextValue)
node = nextValue
} else {
done()
}
}
}
/** Returns an [[Iterator]] over the next siblings of this node */
fun Node.previousSiblings() : Iterator<Node> = PreviousSiblingIterator(this)
class PreviousSiblingIterator(var node: Node) : AbstractIterator<Node>() {
override fun computeNext(): Unit {
val next = node.getPreviousSibling()
if (next != null) {
setNext(next)
val nextValue = node.getPreviousSibling()
if (nextValue != null) {
setNext(nextValue)
node = nextValue
} else {
done()
}
@@ -0,0 +1,50 @@
package test.dom
import kotlin.*
import kotlin.dom.*
import kotlin.util.*
import kotlin.test.*
import org.w3c.dom.*
import org.junit.Test as test
class NextSiblingTest {
test fun nextSibling() {
val doc = createDocument()
doc.addElement("foo") {
id = "id1"
style = "bold"
classes = "bar"
addElement("child") {
id = "id2"
classes = "another"
addElement("grandChild") {
id = "id3"
classes = " bar tiny"
addText("Hello World!")
// TODO support neater syntax sugar for adding text?
// += "Hello World!"
}
addElement("grandChild2") {
id = "id4"
classes = "tiny thing bar "
addText("Hello World!")
// TODO support neater syntax sugar for adding text?
// += "Hello World!"
}
}
}
println("builder document: ${doc.toXmlString()}")
val elems = doc["#id3"]
val element = elems.first()
val elements = element.nextElements().toList()
val nodes = element.nextSiblings().toList()
assertEquals(1, elements.size())
assertEquals(1, nodes.size())
}
}