moved the common test helper methods into the kool.test package as a reusable library

This commit is contained in:
James Strachan
2012-02-28 11:56:49 +00:00
parent a8435c0fe6
commit 949b2ce070
20 changed files with 361 additions and 354 deletions
+2 -1
View File
@@ -133,7 +133,7 @@
</java> </java>
</target> </target>
<target name="compileTestlib" depends="jarRT"> <target name="compileTestlib" depends="compileKunit">
<mkdir dir="${output}/classes/testlib"/> <mkdir dir="${output}/classes/testlib"/>
<java classname="org.jetbrains.jet.cli.KotlinCompiler" failonerror="true" fork="true"> <java classname="org.jetbrains.jet.cli.KotlinCompiler" failonerror="true" fork="true">
<classpath> <classpath>
@@ -157,6 +157,7 @@
<junit printsummary="yes" haltonfailure="true"> <junit printsummary="yes" haltonfailure="true">
<classpath> <classpath>
<pathelement location="${kotlin-home}/lib/kotlin-runtime.jar"/> <pathelement location="${kotlin-home}/lib/kotlin-runtime.jar"/>
<pathelement location="${kotlin-home}/lib/kotlin-test.jar"/>
<pathelement location="${basedir}/testlib/lib/junit-4.9.jar"/> <pathelement location="${basedir}/testlib/lib/junit-4.9.jar"/>
<fileset dir="${basedir}/testlib/lib"> <fileset dir="${basedir}/testlib/lib">
<include name="**/*.jar"/> <include name="**/*.jar"/>
+23 -19
View File
@@ -4,66 +4,62 @@
package kool.test package kool.test
import org.junit.Assert import org.junit.Assert
import junit.framework.TestCase
/** Asserts that the given block returns true */ /** Asserts that the given block returns true */
fun assert(message: String, block: ()-> Boolean) { inline fun assertTrue(message: String, block: ()-> Boolean) {
val actual = block() val actual = block()
Assert.assertTrue(message, actual) Assert.assertTrue(message, actual)
} }
/** Asserts that the given block returns true */ /** Asserts that the given block returns true */
fun assert(block: ()-> Boolean) = assert(block.toString(), block) inline fun assertTrue(block: ()-> Boolean) = assertTrue(block.toString(), block)
/** Asserts that the given block returns false */ /** Asserts that the given block returns false */
fun assertNot(message: String, block: ()-> Boolean) { inline fun assertNot(message: String, block: ()-> Boolean) {
assert(message){ !block() } assertTrue(message){ !block() }
} }
/** Asserts that the given block returns true */ /** Asserts that the given block returns true */
fun assertNot(block: ()-> Boolean) = assertNot(block.toString(), block) inline fun assertNot(block: ()-> Boolean) = assertNot(block.toString(), block)
/** Asserts that the expression is true with an optional message */ /** Asserts that the expression is true with an optional message */
fun assert(actual: Boolean, message: String = "") { inline fun assertTrue(actual: Boolean, message: String = "") {
assertTrue(actual, message)
}
/** Asserts that the expression is true with an optional message */
fun assertTrue(actual: Boolean, message: String = "") {
return assertEquals(true, actual, message) return assertEquals(true, actual, message)
} }
/** Asserts that the expression is false with an optional message */ /** Asserts that the expression is false with an optional message */
fun assertFalse(actual: Boolean, message: String = "") { inline fun assertFalse(actual: Boolean, message: String = "") {
return assertEquals(false, actual, message) return assertEquals(false, actual, message)
} }
/** Asserts that the expected value is equal to the actual value, with an optional message */ /** Asserts that the expected value is equal to the actual value, with an optional message */
fun assertEquals(expected: Any?, actual: Any?, message: String = "") { inline fun assertEquals(expected: Any?, actual: Any?, message: String = "") {
Assert.assertEquals(message, expected, actual) Assert.assertEquals(message, expected, actual)
} }
/** Asserts that the expression is not null, with an optional message */ /** Asserts that the expression is not null, with an optional message */
fun assertNotNull(actual: Any?, message: String = "") { inline fun assertNotNull(actual: Any?, message: String = "") {
Assert.assertNotNull(message, actual) Assert.assertNotNull(message, actual)
} }
/** Asserts that the expression is null, with an optional message */ /** Asserts that the expression is null, with an optional message */
fun assertNull(actual: Any?, message: String = "") { inline fun assertNull(actual: Any?, message: String = "") {
Assert.assertNull(message, actual) Assert.assertNull(message, actual)
} }
/** Marks a test as having failed if this point in the execution path is reached, with an optional message */ /** Marks a test as having failed if this point in the execution path is reached, with an optional message */
fun fail(message: String = "") { inline fun fail(message: String = "") {
Assert.fail(message) Assert.fail(message)
} }
/** Asserts that given function block returns the given expected value */ /** Asserts that given function block returns the given expected value */
fun <T> expect(expected: T, block: ()-> T) { inline fun <T> expect(expected: T, block: ()-> T) {
expect(expected, block.toString(), block) expect(expected, block.toString(), block)
} }
/** Asserts that given function block returns the given expected value and use the given message if it fails */ /** Asserts that given function block returns the given expected value and use the given message if it fails */
fun <T> expect(expected: T, message: String, block: ()-> T) { inline fun <T> expect(expected: T, message: String, block: ()-> T) {
val actual = block() val actual = block()
assertEquals(expected, actual, message) assertEquals(expected, actual, message)
} }
@@ -95,6 +91,14 @@ fun <T: Exception> failsWith(block: ()-> Any) {
* Comments out a block of test code until it is implemented while keeping a link to the code * Comments out a block of test code until it is implemented while keeping a link to the code
* to implement in your unit test output * to implement in your unit test output
*/ */
fun todo(block: ()-> Any) { inline fun todo(block: ()-> Any) {
println("TODO at " + (Exception() as java.lang.Throwable).getStackTrace()?.get(1) + " for " + block) println("TODO at " + (Exception() as java.lang.Throwable).getStackTrace()?.get(1) + " for " + block)
} }
/**
* Useful base class for test cases using the old JUnit 3 naming convention of functions
* starting with "test*" as being a test case
*/
abstract class TestSupport() : TestCase() {
}
+2 -1
View File
@@ -3,7 +3,8 @@ import kotlin.modules.*
fun project() { fun project() {
module("testlib") { module("testlib") {
// TODO how to refer to the dir of the module? // TODO how to refer to the dir of the module?
classpath += "testlib/lib/junit-4.9.jar" classpath += "dist/kotlinc/lib/kotlin-test.jar"
classpath += "kunit/lib/junit-4.9.jar"
addSourceFiles("test") addSourceFiles("test")
} }
+20 -19
View File
@@ -1,11 +1,12 @@
package test.collections package test.collections
import kool.test.*
// TODO can we avoid importing all this stuff by default I wonder? // TODO can we avoid importing all this stuff by default I wonder?
// e.g. making println and the collection builder methods public by default? // e.g. making println and the collection builder methods public by default?
import std.* import std.*
import std.io.* import std.io.*
import std.util.* import std.util.*
import stdhack.test.*
import java.util.* import java.util.*
class CollectionTest() : TestSupport() { class CollectionTest() : TestSupport() {
@@ -22,7 +23,7 @@ class CollectionTest() : TestSupport() {
val data = arrayList("foo", "bar") val data = arrayList("foo", "bar")
fun testAny() { fun testAny() {
assert { assertTrue {
data.any{it.startsWith("f")} data.any{it.startsWith("f")}
} }
assertNot { assertNot {
@@ -31,7 +32,7 @@ class CollectionTest() : TestSupport() {
} }
fun testAll() { fun testAll() {
assert { assertTrue {
data.all{it.length == 3} data.all{it.length == 3}
} }
assertNot { assertNot {
@@ -47,7 +48,7 @@ class CollectionTest() : TestSupport() {
fun testFilter() { fun testFilter() {
val foo = data.filter{it.startsWith("f")} val foo = data.filter{it.startsWith("f")}
assert { assertTrue {
foo.all{it.startsWith("f")} foo.all{it.startsWith("f")}
} }
assertEquals(1, foo.size) assertEquals(1, foo.size)
@@ -57,7 +58,7 @@ class CollectionTest() : TestSupport() {
fun testFilterNot() { fun testFilterNot() {
val foo = data.filterNot{it.startsWith("b")} val foo = data.filterNot{it.startsWith("b")}
assert { assertTrue {
foo.all{it.startsWith("f")} foo.all{it.startsWith("f")}
} }
assertEquals(1, foo.size) assertEquals(1, foo.size)
@@ -68,13 +69,13 @@ class CollectionTest() : TestSupport() {
// TODO would be nice to avoid the <String> // TODO would be nice to avoid the <String>
val foo = data.filter(linkedList<String>()){it.startsWith("f")} val foo = data.filter(linkedList<String>()){it.startsWith("f")}
assert { assertTrue {
foo.all{it.startsWith("f")} foo.all{it.startsWith("f")}
} }
assertEquals(1, foo.size) assertEquals(1, foo.size)
assertEquals(linkedList("foo"), foo) assertEquals(linkedList("foo"), foo)
assert { assertTrue {
foo is LinkedList<String> foo is LinkedList<String>
} }
} }
@@ -83,13 +84,13 @@ class CollectionTest() : TestSupport() {
// TODO would be nice to avoid the <String> // TODO would be nice to avoid the <String>
val foo = data.filter(hashSet<String>()){it.startsWith("f")} val foo = data.filter(hashSet<String>()){it.startsWith("f")}
assert { assertTrue {
foo.all{it.startsWith("f")} foo.all{it.startsWith("f")}
} }
assertEquals(1, foo.size) assertEquals(1, foo.size)
assertEquals(hashSet("foo"), foo) assertEquals(hashSet("foo"), foo)
assert { assertTrue {
foo is HashSet<String> foo is HashSet<String>
} }
} }
@@ -146,7 +147,7 @@ class CollectionTest() : TestSupport() {
// TODO would be nice to be able to write this as this // TODO would be nice to be able to write this as this
// numbers.map{it.toString()}.fold(""){it + it2} // numbers.map{it.toString()}.fold(""){it + it2}
numbers.map<Int,String>{it.toString()}.fold(""){(it, it2) -> it + it2} numbers.map<Int, String>{it.toString()}.fold(""){(it, it2) -> it + it2}
} }
} }
@@ -156,7 +157,7 @@ class CollectionTest() : TestSupport() {
// TODO would be nice to be able to write this as this // TODO would be nice to be able to write this as this
// numbers.map{it.toString()}.foldRight(""){it + it2} // numbers.map{it.toString()}.foldRight(""){it + it2}
numbers.map<Int,String>{it.toString()}.foldRight(""){(it, it2) -> it + it2} numbers.map<Int, String>{it.toString()}.foldRight(""){(it, it2) -> it + it2}
} }
} }
@@ -166,7 +167,7 @@ class CollectionTest() : TestSupport() {
/* /*
TODO inference engine should not need this type info? TODO inference engine should not need this type info?
*/ */
val byLength = words.groupBy<String,Int>{it.length} val byLength = words.groupBy<String, Int>{it.length}
assertEquals(4, byLength.size()) assertEquals(4, byLength.size())
println("Grouped by length is: $byLength") println("Grouped by length is: $byLength")
@@ -190,8 +191,8 @@ class CollectionTest() : TestSupport() {
we should be able to remove the explicit type on the function we should be able to remove the explicit type on the function
http://youtrack.jetbrains.net/issue/KT-1145 http://youtrack.jetbrains.net/issue/KT-1145
*/ */
val lengths = data.map<String,Int>{s -> s.length} val lengths = data.map<String, Int>{s -> s.length}
assert { assertTrue {
lengths.all{it == 3} lengths.all{it == 3}
} }
assertEquals(2, lengths.size) assertEquals(2, lengths.size)
@@ -220,7 +221,7 @@ class CollectionTest() : TestSupport() {
val arr = data.toArray() val arr = data.toArray()
println("Got array ${arr}") println("Got array ${arr}")
todo { todo {
assert { assertTrue {
arr is Array<String> arr is Array<String>
} }
} }
@@ -259,13 +260,13 @@ class CollectionTest() : TestSupport() {
assertFalse(data.contains("some")) assertFalse(data.contains("some"))
// TODO: Problems with generation // TODO: Problems with generation
// assertTrue(IterableWrapper(data).contains("bar")) // assertTrue(IterableWrapper(data).contains("bar"))
// assertFalse(IterableWrapper(data).contains("some")) // assertFalse(IterableWrapper(data).contains("some"))
assertFalse(hashSet<Int>().contains(12)) assertFalse(hashSet<Int>().contains(12))
assertTrue(linkedList(15, 19, 20).contains(15)) assertTrue(linkedList(15, 19, 20).contains(15))
// assertTrue(IterableWrapper(hashSet(45, 14, 13)).contains(14)) // assertTrue(IterableWrapper(hashSet(45, 14, 13)).contains(14))
// assertFalse(IterableWrapper(linkedList<Int>()).contains(15)) // assertFalse(IterableWrapper(linkedList<Int>()).contains(15))
} }
} }
+1 -1
View File
@@ -1,7 +1,7 @@
import java.util.Vector import java.util.Vector
import junit.framework.TestCase import junit.framework.TestCase
import stdhack.test.assertEquals import kool.test.assertEquals
class EnumerationIteratorTest() : TestCase() { class EnumerationIteratorTest() : TestCase() {
fun testIteration () { fun testIteration () {
+1 -1
View File
@@ -1,7 +1,7 @@
package test.standard package test.standard
import std.* import std.*
import stdhack.test.* import kool.test.*
class GetOrElseTest() : TestSupport() { class GetOrElseTest() : TestSupport() {
val v1: String? = "hello" val v1: String? = "hello"
+1 -1
View File
@@ -1,6 +1,6 @@
package test.collections package test.collections
import stdhack.test.* import kool.test.*
import std.io.* import std.io.*
import std.util.* import std.util.*
+1 -1
View File
@@ -1,6 +1,6 @@
package testjc package testjc
import stdhack.test.* import kool.test.*
class C() class C()
+1 -1
View File
@@ -1,6 +1,6 @@
package test.collections package test.collections
import stdhack.test.* import kool.test.*
// TODO can we avoid importing all this stuff by default I wonder? // TODO can we avoid importing all this stuff by default I wonder?
// e.g. making println and the collection builder methods public by default? // e.g. making println and the collection builder methods public by default?
+5 -5
View File
@@ -1,6 +1,6 @@
package test.collections package test.collections
import stdhack.test.* import kool.test.*
// TODO can we avoid importing all this stuff by default I wonder? // TODO can we avoid importing all this stuff by default I wonder?
// e.g. making println and the collection builder methods public by default? // e.g. making println and the collection builder methods public by default?
@@ -10,7 +10,7 @@ import std.util.*
import java.util.* import java.util.*
class MapTest() : TestSupport() { class MapTest() : TestSupport() {
val data: java.util.Map<String,Int> = java.util.HashMap<String, Int>() val data: java.util.Map<String, Int> = java.util.HashMap<String, Int>()
fun testGetOrElse() { fun testGetOrElse() {
val a = data.getOrElse("foo"){2} val a = data.getOrElse("foo"){2}
@@ -32,19 +32,19 @@ class MapTest() : TestSupport() {
} }
fun testSizeAndEmpty() { fun testSizeAndEmpty() {
assert{ data.empty } assertTrue{ data.empty }
assertEquals(data.size, 0) assertEquals(data.size, 0)
} }
fun testSetViaIndexOperators() { fun testSetViaIndexOperators() {
val map = java.util.HashMap<String, String>() val map = java.util.HashMap<String, String>()
assert{ map.empty } assertTrue{ map.empty }
// TODO cannot use map.size due to compiler bug // TODO cannot use map.size due to compiler bug
assertEquals(map.size(), 0) assertEquals(map.size(), 0)
map["name"] = "James" map["name"] = "James"
assert{ !map.empty } assertTrue{ !map.empty }
// TODO cannot use map.size due to compiler bug // TODO cannot use map.size due to compiler bug
assertEquals(map.size(), 1) assertEquals(map.size(), 1)
assertEquals("James", map["name"]) assertEquals("James", map["name"])
+1 -1
View File
@@ -4,7 +4,7 @@ import std.math.*
import java.math.BigInteger import java.math.BigInteger
import java.math.BigDecimal import java.math.BigDecimal
import stdhack.test.* import kool.test.*
class MathTest : TestSupport() { class MathTest : TestSupport() {
fun testBigInteger() { fun testBigInteger() {
+2 -2
View File
@@ -3,7 +3,7 @@ package test.collections
import std.* import std.*
import std.io.* import std.io.*
import std.util.* import std.util.*
import stdhack.test.* import kool.test.*
import java.util.* import java.util.*
import java.io.* import java.io.*
@@ -15,7 +15,7 @@ class OldStdlibTest() : TestSupport() {
} }
fun testCollectionSize() { fun testCollectionSize() {
assert { assertTrue {
Arrays.asList(0, 1, 2)?.size == 3 Arrays.asList(0, 1, 2)?.size == 3
} }
} }
+7 -7
View File
@@ -3,14 +3,14 @@ package test.collections
import std.* import std.*
import std.io.* import std.io.*
import std.util.* import std.util.*
import stdhack.test.* import kool.test.*
import java.util.* import java.util.*
class SetTest() : TestSupport() { class SetTest() : TestSupport() {
val data = hashSet("foo", "bar") val data = hashSet("foo", "bar")
fun testAny() { fun testAny() {
assert { assertTrue {
data.any{it.startsWith("f")} data.any{it.startsWith("f")}
} }
assertNot { assertNot {
@@ -19,7 +19,7 @@ class SetTest() : TestSupport() {
} }
fun testAll() { fun testAll() {
assert { assertTrue {
data.all{it.length == 3} data.all{it.length == 3}
} }
assertNot { assertNot {
@@ -30,13 +30,13 @@ class SetTest() : TestSupport() {
fun testFilter() { fun testFilter() {
val foo = data.filter{it.startsWith("f")}.toSet() val foo = data.filter{it.startsWith("f")}.toSet()
assert { assertTrue {
foo.all{it.startsWith("f")} foo.all{it.startsWith("f")}
} }
assertEquals(1, foo.size) assertEquals(1, foo.size)
assertEquals(hashSet("foo"), foo) assertEquals(hashSet("foo"), foo)
assert("Filter on a Set should return a Set") { assertTrue("Filter on a Set should return a Set") {
foo is Set<String> foo is Set<String>
} }
} }
@@ -59,8 +59,8 @@ class SetTest() : TestSupport() {
we should be able to remove the explicit type on the function we should be able to remove the explicit type on the function
http://youtrack.jetbrains.net/issue/KT-849 http://youtrack.jetbrains.net/issue/KT-849
*/ */
val lengths = data.map<String,Int>{(s: String) -> s.length} val lengths = data.map<String, Int>{(s: String) -> s.length}
assert { assertTrue {
lengths.all{it == 3} lengths.all{it == 3}
} }
assertEquals(2, lengths.size) assertEquals(2, lengths.size)
+2 -2
View File
@@ -1,7 +1,7 @@
package test.collections package test.collections
import std.* import std.*
import stdhack.test.* import kool.test.*
class StandardCollectionTest() : TestSupport() { class StandardCollectionTest() : TestSupport() {
@@ -13,7 +13,7 @@ class StandardCollectionTest() : TestSupport() {
// TODO requires KT-924 to be implemented // TODO requires KT-924 to be implemented
val data: Iterable<String> = std.util.arrayList("foo", "bar") val data: Iterable<String> = std.util.arrayList("foo", "bar")
assert { assertTrue {
data.any{it.startsWith("f")} data.any{it.startsWith("f")}
} }
assertNot { assertNot {
+1 -1
View File
@@ -2,7 +2,7 @@ package test.stdlib.issues
import java.util.List import java.util.List
import std.util.* import std.util.*
import stdhack.test.* import kool.test.*
private fun listDifference<T>(first : List<T>, second : List<T>) : List<T> { private fun listDifference<T>(first : List<T>, second : List<T>) : List<T> {
return first.filter{ !second.contains(it) }.toList() return first.filter{ !second.contains(it) }.toList()
+1 -1
View File
@@ -1,7 +1,7 @@
package testString package testString
import std.io.* import std.io.*
import stdhack.test.* import kool.test.*
import junit.framework.* import junit.framework.*
+3 -3
View File
@@ -3,7 +3,7 @@ package test.dom
import std.* import std.*
import std.dom.* import std.dom.*
import std.util.* import std.util.*
import stdhack.test.* import kool.test.*
import org.w3c.dom.* import org.w3c.dom.*
class DomBuilderTest() : TestSupport() { class DomBuilderTest() : TestSupport() {
@@ -11,7 +11,7 @@ class DomBuilderTest() : TestSupport() {
fun testBuildDocument() { fun testBuildDocument() {
var doc = createDocument() var doc = createDocument()
assert { assertTrue {
doc["grandchild"].isEmpty() doc["grandchild"].isEmpty()
} }
@@ -53,7 +53,7 @@ class DomBuilderTest() : TestSupport() {
val root = doc.rootElement val root = doc.rootElement
if (root != null) { if (root != null) {
assert { assertTrue {
root.hasClass("bar") root.hasClass("bar")
} }
+1 -1
View File
@@ -2,7 +2,7 @@ package test.dom
import std.* import std.*
import std.dom.* import std.dom.*
import stdhack.test.* import kool.test.*
import org.w3c.dom.* import org.w3c.dom.*
class DomTest() : TestSupport() { class DomTest() : TestSupport() {
@@ -2,7 +2,7 @@ package test.language
import junit.framework.TestCase import junit.framework.TestCase
import java.util.Collection import java.util.Collection
import stdhack.test.* import kool.test.*
class NullableCollectionsTest : TestCase() { class NullableCollectionsTest : TestCase() {
+1 -1
View File
@@ -3,7 +3,7 @@ package test.properties
import std.* import std.*
import std.properties.* import std.properties.*
import std.util.* import std.util.*
import stdhack.test.* import kool.test.*
import java.util.* import java.util.*
import junit.framework.TestCase import junit.framework.TestCase