From 7193d21a9be25bf8a705492493971f3267687098 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Mon, 5 Dec 2011 00:05:57 +0100 Subject: Test pack for various flavors of reflection. --- test/files/run/reify_anonymous.check | 1 + test/files/run/reify_anonymous.scala | 14 +++++++ test/files/run/reify_generic.check | 1 + test/files/run/reify_generic.scala | 15 +++++++ test/files/run/reify_inheritance.check | 1 + test/files/run/reify_inheritance.scala | 23 +++++++++++ test/files/run/reify_printf.check | 0 test/files/run/reify_printf.scala | 75 ++++++++++++++++++++++++++++++++++ 8 files changed, 130 insertions(+) create mode 100644 test/files/run/reify_anonymous.check create mode 100644 test/files/run/reify_anonymous.scala create mode 100644 test/files/run/reify_generic.check create mode 100644 test/files/run/reify_generic.scala create mode 100644 test/files/run/reify_inheritance.check create mode 100644 test/files/run/reify_inheritance.scala create mode 100644 test/files/run/reify_printf.check create mode 100644 test/files/run/reify_printf.scala (limited to 'test/files/run') diff --git a/test/files/run/reify_anonymous.check b/test/files/run/reify_anonymous.check new file mode 100644 index 0000000000..b8626c4cff --- /dev/null +++ b/test/files/run/reify_anonymous.check @@ -0,0 +1 @@ +4 diff --git a/test/files/run/reify_anonymous.scala b/test/files/run/reify_anonymous.scala new file mode 100644 index 0000000000..1e7f3fe856 --- /dev/null +++ b/test/files/run/reify_anonymous.scala @@ -0,0 +1,14 @@ +import scala.tools.nsc.reporters._ +import scala.tools.nsc.Settings +import reflect.runtime.Mirror.ToolBox + +object Test extends App { + val code = scala.reflect.Code.lift{ + println(new {def x = 2; def y = x * x}.y) + }; + + val reporter = new ConsoleReporter(new Settings) + val toolbox = new ToolBox(reporter) + val ttree = toolbox.typeCheck(code.tree) + toolbox.runExpr(ttree) +} diff --git a/test/files/run/reify_generic.check b/test/files/run/reify_generic.check new file mode 100644 index 0000000000..b8626c4cff --- /dev/null +++ b/test/files/run/reify_generic.check @@ -0,0 +1 @@ +4 diff --git a/test/files/run/reify_generic.scala b/test/files/run/reify_generic.scala new file mode 100644 index 0000000000..aef038b2d8 --- /dev/null +++ b/test/files/run/reify_generic.scala @@ -0,0 +1,15 @@ +import scala.tools.nsc.reporters._ +import scala.tools.nsc.Settings +import reflect.runtime.Mirror.ToolBox + +object Test extends App { + val code = scala.reflect.Code.lift{ + val product = List(1, 2, 3).head * List[Any](4, 2, 0).head.asInstanceOf[Int] + println(product) + }; + + val reporter = new ConsoleReporter(new Settings) + val toolbox = new ToolBox(reporter) + val ttree = toolbox.typeCheck(code.tree) + toolbox.runExpr(ttree) +} diff --git a/test/files/run/reify_inheritance.check b/test/files/run/reify_inheritance.check new file mode 100644 index 0000000000..25bf17fc5a --- /dev/null +++ b/test/files/run/reify_inheritance.check @@ -0,0 +1 @@ +18 \ No newline at end of file diff --git a/test/files/run/reify_inheritance.scala b/test/files/run/reify_inheritance.scala new file mode 100644 index 0000000000..2a1b5f764f --- /dev/null +++ b/test/files/run/reify_inheritance.scala @@ -0,0 +1,23 @@ +import scala.tools.nsc.reporters._ +import scala.tools.nsc.Settings +import reflect.runtime.Mirror.ToolBox + +object Test extends App { + val code = scala.reflect.Code.lift{ + class C { + def x = 2 + def y = x * x + } + + class D extends C { + override def x = 3 + } + + println(new D().y * new C().x) + }; + + val reporter = new ConsoleReporter(new Settings) + val toolbox = new ToolBox(reporter) + val ttree = toolbox.typeCheck(code.tree) + toolbox.runExpr(ttree) +} diff --git a/test/files/run/reify_printf.check b/test/files/run/reify_printf.check new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/files/run/reify_printf.scala b/test/files/run/reify_printf.scala new file mode 100644 index 0000000000..30901b98c2 --- /dev/null +++ b/test/files/run/reify_printf.scala @@ -0,0 +1,75 @@ +import java.io.{ ByteArrayOutputStream, PrintStream } +import scala.reflect.Code +import scala.reflect.mirror._ +import scala.reflect.api._ +import scala.reflect.api.Trees +import scala.reflect.internal.Types +import reflect.runtime.Mirror.ToolBox +import scala.tools.nsc.reporters._ +import scala.tools.nsc.Settings +import scala.util.matching.Regex + +object Test extends App { + val tree = tree_printf(Code.lift("hello %s").tree, Code.lift("world").tree) + + val reporter = new ConsoleReporter(new Settings) + val toolbox = new ToolBox(reporter, args mkString " ") + val ttree = toolbox.typeCheck(tree) + + val output = new ByteArrayOutputStream() + Console.setOut(new PrintStream(output)) + val evaluated = toolbox.runExpr(ttree) + + assert(output.toString() == "hello world", output.toString() +" == hello world") + + /* + macro def printf(format: String, params: Any*) : String = tree_printf(format: Tree, (params: Seq[Tree]): _*) + */ + + var i = 0 + def gensym(name: String) = { i += 1; newTermName(name + i) } + + def createTempValDef( value : Tree, tpe : Type ) : (Option[Tree],Tree) = { + val local = gensym("temp") + ( + Some( + ValDef( + Modifiers() + , local + , TypeTree().setType(tpe) + , value + ) + ) + , Ident(local) + ) + } + + def tree_printf(format: Tree, params: Tree*) = { + val Literal(Constant(s_format: String)) = format + val paramsStack = scala.collection.mutable.Stack(params: _*) + val parsed = s_format.split("(?<=%[\\w%])|(?=%[\\w%])") map { + case "%d" => createTempValDef( paramsStack.pop, classToType(classOf[Int]) ) + case "%s" => createTempValDef( paramsStack.pop, classToType(classOf[String]) ) + case "%%" => { + (None:Option[Tree], Literal(Constant("%"))) + } + case part => { + (None:Option[Tree], Literal(Constant(part))) + } + } + + val evals = for ((Some(eval), _) <- parsed if eval != None) yield (eval: Tree) + val prints = for ((_, ref) <- parsed) yield + Apply( + Select( + Select( + Ident( newTermName("scala") ) + , newTermName("Predef") + ) + , newTermName("print") + ) + , List(ref) + ): Tree + Block((evals ++ prints).toList, Literal(Constant(()))) + } +} -- cgit v1.2.3