summaryrefslogtreecommitdiff
path: root/test/files/run/macro-toplevel-companion-c.scala
blob: 0e9990315893b1e00636a3dda2535d29e12243bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import scala.tools.partest._
import java.io._

object Test extends DirectTest {
  def code = ???

  def macros_1 = """
    package test

    import scala.reflect.macros.Context
    import language.experimental.macros

    object Macros {
      def impl(c: Context) = {
        import c.universe._
        val Block(List(cdef: ClassDef), _) = reify{ class C }.tree
        val ref = c.topLevelRef(TypeName("test.C")) orElse c.introduceTopLevel("test", cdef)
        c.literalUnit
      }

      def foo = macro impl
    }
  """
  def compileMacros() = {
    val classpath = List(sys.props("partest.lib"), sys.props("partest.reflect")) mkString sys.props("path.separator")
    compileString(newCompiler("-language:experimental.macros", "-cp", classpath, "-d", testOutput.path))(macros_1)
  }

  def test_2 = """
    package test
    object C { Macros.foo }
  """
  def compileTest() = {
    val classpath = List(sys.props("partest.lib"), testOutput.path) mkString sys.props("path.separator")
    compileString(newCompiler("-cp", classpath, "-d", testOutput.path))(test_2)
  }

  def show(): Unit = {
    // redirect err to string, for logging
    val prevErr = System.err
    val baos = new ByteArrayOutputStream()
    System.setErr(new PrintStream(baos))
    log("Compiling Macros_1...")
    if (compileMacros()) {
      log("Compiling Test_2...")
      if (compileTest()) log("Success!") else log("Failed...")
    }
    println("""macroSynthetic-.*?\.scala""".r.replaceAllIn(baos.toString, "<synthetic file name>"))
    System.setErr(prevErr)
  }
}