added lazy message versions of require/assert/check methods, #KT-693 fixed
This commit is contained in:
@@ -9,7 +9,7 @@ object Assertions {
|
||||
* Throws an [[AssertionError]] if the `value` is false and runtime assertions have been
|
||||
* enabled on the JVM using the `-ea` JVM option.
|
||||
*/
|
||||
inline fun assert(value:Boolean):Unit {
|
||||
inline fun assert(value: Boolean): Unit {
|
||||
if(Assertions._ENABLED) {
|
||||
if(!value) {
|
||||
throw AssertionError();
|
||||
@@ -21,7 +21,7 @@ inline fun assert(value:Boolean):Unit {
|
||||
* 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.
|
||||
*/
|
||||
inline fun assert(value:Boolean, message:Any) {
|
||||
inline fun assert(value: Boolean, message: Any) {
|
||||
if(Assertions._ENABLED) {
|
||||
if(!value) {
|
||||
throw AssertionError(message);
|
||||
@@ -29,10 +29,23 @@ inline fun assert(value:Boolean, message:Any) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
inline fun assert(value: Boolean, lazyMessage: () -> String) {
|
||||
if(Assertions._ENABLED) {
|
||||
if(!value) {
|
||||
val message = lazyMessage()
|
||||
throw AssertionError(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an [[IllegalArgumentException]] if the `value` is false.
|
||||
*/
|
||||
inline fun require(value:Boolean):Unit {
|
||||
inline fun require(value: Boolean): Unit {
|
||||
if(!value) {
|
||||
throw IllegalArgumentException();
|
||||
}
|
||||
@@ -41,16 +54,26 @@ inline fun require(value:Boolean):Unit {
|
||||
/**
|
||||
* Throws an [[IllegalArgumentException]] with specified `message` if the `value` is false.
|
||||
*/
|
||||
inline fun require(value:Boolean, message:Any):Unit {
|
||||
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.
|
||||
*/
|
||||
inline fun require(value: Boolean, lazyMessage: () -> String): Unit {
|
||||
if(!value) {
|
||||
val message = lazyMessage()
|
||||
throw IllegalArgumentException(message.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an [[IllegalStateException]] if the `value` is false.
|
||||
*/
|
||||
inline fun check(value:Boolean):Unit {
|
||||
inline fun check(value: Boolean): Unit {
|
||||
if(!value) {
|
||||
throw IllegalStateException();
|
||||
}
|
||||
@@ -59,10 +82,22 @@ inline fun check(value:Boolean):Unit {
|
||||
/**
|
||||
* Throws an [[IllegalStateException]] with specified `message` if the `value` is false.
|
||||
*/
|
||||
inline fun check(value:Boolean, message:Any):Unit {
|
||||
inline fun check(value: Boolean, message: Any): Unit {
|
||||
if(!value) {
|
||||
throw IllegalStateException(message.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Throws an [[IllegalStateException]] with specified `message` if the `value` is false.
|
||||
*/
|
||||
inline fun check(value: Boolean, lazyMessage: () -> String): Unit {
|
||||
if(!value) {
|
||||
val message = lazyMessage()
|
||||
throw IllegalStateException(message.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -8,6 +8,11 @@ class PreconditionsTest() : TestCase() {
|
||||
|
||||
fun testPassingRequire() {
|
||||
require(true)
|
||||
|
||||
var called = false
|
||||
require(true) { called = true; "some message" }
|
||||
|
||||
assertFalse(called)
|
||||
}
|
||||
|
||||
fun testFailingRequire() {
|
||||
@@ -36,8 +41,24 @@ class PreconditionsTest() : TestCase() {
|
||||
}
|
||||
}
|
||||
|
||||
fun testFailingRequireWithLazyMessage() {
|
||||
val error = fails {
|
||||
require(false) {"Hello"}
|
||||
}
|
||||
if(error is IllegalArgumentException) {
|
||||
assertEquals("Hello", error.getMessage())
|
||||
} else {
|
||||
fail("Invalid exception type: "+error)
|
||||
}
|
||||
}
|
||||
|
||||
fun testPassingCheck() {
|
||||
check(true)
|
||||
|
||||
var called = false
|
||||
check(true) { called = true; "some message" }
|
||||
|
||||
assertFalse(called)
|
||||
}
|
||||
|
||||
fun testFailingCheck() {
|
||||
@@ -66,10 +87,25 @@ class PreconditionsTest() : TestCase() {
|
||||
}
|
||||
}
|
||||
|
||||
fun testFailingCheckWithLazyMessage() {
|
||||
val error = fails {
|
||||
check(false) {"Hello"}
|
||||
}
|
||||
if(error is IllegalStateException) {
|
||||
assertEquals("Hello", error.getMessage())
|
||||
} else {
|
||||
fail("Invalid exception type: "+error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO: uncomment when KT-1540 is resolved.
|
||||
// fun testPassingAssert() {
|
||||
// assert(true)
|
||||
// var called = false
|
||||
// assert(true) { called = true; "some message" }
|
||||
//
|
||||
// assertFalse(called)
|
||||
// }
|
||||
//
|
||||
//
|
||||
@@ -98,4 +134,15 @@ class PreconditionsTest() : TestCase() {
|
||||
// fail("Invalid exception type: "+error)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fun testFailingAssertWithLazyMessage() {
|
||||
// val error = fails {
|
||||
// assert(false) {"Hello"}
|
||||
// }
|
||||
// if(error is IllegalStateException) {
|
||||
// assertEquals("Hello", error.getMessage())
|
||||
// } else {
|
||||
// fail("Invalid exception type: "+error)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user