moved the stdlib tests into the same directory as the stdlib, so it works a bit better with maven support in IDEA

This commit is contained in:
James Strachan
2012-03-14 16:14:04 +00:00
parent 9cd21d3a4d
commit 7ef65c0099
35 changed files with 2 additions and 2 deletions
+123
View File
@@ -0,0 +1,123 @@
package test.dom
import kotlin.*
import kotlin.dom.*
import kotlin.util.*
import kotlin.test.*
import org.w3c.dom.*
import junit.framework.TestCase
class DomBuilderTest() : TestCase() {
fun testBuildDocument() {
var doc = createDocument()
assertTrue {
doc["grandchild"].isEmpty()
}
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 = "id3"
classes = "tiny thing bar "
addText("Hello World!")
// TODO support neater syntax sugar for adding text?
// += "Hello World!"
}
}
}
println("builder document: ${doc.toXmlString()}")
// test css selections on document
assertEquals(0, doc[".doesNotExist"].size())
assertEquals(1, doc[".another"].size())
assertEquals(3, doc[".bar"].size())
assertEquals(2, doc[".tiny"].size())
// element tag selections
assertEquals(0, doc["doesNotExist"].size())
assertEquals(1, doc["foo"].size())
assertEquals(1, doc["child"].size())
assertEquals(1, doc["grandChild"].size())
// id selections
assertEquals(1, doc["#id1"].size())
assertEquals(1, doc["#id2"].size())
assertEquals(1, doc["#id3"].size())
val root = doc.rootElement
if (root != null) {
assertTrue {
root.hasClass("bar")
}
// test css selections on element
assertEquals(0, root[".doesNotExist"].size())
assertEquals(1, root[".another"].size())
assertEquals(2, root[".bar"].size())
assertEquals(2, root[".tiny"].size())
// element tag selections
assertEquals(0, root["doesNotExist"].size())
assertEquals(0, root["foo"].size())
assertEquals(1, root["child"].size())
assertEquals(1, root["grandChild"].size())
// id selections
assertEquals(1, root["#id1"].size())
assertEquals(1, root["#id2"].size())
assertEquals(1, root["#id3"].size())
} else {
fail("No root!")
}
val grandChild = doc["grandChild"].first
if (grandChild != null) {
println("got element ${grandChild.toXmlString()} with text '${grandChild.text}`")
assertEquals("Hello World!", grandChild.text)
assertEquals(" bar tiny", grandChild.attribute("class"))
// test the classSet
val classSet = grandChild.classSet
assertTrue(classSet.contains("bar"))
assertTrue(classSet.contains("tiny"))
assertTrue(classSet.size == 2 )
assertFalse(classSet.contains("doesNotExist"))
// lets add a new class and some existing classes
grandChild.addClass("bar")
grandChild.addClass("newThingy")
assertEquals("bar tiny newThingy", grandChild.classes)
// remove
grandChild.removeClass("bar")
assertEquals("tiny newThingy", grandChild.classes)
grandChild.removeClass("tiny")
assertEquals("newThingy", grandChild.classes)
} else {
fail("Not an Element $grandChild")
}
val children = doc.rootElement.children()
val xml = children.toXmlString()
println("root element has children: ${xml}")
assertEquals(1, children.size())
}
}
+39
View File
@@ -0,0 +1,39 @@
package test.dom
import kotlin.*
import kotlin.dom.*
import kotlin.test.*
import org.w3c.dom.*
import junit.framework.TestCase
class DomTest() : TestCase() {
fun testCreateDocument() {
var doc = createDocument()
assertNotNull(doc, "Should have created a document")
val e = doc.createElement("foo").sure()
assertCssClass(e, "")
// now lets update the cssClass property
e.classes = "foo"
assertCssClass(e, "foo")
// now using the attribute directly
e.setAttribute("class", "bar")
assertCssClass(e, "bar")
doc += e
println("document ${doc.toXmlString()}")
}
fun assertCssClass(e: Element, value: String?): Unit {
val cl = e.classes
val cl2 = e.getAttribute("class")
println("element ${e.toXmlString()} has cssClass `${cl}` class attr `${cl2}`")
assertEquals(value, cl, "value of element.cssClass")
assertEquals(value, cl2, "value of element.getAttribute(\"class\")")
}
}