added lazy message versions of require/assert/check methods, #KT-693 fixed
This commit is contained in:
@@ -29,6 +29,19 @@ 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.
|
* Throws an [[IllegalArgumentException]] if the `value` is false.
|
||||||
*/
|
*/
|
||||||
@@ -47,6 +60,16 @@ inline fun require(value:Boolean, message:Any):Unit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Throws an [[IllegalStateException]] if the `value` is false.
|
||||||
*/
|
*/
|
||||||
@@ -66,3 +89,15 @@ inline fun check(value:Boolean, message:Any):Unit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
fun testPassingRequire() {
|
||||||
require(true)
|
require(true)
|
||||||
|
|
||||||
|
var called = false
|
||||||
|
require(true) { called = true; "some message" }
|
||||||
|
|
||||||
|
assertFalse(called)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFailingRequire() {
|
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() {
|
fun testPassingCheck() {
|
||||||
check(true)
|
check(true)
|
||||||
|
|
||||||
|
var called = false
|
||||||
|
check(true) { called = true; "some message" }
|
||||||
|
|
||||||
|
assertFalse(called)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFailingCheck() {
|
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.
|
// TODO: uncomment when KT-1540 is resolved.
|
||||||
// fun testPassingAssert() {
|
// fun testPassingAssert() {
|
||||||
// assert(true)
|
// 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)
|
// 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