started compiling more of the kotlin library to JS; now testing the String methods in JS

This commit is contained in:
James Strachan
2012-06-19 07:23:42 -05:00
parent 33b5e46201
commit 071e276e08
18 changed files with 705 additions and 362 deletions
+69
View File
@@ -0,0 +1,69 @@
package test.string
import kotlin.test.*
import org.junit.Test as test
class StringJVMTest {
test fun stringIterator() {
var sum = 0
for(c in "239")
sum += (c.toInt() - '0'.toInt())
assertTrue(sum == 14)
}
test fun stringBuilderIterator() {
var sum = 0
val sb = StringBuilder()
for(c in "239")
sb.append(c)
println(sb)
for(c in sb)
sum += (c.toInt() - '0'.toInt())
assertTrue(sum == 14)
}
test fun orEmpty() {
val s: String? = "hey"
val ns: String? = null
assertEquals("hey", s.orEmpty())
assertEquals("", ns.orEmpty())
}
test fun toShort() {
assertEquals(77.toShort(), "77".toShort())
}
test fun toInt() {
assertEquals(77, "77".toInt())
}
test fun toLong() {
assertEquals(77.toLong(), "77".toLong())
}
test fun count() {
val text = "hello there\tfoo\nbar"
val whitespaceCount = text.count { it.isWhitespace() }
assertEquals(3, whitespaceCount)
}
test fun testSplitByChar() {
val s = "ab\n[|^$&\\]^cd"
var list = s.split('b');
assertEquals(2, list.size)
assertEquals("a", list[0])
assertEquals("\n[|^$&\\]^cd", list[1])
list = s.split('^')
assertEquals(3, list.size)
assertEquals("cd", list[2])
list = s.split('.')
assertEquals(1, list.size)
assertEquals(s, list[0])
}
}
+14 -58
View File
@@ -1,69 +1,25 @@
package test.string
package test
import kotlin.io.*
import kotlin.test.*
import org.junit.Test as test
class StringTest {
test fun stringIterator() {
var sum = 0
for(c in "239")
sum += (c.toInt() - '0'.toInt())
assertTrue(sum == 14)
test fun startsWith() {
assertTrue("abcd".startsWith("ab"))
assertTrue("abcd".startsWith("abcd"))
assertTrue("abcd".startsWith("a"))
assertFalse("abcd".startsWith("abcde"))
assertFalse("abcd".startsWith("b"))
assertFalse("".startsWith('a'))
}
test fun stringBuilderIterator() {
var sum = 0
val sb = StringBuilder()
for(c in "239")
sb.append(c)
println(sb)
for(c in sb)
sum += (c.toInt() - '0'.toInt())
assertTrue(sum == 14)
}
test fun orEmpty() {
val s: String? = "hey"
val ns: String? = null
assertEquals("hey", s.orEmpty())
assertEquals("", ns.orEmpty())
}
test fun toShort() {
assertEquals(77.toShort(), "77".toShort())
}
test fun toInt() {
assertEquals(77, "77".toInt())
}
test fun toLong() {
assertEquals(77.toLong(), "77".toLong())
}
test fun count() {
val text = "hello there\tfoo\nbar"
val whitespaceCount = text.count { it.isWhitespace() }
assertEquals(3, whitespaceCount)
}
test fun testSplitByChar() {
val s = "ab\n[|^$&\\]^cd"
var list = s.split('b');
assertEquals(2, list.size)
assertEquals("a", list[0])
assertEquals("\n[|^$&\\]^cd", list[1])
list = s.split('^')
assertEquals(3, list.size)
assertEquals("cd", list[2])
list = s.split('.')
assertEquals(1, list.size)
assertEquals(s, list[0])
test fun endsWith() {
assertTrue("abcd".endsWith("d"))
assertTrue("abcd".endsWith("abcd"))
assertFalse("abcd".endsWith("b"))
assertFalse("".endsWith('a'))
}
test fun testStartsWithChar() {
+3 -6
View File
@@ -1,16 +1,13 @@
package test.dom
import kotlin.*
import kotlin.dom.*
import kotlin.util.*
import kotlin.test.*
import org.w3c.dom.*
import junit.framework.TestCase
import org.junit.Test as test
class DomBuilderTest() : TestCase() {
class DomBuilderTest() {
fun testBuildDocument() {
test fun buildDocument() {
var doc = createDocument()
assertTrue {
@@ -1,8 +1,6 @@
package test.dom
import kotlin.*
import kotlin.dom.*
import kotlin.util.*
import kotlin.test.*
import org.w3c.dom.*
import org.junit.Test as test
@@ -50,6 +50,13 @@ import js.noImpl
return if (answer == "Void") "Unit" else if (answer == "Object") "Any" else answer
}
fun parameterTypeName(klass: Class<out Any?>?): String {
val answer = simpleTypeName(klass)
return if (answer == "String" || answer.endsWith("DocumentType")) {
answer + "?"
} else answer
}
for (klass in classes) {
val interfaces = klass.getInterfaces()
val extends = if (interfaces != null && interfaces.size == 1) ": ${interfaces[0]?.getSimpleName()}" else ""
@@ -108,7 +115,7 @@ import js.noImpl
// TODO in java 7 its not easy with reflection to get the parameter argument name...
var counter = 0
val parameters = parameterTypes.map<Class<out Any?>?, String>{ "arg${++counter}: ${simpleTypeName(it)}" }.makeString(", ")
val parameters = parameterTypes.map<Class<out Any?>?, String>{ "arg${++counter}: ${parameterTypeName(it)}" }.makeString(", ")
val returnType = simpleTypeName(method.getReturnType())
println(" public fun ${method.getName()}($parameters): $returnType = js.noImpl")
}