added count(predicate) helper function to String along with Char.is*() methods from java.lang.Character

This commit is contained in:
James Strachan
2012-04-17 10:12:51 +01:00
parent bd39691072
commit 3b95e7fa7f
3 changed files with 57 additions and 9 deletions
+14 -8
View File
@@ -3,17 +3,17 @@ package test.string
import kotlin.io.*
import kotlin.test.*
import junit.framework.*
import org.junit.Test as test
class StringTest() : TestCase() {
fun testStringIterator() {
class StringTest {
test fun stringIterator() {
var sum = 0
for(c in "239")
sum += (c.toInt() - '0'.toInt())
assertTrue(sum == 14)
}
fun testStringBuilderIterator() {
test fun stringBuilderIterator() {
var sum = 0
val sb = StringBuilder()
for(c in "239")
@@ -26,7 +26,7 @@ class StringTest() : TestCase() {
assertTrue(sum == 14)
}
fun testOrEmpty() {
test fun orEmpty() {
val s: String? = "hey"
val ns: String? = null
@@ -34,16 +34,22 @@ class StringTest() : TestCase() {
assertEquals("", ns.orEmpty())
}
fun testToShort() {
test fun toShort() {
assertEquals(77.toShort(), "77".toShort())
}
fun testToInt() {
test fun toInt() {
assertEquals(77, "77".toInt())
}
fun testToLong() {
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)
}
}