-
Notifications
You must be signed in to change notification settings - Fork 260
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
Document how to write a Truth extension under Kotlin #661
Comments
Depending on how we decide #572, we might also recommend an extension-method overload on the type under test. [edit: To be explicit: This would probably be in addition to the old-style entry point, since the old-style entry point is probably the best way to serve Java users of the Kotlin However, as I noted in that thread, we'll want to think about how sad it would be if some custom (Even some pure-Kotlin extensions are likely to omit anything that we recommend: We've seen lots of custom |
Actually, I'm just now seeing that our internal Kotlin
The former may be more likely to appear in autocomplete. The latter lets us produce more conventional Java-facing APIs, with We should also consider the exposed |
(I notice that the Square's reflective-field-comparison subject currently exposes only a |
Also, we've learned that users need to initialize their factory property to |
...he says :) And now we're seeing some ways that (The big point so far is that [edit: And we might then nudge people toward defining |
I haven't run this by anyone yet, but just to write down at attempt to capture what I know so far, hopefully without any embarrassing mistakes [edit: OK, I've already had to edit it a few times :)]: class FooSubject
private constructor(metadata: FailureMetadata, private val actual: Foo?) :
Subject(metadata, actual) {
...
companion object {
@JvmStatic
fun assertThat(foo: Foo?) = assertAbout(foos()).that(foo)
@JvmStatic
fun foos() = Factory(::FooSubject)
}
} [edit: [edit: I looked more at #660 (comment), and the main downside seems to be that it doesn't work work with |
(https://github.com/Kotlin/KEEP/blob/statics/proposals/statics.md could change matters.) |
In another context, we're discussing whether method references or lambdas are more idiomatic in Kotlin. However, even if we decide that lambdas are more idiomatic in a vacuum, I think I'd continue to suggest method references for Truth extensions, as I believe the comparison is: fun foos() = Factory(::FooSubject) vs. fun foos(): Factory<FooSubject, Foo> = { FooSubject(it) } Or wait, maybe it's this, which would be less bad? I should check sometime, but not now.... fun foos() = Factory { FooSubject(it) } Now, if someone were using Explicit API Mode, then I think the two would be closer in length: We'd probably be comparing my initial lambda version with something like the following method-reference version (but I haven't tested it, including not having tested whether we would still need a second mention of " fun foos(): Factory<FooSubject, Foo> = ::FooSubject |
It's pretty simple (and pretty similar to Java), but it's always nice for users to have an example.
[edit: I now have a draft example below.]
We could also consider suggesting that every
Subject.Factory
come with an extension-method "overload" ofStandardSubjectBuilder.that
(#660).[edit: See also #660 (comment), which suggests that we may be able to make extensions easier to write under Kotlin than under Java.]
The text was updated successfully, but these errors were encountered: