Adding check method to stdlib as detailed in KT-1530
This commit is contained in:
@@ -21,7 +21,7 @@ inline fun assert(value:Boolean):Unit {
|
|||||||
* Throws an [[AssertionError]] with specified `message` if the `value` is false
|
* 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.
|
* and runtime assertions have been enabled on the JVM using the `-ea` JVM option.
|
||||||
*/
|
*/
|
||||||
inline fun assert<T>(value:Boolean, message:T) {
|
inline fun assert(value:Boolean, message:Any) {
|
||||||
if(Assertions._ENABLED) {
|
if(Assertions._ENABLED) {
|
||||||
if(!value) {
|
if(!value) {
|
||||||
throw AssertionError(message);
|
throw AssertionError(message);
|
||||||
@@ -39,10 +39,30 @@ inline fun require(value:Boolean):Unit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throws an [[IllegalArgumentException]] with specified `message` if the `value` is false.
|
* Throws an [[IllegalArgumentException]] with specified `message` if the `value` is false.
|
||||||
*/
|
*/
|
||||||
inline fun require<T>(value:Boolean, message:T) {
|
inline fun require(value:Boolean, message:Any):Unit {
|
||||||
if(!value) {
|
if(!value) {
|
||||||
throw IllegalArgumentException(message.toString());
|
throw IllegalArgumentException(message.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws an [[IllegalStateException]] if the `value` is false.
|
||||||
|
*/
|
||||||
|
inline fun check(value:Boolean):Unit {
|
||||||
|
if(!value) {
|
||||||
|
throw IllegalStateException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws an [[IllegalStateException]] with specified `message` if the `value` is false.
|
||||||
|
*/
|
||||||
|
inline fun check(value:Boolean, message:Any):Unit {
|
||||||
|
if(!value) {
|
||||||
|
throw IllegalStateException(message.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -36,14 +36,66 @@ class PreconditionsTest() : TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Not sure why the assert method is not being resolved.
|
// TODO: uncomment when KT-1540 is resolved.
|
||||||
// fun ignorePassingAssert() {
|
// fun testPassingCheck() {
|
||||||
// assert(true)
|
// check1(true)
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// fun ignoreFailingAssert() {
|
// fun testFailingCheck() {
|
||||||
// failsWith<AssertionError> {
|
// val error = fails {
|
||||||
// assert(false)
|
// check1(false)
|
||||||
|
// }
|
||||||
|
// if(error is IllegalStateException) {
|
||||||
|
// assertNull(error.getMessage())
|
||||||
|
// } else {
|
||||||
|
// fail("Invalid exception type: "+error)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fun testPassingCheckWithMessage() {
|
||||||
|
// check2(true, "Hello")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fun testFailingCheckWithMessage() {
|
||||||
|
// val error = fails {
|
||||||
|
// check2(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() {
|
||||||
|
// assert1(true)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fun testFailingAssert() {
|
||||||
|
// val error = fails {
|
||||||
|
// assert1(false)
|
||||||
|
// }
|
||||||
|
// if(error is IllegalStateException) {
|
||||||
|
// assertNull(error.getMessage())
|
||||||
|
// } else {
|
||||||
|
// fail("Invalid exception type: "+error)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fun testPassingAssertWithMessage() {
|
||||||
|
// assert2(true, "Hello")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fun testFailingAssertWithMessage() {
|
||||||
|
// val error = fails {
|
||||||
|
// assert2(false, "Hello")
|
||||||
|
// }
|
||||||
|
// if(error is IllegalStateException) {
|
||||||
|
// assertEquals("Hello", error.getMessage())
|
||||||
|
// } else {
|
||||||
|
// fail("Invalid exception type: "+error)
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user