mirror of
https://github.com/sbt/sbt.git
synced 2026-09-02 23:49:00 +02:00
Unit test for dependency extraction from macro applications
Add a unit test which checks whether we capture dependencies introduced by arguments to macros. Those dependencies are special because macros get expanded during type checking and arguments to macros are not visible during regular tree walk.
This commit is contained in:
@@ -65,6 +65,19 @@ class DependencySpecification extends Specification {
|
||||
inheritance('D) === Set('A, 'C)
|
||||
}
|
||||
|
||||
"Extracted source dependencies from macro arguments" in {
|
||||
val sourceDependencies = extractSourceDependenciesFromMacroArgument
|
||||
val memberRef = sourceDependencies.memberRef
|
||||
val inheritance = sourceDependencies.inheritance
|
||||
|
||||
memberRef('A) === Set('B, 'C)
|
||||
inheritance('A) === Set.empty
|
||||
memberRef('B) === Set.empty
|
||||
inheritance('B) === Set.empty
|
||||
memberRef('C) === Set.empty
|
||||
inheritance('C) === Set.empty
|
||||
}
|
||||
|
||||
private def extractSourceDependenciesPublic: ExtractedSourceDependencies = {
|
||||
val srcA = "class A"
|
||||
val srcB = "class B extends D[A]"
|
||||
@@ -109,4 +122,25 @@ class DependencySpecification extends Specification {
|
||||
compilerForTesting.extractDependenciesFromSrcs('A -> srcA, 'B -> srcB, 'C -> srcC, 'D -> srcD)
|
||||
sourceDependencies
|
||||
}
|
||||
|
||||
private def extractSourceDependenciesFromMacroArgument: ExtractedSourceDependencies = {
|
||||
val srcA = "class A { println(B.printTree(C.foo)) }"
|
||||
val srcB = """
|
||||
|import scala.language.experimental.macros
|
||||
|import scala.reflect.macros._
|
||||
|object B {
|
||||
| def printTree(arg: Any) = macro printTreeImpl
|
||||
| def printTreeImpl(c: Context)(arg: c.Expr[Any]): c.Expr[String] = {
|
||||
| val argStr = arg.tree.toString
|
||||
| val literalStr = c.universe.Literal(c.universe.Constant(argStr))
|
||||
| c.Expr[String](literalStr)
|
||||
| }
|
||||
|}""".stripMargin
|
||||
val srcC = "object C { val foo = 1 }"
|
||||
|
||||
val compilerForTesting = new ScalaCompilerForUnitTesting(nameHashing = true)
|
||||
val sourceDependencies =
|
||||
compilerForTesting.extractDependenciesFromSrcs(List(Map('B -> srcB, 'C -> srcC), Map('A -> srcA)))
|
||||
sourceDependencies
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user