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
|
|
|
|
|
|
|
internal class UnwrapTest {
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `unwrap` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsValueIfOk() {
|
|
|
|
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
|
|
|
|
internal fun throwsExceptionIfErr() {
|
|
|
|
assertFailsWith<UnwrapException>("called Result.wrap on an Err value 5000") {
|
|
|
|
Err(5000).unwrap()
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `expect` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsValueIfOk() {
|
|
|
|
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
|
|
|
|
internal fun throwsExceptionIfErr() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `unwrapError` {
|
|
|
|
@Test
|
|
|
|
internal fun throwsExceptionIfOk() {
|
|
|
|
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
|
|
|
|
internal fun returnsErrorIfErr() {
|
|
|
|
assertEquals(
|
|
|
|
expected = "example",
|
|
|
|
actual = Err("example").unwrapError()
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `expectError` {
|
|
|
|
@Test
|
|
|
|
internal fun throwsExceptionIfOk() {
|
|
|
|
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
|
|
|
|
internal fun returnsErrorIfErr() {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|