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

Best way to add new keywords like in the Spock test #72

Closed
CamilYed opened this issue Apr 6, 2022 · 4 comments
Closed

Best way to add new keywords like in the Spock test #72

CamilYed opened this issue Apr 6, 2022 · 4 comments

Comments

@CamilYed
Copy link

CamilYed commented Apr 6, 2022

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?

@CamilYed CamilYed changed the title Best way to add new keywords like on Spock test Best way to add new keywords like in the Spock test Apr 6, 2022
@drieks
Copy link
Collaborator

drieks commented Apr 7, 2022

Hi @CamilYed,

can you provide a short code example? I'm not sure if I understand your question correctly.

@CamilYed
Copy link
Author

fun `some test`() {
   Given:
     repository.add(Order(id = 1))
   When: 
      val order= repository.find(orderId = 1)
   Then:
      order != null
}

Like in Spock framework
https://github.com/CamilYed/readable-tests-by-example/blob/master/src/integrationTest/groovy/tech/allegro/blog/vinyl/shop/order/OrderPaymentAcceptanceSpec.groovy

Spock makes some AST modifications.

@drieks
Copy link
Collaborator

drieks commented Apr 15, 2022

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:

fun main() {
  given@ {
      // ...
  }
  `when`@ {
      // ...
  }
  then@ {
      // ...
  }
}

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.

@CamilYed
Copy link
Author

Hi,
thank you for clarifying that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants