Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added lazy message versions of require/assert/check methods, #KT-693 …

…fixed
  • Loading branch information...
commit 5c7ee4d8702f5bc14952ae90de20708956c848f9 1 parent 7aef088
@jstrachan jstrachan authored
View
47 libraries/stdlib/src/Preconditions.kt
@@ -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);
@@ -30,9 +30,22 @@ 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,8 +82,20 @@ 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());
}
}
View
47 libraries/stdlib/test/PreconditionsTest.kt
@@ -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)
+// }
+// }
}
Please sign in to comment.
Something went wrong with that request. Please try again.