moved the useful test methods into the kotlin-test jar so its easy to reuse on other modules/projects

This commit is contained in:
James Strachan
2012-02-28 11:21:45 +00:00
parent d6572f8094
commit a8435c0fe6
5 changed files with 149 additions and 2 deletions
+26 -2
View File
@@ -61,6 +61,27 @@
</java>
</target>
<target name="compileKunit" depends="jarRT">
<mkdir dir="${output}/classes/kunit"/>
<java classname="org.jetbrains.jet.cli.KotlinCompiler" failonerror="true" fork="true">
<classpath>
<path refid="classpath"/>
<pathelement location="${kotlin-home}/lib/kotlin-compiler.jar"/>
</classpath>
<arg value="-stdlib"/>
<arg value="${kotlin-home}/lib/kotlin-runtime.jar"/>
<arg value="-src"/>
<arg value="${basedir}/kunit/src/main/kotlin"/>
<arg value="-output"/>
<arg value="${output}/classes/kunit"/>
<arg value="-module"/>
<arg value="${basedir}/kunit/module.kt"/>
</java>
<jar destfile="${kotlin-home}/lib/kotlin-test.jar">
<fileset dir="${output}/classes/kunit"/>
</jar>
</target>
<target name="compileKdoc">
<mkdir dir="${output}/classes/kdoc"/>
<java classname="org.jetbrains.jet.cli.KotlinCompiler" failonerror="true" fork="true">
@@ -77,6 +98,9 @@
<arg value="-module"/>
<arg value="${basedir}/kdoc/module.kt"/>
</java>
<jar destfile="${kotlin-home}/lib/kotlin-doc.jar">
<fileset dir="${output}/classes/kdoc"/>
</jar>
</target>
@@ -172,7 +196,7 @@
</java>
</target>
<target name="jarRT" depends="compile,compileStdlib">
<target name="jarRT" depends="compile,copyKotlinJars,compileStdlib">
<jar destfile="${kotlin-home}/lib/kotlin-runtime.jar">
<fileset dir="${output}/classes/runtime"/>
<fileset dir="${output}/classes/stdlib"/>
@@ -212,7 +236,7 @@
</copy>
</target>
<target name="dist" depends="init,jarRT,copyKotlinJars,jarJDKHeaders,jar,buildToolsJar"/>
<target name="dist" depends="init,jarRT,copyKotlinJars,jarJDKHeaders,jar,buildToolsJar,compileKunit,compileKdoc"/>
<target name="doc" depends="dist,docStdlib"/>
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinRuntime" level="project" />
</component>
</module>
Binary file not shown.
+10
View File
@@ -0,0 +1,10 @@
import kotlin.modules.*
fun project() {
module("kunit") {
// TODO how to refer to the dir of the module?
classpath += "kunit/lib/junit-4.9.jar"
addSourceFiles("src/main/kotlin")
}
}
+100
View File
@@ -0,0 +1,100 @@
/**
* A number of helper methods for writing Kool unit tests
*/
package kool.test
import org.junit.Assert
/** Asserts that the given block returns true */
fun assert(message: String, block: ()-> Boolean) {
val actual = block()
Assert.assertTrue(message, actual)
}
/** Asserts that the given block returns true */
fun assert(block: ()-> Boolean) = assert(block.toString(), block)
/** Asserts that the given block returns false */
fun assertNot(message: String, block: ()-> Boolean) {
assert(message){ !block() }
}
/** Asserts that the given block returns true */
fun assertNot(block: ()-> Boolean) = assertNot(block.toString(), block)
/** Asserts that the expression is true with an optional message */
fun assert(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)
}
/** Asserts that the expression is false with an optional message */
fun assertFalse(actual: Boolean, message: String = "") {
return assertEquals(false, actual, message)
}
/** Asserts that the expected value is equal to the actual value, with an optional message */
fun assertEquals(expected: Any?, actual: Any?, message: String = "") {
Assert.assertEquals(message, expected, actual)
}
/** Asserts that the expression is not null, with an optional message */
fun assertNotNull(actual: Any?, message: String = "") {
Assert.assertNotNull(message, actual)
}
/** Asserts that the expression is null, with an optional message */
fun assertNull(actual: Any?, message: String = "") {
Assert.assertNull(message, actual)
}
/** Marks a test as having failed if this point in the execution path is reached, with an optional message */
fun fail(message: String = "") {
Assert.fail(message)
}
/** Asserts that given function block returns the given expected value */
fun <T> expect(expected: T, block: ()-> T) {
expect(expected, block.toString(), block)
}
/** 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) {
val actual = block()
assertEquals(expected, actual, message)
}
/** Asserts that given function block fails by throwing an exception */
fun fails(block: ()-> Any): Exception? {
try {
block()
Assert.fail("Expected an exception to be thrown")
return null
} catch (e: Exception) {
println("Caught excepted exception: $e")
return e
}
}
/** Asserts that a block fails with a specific exception being thrown */
fun <T: Exception> failsWith(block: ()-> Any) {
try {
block()
Assert.fail("Expected an exception to be thrown")
} catch (e: T) {
println("Caught excepted exception: $e")
// OK
}
}
/**
* 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
*/
fun todo(block: ()-> Any) {
println("TODO at " + (Exception() as java.lang.Throwable).getStackTrace()?.get(1) + " for " + block)
}