From e0a4f9ba76401cb687d3654ee9d0d8b0707de43e Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 18 Apr 2017 23:40:52 +0300 Subject: [PATCH] Rewrite precondition samples and refactor precondition tests Precondition tests are shared for JS target. Samples do not use assertFailsWith as expression, as it's going to be converted as a special template. --- .../test/samples/misc/preconditions.kt | 32 ++++----- ...ditionsJVMTest.kt => PreconditionsTest.kt} | 67 ++++++------------- 2 files changed, 35 insertions(+), 64 deletions(-) rename libraries/stdlib/test/utils/{PreconditionsJVMTest.kt => PreconditionsTest.kt} (59%) diff --git a/libraries/stdlib/samples/test/samples/misc/preconditions.kt b/libraries/stdlib/samples/test/samples/misc/preconditions.kt index f38cc4eaeba..8bd1a057fb9 100644 --- a/libraries/stdlib/samples/test/samples/misc/preconditions.kt +++ b/libraries/stdlib/samples/test/samples/misc/preconditions.kt @@ -6,28 +6,25 @@ import kotlin.test.* class Preconditions { @Sample + @Suppress("UNUSED_VARIABLE") fun failWithError() { val name: String? = null - val exception = assertFailsWith { - val nonNullName = name ?: error("Name is missing") - } - - assertPrints(exception.message, "Name is missing") + assertFailsWith { val nonNullName = name ?: error("Name is missing") } } @Sample fun failRequireWithLazyMessage() { - fun getIndices(count: Int) { + fun getIndices(count: Int): List { require(count >= 0) { "Count must be non-negative, was $count" } // ... + return List(count) { it + 1 } } - val exception = assertFailsWith { - getIndices(-1) - } - assertPrints(exception.message, "Count must be non-negative, was -1") + assertFailsWith { getIndices(-1) } + + assertPrints(getIndices(3), "[1, 2, 3]") } @Sample @@ -41,15 +38,12 @@ class Preconditions { return state } - assertFailsWith { - getStateValue() - } + assertFailsWith { getStateValue() } - assertFailsWith { - someState = "" - getStateValue() - }.let { exception -> - assertPrints(exception.message, "State must be non-empty") - } + someState = "" + assertFailsWith { getStateValue() } + + someState = "non-empty-state" + assertPrints(getStateValue(), "non-empty-state") } } diff --git a/libraries/stdlib/test/utils/PreconditionsJVMTest.kt b/libraries/stdlib/test/utils/PreconditionsTest.kt similarity index 59% rename from libraries/stdlib/test/utils/PreconditionsJVMTest.kt rename to libraries/stdlib/test/utils/PreconditionsTest.kt index 9b17028e7e8..581f0815bda 100644 --- a/libraries/stdlib/test/utils/PreconditionsJVMTest.kt +++ b/libraries/stdlib/test/utils/PreconditionsTest.kt @@ -1,10 +1,9 @@ -@file:kotlin.jvm.JvmVersion package test.utils import org.junit.Test import kotlin.test.* -class PreconditionsJVMTest() { +class PreconditionsTest() { @Test fun passingRequire() { require(true) @@ -15,14 +14,14 @@ class PreconditionsJVMTest() { } @Test fun failingRequire() { - val error = assertFailsWith(IllegalArgumentException::class) { + val error = assertFailsWith { require(false) } assertNotNull(error.message) } @Test fun failingRequireWithLazyMessage() { - val error = assertFailsWith(IllegalArgumentException::class) { + val error = assertFailsWith { require(false) { "Hello" } } assertEquals("Hello", error.message) @@ -37,14 +36,14 @@ class PreconditionsJVMTest() { } @Test fun failingCheck() { - val error = assertFailsWith(IllegalStateException::class) { + val error = assertFailsWith { check(false) } assertNotNull(error.message) } @Test fun failingCheckWithLazyMessage() { - val error = assertFailsWith(IllegalStateException::class) { + val error = assertFailsWith { check(false) { "Hello" } } assertEquals("Hello", error.message) @@ -57,14 +56,14 @@ class PreconditionsJVMTest() { } @Test fun requireNotNullFails() { - assertFailsWith(IllegalArgumentException::class) { + assertFailsWith { val s2: String? = null requireNotNull(s2) } } @Test fun requireNotNullWithLazyMessage() { - val error = assertFailsWith(IllegalArgumentException::class) { + val error = assertFailsWith { val obj: Any? = null requireNotNull(obj) { "Message" } } @@ -85,12 +84,13 @@ class PreconditionsJVMTest() { } @Test fun checkNotNullFails() { - assertFailsWith(IllegalStateException::class) { + assertFailsWith { val s2: String? = null checkNotNull(s2) } } + @kotlin.jvm.JvmVersion @Test fun passingAssert() { assert(true) var called = false @@ -100,51 +100,28 @@ class PreconditionsJVMTest() { } + @kotlin.jvm.JvmVersion @Test fun failingAssert() { - val error = assertFails { + val error = assertFailsWith { assert(false) } - if (error is AssertionError) { - assertEquals("Assertion failed", error.message) - } else { - fail("Invalid exception type: " + error) + assertEquals("Assertion failed", error.message) + } + + + @kotlin.jvm.JvmVersion + @Test fun failingAssertWithMessage() { + val error = assertFailsWith { + assert(false) { "Hello" } } + assertEquals("Hello", error.message) } @Test fun error() { - val error = assertFails { + val error = assertFailsWith { error("There was a problem") } - if (error is IllegalStateException) { - assertEquals("There was a problem", error.message) - } else { - fail("Invalid exception type: " + error) - } + assertEquals("There was a problem", error.message) } - @Test fun passingAssertWithMessage() { - assert(true) { "Hello" } - } - - @Test fun failingAssertWithMessage() { - val error = assertFails { - assert(false) { "Hello" } - } - if (error is AssertionError) { - assertEquals("Hello", error.message) - } else { - fail("Invalid exception type: " + error) - } - } - - @Test fun failingAssertWithLazyMessage() { - val error = assertFails { - assert(false) { "Hello" } - } - if (error is AssertionError) { - assertEquals("Hello", error.message) - } else { - fail("Invalid exception type: " + error) - } - } } \ No newline at end of file