Make lazy version of assert accept function with Any return value, #KT-6459 Fixed

This commit is contained in:
Ilya Ryzhenkov
2014-12-15 13:13:56 +03:00
parent 47da114599
commit 053586f481
3 changed files with 36 additions and 20 deletions
+3 -3
View File
@@ -7,7 +7,7 @@ deprecated("Must be public to make assert() inlinable")
public val ASSERTIONS_ENABLED: Boolean = _Assertions.javaClass.desiredAssertionStatus()
/**
* Throws an [[AssertionError]] with an optional *message* if the *value* is false
* Throws an [AssertionError] with an optional *message* if the *value* is false
* and runtime assertions have been enabled on the JVM using the *-ea* JVM option.
*/
public fun assert(value: Boolean, message: Any = "Assertion failed") {
@@ -19,10 +19,10 @@ public fun assert(value: Boolean, message: Any = "Assertion failed") {
}
/**
* Throws an [[AssertionError]] with the specified *lazyMessage* if the *value* is false
* Throws an [AssertionError] calculated by *lazyMessage* if the *value* is false
* and runtime assertions have been enabled on the JVM using the *-ea* JVM option.
*/
public inline fun assert(value: Boolean, lazyMessage: () -> String) {
public inline fun assert(value: Boolean, lazyMessage: () -> Any) {
if (ASSERTIONS_ENABLED) {
if (!value) {
val message = lazyMessage()
+22 -17
View File
@@ -5,9 +5,9 @@ import java.lang.IllegalArgumentException
import java.lang.IllegalStateException
/**
* Throws an [[IllegalArgumentException]] with an optional *message* if the *value* is false.
* Throws an [IllegalArgumentException] with an optional *message* if the *value* is false.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt failingRequireWithMessage
* ${code test.collections.PreconditionsTest.failingRequireWithMessage}
*/
public fun require(value: Boolean, message: Any = "Failed requirement"): Unit {
if (!value) {
@@ -16,11 +16,11 @@ public fun require(value: Boolean, message: Any = "Failed requirement"): Unit {
}
/**
* Throws an [[IllegalArgumentException]] with the *lazyMessage* if the *value* is false.
* Throws an [IllegalArgumentException] with the *lazyMessage* if the *value* is false.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt failingRequireWithLazyMessage
* ${code test.collections.PreconditionsTest.failingRequireWithLazyMessage}
*/
public inline fun require(value: Boolean, lazyMessage: () -> String): Unit {
public inline fun require(value: Boolean, lazyMessage: () -> Any): Unit {
if (!value) {
val message = lazyMessage()
throw IllegalArgumentException(message.toString())
@@ -28,10 +28,10 @@ public inline fun require(value: Boolean, lazyMessage: () -> String): Unit {
}
/**
* Throws an [[IllegalArgumentException]] with the given *message* if the *value* is null otherwise
* Throws an [IllegalArgumentException] with the given *message* if the *value* is null otherwise
* the not null value is returned.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt requireNotNull
* ${code test.collections.PreconditionsTest.requireNotNull}
*/
public fun <T:Any> requireNotNull(value: T?, message: Any = "Required value was null"): T {
if (value == null) {
@@ -42,9 +42,9 @@ public fun <T:Any> requireNotNull(value: T?, message: Any = "Required value was
}
/**
* Throws an [[IllegalStateException]] with an optional *message* if the *value* is false.
* Throws an [IllegalStateException] with an optional *message* if the *value* is false.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt failingCheckWithMessage
* ${code test.collections.PreconditionsTest.failingCheckWithMessage}
*/
public fun check(value: Boolean, message: Any = "Check failed"): Unit {
if (!value) {
@@ -53,11 +53,11 @@ public fun check(value: Boolean, message: Any = "Check failed"): Unit {
}
/**
* Throws an [[IllegalStateException]] with the *lazyMessage* if the *value* is false.
* Throws an [IllegalStateException] with the *lazyMessage* if the *value* is false.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt failingCheckWithLazyMessage
* ${code test.collections.PreconditionsTest.failingCheckWithLazyMessage}
*/
public inline fun check(value: Boolean, lazyMessage: () -> String): Unit {
public inline fun check(value: Boolean, lazyMessage: () -> Any): Unit {
if (!value) {
val message = lazyMessage()
throw IllegalStateException(message.toString())
@@ -65,17 +65,22 @@ public inline fun check(value: Boolean, lazyMessage: () -> String): Unit {
}
/**
* Throws an [[IllegalStateException]] with the given *message* if the *value* is null otherwise
* Throws an [IllegalStateException] with the given *message* if the *value* is null otherwise
* the not null value is returned.
*
* @includeFunctionBody ../../test/PreconditionsTest.kt checkNotNull
* ${code test.collections.PreconditionsTest.checkNotNull}
*/
public fun <T:Any> checkNotNull(value: T?, message: String = "Required value was null"): T {
public fun <T:Any> checkNotNull(value: T?, message: Any = "Required value was null"): T {
if (value == null) {
throw IllegalStateException(message)
throw IllegalStateException(message.toString())
} else {
return value
}
}
public fun error(message: String): Nothing = throw RuntimeException(message)
/**
* Throws an [IllegalStateException] with the given *message*
*
* ${code test.collections.PreconditionsTest.error}
*/
public fun error(message: Any): Nothing = throw IllegalStateException(message.toString())
@@ -117,6 +117,17 @@ class PreconditionsTest() {
}
}
test fun error() {
val error = fails {
error("There was a problem")
}
if (error is IllegalStateException) {
assertEquals("There was a problem", error.getMessage())
} else {
fail("Invalid exception type: " + error)
}
}
test fun passingAssertWithMessage() {
assert(true, "Hello")
}