simplified the test cases for the preconditions and add them to the API docs; also added requireNotNull() and checkNotNull() helper methods for converting nullables to non-nullables easily in code with optional exception messages

This commit is contained in:
James Strachan
2012-04-17 08:22:28 +01:00
parent a44a75a6a4
commit 34dae731a6
4 changed files with 120 additions and 117 deletions
+63 -58
View File
@@ -6,98 +6,103 @@ object Assertions {
} }
/** /**
* Throws an [[AssertionError]] if the `value` is false and runtime assertions have been * Throws an [[AssertionError]] with specified *message* if the *value* is false
* enabled on the JVM using the `-ea` JVM option. * and runtime assertions have been enabled on the JVM using the *-ea* JVM option.
*/
public inline fun assert(value: Boolean, message: Any = "Assertion failed") {
if (Assertions._ENABLED) {
if (!value) {
throw AssertionError(message)
}
}
}
/**
* Throws an [[AssertionError]] with specified *message* if the *value* is false
* and runtime assertions have been enabled on the JVM using the *-ea* JVM option.
*/ */
public inline fun assert(value: Boolean): Unit {
if(Assertions._ENABLED) {
if(!value) {
throw AssertionError();
}
}
}
/**
* Throws an [[AssertionError]] with specified `message` if the `value` is false
* and runtime assertions have been enabled on the JVM using the `-ea` JVM option.
*/
public inline fun assert(value: Boolean, message: Any) {
if(Assertions._ENABLED) {
if(!value) {
throw AssertionError(message);
}
}
}
/**
* Throws an [[AssertionError]] with specified `message` if the `value` is false
* and runtime assertions have been enabled on the JVM using the `-ea` JVM option.
*/
public inline fun assert(value: Boolean, lazyMessage: () -> String) { public inline fun assert(value: Boolean, lazyMessage: () -> String) {
if(Assertions._ENABLED) { if (Assertions._ENABLED) {
if(!value) { if (!value) {
val message = lazyMessage() val message = lazyMessage()
throw AssertionError(message); throw AssertionError(message)
} }
} }
} }
/** /**
* Throws an [[IllegalArgumentException]] if the `value` is false. * Throws an [[IllegalArgumentException]] with specified *message* if the *value* is false.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt failingRequireWithMessage
*/ */
public inline fun require(value: Boolean): Unit { public inline fun require(value: Boolean, message: Any = "Failed requirement"): Unit {
if(!value) { if (!value) {
throw IllegalArgumentException(); throw IllegalArgumentException(message.toString())
} }
} }
/** /**
* Throws an [[IllegalArgumentException]] with specified `message` if the `value` is false. * Throws an [[IllegalArgumentException]] with specified *message* if the *value* is false.
*/ *
public inline fun require(value: Boolean, message: Any): Unit { * @includeFunctionBody ../../test/PreconditionsTest.kt failingRequireWithLazyMessage
if(!value) {
throw IllegalArgumentException(message.toString());
}
}
/**
* Throws an [[IllegalArgumentException]] with specified `message` if the `value` is false.
*/ */
public inline fun require(value: Boolean, lazyMessage: () -> String): Unit { public inline fun require(value: Boolean, lazyMessage: () -> String): Unit {
if(!value) { if (!value) {
val message = lazyMessage() val message = lazyMessage()
throw IllegalArgumentException(message.toString()); throw IllegalArgumentException(message.toString())
} }
} }
/** /**
* Throws an [[IllegalStateException]] if the `value` is false. * Throws an [[IllegalArgumentException]] with the given *message* if the *value* is null otherwise
* the not null value is returned.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt requireNotNull
*/ */
public inline fun check(value: Boolean): Unit { public inline fun <T> requireNotNull(value: T?, message: Any = "Required value was null"): T {
if(!value) { if (value == null) {
throw IllegalStateException(); throw IllegalArgumentException(message.toString())
} else {
return value
} }
} }
/** /**
* Throws an [[IllegalStateException]] with specified `message` if the `value` is false. * Throws an [[IllegalStateException]] with specified *message* if the *value* is false.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt failingCheckWithMessage
*/ */
public inline fun check(value: Boolean, message: Any): Unit { public inline fun check(value: Boolean, message: Any = "Check failed"): Unit {
if(!value) { if (!value) {
throw IllegalStateException(message.toString()); throw IllegalStateException(message.toString())
} }
} }
/** /**
* Throws an [[IllegalStateException]] with specified `message` if the `value` is false. * Throws an [[IllegalStateException]] with specified *message* if the *value* is false.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt failingCheckWithMessage
*/ */
public inline fun check(value: Boolean, lazyMessage: () -> String): Unit { public inline fun check(value: Boolean, lazyMessage: () -> String): Unit {
if(!value) { if (!value) {
val message = lazyMessage() val message = lazyMessage()
throw IllegalStateException(message.toString()); throw IllegalStateException(message.toString())
}
}
/**
* Throws an [[IllegalStateException]] with the given *message* if the *value* is null otherwise
* the not null value is returned.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt checkNotNull
*/
public inline fun <T> checkNotNull(value: T?, message: String = "Required value was null"): T {
if (value == null) {
throw IllegalStateException(message)
} else {
return value
} }
} }
+2 -1
View File
@@ -63,8 +63,9 @@ public inline fun assertEquals(expected: Any?, actual: Any?, message: String = "
} }
/** Asserts that the expression is not null, with an optional message */ /** Asserts that the expression is not null, with an optional message */
public inline fun assertNotNull(actual: Any?, message: String = "") { public inline fun <T> assertNotNull(actual: T?, message: String = ""): T {
asserter.assertNotNull(message, actual) asserter.assertNotNull(message, actual)
return actual!!
} }
/** Asserts that the expression is not null, with an optional message and a function block to process the not-null value */ /** Asserts that the expression is not null, with an optional message and a function block to process the not-null value */
+2 -3
View File
@@ -40,8 +40,7 @@ class ExceptionTest {
t.printStackTrace(stream) t.printStackTrace(stream)
} }
assertNotNull(byteBuffer.toByteArray()) { bytes -> val bytes = assertNotNull(byteBuffer.toByteArray())
assertTrue(bytes.size > 10) assertTrue(bytes.size > 10)
}
} }
} }
+53 -55
View File
@@ -1,106 +1,104 @@
package test.collections package test.collections
import junit.framework.TestCase import org.junit.Test as test
import kotlin.test.* import kotlin.test.*
import java.lang.IllegalArgumentException
class PreconditionsTest() : TestCase() { class PreconditionsTest() {
fun testPassingRequire() { test fun passingRequire() {
require(true) require(true)
var called = false var called = false
require(true) { called = true; "some message" } require(true) { called = true; "some message" }
assertFalse(called) assertFalse(called)
} }
fun testFailingRequire() { test fun failingRequire() {
val error = fails { val error = failsWith<IllegalArgumentException> {
require(false) require(false)
} }
if(error is IllegalArgumentException) { assertNotNull(error.getMessage())
assertNull(error.getMessage())
} else {
fail("Invalid exception type: "+error)
}
} }
fun testPassingRequireWithMessage() { test fun passingRequireWithMessage() {
require(true, "Hello") require(true, "Hello")
} }
fun testFailingRequireWithMessage() { test fun failingRequireWithMessage() {
val error = fails { val error = failsWith<IllegalArgumentException> {
require(false, "Hello") require(false, "Hello")
} }
if(error is IllegalArgumentException) { assertEquals("Hello", error.getMessage())
assertEquals("Hello", error.getMessage())
} else {
fail("Invalid exception type: "+error)
}
} }
fun testFailingRequireWithLazyMessage() { test fun failingRequireWithLazyMessage() {
val error = fails { val error = failsWith<IllegalArgumentException> {
require(false) {"Hello"} require(false) {"Hello"}
} }
if(error is IllegalArgumentException) { assertEquals("Hello", error.getMessage())
assertEquals("Hello", error.getMessage())
} else {
fail("Invalid exception type: "+error)
}
} }
fun testPassingCheck() { test fun passingCheck() {
check(true) check(true)
var called = false var called = false
check(true) { called = true; "some message" } check(true) { called = true; "some message" }
assertFalse(called) assertFalse(called)
} }
fun testFailingCheck() { test fun failingCheck() {
val error = fails { val error = failsWith<IllegalStateException> {
check(false) check(false)
} }
if(error is IllegalStateException) { assertNotNull(error.getMessage())
assertNull(error.getMessage())
} else {
fail("Invalid exception type: "+error)
}
} }
fun testPassingCheckWithMessage() { test fun passingCheckWithMessage() {
check(true, "Hello") check(true, "Hello")
} }
fun testFailingCheckWithMessage() { test fun failingCheckWithMessage() {
val error = fails { val error = failsWith<IllegalStateException> {
check(false, "Hello") check(false, "Hello")
} }
if(error is IllegalStateException) { assertEquals("Hello", error.getMessage())
assertEquals("Hello", error.getMessage())
} else {
fail("Invalid exception type: "+error)
}
} }
fun testFailingCheckWithLazyMessage() { test fun failingCheckWithLazyMessage() {
val error = fails { val error = failsWith<IllegalStateException> {
check(false) {"Hello"} check(false) {"Hello"}
} }
if(error is IllegalStateException) { assertEquals("Hello", error.getMessage())
assertEquals("Hello", error.getMessage()) }
} else {
fail("Invalid exception type: "+error) test fun requireNotNull() {
val s1: String? = "S1"
val r1: String = requireNotNull(s1)
assertEquals("S1", r1)
}
test fun requireNotNullFails() {
failsWith<IllegalArgumentException> {
val s2: String? = null
requireNotNull(s2)
} }
} }
test fun checkNotNull() {
val s1: String? = "S1"
val r1: String = checkNotNull(s1)
assertEquals("S1", r1)
}
test fun checkNotNullFails() {
failsWith<IllegalStateException> {
val s2: String? = null
checkNotNull(s2)
}
}
// TODO: uncomment when KT-1540 is resolved. // TODO: uncomment when KT-1540 is resolved.
// fun testPassingAssert() { // test fun passingAssert() {
// assert(true) // assert(true)
// var called = false // var called = false
// assert(true) { called = true; "some message" } // assert(true) { called = true; "some message" }
@@ -109,7 +107,7 @@ class PreconditionsTest() : TestCase() {
// } // }
// //
// //
// fun testFailingAssert() { // test fun failingAssert() {
// val error = fails { // val error = fails {
// assert(false) // assert(false)
// } // }
@@ -120,11 +118,11 @@ class PreconditionsTest() : TestCase() {
// } // }
// } // }
// //
// fun testPassingAssertWithMessage() { // test fun passingAssertWithMessage() {
// assert(true, "Hello") // assert(true, "Hello")
// } // }
// //
// fun testFailingAssertWithMessage() { // test fun failingAssertWithMessage() {
// val error = fails { // val error = fails {
// assert(false, "Hello") // assert(false, "Hello")
// } // }
@@ -135,7 +133,7 @@ class PreconditionsTest() : TestCase() {
// } // }
// } // }
// //
// fun testFailingAssertWithLazyMessage() { // test fun failingAssertWithLazyMessage() {
// val error = fails { // val error = fails {
// assert(false) {"Hello"} // assert(false) {"Hello"}
// } // }