Scala.meta expansion testing harness

Hi,

is that possible to test to what exactly code a scala.meta static annotation was expanded? Say, for data classes “a la carte” (https://github.com/fommil/scala-data-classes) investigate pieces as follows?

  • existence of companion object
  • structure and access modifiers to constructor
  • implementation of methods bodies

Good question @alexander.myltsev !

One way to test the expansion would be to implement it in static function on an object, which you can unit test by passing in custom trees

import scala.meta._
object Macros {
  def myMacro(tree: Tree): Tree = q"expanded.tree"
}
// unit tests
def testMacro = Macros.myMacro(q"class A").isEqual(q"expanded.tree")
// macro annotation
class MyMacroAnnotation extends StaticAnnotation {
  inline def apply(x: Any) = meta {
    x match {
      case t: Tree => Macros.myMacro(t)
      case _ => sys.error(s"$x is unsupported")
    }
  }
}

However, since scalameta/paradise is in milestone phase, note that the tree passed into inline def may not always match 100% with the same tree created with quasiquotes. For example, for comprehensions are not yet “resugared” by the scalahost converter. If you hit on such cases, please report an issue since the trees should be identical.

Scala.meta trees are not path dependant (unlike scala-reflect) so setting up a test fixture typically boils down to import scala.meta._ .