diff --git a/libraries/stdlib/src/kotlin/Preconditions.kt b/libraries/stdlib/src/kotlin/Preconditions.kt index 832244e16ad..e618dfb9647 100644 --- a/libraries/stdlib/src/kotlin/Preconditions.kt +++ b/libraries/stdlib/src/kotlin/Preconditions.kt @@ -6,98 +6,103 @@ object Assertions { } /** - * Throws an [[AssertionError]] if the `value` is false and runtime assertions have been - * enabled on the JVM using the `-ea` JVM option. +* 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. +*/ +public inline fun assert(value: Boolean, message: Any = "Assertion failed") { + 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. */ -public inline fun assert(value: Boolean): Unit { - if(Assertions._ENABLED) { - if(!value) { - throw AssertionError(); - } - } -} - -/** -* 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. -*/ -public 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. -*/ public inline fun assert(value: Boolean, lazyMessage: () -> String) { - if(Assertions._ENABLED) { - if(!value) { + if (Assertions._ENABLED) { + if (!value) { val message = lazyMessage() - throw AssertionError(message); + throw AssertionError(message) } } } /** - * Throws an [[IllegalArgumentException]] if the `value` is false. + * Throws an [[IllegalArgumentException]] with specified *message* if the *value* is false. + * + * @includeFunctionBody ../../test/PreconditionsTest.kt failingRequireWithMessage */ -public inline fun require(value: Boolean): Unit { - if(!value) { - throw IllegalArgumentException(); +public inline fun require(value: Boolean, message: Any = "Failed requirement"): Unit { + if (!value) { + throw IllegalArgumentException(message.toString()) } } /** - * Throws an [[IllegalArgumentException]] with specified `message` if the `value` is false. - */ -public 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. + * Throws an [[IllegalArgumentException]] with specified *message* if the *value* is false. + * + * @includeFunctionBody ../../test/PreconditionsTest.kt failingRequireWithLazyMessage */ public inline fun require(value: Boolean, lazyMessage: () -> String): Unit { - if(!value) { + if (!value) { val message = lazyMessage() - throw IllegalArgumentException(message.toString()); + throw IllegalArgumentException(message.toString()) } } /** - * Throws an [[IllegalStateException]] if the `value` is false. + * Throws an [[IllegalArgumentException]] with the given *message* if the *value* is null otherwise + * the not null value is returned. + * + * @includeFunctionBody ../../test/PreconditionsTest.kt requireNotNull */ -public inline fun check(value: Boolean): Unit { - if(!value) { - throw IllegalStateException(); +public inline fun requireNotNull(value: T?, message: Any = "Required value was null"): T { + if (value == null) { + throw IllegalArgumentException(message.toString()) + } else { + return value } } /** - * Throws an [[IllegalStateException]] with specified `message` if the `value` is false. + * Throws an [[IllegalStateException]] with specified *message* if the *value* is false. + * + * @includeFunctionBody ../../test/PreconditionsTest.kt failingCheckWithMessage */ -public inline fun check(value: Boolean, message: Any): Unit { - if(!value) { - throw IllegalStateException(message.toString()); +public inline fun check(value: Boolean, message: Any = "Check failed"): Unit { + if (!value) { + throw IllegalStateException(message.toString()) } } - - /** - * Throws an [[IllegalStateException]] with specified `message` if the `value` is false. + * Throws an [[IllegalStateException]] with specified *message* if the *value* is false. + * + * @includeFunctionBody ../../test/PreconditionsTest.kt failingCheckWithMessage */ public inline fun check(value: Boolean, lazyMessage: () -> String): Unit { - if(!value) { + if (!value) { val message = lazyMessage() - throw IllegalStateException(message.toString()); + throw IllegalStateException(message.toString()) + } +} + +/** + * Throws an [[IllegalStateException]] with the given *message* if the *value* is null otherwise + * the not null value is returned. + * + * @includeFunctionBody ../../test/PreconditionsTest.kt checkNotNull + */ +public inline fun checkNotNull(value: T?, message: String = "Required value was null"): T { + if (value == null) { + throw IllegalStateException(message) + } else { + return value } } + diff --git a/libraries/stdlib/src/kotlin/test/Test.kt b/libraries/stdlib/src/kotlin/test/Test.kt index fa544a26837..245f31104be 100644 --- a/libraries/stdlib/src/kotlin/test/Test.kt +++ b/libraries/stdlib/src/kotlin/test/Test.kt @@ -63,8 +63,9 @@ public inline fun assertEquals(expected: Any?, actual: Any?, message: String = " } /** Asserts that the expression is not null, with an optional message */ -public inline fun assertNotNull(actual: Any?, message: String = "") { +public inline fun assertNotNull(actual: T?, message: String = ""): T { asserter.assertNotNull(message, actual) + return actual!! } /** Asserts that the expression is not null, with an optional message and a function block to process the not-null value */ diff --git a/libraries/stdlib/test/ExceptionTest.kt b/libraries/stdlib/test/ExceptionTest.kt index 15dea08327a..07b7b2744a8 100644 --- a/libraries/stdlib/test/ExceptionTest.kt +++ b/libraries/stdlib/test/ExceptionTest.kt @@ -40,8 +40,7 @@ class ExceptionTest { t.printStackTrace(stream) } - assertNotNull(byteBuffer.toByteArray()) { bytes -> - assertTrue(bytes.size > 10) - } + val bytes = assertNotNull(byteBuffer.toByteArray()) + assertTrue(bytes.size > 10) } } diff --git a/libraries/stdlib/test/PreconditionsTest.kt b/libraries/stdlib/test/PreconditionsTest.kt index 3274f3d19e8..082b0bd1635 100644 --- a/libraries/stdlib/test/PreconditionsTest.kt +++ b/libraries/stdlib/test/PreconditionsTest.kt @@ -1,106 +1,104 @@ package test.collections -import junit.framework.TestCase +import org.junit.Test as test import kotlin.test.* -import java.lang.IllegalArgumentException -class PreconditionsTest() : TestCase() { +class PreconditionsTest() { - fun testPassingRequire() { + test fun passingRequire() { require(true) var called = false require(true) { called = true; "some message" } - assertFalse(called) } - fun testFailingRequire() { - val error = fails { + test fun failingRequire() { + val error = failsWith { require(false) } - if(error is IllegalArgumentException) { - assertNull(error.getMessage()) - } else { - fail("Invalid exception type: "+error) - } + assertNotNull(error.getMessage()) } - fun testPassingRequireWithMessage() { + test fun passingRequireWithMessage() { require(true, "Hello") } - fun testFailingRequireWithMessage() { - val error = fails { + test fun failingRequireWithMessage() { + val error = failsWith { require(false, "Hello") } - if(error is IllegalArgumentException) { - assertEquals("Hello", error.getMessage()) - } else { - fail("Invalid exception type: "+error) - } + assertEquals("Hello", error.getMessage()) } - fun testFailingRequireWithLazyMessage() { - val error = fails { + test fun failingRequireWithLazyMessage() { + val error = failsWith { require(false) {"Hello"} } - if(error is IllegalArgumentException) { - assertEquals("Hello", error.getMessage()) - } else { - fail("Invalid exception type: "+error) - } + assertEquals("Hello", error.getMessage()) } - fun testPassingCheck() { + test fun passingCheck() { check(true) var called = false check(true) { called = true; "some message" } - assertFalse(called) } - fun testFailingCheck() { - val error = fails { + test fun failingCheck() { + val error = failsWith { check(false) } - if(error is IllegalStateException) { - assertNull(error.getMessage()) - } else { - fail("Invalid exception type: "+error) - } + assertNotNull(error.getMessage()) } - fun testPassingCheckWithMessage() { + test fun passingCheckWithMessage() { check(true, "Hello") } - fun testFailingCheckWithMessage() { - val error = fails { + test fun failingCheckWithMessage() { + val error = failsWith { check(false, "Hello") } - if(error is IllegalStateException) { - assertEquals("Hello", error.getMessage()) - } else { - fail("Invalid exception type: "+error) - } + assertEquals("Hello", error.getMessage()) } - fun testFailingCheckWithLazyMessage() { - val error = fails { + test fun failingCheckWithLazyMessage() { + val error = failsWith { check(false) {"Hello"} } - if(error is IllegalStateException) { - assertEquals("Hello", error.getMessage()) - } else { - fail("Invalid exception type: "+error) + assertEquals("Hello", error.getMessage()) + } + + test fun requireNotNull() { + val s1: String? = "S1" + val r1: String = requireNotNull(s1) + assertEquals("S1", r1) + } + + test fun requireNotNullFails() { + failsWith { + val s2: String? = null + requireNotNull(s2) } } + test fun checkNotNull() { + val s1: String? = "S1" + val r1: String = checkNotNull(s1) + assertEquals("S1", r1) + } + + test fun checkNotNullFails() { + failsWith { + val s2: String? = null + checkNotNull(s2) + } + } // TODO: uncomment when KT-1540 is resolved. -// fun testPassingAssert() { +// test fun passingAssert() { // assert(true) // var called = false // assert(true) { called = true; "some message" } @@ -109,7 +107,7 @@ class PreconditionsTest() : TestCase() { // } // // -// fun testFailingAssert() { +// test fun failingAssert() { // val error = fails { // assert(false) // } @@ -120,11 +118,11 @@ class PreconditionsTest() : TestCase() { // } // } // -// fun testPassingAssertWithMessage() { +// test fun passingAssertWithMessage() { // assert(true, "Hello") // } // -// fun testFailingAssertWithMessage() { +// test fun failingAssertWithMessage() { // val error = fails { // assert(false, "Hello") // } @@ -135,7 +133,7 @@ class PreconditionsTest() : TestCase() { // } // } // -// fun testFailingAssertWithLazyMessage() { +// test fun failingAssertWithLazyMessage() { // val error = fails { // assert(false) {"Hello"} // }