use service loading to detect the kunit asserter if its on the classpath

This commit is contained in:
James Strachan
2012-03-13 11:00:16 +00:00
parent 3bf3284341
commit 50a42a2f07
4 changed files with 34 additions and 21 deletions
+1
View File
@@ -6,6 +6,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
@@ -0,0 +1 @@
org.jetbrains.kotlin.test.junit.JUnitAsserter
+6
View File
@@ -14,6 +14,12 @@
<artifactId>stdlib</artifactId> <artifactId>stdlib</artifactId>
<dependencies> <dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kunit</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
+26 -21
View File
@@ -7,28 +7,33 @@ import java.util.ServiceLoader
private var _asserter: Asserter? = null private var _asserter: Asserter? = null
fun asserter(): Asserter { public var asserter: Asserter
if (_asserter == null) { get() {
/* TODO
val loader = ServiceLoader.load(javaClass<Asserter>)
for (a in loader) {
if (a != null) {
_asserter = a
break
}
}
*/
if (_asserter == null) { if (_asserter == null) {
_asserter = DefaultAsserter() val klass = javaClass<Asserter>()
val loader = ServiceLoader.load(klass)
for (a in loader) {
if (a != null) {
_asserter = a
break
}
}
if (_asserter == null) {
_asserter = DefaultAsserter()
}
//debug("using asserter $_asserter")
} }
return _asserter.sure()
}
set(value) {
_asserter = value
} }
return _asserter.sure()
}
/** Asserts that the given block returns true */ /** Asserts that the given block returns true */
inline fun assertTrue(message: String, block: ()-> Boolean) { inline fun assertTrue(message: String, block: ()-> Boolean) {
val actual = block() val actual = block()
asserter().assertTrue(message, actual) asserter.assertTrue(message, actual)
} }
/** Asserts that the given block returns true */ /** Asserts that the given block returns true */
@@ -54,22 +59,22 @@ inline fun assertFalse(actual: Boolean, message: String = "") {
/** 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 */
inline fun assertEquals(expected: Any?, actual: Any?, message: String = "") { inline fun assertEquals(expected: Any?, actual: Any?, message: String = "") {
asserter().assertEquals(message, expected, actual) asserter.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 */
inline fun assertNotNull(actual: Any?, message: String = "") { inline fun assertNotNull(actual: Any?, message: String = "") {
asserter().assertNotNull(message, actual) asserter.assertNotNull(message, actual)
} }
/** Asserts that the expression is null, with an optional message */ /** Asserts that the expression is null, with an optional message */
inline fun assertNull(actual: Any?, message: String = "") { inline fun assertNull(actual: Any?, message: String = "") {
asserter().assertNull(message, actual) asserter.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 */
inline fun fail(message: String = "") { inline fun fail(message: String = "") {
asserter().fail(message) asserter.fail(message)
} }
/** Asserts that given function block returns the given expected value */ /** Asserts that given function block returns the given expected value */
@@ -87,7 +92,7 @@ inline fun <T> expect(expected: T, message: String, block: ()-> T) {
fun fails(block: ()-> Unit): Throwable? { fun fails(block: ()-> Unit): Throwable? {
try { try {
block() block()
asserter().fail("Expected an exception to be thrown") asserter.fail("Expected an exception to be thrown")
return null return null
} catch (e: Throwable) { } catch (e: Throwable) {
println("Caught excepted exception: $e") println("Caught excepted exception: $e")
@@ -99,7 +104,7 @@ fun fails(block: ()-> Unit): Throwable? {
fun <T: Throwable> failsWith(block: ()-> Unit) { fun <T: Throwable> failsWith(block: ()-> Unit) {
try { try {
block() block()
asserter().fail("Expected an exception to be thrown") asserter.fail("Expected an exception to be thrown")
} catch (e: T) { } catch (e: T) {
println("Caught excepted exception: $e") println("Caught excepted exception: $e")
// OK // OK