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.
This commit is contained in:
Ilya Gorbunov
2017-04-18 23:40:52 +03:00
parent 2c32bae5ca
commit e0a4f9ba76
2 changed files with 35 additions and 64 deletions
@@ -6,28 +6,25 @@ import kotlin.test.*
class Preconditions { class Preconditions {
@Sample @Sample
@Suppress("UNUSED_VARIABLE")
fun failWithError() { fun failWithError() {
val name: String? = null val name: String? = null
val exception = assertFailsWith<IllegalStateException> { assertFailsWith<IllegalStateException> { val nonNullName = name ?: error("Name is missing") }
val nonNullName = name ?: error("Name is missing")
}
assertPrints(exception.message, "Name is missing")
} }
@Sample @Sample
fun failRequireWithLazyMessage() { fun failRequireWithLazyMessage() {
fun getIndices(count: Int) { fun getIndices(count: Int): List<Int> {
require(count >= 0) { "Count must be non-negative, was $count" } require(count >= 0) { "Count must be non-negative, was $count" }
// ... // ...
return List(count) { it + 1 }
} }
val exception = assertFailsWith<IllegalArgumentException> { assertFailsWith<IllegalArgumentException> { getIndices(-1) }
getIndices(-1)
} assertPrints(getIndices(3), "[1, 2, 3]")
assertPrints(exception.message, "Count must be non-negative, was -1")
} }
@Sample @Sample
@@ -41,15 +38,12 @@ class Preconditions {
return state return state
} }
assertFailsWith<IllegalStateException> { assertFailsWith<IllegalStateException> { getStateValue() }
getStateValue()
}
assertFailsWith<IllegalStateException> { someState = ""
someState = "" assertFailsWith<IllegalStateException> { getStateValue() }
getStateValue()
}.let { exception -> someState = "non-empty-state"
assertPrints(exception.message, "State must be non-empty") assertPrints(getStateValue(), "non-empty-state")
}
} }
} }
@@ -1,10 +1,9 @@
@file:kotlin.jvm.JvmVersion
package test.utils package test.utils
import org.junit.Test import org.junit.Test
import kotlin.test.* import kotlin.test.*
class PreconditionsJVMTest() { class PreconditionsTest() {
@Test fun passingRequire() { @Test fun passingRequire() {
require(true) require(true)
@@ -15,14 +14,14 @@ class PreconditionsJVMTest() {
} }
@Test fun failingRequire() { @Test fun failingRequire() {
val error = assertFailsWith(IllegalArgumentException::class) { val error = assertFailsWith<IllegalArgumentException> {
require(false) require(false)
} }
assertNotNull(error.message) assertNotNull(error.message)
} }
@Test fun failingRequireWithLazyMessage() { @Test fun failingRequireWithLazyMessage() {
val error = assertFailsWith(IllegalArgumentException::class) { val error = assertFailsWith<IllegalArgumentException> {
require(false) { "Hello" } require(false) { "Hello" }
} }
assertEquals("Hello", error.message) assertEquals("Hello", error.message)
@@ -37,14 +36,14 @@ class PreconditionsJVMTest() {
} }
@Test fun failingCheck() { @Test fun failingCheck() {
val error = assertFailsWith(IllegalStateException::class) { val error = assertFailsWith<IllegalStateException> {
check(false) check(false)
} }
assertNotNull(error.message) assertNotNull(error.message)
} }
@Test fun failingCheckWithLazyMessage() { @Test fun failingCheckWithLazyMessage() {
val error = assertFailsWith(IllegalStateException::class) { val error = assertFailsWith<IllegalStateException> {
check(false) { "Hello" } check(false) { "Hello" }
} }
assertEquals("Hello", error.message) assertEquals("Hello", error.message)
@@ -57,14 +56,14 @@ class PreconditionsJVMTest() {
} }
@Test fun requireNotNullFails() { @Test fun requireNotNullFails() {
assertFailsWith(IllegalArgumentException::class) { assertFailsWith<IllegalArgumentException> {
val s2: String? = null val s2: String? = null
requireNotNull(s2) requireNotNull(s2)
} }
} }
@Test fun requireNotNullWithLazyMessage() { @Test fun requireNotNullWithLazyMessage() {
val error = assertFailsWith(IllegalArgumentException::class) { val error = assertFailsWith<IllegalArgumentException> {
val obj: Any? = null val obj: Any? = null
requireNotNull(obj) { "Message" } requireNotNull(obj) { "Message" }
} }
@@ -85,12 +84,13 @@ class PreconditionsJVMTest() {
} }
@Test fun checkNotNullFails() { @Test fun checkNotNullFails() {
assertFailsWith(IllegalStateException::class) { assertFailsWith<IllegalStateException> {
val s2: String? = null val s2: String? = null
checkNotNull(s2) checkNotNull(s2)
} }
} }
@kotlin.jvm.JvmVersion
@Test fun passingAssert() { @Test fun passingAssert() {
assert(true) assert(true)
var called = false var called = false
@@ -100,51 +100,28 @@ class PreconditionsJVMTest() {
} }
@kotlin.jvm.JvmVersion
@Test fun failingAssert() { @Test fun failingAssert() {
val error = assertFails { val error = assertFailsWith<AssertionError> {
assert(false) assert(false)
} }
if (error is AssertionError) { assertEquals("Assertion failed", error.message)
assertEquals("Assertion failed", error.message) }
} else {
fail("Invalid exception type: " + error)
@kotlin.jvm.JvmVersion
@Test fun failingAssertWithMessage() {
val error = assertFailsWith<AssertionError> {
assert(false) { "Hello" }
} }
assertEquals("Hello", error.message)
} }
@Test fun error() { @Test fun error() {
val error = assertFails { val error = assertFailsWith<IllegalStateException> {
error("There was a problem") error("There was a problem")
} }
if (error is IllegalStateException) { assertEquals("There was a problem", error.message)
assertEquals("There was a problem", error.message)
} else {
fail("Invalid exception type: " + error)
}
} }
@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)
}
}
} }