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 {
@Sample
@Suppress("UNUSED_VARIABLE")
fun failWithError() {
val name: String? = null
val exception = assertFailsWith<IllegalStateException> {
val nonNullName = name ?: error("Name is missing")
}
assertPrints(exception.message, "Name is missing")
assertFailsWith<IllegalStateException> { val nonNullName = name ?: error("Name is missing") }
}
@Sample
fun failRequireWithLazyMessage() {
fun getIndices(count: Int) {
fun getIndices(count: Int): List<Int> {
require(count >= 0) { "Count must be non-negative, was $count" }
// ...
return List(count) { it + 1 }
}
val exception = assertFailsWith<IllegalArgumentException> {
getIndices(-1)
}
assertPrints(exception.message, "Count must be non-negative, was -1")
assertFailsWith<IllegalArgumentException> { getIndices(-1) }
assertPrints(getIndices(3), "[1, 2, 3]")
}
@Sample
@@ -41,15 +38,12 @@ class Preconditions {
return state
}
assertFailsWith<IllegalStateException> {
getStateValue()
}
assertFailsWith<IllegalStateException> { getStateValue() }
assertFailsWith<IllegalStateException> {
someState = ""
getStateValue()
}.let { exception ->
assertPrints(exception.message, "State must be non-empty")
}
someState = ""
assertFailsWith<IllegalStateException> { getStateValue() }
someState = "non-empty-state"
assertPrints(getStateValue(), "non-empty-state")
}
}
@@ -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<IllegalArgumentException> {
require(false)
}
assertNotNull(error.message)
}
@Test fun failingRequireWithLazyMessage() {
val error = assertFailsWith(IllegalArgumentException::class) {
val error = assertFailsWith<IllegalArgumentException> {
require(false) { "Hello" }
}
assertEquals("Hello", error.message)
@@ -37,14 +36,14 @@ class PreconditionsJVMTest() {
}
@Test fun failingCheck() {
val error = assertFailsWith(IllegalStateException::class) {
val error = assertFailsWith<IllegalStateException> {
check(false)
}
assertNotNull(error.message)
}
@Test fun failingCheckWithLazyMessage() {
val error = assertFailsWith(IllegalStateException::class) {
val error = assertFailsWith<IllegalStateException> {
check(false) { "Hello" }
}
assertEquals("Hello", error.message)
@@ -57,14 +56,14 @@ class PreconditionsJVMTest() {
}
@Test fun requireNotNullFails() {
assertFailsWith(IllegalArgumentException::class) {
assertFailsWith<IllegalArgumentException> {
val s2: String? = null
requireNotNull(s2)
}
}
@Test fun requireNotNullWithLazyMessage() {
val error = assertFailsWith(IllegalArgumentException::class) {
val error = assertFailsWith<IllegalArgumentException> {
val obj: Any? = null
requireNotNull(obj) { "Message" }
}
@@ -85,12 +84,13 @@ class PreconditionsJVMTest() {
}
@Test fun checkNotNullFails() {
assertFailsWith(IllegalStateException::class) {
assertFailsWith<IllegalStateException> {
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<AssertionError> {
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<AssertionError> {
assert(false) { "Hello" }
}
assertEquals("Hello", error.message)
}
@Test fun error() {
val error = assertFails {
val error = assertFailsWith<IllegalStateException> {
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)
}
}
}