GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Add contracts to Kotlin-specific assertions by awelless · Pull Request #3259 · junit-team/junit-framework Conversation
|
| Assertions.assertTimeoutPreemptively(timeout, executable) | ||
| fun <R> assertTimeoutPreemptively(timeout: Duration, executable: () -> R): R { | ||
| contract { | ||
| callsInPlace(executable, EXACTLY_ONCE) |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is it actually true for things that can/expected to throw?
(I have a feeling we can't say "exactly once", but I might be misinterpreting the enum.)
Examples where it would not have compiled without the contract, and now it's a runtime error:
val value: String // Swapped version of fun `assertThrows with value initialization in lambda`() in this PR assertThrows<AssertionError> { Assertions.fail("message") value = "string" } assertEquals("string", value) // AssertionFailedError: expected: <string> but was: <null>
val x: String assertThrows<IOException> { File("//").readText() // Deterministic IOException due to invalid name. x = "abc" // Will never execute, because it always throws. } // Kotlin thinks x is initialized here, because of the EXACTLY_ONCE. assertEquals(3, x.length) // Consistent NPE, due to "effectively unreachable code" above.
val x: String assertThrows<AssertionError> { assertTimeoutPreemptively(Duration.seconds(1)) { Thread.sleep(2000) x = "" // Will never execute, because it always times out. } } // Kotlin thinks x is initialized here, because of the chain of EXACTLY_ONCE. println(x.length) // Consistent NPE, due to "effectively unreachable code" above.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What happens if you write this in Kotlin with try/catch? I presume the compiler pitches a fit?
val x: String try { File("//").readText() // Deterministic IOException due to invalid name. x = "abc" // Will never execute, because it always throws. } catch (e: IOException) { } assertEquals(3, x.length)
What about this?
val x: String File("//").apply { readText() // Deterministic IOException due to invalid name. x = "abc" // Will never execute, because it always throws. } assertEquals(3, x.length)
If the apply case fails in the same way as your example, I think it's probably fine?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This looks like it will compile fine:
import java.io.File var x: String File("//").apply { readText() // Deterministic IOException due to invalid name. x = "abc" // Will never execute, because it always throws. } if (3 == x.length) { }
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Huh, very good point! 😁
try-catch example will not compile, but because not all code paths initialize x, but the point is valid there too, you can resolve this thread (I can't).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You could theoretically reimplement this logic, in kotlin, and expose it as an inline function. Then the try/catch will be inlined. Then there will be no need for the contract
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, I would be explicit with second param though, just for readability. If the Kotlin compiler is improved, is it easy to swap to a "better exactly once"? Or would it break users?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I haven't noticed any improvements in contracts. InvocationKind.UNKNOWN is set for assertTimeoutPreemptively methods because of the same reasons as for assertThrows methods
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| assertThrows<AssertionError> { | ||
| value = "string" | ||
| Assertions.fail("message") |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Should this just be
| Assertions.fail("message") | |
| fail("message") |
considering this is Kotlin code and there's a : Nothing signature top level fail.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Contracts work really weird with Nothing return type. Compiler warns that expression after assertThrows is not reachable while it is.
In the thread above I proposed not to specify InvocationKind for assertThrows methods. If there's no InvocationKind specified, value assignment in lambda will be forbidden and this test will make no sense
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
In that case I agree with UNKNOWN for invocation kind, because not being able to call production code that declares Nothing as return type is a big restriction. The point of assertThrows is that it tests exceptions thrown from a method, and Nothing declares exactly that.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
In such case I'll set InvocationKind.UNKNOWN for assertThrows methods
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Looks to be in a reasonable state. Have some thoughts though.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| Assertions.assertTimeoutPreemptively(timeout, executable) | ||
| fun <R> assertTimeoutPreemptively(timeout: Duration, executable: () -> R): R { | ||
| contract { | ||
| callsInPlace(executable, EXACTLY_ONCE) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, I would be explicit with second param though, just for readability. If the Kotlin compiler is improved, is it easy to swap to a "better exactly once"? Or would it break users?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Did a full review, wrote down what I noticed. Feel free to ignore some of them :)
Please resolve the conversations that are resolved, I can't do it, because only the PR Author and repo Members can.
And then we'll probably need a round of review from Marc or Jonathan.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| inline fun <reified T : Throwable> assertThrows(message: String, executable: () -> Unit): T { | ||
| contract { | ||
| callsInPlace(executable, UNKNOWN) | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to test for this contract?
As in: if it's removed, something should stop compiling or fail a test, right?
Probably the one in the related convo: #3259 (comment)
(This is probably true for all of them.)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I haven't found a way to test callsInPlace(..., UNKNOWN) contracts.
I though having another function with a contract inside could work, but it seems, kotlin compiler doesn't check whether a function is really called in place in the functions it's passed to.
fun notWorkingTest(value: () -> Unit) { contract { callsInPlace(value, UNKNOWN) } assertThrows<AssertionError>(value) // works fine even when assertThrows doesn't have a contract }
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| val error = assertThrows<AssertionFailedError> { | ||
| assertInstanceOf<String>(null) | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This feels meta, using a Kotlin assertion to test a Kotlin assertion, but it's probably ok, since they have different use cases for these two.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
We have a few tests for assertThrows. Also, a similar approach is used for assertDoesNotThrow tests
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| fun assertAll(heading: String?, vararg executables: () -> Unit) = | ||
| assertAll(heading, executables.toList().stream()) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Reading this on GitHub makes me confused about the return type.
Again, not your code, but all your return types are nice and clean, probably worth aligning.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I've noticed that return type is not specified only for one-liners that return Unit. I assume, to resemble usual methods, where it isn't mandatory to specify Unit return type. For one-liners that return anything besides Unit, a return type is already specified
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| */ | ||
| inline fun <reified T : Throwable> assertThrows(noinline message: () -> String, executable: () -> Unit): T { | ||
| contract { | ||
| callsInPlace(executable, UNKNOWN) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Why is this unknown?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Contracts doesn't work well as being discussed in this conversation.
Perhaps, not specifying contracts for assertThrows and assertTimeoutPreemptively is a better option. For example, kotlin's runCatching method doesn't specify any contracts.
What do you think?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Contracts have been removed for all lambdas that may throw an exception
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth adding a comment into those methods that they intentionally don't have contracts and why?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Imo, adding comments regarding a contract absence everywhere adds nothing but noise. A person who's concerned by the missing contracts can check the commits and see why they aren't in place
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Is there anything else that should be added/adjusted? Otherwise, I'd say the pr is in its final form |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
What do you think about the pr, @marcphilipp, @JLLeitschuh? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Hi. I believe the change is still relevant and the pr is ready to be reviewed. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
We'll consider it for 5.11, just need to find some time to review. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Bump, we're really missing this PR in Kotlin world. What needs to be done still? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I think the PR needs to be updated to resolve the conflicts, checked for completeness, and then reviewed by someone from the core team. @awelless Do you have time to do a rebase? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I'd still love to see these added, happy to do a re-review after someone does a rebase |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@marcphilipp |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ): R = Assertions.assertTimeoutPreemptively(timeout, executable, message) | ||
| ): R { | ||
| contract { | ||
| callsInPlace(message, AT_MOST_ONCE) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't executable also have a contract?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Added in #4192.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It was ignored deliberately. Please, see this comment.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Oh, I had forgotten that discussion. Thanks for the reminder! 👍
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Now I wonder, whether we should add the explanation why some of the lambdas don't have a contract. I'm not sure, if the actual code is the best place, though.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think that's a good idea and I don't know a better place. Do you have time to submit a PR?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Could be a single sentence in code (each method), and a section at the top/bottom of the file?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think a single comment in each of those methods should suffice. Something like
// No contract for `executable` because ...
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Most likely I won't be able to do this in December.
I may take a look in January, if someone else hasn't done that by then.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I've taken care of it in ebf60bb
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@marcphilipp did this not make 5.11.4? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
No, it's targeting 5.12 which should be released in January if things go according to plan. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
After bumping Kotlin's @awelless @TWiStErRob @JLLeitschuh Does one of you have time to take a look? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Add Kotlin contracts to exposed Kotlin API
There was an error while loading. Please reload this page.