Make lambda return type of expect{Err} return Any

When constructing the exception message the toString method will be
implicitly called on the result of the lambda.

Takes inspiration from Kotlin's require function [1].

[1]: https://github.com/JetBrains/kotlin/blob/v1.2.0/libraries/stdlib/src/kotlin/util/Preconditions.kt#L26
This commit is contained in:
Michael Bull 2017-11-28 18:23:36 +00:00
parent 8e3597322a
commit e455be2cc8
2 changed files with 20 additions and 12 deletions

View File

@ -29,7 +29,7 @@ infix fun <V, E> Result<V, E>.expect(message: String): V {
* @param message The message to include in the [UnwrapException] if the [Result] is an [Err].
* @throws UnwrapException if the [Result] is an [Err], with the specified [message].
*/
infix inline fun <V, E> Result<V, E>.expect(message: () -> String): V {
infix inline fun <V, E> Result<V, E>.expect(message: () -> Any): V {
return when (this) {
is Ok -> value
is Err -> throw UnwrapException("${message()} $error")
@ -63,7 +63,7 @@ infix fun <V, E> Result<V, E>.expectError(message: String): E {
* @param message The message to include in the [UnwrapException] if the [Result] is [Ok].
* @throws UnwrapException if the [Result] is [Ok], with the specified [message].
*/
infix inline fun <V, E> Result<V, E>.expectError(message: () -> String): E {
infix inline fun <V, E> Result<V, E>.expectError(message: () -> Any): E {
return when (this) {
is Ok -> throw UnwrapException("${message()} $value")
is Err -> error

View File

@ -14,9 +14,9 @@ internal class UnwrapTest {
@Test
internal fun `unwrap should throw an UnwrapException if not ok`() {
val throwable = assertThrows(UnwrapException::class.java, {
val throwable = assertThrows(UnwrapException::class.java) {
Err(5000).unwrap()
})
}
assertThat(throwable.message, equalTo("called Result.wrap on an Err value 5000"))
}
@ -29,18 +29,22 @@ internal class UnwrapTest {
@Test
internal fun `expect should throw an UnwrapException with a specified message if not ok`() {
val throwable = assertThrows(UnwrapException::class.java, {
Err(1994).expect { "the year should be" }
})
val message = object {
override fun toString() = "the year should be"
}
val throwable = assertThrows(UnwrapException::class.java) {
Err(1994).expect { message }
}
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, {
val throwable = assertThrows(UnwrapException::class.java) {
Ok("example").unwrapError()
})
}
assertThat(throwable.message, equalTo("called Result.unwrapError on an Ok value example"))
}
@ -53,9 +57,13 @@ internal class UnwrapTest {
@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" }
})
val message = object {
override fun toString() = "the year should be"
}
val throwable = assertThrows(UnwrapException::class.java) {
Ok(2010).expectError { message }
}
assertThat(throwable.message, equalTo("the year should be 2010"))
}