groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mariogarcia <...@git.apache.org>
Subject [GitHub] groovy pull request #439: WIP Add groovy-macro docs
Date Sun, 09 Oct 2016 14:48:38 GMT
Github user mariogarcia commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/439#discussion_r82523893
  
    --- Diff: src/spec/doc/core-metaprogramming.adoc ---
    @@ -2868,6 +3015,90 @@ The difference is that when you use `assertScript`, the code in
the `assertScrip
     unit test is executed*. That is to say that this time, the `Subject` class will be compiled
with debugging active, and
     the breakpoint is going to be hit.
     
    +===== ASTMatcher
    +
    +Sometimes you may want to make assertions over AST nodes whether to filter those nodes,
or to make sure a given
    +transformation has built the expected AST node.
    +
    +**Filtering nodes**
    +
    +For instance if you would like to apply a given transformation only to a specific set
of AST nodes, you could
    +use **ASTMatcher** to filter these nodes. The following example shows how to transform
a given expression to
    +another. Using **ASTMatcher** it looks for a specific expression `1 + 1` and it transforms
it to `3`. That's why
    +I called it the `@Joking` example.
    +
    +First we create the `@Joking` annotation that only can be applied to methods:
    +
    +[source,groovy]
    +----
    +include::{projectdir}/src/spec/test/metaprogramming/ASTMatcherFilteringTest.groovy[tags=jokingannotation,indent=0]
    +----
    +
    +Then the transformation, that only applies an instance of `org.codehaus.groovy.ast.ClassCodeExpressionTransformer`
    +to all the expressions within the method code block.
    +
    +[source,groovy]
    +----
    +include::{projectdir}/src/spec/test/metaprogramming/ASTMatcherFilteringTest.groovy[tags=jokingtransformation,indent=0]
    +----
    +<1> Get the method's code statement and apply the expression transformer
    +
    +And then is when the **ASTMatcher** is used to apply the transformation only to those
expressions matching
    +the expression `1 + 1`.
    +
    +[source,groovy]
    +----
    +include::{projectdir}/src/spec/test/metaprogramming/ASTMatcherFilteringTest.groovy[tags=jokingexpressiontransformer,indent=0]
    +----
    +<1> Builds the expression used as reference pattern
    +<2> Checks the current expression evaluated matches the reference expression
    +<3> If it matches then replaces the current expression with the expression built
with `macro`
    +
    +Then you could test the implementation as follows:
    +
    +[source,groovy]
    +----
    +include::{projectdir}/src/spec/test/metaprogramming/ASTMatcherFilteringTest.groovy[tags=jokingexample,indent=0]
    +----
    +
    +**Unit testing AST transforms**
    +
    +Normally we test AST transformations just checking that the final use of the transformation
does what we expect. But
    +it would be great if we could have an easy way to check, for example, that the nodes
the transformation adds are what
    +we expected from the beginning.
    +
    +The following transformation adds a new method `giveMeTwo` to an annotated class.
    +
    +[source,groovy]
    +----
    +include::{projectdir}/src/spec/test/metaprogramming/ASTMatcherTestingTest.groovy[tags=twiceasttransformation,indent=0]
    +----
    +<1> Adding the method to the annotated class
    +<2> Building a binary expression. The binary expression uses the same variable
expression in both
    +sides of the `+` token (check `varX` method at **org.codehaus.groovy.ast.tool.GeneralUtils**).
    +<3> Builds a new **ClassNode** with a method called `giveMeTwo` which returns the
result of an expression
    +passed as parameter.
    +
    +Now instead of creating a test executing the transformation over a given sample code.
I would like to check that
    +the construction of the binary expression is donde properly:
    --- End diff --
    
    Changed!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message