2017-10-21 23:59:16 +00:00
|
|
|
package com.github.michaelbull.result
|
2017-10-21 15:52:29 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
import kotlin.test.Test
|
|
|
|
import kotlin.test.assertEquals
|
|
|
|
import kotlin.test.assertFailsWith
|
2017-10-21 15:52:29 +00:00
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class UnwrapTest {
|
|
|
|
class Unwrap {
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsValueIfOk() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertEquals(
|
|
|
|
expected = 5000,
|
|
|
|
actual = Ok(5000).unwrap()
|
|
|
|
)
|
2017-11-28 18:23:36 +00:00
|
|
|
}
|
2017-10-21 18:17:01 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun throwsExceptionIfErr() {
|
2020-01-11 10:14:59 +00:00
|
|
|
assertFailsWith<UnwrapException>("called Result.unwrap on an Err value 5000") {
|
2019-08-09 20:53:52 +00:00
|
|
|
@Suppress("IMPLICIT_NOTHING_AS_TYPE_PARAMETER")
|
2017-12-16 19:30:54 +00:00
|
|
|
Err(5000).unwrap()
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class Expect {
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsValueIfOk() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertEquals(
|
|
|
|
expected = 1994,
|
|
|
|
actual = Ok(1994).expect { "the year should be" }
|
|
|
|
)
|
2017-11-28 18:23:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun throwsExceptionIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
val message = object {
|
|
|
|
override fun toString() = "the year should be"
|
|
|
|
}
|
2017-10-21 18:17:01 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
assertFailsWith<UnwrapException>("the year should be 1994") {
|
2019-08-09 20:53:52 +00:00
|
|
|
@Suppress("IMPLICIT_NOTHING_AS_TYPE_PARAMETER")
|
2017-12-16 19:30:54 +00:00
|
|
|
Err(1994).expect { message }
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class UnwrapError {
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun throwsExceptionIfOk() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertFailsWith<UnwrapException>("called Result.unwrapError on an Ok value example") {
|
2019-08-09 20:53:52 +00:00
|
|
|
@Suppress("IMPLICIT_NOTHING_AS_TYPE_PARAMETER")
|
2017-12-16 19:30:54 +00:00
|
|
|
Ok("example").unwrapError()
|
|
|
|
}
|
2017-11-28 18:23:36 +00:00
|
|
|
}
|
2017-10-21 18:17:01 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsErrorIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertEquals(
|
|
|
|
expected = "example",
|
|
|
|
actual = Err("example").unwrapError()
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class ExpectError {
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun throwsExceptionIfOk() {
|
2017-12-16 19:30:54 +00:00
|
|
|
val message = object {
|
|
|
|
override fun toString() = "the year should be"
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
assertFailsWith<UnwrapException>("the year should be 2010") {
|
2019-08-09 20:53:52 +00:00
|
|
|
@Suppress("IMPLICIT_NOTHING_AS_TYPE_PARAMETER")
|
2017-12-16 19:30:54 +00:00
|
|
|
Ok(2010).expectError { message }
|
|
|
|
}
|
2017-11-28 18:23:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsErrorIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertEquals(
|
|
|
|
expected = 2010,
|
|
|
|
actual = Err(2010).expectError { "the year should be" }
|
|
|
|
)
|
2017-11-28 18:23:36 +00:00
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
}
|