Skip to content

Commit

Permalink
Add tests for scalamacros#2
Browse files Browse the repository at this point in the history
  • Loading branch information
stanch committed Sep 4, 2013
1 parent 64e3b25 commit 1365258
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/src/main/scala/shove.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import scala.reflect.macros.Context
import scala.language.experimental.macros
import scala.annotation.StaticAnnotation

object shoveMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
import Flag._
val Apply(Select(New(AppliedTypeTree(_, List(victim))), _), _) = c.prefix.tree
val result = {
annottees.map(_.tree).toList match {
case ValDef(mods, name, tpt, rhs) :: Nil =>
ClassDef(
Modifiers(IMPLICIT),
newTypeName(s"${victim}With$name"),
List(),
Template(
List(Select(Ident(newTermName("scala")), newTypeName("AnyRef"))),
emptyValDef,
List(
ValDef(Modifiers(PRIVATE | LOCAL), newTermName("x"), victim, EmptyTree),
DefDef(
Modifiers(),
nme.CONSTRUCTOR,
List(),
List(List(ValDef(Modifiers(PARAM), newTermName("x"), victim, EmptyTree))),
TypeTree(),
Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), c.literalUnit.tree)
),
ValDef(mods, name, tpt, rhs)
)
)
)
}
}
c.Expr[Any](result)
}
}

class shove[A] extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro shoveMacro.impl
}

package pkg {
class shove extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro shoveMacro.impl
}
}
11 changes: 11 additions & 0 deletions tests/src/test/scala/annotations/run/TypeArgs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import org.scalatest.FunSuite
import scala.reflect.runtime.universe._

class TypeArgs extends FunSuite {
test("macro annotations with type args expand") {
@shove[Int] val description = "I’m an Int!"
@shove[String] val bar = "I’m a String!"
assert(5.description === "I’m an Int!")
assert("foo".bar === "I’m a String!")
}
}

0 comments on commit 1365258

Please sign in to comment.