Lazily evaluate message argument in expect/expectError

This commit is contained in:
Michael Bull 2017-11-22 23:10:23 +00:00
parent 02de42f959
commit ad7adacf39
2 changed files with 18 additions and 8 deletions

View File

@ -16,6 +16,11 @@ fun <V, E> Result<V, E>.unwrap(): V {
}
}
@Deprecated("Use lazy evaluation.", ReplaceWith("expect { message }"))
infix fun <V, E> Result<V, E>.expect(message: String): V {
return expect { message }
}
/**
* Unwraps a [Result], yielding the [value][Ok.value].
*
@ -24,10 +29,10 @@ fun <V, E> Result<V, E>.unwrap(): 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 fun <V, E> Result<V, E>.expect(message: String): V {
infix inline fun <V, E> Result<V, E>.expect(message: () -> String): V {
return when (this) {
is Ok -> value
is Err -> throw UnwrapException("$message $error")
is Err -> throw UnwrapException("${message()} $error")
}
}
@ -45,6 +50,11 @@ fun <V, E> Result<V, E>.unwrapError(): E {
}
}
@Deprecated("Use lazy evaluation.", ReplaceWith("expectError { message }"))
infix fun <V, E> Result<V, E>.expectError(message: String): E {
return expectError { message }
}
/**
* Unwraps a [Result], yielding the [error][Err.error].
*
@ -53,9 +63,9 @@ fun <V, E> Result<V, E>.unwrapError(): 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 fun <V, E> Result<V, E>.expectError(message: String): E {
infix inline fun <V, E> Result<V, E>.expectError(message: () -> String): E {
return when (this) {
is Ok -> throw UnwrapException("$message $value")
is Ok -> throw UnwrapException("${message()} $value")
is Err -> error
}
}

View File

@ -23,14 +23,14 @@ internal class UnwrapTest {
@Test
internal fun `expect should return the result value if ok`() {
val value = Ok(1994).expect("the year should be")
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, {
Err(1994).expect("the year should be")
Err(1994).expect { "the year should be" }
})
assertThat(throwable.message, equalTo("the year should be 1994"))
@ -54,7 +54,7 @@ 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")
Ok(2010).expectError { "the year should be" }
})
assertThat(throwable.message, equalTo("the year should be 2010"))
@ -62,7 +62,7 @@ internal class UnwrapTest {
@Test
internal fun `expectError should return the result error if not ok`() {
val error = Err(2010).expectError("the year should be")
val error = Err(2010).expectError { "the year should be" }
assertThat(error, equalTo(2010))
}
}