2017-10-21 23:59:16 +00:00
|
|
|
package com.github.michaelbull.result
|
2017-10-21 15:52:29 +00:00
|
|
|
|
|
|
|
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`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val value = Ok(5000).unwrap()
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(value, equalTo(5000))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `unwrap should throw an UnwrapException if not ok`() {
|
2017-10-21 18:17:01 +00:00
|
|
|
val throwable = assertThrows(UnwrapException::class.java, {
|
2017-10-22 15:01:05 +00:00
|
|
|
Err(5000).unwrap()
|
2017-10-21 18:17:01 +00:00
|
|
|
})
|
|
|
|
|
2017-10-22 15:01:05 +00:00
|
|
|
assertThat(throwable.message, equalTo("called Result.wrap on an Err value 5000"))
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `expect should return the result value if ok`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val value = Ok(1994).expect("the year should be")
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(value, equalTo(1994))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `expect should throw an UnwrapException with a specified message if not ok`() {
|
2017-10-21 18:17:01 +00:00
|
|
|
val throwable = assertThrows(UnwrapException::class.java, {
|
2017-10-22 15:01:05 +00:00
|
|
|
Err(1994).expect("the year should be")
|
2017-10-21 18:17:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
assertThat(throwable.message, equalTo("the year should be 1994"))
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `unwrapError should throw an UnwrapException if ok`() {
|
2017-10-21 18:17:01 +00:00
|
|
|
val throwable = assertThrows(UnwrapException::class.java, {
|
2017-10-22 14:05:02 +00:00
|
|
|
Ok("example").unwrapError()
|
2017-10-21 18:17:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
assertThat(throwable.message, equalTo("called Result.unwrapError on an Ok value example"))
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `unwrapError should return the result error if not ok`() {
|
2017-10-22 15:01:05 +00:00
|
|
|
val error = Err("example").unwrapError()
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(error, equalTo("example"))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `expectError should throw an UnwrapException with a specified message if ok`() {
|
2017-10-21 18:17:01 +00:00
|
|
|
val throwable = assertThrows(UnwrapException::class.java, {
|
2017-10-22 14:05:02 +00:00
|
|
|
Ok(2010).expectError("the year should be")
|
2017-10-21 18:17:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
assertThat(throwable.message, equalTo("the year should be 2010"))
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `expectError should return the result error if not ok`() {
|
2017-10-22 15:01:05 +00:00
|
|
|
val error = Err(2010).expectError("the year should be")
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(error, equalTo(2010))
|
|
|
|
}
|
|
|
|
}
|