GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add contracts to Kotlin-specific assertions by awelless · Pull Request #3259 · junit-team/junit-framework

Conversation

@awelless

@awelless awelless commented Apr 17, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Overview

Resolves #1866.
Introduced assertNull, assertNotNull methods.
Introduced contracts for assertNull, assertNotNull, assertThrows and assertDoesNotThrow methods.

Added smart casting tests for assertInstanceOf methods.


I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

  • There are no TODOs left in the code
  • Method preconditions are checked and documented in the method's Javadoc
  • Coding conventions (e.g. for logging) have been followed
  • Change is covered by automated tests including corner cases, errors, and exception handling
  • Public API has Javadoc and @API annotations
  • Change is documented in the User Guide and Release Notes

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
marcphilipp
marcphilipp reviewed Apr 22, 2023
View reviewed changes
Comment thread junit-jupiter-api/junit-jupiter-api.gradle.kts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@marcphilipp marcphilipp added the status: waiting-for-feedback label Apr 22, 2023
@awelless
awelless requested a review from marcphilipp April 22, 2023 21:36
@marcphilipp marcphilipp removed the status: waiting-for-feedback label Apr 23, 2023
marcphilipp
marcphilipp reviewed Apr 23, 2023
View reviewed changes
Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

@awelless
awelless requested a review from marcphilipp April 23, 2023 19:06
JLLeitschuh
JLLeitschuh reviewed May 26, 2023
View reviewed changes
Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt

@JLLeitschuh JLLeitschuh May 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think fun fail(message: (() -> String)?) should also have a contract added to it?

Also, can contracts be added to vararg executables: () -> Unit?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless May 26, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callsInPlace supports only non-nullable arguments.
It would be great to have something like:

fun fail(message: (() -> String)?): Nothing {
    contract {
        if (message != null) {
            callsInPlace(message, EXACTLY_ONCE)
        }
    }

    Assertions.fail<Nothing>(message)
}

but if is not allowed in a contract definition.

Similar thing applies to vararg executables: () -> Unit

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TWiStErRob TWiStErRob May 26, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the if be partially resolved like this? So that more (most) calls end up using the contract description for analysis?

// Covers method references `fail(::foo)`, inline lambdas `fail { }`.
fun fail(message: () -> String): Nothing {
    contract {
        callsInPlace(message, EXACTLY_ONCE)
    }
    Assertions.fail<Nothing>(message)
}
// Covers backwards compatibility and potential edge cases like `fail(foo.bar)` where bar is a nullable functional type.
fun fail(message: (() -> String)?): Nothing {
    Assertions.fail<Nothing>(message)
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless May 27, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method signatures are same from JVM perspective.

Platform declaration clash: The following declarations have the same JVM signature (fail(Lkotlin/jvm/functions/Function0;)Ljava/lang/Void;):
    fun fail(message: (() -> String)?): Nothing defined in org.junit.jupiter.api in file Assertions.kt
    fun fail(message: () -> String): Nothing defined in org.junit.jupiter.api in file Assertions.kt

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TWiStErRob TWiStErRob May 27, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I didn't get that when I tried. Anyway, @JvmName usually helps. The question is if this is a feasible solution to get contract in for most, and still keep supporting other edge cases. What's the use case for nullable lambda here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Jun 2, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, creating a separate method with a contract and non-nullable lambda allows kotlin compiler to carry out contract checks when it's sure the passed lambda is not null.
I assume, lambda is made nullable to resemble existing java api where messageSupplier can be nullable

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Jun 3, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callsInPlace for fail method is also specified with InvocationKind.UNKNOWN .
If we set it to EXACTLY_ONCE the following code compiles:

val expectedValue = "value"
val value: String

try {
    fail {
        value = expectedValue
        "message"
    }
} catch (_: AssertionFailedError) {
    value = "another value"
}

assertEquals(expectedValue, value)

Here we reassign value albeit it is val. I reckon it isn't desirable to allow this code to compile

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
TWiStErRob
TWiStErRob reviewed May 26, 2023
View reviewed changes
Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Outdated
Assertions.assertTimeoutPreemptively(timeout, executable)
fun <R> assertTimeoutPreemptively(timeout: Duration, executable: () -> R): R {
contract {
callsInPlace(executable, EXACTLY_ONCE)

@TWiStErRob TWiStErRob May 26, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@JLLeitschuh JLLeitschuh May 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@JLLeitschuh JLLeitschuh May 26, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TWiStErRob TWiStErRob May 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@JLLeitschuh JLLeitschuh May 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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

https://github.com/junit-team/junit5/blob/ffab3a37f4a4e33529cca4ef9a464c244756c6fc/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertThrows.java#L49-L74

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless May 26, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contracts don't work well when we suppress exception. Issues one and two.
To prevent any kind of errors and confusion, I suggest to replace callsInPlace(executable, EXACTLY_ONCE) with callsInPlace(executable) in all assertThrows methods.

What do you think?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TWiStErRob TWiStErRob Aug 10, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Sep 1, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
TWiStErRob
TWiStErRob reviewed May 26, 2023
View reviewed changes
Comment thread junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/KotlinAssertionsTests.kt Outdated

assertThrows<AssertionError> {
value = "string"
Assertions.fail("message")

@TWiStErRob TWiStErRob May 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be

Suggested change
Assertions.fail("message")
fail("message")

considering this is Kotlin code and there's a : Nothing signature top level fail.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless May 26, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TWiStErRob TWiStErRob May 26, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Jun 2, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@awelless
awelless requested review from JLLeitschuh and TWiStErRob May 26, 2023 20:15
TWiStErRob
TWiStErRob reviewed Jun 3, 2023
View reviewed changes
Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

@awelless
awelless requested a review from TWiStErRob June 3, 2023 21:39
@awelless
awelless force-pushed the main branch from ed02ba8 to 19922d6 Compare August 10, 2023 07:12
TWiStErRob
TWiStErRob approved these changes Aug 10, 2023
View reviewed changes

@TWiStErRob TWiStErRob left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Outdated
Assertions.assertTimeoutPreemptively(timeout, executable)
fun <R> assertTimeoutPreemptively(timeout: Duration, executable: () -> R): R {
contract {
callsInPlace(executable, EXACTLY_ONCE)

@TWiStErRob TWiStErRob Aug 10, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
TWiStErRob
TWiStErRob reviewed Aug 25, 2023
View reviewed changes
Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

@awelless
awelless requested a review from TWiStErRob August 27, 2023 14:36
TWiStErRob
TWiStErRob reviewed Aug 27, 2023
View reviewed changes

@TWiStErRob TWiStErRob left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

awelless reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Outdated
Comment on lines +338 to +341
inline fun <reified T : Throwable> assertThrows(message: String, executable: () -> Unit): T {
contract {
callsInPlace(executable, UNKNOWN)
}

@TWiStErRob TWiStErRob Aug 27, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Sep 1, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/KotlinAssertionsTests.kt Outdated
Comment on lines +249 to +251
val error = assertThrows<AssertionFailedError> {
assertInstanceOf<String>(null)
}

@TWiStErRob TWiStErRob Aug 27, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Sep 1, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/KotlinAssertionsTests.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Outdated
Comment on lines 102 to 103
fun assertAll(heading: String?, vararg executables: () -> Unit) =
assertAll(heading, executables.toList().stream())

@TWiStErRob TWiStErRob Aug 27, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Sep 1, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
JLLeitschuh
JLLeitschuh reviewed Sep 4, 2023
View reviewed changes
Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt Outdated
*/
inline fun <reified T : Throwable> assertThrows(noinline message: () -> String, executable: () -> Unit): T {
contract {
callsInPlace(executable, UNKNOWN)

@JLLeitschuh JLLeitschuh Sep 4, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this unknown?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TWiStErRob TWiStErRob Sep 4, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See these two:

  • Add contracts to Kotlin-specific assertions #3259 (comment)
  • Add contracts to Kotlin-specific assertions #3259 (comment)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Sep 4, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

TWiStErRob reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@awelless awelless Sep 8, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TWiStErRob TWiStErRob Sep 8, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Sep 11, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

TWiStErRob reacted with eyes emoji
All reactions
  • 👀 1 reaction
@awelless
awelless requested a review from JLLeitschuh September 4, 2023 20:37
@awelless

awelless commented Sep 23, 2023

Copy link
Copy Markdown
Contributor Author

Is there anything else that should be added/adjusted? Otherwise, I'd say the pr is in its final form

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

TWiStErRob
TWiStErRob approved these changes Sep 23, 2023
View reviewed changes

@TWiStErRob TWiStErRob left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/KotlinAssertionsTests.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

@awelless

awelless commented Oct 3, 2023

Copy link
Copy Markdown
Contributor Author

What do you think about the pr, @marcphilipp, @JLLeitschuh?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@awelless

awelless commented Dec 15, 2023

Copy link
Copy Markdown
Contributor Author

Hi. I believe the change is still relevant and the pr is ready to be reviewed.

TWiStErRob reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sbrannen sbrannen changed the title Added Kotlin Contracts to Exposed Kotlin API Add Kotlin contracts to exposed Kotlin API Dec 16, 2023
@marcphilipp

marcphilipp commented Dec 18, 2023

Copy link
Copy Markdown
Member

We'll consider it for 5.11, just need to find some time to review.

awelless reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@TWiStErRob

TWiStErRob commented Nov 12, 2024

Copy link
Copy Markdown
Contributor

Bump, we're really missing this PR in Kotlin world. What needs to be done still?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@marcphilipp

marcphilipp commented Nov 13, 2024

Copy link
Copy Markdown
Member

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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JLLeitschuh

JLLeitschuh commented Nov 13, 2024

Copy link
Copy Markdown
Contributor

I'd still love to see these added, happy to do a re-review after someone does a rebase

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@awelless

awelless commented Nov 14, 2024

Copy link
Copy Markdown
Contributor Author

@marcphilipp
yes, I'll rebasethe PR this weekend.

marcphilipp reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JLLeitschuh
JLLeitschuh reviewed Dec 8, 2024
View reviewed changes
Comment thread junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt
): R = Assertions.assertTimeoutPreemptively(timeout, executable, message)
): R {
contract {
callsInPlace(message, AT_MOST_ONCE)

@JLLeitschuh JLLeitschuh Dec 8, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@marcphilipp marcphilipp Dec 9, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Dec 9, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@marcphilipp marcphilipp Dec 10, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Dec 10, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@marcphilipp marcphilipp Dec 10, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TWiStErRob TWiStErRob Dec 10, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@marcphilipp marcphilipp Dec 11, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@awelless awelless Dec 12, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@marcphilipp marcphilipp Dec 13, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for 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.

Uh oh!

There was an error while loading. Please reload this page.

TWiStErRob reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
marcphilipp added a commit that referenced this pull request Dec 9, 2024
@marcphilipp
Add contracts for executable parameters
8ad2527
Follow-up on #3259
@marcphilipp marcphilipp mentioned this pull request Dec 9, 2024
Add contracts for executable parameters #4192
Closed
marcphilipp added a commit that referenced this pull request Dec 13, 2024
@marcphilipp
Add comments explaining lack of contracts for Executable parameter
ebf60bb
Follow-up on #3259
@TWiStErRob

TWiStErRob commented Dec 16, 2024

Copy link
Copy Markdown
Contributor

@marcphilipp did this not make 5.11.4?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@marcphilipp

marcphilipp commented Dec 16, 2024

Copy link
Copy Markdown
Member

No, it's targeting 5.12 which should be released in January if things go according to plan.

TWiStErRob reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sbrannen sbrannen added type: enhancement theme: programming model component: Jupiter component: Kotlin labels Jan 26, 2025
@sbrannen sbrannen assigned marcphilipp Jan 26, 2025
This was referenced Feb 24, 2025
Bump junit from 5.11.4 to 5.12.0 kousen/java_8_recipes#125
Closed
Bump junit from 5.11.4 to 5.12.0 kousen/IntroGroovy#128
Closed
Bump junit from 5.11.4 to 5.12.0 kousen/junit5_workshop#251
Closed
Bump org.junit.jupiter:junit-jupiter from 5.11.4 to 5.12.0 kousen/http-client-demo#43
Merged
Bump org.junit.jupiter:junit-jupiter from 5.11.4 to 5.12.0 kousen/functional_modern_java#31
Closed
Bump org.junit.jupiter:junit-jupiter from 5.11.4 to 5.12.0 kousen/java_upgrade#44
Merged
Bump org.junit.jupiter:junit-jupiter from 5.11.4 to 5.12.0 kousen/java_9_recipes#45
Closed
Bump junit from 5.11.4 to 5.12.0 kousen/java_groovy_kotlin#194
Merged
@marcphilipp

marcphilipp commented Mar 5, 2025

Copy link
Copy Markdown
Member

After bumping Kotlin's apiVersion and languageVersion this is causing some compiler warnings:

  • Resolve Kotlin 2.1 compiler warnings for contracts #4371

@awelless @TWiStErRob @JLLeitschuh Does one of you have time to take a look?

awelless reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Mar 17, 2025
Bump junit from 5.11.4 to 5.12.1 kousen/java_8_recipes#127
Closed
Bump junit from 5.11.4 to 5.12.1 kousen/IntroGroovy#130
Closed
Bump junit from 5.11.4 to 5.12.1 kousen/junit5_workshop#254
Merged
Bump org.junit.jupiter:junit-jupiter from 5.11.4 to 5.12.1 kousen/java_9_recipes#46
Closed
Bump org.junit.jupiter:junit-jupiter from 5.11.4 to 5.12.1 kousen/functional_modern_java#32
Closed
This was referenced Apr 14, 2025
Bump org.junit.jupiter:junit-jupiter from 5.11.4 to 5.12.2 kousen/java_9_recipes#47
Closed
Bump org.junit.jupiter:junit-jupiter from 5.11.4 to 5.12.2 kousen/functional_modern_java#33
Closed
Bump junit from 5.11.4 to 5.12.2 kousen/java_8_recipes#131
Closed
Bump junit from 5.11.4 to 5.12.2 kousen/IntroGroovy#132
Closed
@coderabbitai coderabbitai Bot mentioned this pull request Sep 19, 2026
test: テストの重複を整理し境界条件・失敗ケースを補強 lambdarc/lite-memo#182
Merged
1 of 2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@marcphilipp marcphilipp marcphilipp approved these changes
+2 more reviewers
@JLLeitschuh JLLeitschuh JLLeitschuh left review comments
@TWiStErRob TWiStErRob TWiStErRob approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@marcphilipp marcphilipp

Labels

component: Jupiter component: Kotlin theme: programming model type: enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Add Kotlin contracts to exposed Kotlin API

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@awelless @marcphilipp @TWiStErRob @JLLeitschuh @sbrannen
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.