diff --git a/libraries/stdlib/src/kotlin/AssertionsJVM.kt b/libraries/stdlib/src/kotlin/AssertionsJVM.kt index f497d32cf17..c974cbf8654 100644 --- a/libraries/stdlib/src/kotlin/AssertionsJVM.kt +++ b/libraries/stdlib/src/kotlin/AssertionsJVM.kt @@ -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() diff --git a/libraries/stdlib/src/kotlin/Preconditions.kt b/libraries/stdlib/src/kotlin/Preconditions.kt index c08bf38b8c2..8646dcf21c9 100644 --- a/libraries/stdlib/src/kotlin/Preconditions.kt +++ b/libraries/stdlib/src/kotlin/Preconditions.kt @@ -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 requireNotNull(value: T?, message: Any = "Required value was null"): T { if (value == null) { @@ -42,9 +42,9 @@ public fun 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 checkNotNull(value: T?, message: String = "Required value was null"): T { +public fun 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()) diff --git a/libraries/stdlib/test/PreconditionsTest.kt b/libraries/stdlib/test/PreconditionsTest.kt index c88448050ff..9bc75893d81 100644 --- a/libraries/stdlib/test/PreconditionsTest.kt +++ b/libraries/stdlib/test/PreconditionsTest.kt @@ -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") }