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:
@@ -6,98 +6,103 @@ object Assertions {
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an [[AssertionError]] if the `value` is false and runtime assertions have been
|
||||
* enabled on the JVM using the `-ea` JVM option.
|
||||
* 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 = "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) {
|
||||
if(Assertions._ENABLED) {
|
||||
if(!value) {
|
||||
if (Assertions._ENABLED) {
|
||||
if (!value) {
|
||||
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 {
|
||||
if(!value) {
|
||||
throw IllegalArgumentException();
|
||||
public inline fun require(value: Boolean, message: Any = "Failed requirement"): Unit {
|
||||
if (!value) {
|
||||
throw IllegalArgumentException(message.toString())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an [[IllegalArgumentException]] with specified `message` if the `value` is false.
|
||||
*/
|
||||
public inline fun require(value: Boolean, message: Any): Unit {
|
||||
if(!value) {
|
||||
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.
|
||||
*
|
||||
* @includeFunctionBody ../../test/PreconditionsTest.kt failingRequireWithLazyMessage
|
||||
*/
|
||||
public inline fun require(value: Boolean, lazyMessage: () -> String): Unit {
|
||||
if(!value) {
|
||||
if (!value) {
|
||||
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 {
|
||||
if(!value) {
|
||||
throw IllegalStateException();
|
||||
public inline fun <T> requireNotNull(value: T?, message: Any = "Required value was null"): T {
|
||||
if (value == null) {
|
||||
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 {
|
||||
if(!value) {
|
||||
throw IllegalStateException(message.toString());
|
||||
public inline fun check(value: Boolean, message: Any = "Check failed"): Unit {
|
||||
if (!value) {
|
||||
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 {
|
||||
if(!value) {
|
||||
if (!value) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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 */
|
||||
public inline fun assertNotNull(actual: Any?, message: String = "") {
|
||||
public inline fun <T> assertNotNull(actual: T?, message: String = ""): T {
|
||||
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 */
|
||||
|
||||
@@ -40,8 +40,7 @@ class ExceptionTest {
|
||||
t.printStackTrace(stream)
|
||||
}
|
||||
|
||||
assertNotNull(byteBuffer.toByteArray()) { bytes ->
|
||||
assertTrue(bytes.size > 10)
|
||||
}
|
||||
val bytes = assertNotNull(byteBuffer.toByteArray())
|
||||
assertTrue(bytes.size > 10)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,106 +1,104 @@
|
||||
package test.collections
|
||||
|
||||
import junit.framework.TestCase
|
||||
import org.junit.Test as test
|
||||
import kotlin.test.*
|
||||
import java.lang.IllegalArgumentException
|
||||
|
||||
class PreconditionsTest() : TestCase() {
|
||||
class PreconditionsTest() {
|
||||
|
||||
fun testPassingRequire() {
|
||||
test fun passingRequire() {
|
||||
require(true)
|
||||
|
||||
var called = false
|
||||
require(true) { called = true; "some message" }
|
||||
|
||||
assertFalse(called)
|
||||
}
|
||||
|
||||
fun testFailingRequire() {
|
||||
val error = fails {
|
||||
test fun failingRequire() {
|
||||
val error = failsWith<IllegalArgumentException> {
|
||||
require(false)
|
||||
}
|
||||
if(error is IllegalArgumentException) {
|
||||
assertNull(error.getMessage())
|
||||
} else {
|
||||
fail("Invalid exception type: "+error)
|
||||
}
|
||||
assertNotNull(error.getMessage())
|
||||
}
|
||||
|
||||
fun testPassingRequireWithMessage() {
|
||||
test fun passingRequireWithMessage() {
|
||||
require(true, "Hello")
|
||||
}
|
||||
|
||||
fun testFailingRequireWithMessage() {
|
||||
val error = fails {
|
||||
test fun failingRequireWithMessage() {
|
||||
val error = failsWith<IllegalArgumentException> {
|
||||
require(false, "Hello")
|
||||
}
|
||||
if(error is IllegalArgumentException) {
|
||||
assertEquals("Hello", error.getMessage())
|
||||
} else {
|
||||
fail("Invalid exception type: "+error)
|
||||
}
|
||||
assertEquals("Hello", error.getMessage())
|
||||
}
|
||||
|
||||
fun testFailingRequireWithLazyMessage() {
|
||||
val error = fails {
|
||||
test fun failingRequireWithLazyMessage() {
|
||||
val error = failsWith<IllegalArgumentException> {
|
||||
require(false) {"Hello"}
|
||||
}
|
||||
if(error is IllegalArgumentException) {
|
||||
assertEquals("Hello", error.getMessage())
|
||||
} else {
|
||||
fail("Invalid exception type: "+error)
|
||||
}
|
||||
assertEquals("Hello", error.getMessage())
|
||||
}
|
||||
|
||||
fun testPassingCheck() {
|
||||
test fun passingCheck() {
|
||||
check(true)
|
||||
|
||||
var called = false
|
||||
check(true) { called = true; "some message" }
|
||||
|
||||
assertFalse(called)
|
||||
}
|
||||
|
||||
fun testFailingCheck() {
|
||||
val error = fails {
|
||||
test fun failingCheck() {
|
||||
val error = failsWith<IllegalStateException> {
|
||||
check(false)
|
||||
}
|
||||
if(error is IllegalStateException) {
|
||||
assertNull(error.getMessage())
|
||||
} else {
|
||||
fail("Invalid exception type: "+error)
|
||||
}
|
||||
assertNotNull(error.getMessage())
|
||||
}
|
||||
|
||||
fun testPassingCheckWithMessage() {
|
||||
test fun passingCheckWithMessage() {
|
||||
check(true, "Hello")
|
||||
}
|
||||
|
||||
fun testFailingCheckWithMessage() {
|
||||
val error = fails {
|
||||
test fun failingCheckWithMessage() {
|
||||
val error = failsWith<IllegalStateException> {
|
||||
check(false, "Hello")
|
||||
}
|
||||
if(error is IllegalStateException) {
|
||||
assertEquals("Hello", error.getMessage())
|
||||
} else {
|
||||
fail("Invalid exception type: "+error)
|
||||
}
|
||||
assertEquals("Hello", error.getMessage())
|
||||
}
|
||||
|
||||
fun testFailingCheckWithLazyMessage() {
|
||||
val error = fails {
|
||||
test fun failingCheckWithLazyMessage() {
|
||||
val error = failsWith<IllegalStateException> {
|
||||
check(false) {"Hello"}
|
||||
}
|
||||
if(error is IllegalStateException) {
|
||||
assertEquals("Hello", error.getMessage())
|
||||
} else {
|
||||
fail("Invalid exception type: "+error)
|
||||
assertEquals("Hello", error.getMessage())
|
||||
}
|
||||
|
||||
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.
|
||||
// fun testPassingAssert() {
|
||||
// test fun passingAssert() {
|
||||
// assert(true)
|
||||
// var called = false
|
||||
// assert(true) { called = true; "some message" }
|
||||
@@ -109,7 +107,7 @@ class PreconditionsTest() : TestCase() {
|
||||
// }
|
||||
//
|
||||
//
|
||||
// fun testFailingAssert() {
|
||||
// test fun failingAssert() {
|
||||
// val error = fails {
|
||||
// assert(false)
|
||||
// }
|
||||
@@ -120,11 +118,11 @@ class PreconditionsTest() : TestCase() {
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fun testPassingAssertWithMessage() {
|
||||
// test fun passingAssertWithMessage() {
|
||||
// assert(true, "Hello")
|
||||
// }
|
||||
//
|
||||
// fun testFailingAssertWithMessage() {
|
||||
// test fun failingAssertWithMessage() {
|
||||
// val error = fails {
|
||||
// assert(false, "Hello")
|
||||
// }
|
||||
@@ -135,7 +133,7 @@ class PreconditionsTest() : TestCase() {
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fun testFailingAssertWithLazyMessage() {
|
||||
// test fun failingAssertWithLazyMessage() {
|
||||
// val error = fails {
|
||||
// assert(false) {"Hello"}
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user