Skip to content

Commit

Permalink
added lazy message versions of require/assert/check methods, #KT-693 …
Browse files Browse the repository at this point in the history
…fixed
  • Loading branch information
jstrachan committed Mar 27, 2012
1 parent 7aef088 commit 5c7ee4d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
47 changes: 41 additions & 6 deletions libraries/stdlib/src/Preconditions.kt
Expand Up @@ -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();
Expand All @@ -21,18 +21,31 @@ 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);
}
}
}

/**
* 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();
}
Expand All @@ -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();
}
Expand All @@ -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());
}
}
Expand Down
47 changes: 47 additions & 0 deletions libraries/stdlib/test/PreconditionsTest.kt
Expand Up @@ -8,6 +8,11 @@ class PreconditionsTest() : TestCase() {

fun testPassingRequire() {
require(true)

var called = false
require(true) { called = true; "some message" }

assertFalse(called)
}

fun testFailingRequire() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
// }
//
//
Expand Down Expand Up @@ -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)
// }
// }
}

0 comments on commit 5c7ee4d

Please sign in to comment.