Use 'language injection' on assertions, so embedded languages are easier to use in IntelliJ
See original GitHub issueI’d like the JSON Path matchers to be easier to use. IntelliJ has a nice feature which will apply highlighting and formatting to a string if it’s specially marked https://www.jetbrains.com/help/idea/using-language-injections.html
It would be very convenient if Kotest applied this to the JSONPath assertions.
Example language injection
import org.intellij.lang.annotations.Language
@Language("JSONPath")
val myJsonPath = "$.store.book[?(@.price < 10)].title"
// language=JSONPath
val myInvalidJsonPath = "$.store.book[(@.length-1)]title"
Note the invalid path has a red error highlight: it’s missing a .
.
Current usage / workaround
These can be applied locally, which is a bit more inconvenient because it requires repetitive commenting
response.body?.shouldContainJsonKey( /* language=JSONPath */ "$.store.book[(@.length-1)].title")
response.body?.shouldNotContainJsonKey(/* language=JSONPath */ "$.store.book[(@.length-1)].age")
(I tried setting up something with @Language("JsonPath")
but it didn’t work - maybe it’s possible with external annotations?)
Suggestion
If the @Language(...)
annotation is added to Kotest, then all users will automatically get the benefits (at least if their IDE supports it).
Change this method:
to be like this:
import org.intellij.lang.annotations.Language
inline fun <reified T> String?.shouldContainJsonKeyValue(
@Language("JSONPath") path: String,
value: T,
) //...
(I’m not sure if adding the // language=JSONPath
in Kotest will work the same as adding the annotation - the annotation seems more reliable from my brief experiments so I recommend that.)
I can make a PR to add them to the JSON Path assertions, as a proof-of-concept. My question is: how widely should these matchers be applied? Would any other assertions benefit?
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
edit: Noticed that the particular assertion is in jvmMain. I think we should do it 🙂
– Most assertions are defined in common target, and
@Language
annotations are sadl not multiplatform compatible. I’ve been itching to do this for a while. Do you have any ideas for workarounds?See https://youtrack.jetbrains.com/issue/KTIJ-16340
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.