Skip to content

Commit

Permalink
Lazily evaluate message argument in expect/expectError
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Nov 22, 2017
1 parent 02de42f commit ad7adac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/main/kotlin/com/github/michaelbull/result/Unwrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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].
*
Expand All @@ -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")
}
}

Expand All @@ -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].
*
Expand All @@ -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
}
}
8 changes: 4 additions & 4 deletions src/test/kotlin/com/github/michaelbull/result/UnwrapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -54,15 +54,15 @@ 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"))
}

@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))
}
}

0 comments on commit ad7adac

Please sign in to comment.