Open JLLeitschuh opened 5 years ago
This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. Thank you for your contribution.
This sounds like a useful usability feature!
This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. Thank you for your contribution.
Please do this.
This issue has been automatically closed due to inactivity. If you have a good use case for this feature, please feel free to reopen the issue.
@junit-team What is missing to move this forward?
@JLLeitschuh I think these might also be deliverables for full coverage:
assertNull
to get Kotlin compilation protection for val actual = ...; assertNull(actual); actual.foo()
.foo()
would be a 100% NPE with current solution and it wouldn't compile with contracts)assertInstanceOf
(similar to non-null, but with implies (actualValue is T)
)Is anyone working on this issue?
Not currently, but I figure at this point it would be safe to do so as Kotlin 1.3 is pretty EOL as of 2 years ago: https://endoflife.date/kotlin
Also, this library currently supports Kotlin versions 1.3 and above, so it would now be safe to add:
If someone wants to open a pull request to add support for this, I'm more than happy to review it
Okay. I could start working on it. However, it might take some time
@JLLeitschuh how can this be tested with automation? Because contracts should induce Kotlin compilation failures. I guess the positive cases of smart casts as results of contract can be tested:
val foo: String? = ...
assertNotNull(foo)
assertEquals(1, foo.size)
(if the contracts stop working, the above test will not compile)
As you suggested, I would use the compiler as your validation
I think these might also be deliverables for full coverage: ...
assertInstanceOf
(similar to non-null, but withimplies (actualValue is T)
)
Contracts with reified generics are supported only since kotlin 1.4. Therefore, assertInstanceOf
can't be implemented for now
I wondered if the that error is suppressed will it emit 1.4 compatible bytecode for the contract? Therefore making it possible to use the same jar binary from both 1.3 and 1.4.
Since kotlin version has been bumped to 1.6, it should be possible to implement assertInstanceOf
with a contract specified
Foreward
First off, I want to thank the Junit 5 team from being so willing to officially support Kotlin as a first-class citizen in the Junit 5 library. It has been absolutely wonderful being able to use my own contributions in all of my Kotlin projects.
Feature Request
I believe that this API can be further enhanced with the new Kotlin 1.3 feature, Contracts.
Contracts are making guarantees to the compiler that various methods have certain characteristics.
Here's an example from the Kotlin Std-Lib:
Before Kotlin Contracts, the following code wouldn't have compiled:
I believe that JUnit 5 has a few places where these contracts would be valuable.
Examples
assertNotNull
The above would allow something like this:
Alternatively, it would also allow for this sort of use case:
assertThrows
/assertDoesNotThrow
Since the
callable
passed toassertThrows
is only ever called once, we can expose that in the contract.Similar
This would for something like this:
Caveats
Kotlin Contracts are only supported in Kotlin 1.3 and higher. This would require a discussion regarding what version of Kotlin the Junit 5 team want's to officially support.
Deliverables
assertNotNull
assertNotNull
assertThrows
assertDoesNotThrow