MacroGroovy

Hi!

I would like to donate my MacroGroovy project to groovy-core. Since it shares code with AstBuilderTransformation, I decided to refactor existing code of it because I don't want to make duplicates. 


It's still in "work in progress"  state and i'm expecting comments and suggestions from you.
You're right that assertSyntaxTree is very limited, and that is precisely
why ASTMatcher is being built: it does the job of comparing AST nodes. Take
a look at the ASTMatcher test case here:
https://github.com/melix/groovy-core/blob/4f46deb36f29c69ac62ee333efdf9a835828aab9/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy#L37-37

2014-11-14 12:14 GMT+01:00 Marcin Grzejszczak <notifications@github.com>:

> In subprojects/groovy-macro/src/test/groovy/groovy/SimpleMacroTest.groovy:
>
> > +    }
> > +
> > +    public void testAsIsWithCompilePhase() {
> > +        def expected = new BlockStatement([
> > +                new ReturnStatement(new MethodCallExpression(THIS_EXPRESSION, "println", new ArgumentListExpression(new ConstantExpression("foo"))))
> > +        ] as List<Statement>, new VariableScope());
> > +
> > +        def result = macro(CompilePhase.FINALIZATION, true) {
> > +            println "foo"
> > +        }
> > +
> > +        assertSyntaxTree(expected, result);
> > +    }
> > +
> > +    protected void assertSyntaxTree(Object expected, Object result) {
> > +        AstAssert.assertSyntaxTree([expected], [result])
>
> Does this assertion really work? I mean does it check anything else than
> equality between types (e.g. ClassNode == ClassNode) and that the
> superclasses are ok? I thought that it would recursively check whether two
> ClassNodes are logically the same?
>
> I was trying to write a test to check the MacroClass in terms of creating
> methods of different visibility:
>
>  def result = new MacroClass() {
>             class A {
>                 private void privateMethod(){}
>                 protected String protectedMethod(){return "OK"}
>                 public long publicMethod(long arg){return arg + 1L}
>                 @PackageScope void packageScopeMethod() {}
>             }
>         }
>
> this doesn't compile due to:
>
> startup failed:
> /tmp/g/subprojects/groovy-macro/src/test/groovy/groovy/SimpleMacroTest.groovy: 60: [Static type checking] - Cannot return value of type java.lang.String on method returning type String
>  @ line 60, column 59.
>    ring protectedMethod(){return "OK"}
>                                  ^
>
> /tmp/g/subprojects/groovy-macro/src/test/groovy/groovy/SimpleMacroTest.groovy: 61: [Static type checking] - Cannot find matching method java.lang.Object#plus(long). Please check if the declared type is right and if the method exists.
>  @ line 61, column 63.
>    icMethod(long arg){return arg + 1L}
>                                  ^
>
> so I reverted that cause I wanted to check whether simple stuff can be
> done:
>
> def result = new MacroClass() {
>             class A {
>                 private void privateMethod(){}
>                 protected String protectedMethod(){return null}
>                 public long publicMethod(long arg){return 1L}
>                 @PackageScope void packageScopeMethod() {}
>             }
>         }
>
> now that worked. Then I created manually the statments that should form
> the expected ClassNode. TO my surprise it worked at a first time! Of course
> I knew that it's impossible for me not to have made a mistake ;) So I tried
> to check how does this assertion work and I wrote sth like this:
>
> assertSyntaxTree(new ClassNode(CodeVisitorSupport), result);
>
> Which of course doesn't make any sense (or it least it shouldn't) but the
> assertion passed! Both are ClassNodes and don't have supertypes.
>
> Maybe I've done sth wrong and shouldn't have written it here so sorry for
> my potential stupidity ;)
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/groovy/groovy-core/pull/470/files#r20353455>.
>