-
Notifications
You must be signed in to change notification settings - Fork 641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add matchers for Result #836
Comments
I was trying to do this, but, unfortunately, I started to face some kotlin issues (I guess). /**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
fun main() {
val result = Result.runCatching { "Test 01" }
val errorResult = Result.runCatching { throw java.io.IOException("Some error happened") }
println("Test")
println(test("Test 01", result))
println(test("Some error happened", errorResult))
println()
println("With generics")
println(match("Test 01", "Some error happened").test(result))
println(match("Test 01", "Some error happened").test(errorResult))
println()
println("With kotlintest dsl-like")
println(result should match("Test 01", "Some error happened"))
println(errorResult should match("Test 01", "Some error happened"))
}
infix fun <T> T.should(matcher: Matcher<T>) = matcher.test(this)
fun test(expectedValue: String, result: Result<String>) = if (result.isSuccess) {
expectedValue == result.getOrNull()
} else {
expectedValue == result.exceptionOrNull()?.message
}
fun <T> match(expectedValue: T, errorMessage: String) = object: Matcher<Result<T>> {
override fun test(value: Result<T>) = if (value.isSuccess) {
print("I'm a success")
Pair(
value.getOrNull() == expectedValue,
Pair(value.getOrNull(), expectedValue)
)
} else {
print("I'm an error")
Pair(
value.exceptionOrNull()?.message == errorMessage,
Pair(value.exceptionOrNull()?.message, errorMessage)
)
}
}
interface Matcher<T> {
fun test(value: T): Pair<Boolean, Pair<Any?, Any?>>
} The result is this 👇
For some reason, when I pass the result to a generic, it creates a new "Success Result" and instead of getting "Test 01" from the |
@sksamuel @Kerooker ☝️ I also create an issue on kotlin issue tracker. Didn't find any similar bug. |
I see. I'll comment about this in KotlinLang's Slack, let's see if it's indeed a bug. We can keep this on-hold for a bit :/ |
The type T is a Result itself. So your |
Please swap the generics for concrete types and see if you can find the problem, this code is very unreadable here like this |
As I showed on the code that I posted before, I already tried without generic and it works. Unfortunately, the entire library is built upon those generics and doesn't make any sense to change that. Or even create a lot of repetition just for that. They commented on the issue on kotlin issue tracker that it's a bug on kotlin/jvm about boxing/unboxing. They already added the task as a subtask from another group and maybe will have a solution after some time. |
What's wrong with something like this: https://pl.kotl.in/zpkSnkEeD |
The problem is when you add one more level of complexity like this. |
We could do it without the infix version for now: https://pl.kotl.in/5RZM9cKCT |
Jetbrains added the
Result
type, a bit like theTry
type from arrow.We should add matchers for this.
The text was updated successfully, but these errors were encountered: