Testing Compilation of Generated Code and Possible Usages

12 views
Skip to first unread message

Joseph Whiting

unread,
Apr 9, 2018, 7:30:45 AM4/9/18
to treehugger-scala
Nice library, and the docs are great! Is it still active? If so I'd really appreciate some advice on how to test my usage of it.

I'm interested in using this library to generate DSLs. I'd like to write unit tests for the code generator to check that:
1) The generated code compiles, since as I understand it I could generate invalid code using treehugger
2) Certain usages of the generated code compile, and others don't - ie. testing that the DSLs work how I expect them to and provide the type safety I want

Scalatest provides assertCompiles which is a macro that can be applied to string literals, is there something similar I could use with treehugger's resultant AST (or even better the generated source code string)? I can't quite seem to find what I'm looking for, wondering if I might be barking up the wrong tree entirely.

Any help, tips or general pointers are very much appreciated

Joseph Whiting

eugene yokota

unread,
Apr 11, 2018, 4:30:46 AM4/11/18
to treehugg...@googlegroups.com
I think it depends on what you want to test.
treehugger can generate String, so you can pass that along to assertCompiles if that's what you want.
You can also test the behavior of the code by actually running it as part of integration test.

-eugene


Joseph Whiting

--
You received this message because you are subscribed to the Google Groups "treehugger-scala" group.
To unsubscribe from this group and stop receiving emails from it, send an email to treehugger-scala+unsubscribe@googlegroups.com.
To post to this group, send email to treehugger-scala@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/treehugger-scala/851e0855-729d-41f4-b3ae-50cfb5988adf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joseph Whiting

unread,
Apr 11, 2018, 6:41:33 AM4/11/18
to treehugger-scala
Thanks for the advice Eugene. I think the integration tests using scripted like you suggest will be really good for the future, although I think I also want something a little lower level. Particularly as there's a few things that could go wrong with the plugin implementation bit.
The problem with assertCompiles is that treehugger generates that string at runtime and because assertCompiles is implemented as a macro that won't work. Luckily I have found a way that works using the reflection library, I've written it up in a bit more detail here.

Thanks again for your kind help,
Joseph


On Wednesday, 11 April 2018 09:30:46 UTC+1, Eugene Yokota wrote:
I think it depends on what you want to test.
treehugger can generate String, so you can pass that along to assertCompiles if that's what you want.
You can also test the behavior of the code by actually running it as part of integration test.

-eugene
On Mon, Apr 9, 2018 at 7:30 AM, Joseph Whiting <whiti...@gmail.com> wrote:
Nice library, and the docs are great! Is it still active? If so I'd really appreciate some advice on how to test my usage of it.

I'm interested in using this library to generate DSLs. I'd like to write unit tests for the code generator to check that:
1) The generated code compiles, since as I understand it I could generate invalid code using treehugger
2) Certain usages of the generated code compile, and others don't - ie. testing that the DSLs work how I expect them to and provide the type safety I want

Scalatest provides assertCompiles which is a macro that can be applied to string literals, is there something similar I could use with treehugger's resultant AST (or even better the generated source code string)? I can't quite seem to find what I'm looking for, wondering if I might be barking up the wrong tree entirely.

Any help, tips or general pointers are very much appreciated

Joseph Whiting

--
You received this message because you are subscribed to the Google Groups "treehugger-scala" group.
To unsubscribe from this group and stop receiving emails from it, send an email to treehugger-sca...@googlegroups.com.
To post to this group, send email to treehugg...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages