From c9d6b8462d01ccab929e5e8d05972e9d85e561ad Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Wed, 7 Mar 2012 13:16:02 -0500 Subject: [PATCH] KT-1530 : stdlib should include assert and require functions to simplify precondition checking --- libraries/stdlib/src/Preconditions.kt | 48 ++++++++++++++++++++ libraries/testlib/test/PreconditionsTest.kt | 49 +++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 libraries/stdlib/src/Preconditions.kt create mode 100644 libraries/testlib/test/PreconditionsTest.kt diff --git a/libraries/stdlib/src/Preconditions.kt b/libraries/stdlib/src/Preconditions.kt new file mode 100644 index 00000000000..061a095332c --- /dev/null +++ b/libraries/stdlib/src/Preconditions.kt @@ -0,0 +1,48 @@ +package kotlin + +object Assertions { + // TODO make private once KT-1528 is fixed. + val _ENABLED = (javaClass()).desiredAssertionStatus() +} + +/** + * 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 { + 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. +*/ +inline fun assert(value:Boolean, message:T) { + if(Assertions._ENABLED) { + if(!value) { + throw AssertionError(message); + } + } +} + +/** + * Throws an [[IllegalArgumentException]] if the `value` is false. + */ +inline fun require(value:Boolean):Unit { + if(!value) { + throw IllegalArgumentException(); + } +} + +/** +* Throws an [[IllegalArgumentException]] with specified `message` if the `value` is false. +*/ +inline fun require(value:Boolean, message:T) { + if(!value) { + throw IllegalArgumentException(message.toString()); + } +} diff --git a/libraries/testlib/test/PreconditionsTest.kt b/libraries/testlib/test/PreconditionsTest.kt new file mode 100644 index 00000000000..278108c8cbd --- /dev/null +++ b/libraries/testlib/test/PreconditionsTest.kt @@ -0,0 +1,49 @@ +package test.collections + +import junit.framework.TestCase +import kotlin.test.* +import java.lang.IllegalArgumentException + +class PreconditionsTest() : TestCase() { + + fun testPassingRequire() { + require(true) + } + + fun testFailingRequire() { + val error = fails { + require(false) + } + if(error is IllegalArgumentException) { + assertNull(error.getMessage()) + } else { + fail("Invalid exception type: "+error) + } + } + + fun testPassingRequireWithMessage() { + require(true, "Hello") + } + + fun testFailingRequireWithMessage() { + val error = fails { + require(false, "Hello") + } + if(error is IllegalArgumentException) { + assertEquals("Hello", error.getMessage()) + } else { + fail("Invalid exception type: "+error) + } + } + +// // Not sure why the assert method is not being resolved. +// fun ignorePassingAssert() { +// assert(true) +// } +// +// fun ignoreFailingAssert() { +// failsWith { +// assert(false) +// } +// } +} \ No newline at end of file