kotlin-result/src/test/kotlin/com/github/michaelbull/result/UnwrapTest.kt
Michael Bull dc4101b8ef Remove factory methods and expose Ok/Error constructors
Also converts them to data classes.
2017-10-22 16:07:24 +01:00

69 lines
2.2 KiB
Kotlin

package com.github.michaelbull.result
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test
internal class UnwrapTest {
@Test
internal fun `unwrap should return the result value if ok`() {
val value = Ok(5000).unwrap()
assertThat(value, equalTo(5000))
}
@Test
internal fun `unwrap should throw an UnwrapException if not ok`() {
val throwable = assertThrows(UnwrapException::class.java, {
Error(5000).unwrap()
})
assertThat(throwable.message, equalTo("called Result.wrap on an Error value 5000"))
}
@Test
internal fun `expect should return the result value if ok`() {
val value = Ok(1994).expect("the year should be")
assertThat(value, equalTo(1994))
}
@Test
internal fun `expect should throw an UnwrapException with a specified message if not ok`() {
val throwable = assertThrows(UnwrapException::class.java, {
Error(1994).expect("the year should be")
})
assertThat(throwable.message, equalTo("the year should be 1994"))
}
@Test
internal fun `unwrapError should throw an UnwrapException if ok`() {
val throwable = assertThrows(UnwrapException::class.java, {
Ok("example").unwrapError()
})
assertThat(throwable.message, equalTo("called Result.unwrapError on an Ok value example"))
}
@Test
internal fun `unwrapError should return the result error if not ok`() {
val error = Error("example").unwrapError()
assertThat(error, equalTo("example"))
}
@Test
internal fun `expectError should throw an UnwrapException with a specified message if ok`() {
val throwable = assertThrows(UnwrapException::class.java, {
Ok(2010).expectError("the year should be")
})
assertThat(throwable.message, equalTo("the year should be 2010"))
}
@Test
internal fun `expectError should return the result error if not ok`() {
val error = Error(2010).expectError("the year should be")
assertThat(error, equalTo(2010))
}
}