Skip to content
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

Introduce assertInstanceOf() extension function for Kotlin #3353

Open
bjmi opened this issue Jun 13, 2023 · 5 comments · May be fixed by #3492
Open

Introduce assertInstanceOf() extension function for Kotlin #3353

bjmi opened this issue Jun 13, 2023 · 5 comments · May be fixed by #3492

Comments

@bjmi
Copy link
Contributor

bjmi commented Jun 13, 2023

This is a follow up of #2492 but for Kotlin.

After discovering the new assertInstanceOf assertion (since 5.8) I find it really expressive and useful.
Could you please also add a Kotlin variant to Assertions.kt? E.g.

Usage:

Now:      assertInstanceOf(String::class.java, actual)
Proposal: assertInstanceOf<String>(actual)

Definition:

inline fun <reified T : Any> assertInstanceOf(actualValue: Any?): T =
    Assertions.assertInstanceOf(T::class.java, actualValue)
...
@marcphilipp
Copy link
Member

@bjmi Thanks for creating the issue! I think this would be a natural addition. Would you be interested in submitting a PR?

@bjmi
Copy link
Contributor Author

bjmi commented Jun 18, 2023

I can give it a try.

bjmi added a commit to bjmi/junit5 that referenced this issue Oct 8, 2023
Resolves junit-team#3353

Signed-off-by: Björn Michael <b.michael@gmx.de>
bjmi added a commit to bjmi/junit5 that referenced this issue Oct 8, 2023
Resolves junit-team#3353

Signed-off-by: Björn Michael <b.michael@gmx.de>
@bjmi bjmi linked a pull request Oct 8, 2023 that will close this issue
6 tasks
@marcphilipp marcphilipp added this to the 5.11 M1 milestone Oct 13, 2023
@marcphilipp marcphilipp self-assigned this Oct 13, 2023
@TWiStErRob
Copy link

@mikhail2048 / @bjmi there are two PRs for this now.

@sbrannen sbrannen changed the title assertInstanceOf for Kotlin Introduce assertInstanceOf() extension function for Kotlin Dec 16, 2023
@andrewparmet
Copy link

andrewparmet commented Dec 23, 2023

It would be great for this implementation to use Kotlin's contracts feature so the instance can be used subsequently as an instance of the expected class.

See also: https://youtrack.jetbrains.com/issue/KT-28298 which has been implemented.

A slight modification of #3492:

inline fun <reified T : Any> assertInstanceOf(actualValue: Any?, message: String? = null): T {
    contract {
        returns() implies (value is T)
    }
    return Assertions.assertInstanceOf(T::class.java, actualValue, message)
}

will allow this pattern without cast:

val bar: Any = foo()
assertInstanceOf<String>(bar)
bar.lowercase() // bar is a String

@TWiStErRob
Copy link

@andrewparmet you can leave a suggestion as a review on a PR in context, more likely to get noticed when the PR is reviewed.

bjmi added a commit to bjmi/junit5 that referenced this issue Dec 30, 2023
Resolves junit-team#3353

Signed-off-by: Björn Michael <b.michael@gmx.de>
@marcphilipp marcphilipp modified the milestones: 5.11 M1, 5.12 Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment