diff --git a/src/main/kotlin/com/mikebull94/result/Unwrap.kt b/src/main/kotlin/com/mikebull94/result/Unwrap.kt index e5b9cb8..156bd91 100644 --- a/src/main/kotlin/com/mikebull94/result/Unwrap.kt +++ b/src/main/kotlin/com/mikebull94/result/Unwrap.kt @@ -18,7 +18,7 @@ fun Result.unwrap(): V { infix fun Result.expect(message: String): V { return when (this) { is Ok -> value - is Error -> throw UnwrapException(message) + is Error -> throw UnwrapException("$message $error") } } @@ -37,7 +37,7 @@ fun Result.unwrapError(): E { */ infix fun Result.expectError(message: String): E { return when (this) { - is Ok -> throw UnwrapException(message) + is Ok -> throw UnwrapException("$message $value") is Error -> error } } diff --git a/src/test/kotlin/com/mikebull94/result/UnwrapTest.kt b/src/test/kotlin/com/mikebull94/result/UnwrapTest.kt index bc9623e..b972a82 100644 --- a/src/test/kotlin/com/mikebull94/result/UnwrapTest.kt +++ b/src/test/kotlin/com/mikebull94/result/UnwrapTest.kt @@ -14,29 +14,35 @@ internal class UnwrapTest { @Test internal fun `unwrap should throw an UnwrapException if not ok`() { - assertThrows(UnwrapException::class.java, { + val throwable = assertThrows(UnwrapException::class.java, { err(5000).unwrap() - }, "called Result.wrap on an Error value 5000") + }) + + 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 1994") + 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`() { - assertThrows(UnwrapException::class.java, { - err(1994).expect("the year should be 1994") - }, "the year should be 1994") + val throwable = assertThrows(UnwrapException::class.java, { + err(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`() { - assertThrows(UnwrapException::class.java, { + val throwable = assertThrows(UnwrapException::class.java, { ok("example").unwrapError() - }, "called Result.unwrapError on an Ok value example") + }) + + assertThat(throwable.message, equalTo("called Result.unwrapError on an Ok value example")) } @Test @@ -47,14 +53,16 @@ internal class UnwrapTest { @Test internal fun `expectError should throw an UnwrapException with a specified message if ok`() { - assertThrows(UnwrapException::class.java, { - ok(2010).expectError("the year should be 2010") - }, "the year should be 2010") + 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 = err(2010).expectError("the year should be 2010") + val error = err(2010).expectError("the year should be") assertThat(error, equalTo(2010)) } }