kotlinx / ast

Generic AST parsing library for kotlin multiplatform
Apache License 2.0
316 stars 22 forks source link

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

Closed CamilYed closed 2 years ago

CamilYed commented 2 years ago

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?

drieks commented 2 years ago

Hi @CamilYed,

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

CamilYed commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Hi, thank you for clarifying that.