Pass error/value to UnwrapException message

This commit is contained in:
Michael Bull 2017-10-21 19:17:01 +01:00
parent 6f21fd634b
commit 9acd99dffd
2 changed files with 22 additions and 14 deletions

View File

@ -18,7 +18,7 @@ fun <V, E> Result<V, E>.unwrap(): V {
infix fun <V, E> Result<V, E>.expect(message: String): V { infix fun <V, E> Result<V, E>.expect(message: String): V {
return when (this) { return when (this) {
is Ok -> value is Ok -> value
is Error -> throw UnwrapException(message) is Error -> throw UnwrapException("$message $error")
} }
} }
@ -37,7 +37,7 @@ fun <V, E> Result<V, E>.unwrapError(): E {
*/ */
infix fun <V, E> Result<V, E>.expectError(message: String): E { infix fun <V, E> Result<V, E>.expectError(message: String): E {
return when (this) { return when (this) {
is Ok -> throw UnwrapException(message) is Ok -> throw UnwrapException("$message $value")
is Error -> error is Error -> error
} }
} }

View File

@ -14,29 +14,35 @@ internal class UnwrapTest {
@Test @Test
internal fun `unwrap should throw an UnwrapException if not ok`() { internal fun `unwrap should throw an UnwrapException if not ok`() {
assertThrows(UnwrapException::class.java, { val throwable = assertThrows(UnwrapException::class.java, {
err(5000).unwrap() err(5000).unwrap()
}, "called Result.wrap on an Error value 5000") })
assertThat(throwable.message, equalTo("called Result.wrap on an Error value 5000"))
} }
@Test @Test
internal fun `expect should return the result value if ok`() { internal fun `expect should return the result value if ok`() {
val value = ok(1994).expect("the year should be 1994") val value = ok(1994).expect("the year should be")
assertThat(value, equalTo(1994)) assertThat(value, equalTo(1994))
} }
@Test @Test
internal fun `expect should throw an UnwrapException with a specified message if not ok`() { internal fun `expect should throw an UnwrapException with a specified message if not ok`() {
assertThrows(UnwrapException::class.java, { val throwable = assertThrows(UnwrapException::class.java, {
err(1994).expect("the year should be 1994") err(1994).expect("the year should be")
}, "the year should be 1994") })
assertThat(throwable.message, equalTo("the year should be 1994"))
} }
@Test @Test
internal fun `unwrapError should throw an UnwrapException if ok`() { internal fun `unwrapError should throw an UnwrapException if ok`() {
assertThrows(UnwrapException::class.java, { val throwable = assertThrows(UnwrapException::class.java, {
ok("example").unwrapError() ok("example").unwrapError()
}, "called Result.unwrapError on an Ok value example") })
assertThat(throwable.message, equalTo("called Result.unwrapError on an Ok value example"))
} }
@Test @Test
@ -47,14 +53,16 @@ internal class UnwrapTest {
@Test @Test
internal fun `expectError should throw an UnwrapException with a specified message if ok`() { internal fun `expectError should throw an UnwrapException with a specified message if ok`() {
assertThrows(UnwrapException::class.java, { val throwable = assertThrows(UnwrapException::class.java, {
ok(2010).expectError("the year should be 2010") ok(2010).expectError("the year should be")
}, "the year should be 2010") })
assertThat(throwable.message, equalTo("the year should be 2010"))
} }
@Test @Test
internal fun `expectError should return the result error if not ok`() { internal fun `expectError should return the result error if not ok`() {
val error = err(2010).expectError("the year should be 2010") val error = err(2010).expectError("the year should be")
assertThat(error, equalTo(2010)) assertThat(error, equalTo(2010))
} }
} }