-
Notifications
You must be signed in to change notification settings - Fork 22
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
Best way to add new keywords like in the Spock test #72
Comments
Hi @CamilYed, can you provide a short code example? I'm not sure if I understand your question correctly. |
fun `some test`() {
Given:
repository.add(Order(id = 1))
When:
val order= repository.find(orderId = 1)
Then:
order != null
} Like in Spock framework Spock makes some AST modifications. |
Hi, if you only want to write tests with given/when/then in kotlin, you can use kotest BehaviorSpec. If you really want to change the ast, you should try to implement a kotlin compiler plugin. I think it is not possible to add a new syntax, because the ast is parsed first and all compiler plugins are invoked after this. I think it is possible to change existing syntax like this:
Here I have used "when" with grave accent because "when" is a keyword in kotlin and the ast created for the "when" part will be different compared to the ast created for the "given"/"then" parts. You also have to keep in mind that many developers are puzzled by an unexpected syntax, because without background knowledge you don't understand it directly. If you really want to try this, you should have a look at https://github.com/google/ksp. This library here (kotlin-ast) is there to parse the code outline of a kotlin file. The code to parse the body of a function is currently not implemented, because I have no time for this. Therefore, it is not possible to parse your example code. Even when the missing code will be implemented, you can only parse the code, you will get a data class that you can analyse at runtime for a given kotlin file. Currently, there is also no code to transform this data class nor to convert it back into to kotlin file. Please let me know if you have any questions. |
Hi, |
Hi I would like to add some additional keyword (labels) knows from Spock library: Given, When, Then, Expect, And.
These labels would be enabled only when junit test class is annotated with my custom annotation for instance
@BddScenario
.I think that currently is not possible or maybe I am wrong?
The text was updated successfully, but these errors were encountered: