question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use 'language injection' on assertions, so embedded languages are easier to use in IntelliJ

See original GitHub issue

I’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 ..

image

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")
image

(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:

https://github.com/kotest/kotest/blob/b832d4941ecc45074bcd6dea3f24d16553f2fc21/kotest-assertions/kotest-assertions-json/src/jvmMain/kotlin/io/kotest/assertions/json/keyvalues.kt#L10-L16

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:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Kantiscommented, Apr 8, 2022

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

0reactions
stale[bot]commented, Jun 12, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Language injections | IntelliJ IDEA Documentation - JetBrains
Language injections let you work with pieces of code in other languages embedded in your code. When you inject a language (such as...
Read more >
Mix and Match Languages with Language Injections in IntelliJ ...
Language injections are great for mixing in multiple languages in one project. They provide code completion, analysis, highlighting, and even a ...
Read more >
Inject a language - IntelliJ IDEA Guide - JetBrains
Language injections let you work with pieces of code in other languages embedded in your code. Injecting a language such as HTML, CSS,...
Read more >
Language injections | IntelliJ IDEA Documentation - JetBrains
Use this page to manage the list of automatic language injections and configure the language injection feature for text, attributes, and parameters ....
Read more >
Groovy Language Documentation
contextual keywords can be used for field and variable names def as = true assert as // contextual keywords can be used for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found