Giter VIP home page Giter VIP logo

Comments (4)

drieks avatar drieks commented on August 21, 2024

Hi @CamilYed,

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

from ast.

CamilYed avatar CamilYed commented on August 21, 2024
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.

from ast.

drieks avatar drieks commented on August 21, 2024

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.

from ast.

CamilYed avatar CamilYed commented on August 21, 2024

Hi,
thank you for clarifying that.

from ast.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.