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() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertFailsWith<UnwrapException>("called Result.wrap on an Err value 5000") {
|
|
|
|
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") {
|
|
|
|
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") {
|
|
|
|
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") {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|