Skip to content

Commit fd1214c

Browse files
committed
Test case for TreeInfo#defPath
I'm about to refactor that method in terms of a TreeAccumulator. Note that I've packaged the test case in `dotty.tools.dotc.ast` I believe this is the best approach for organizing unit tests: the import tax is minimized, and use of relative imports is made less fragile by avoiding creating new packages to test code. I'll reorganize the other unit tests like this if others agree.
1 parent 027abb4 commit fd1214c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
330330
def deepFold[T](z: T)(op: (T, tpd.Tree) => T) =
331331
new DeepFolder(op).apply(z, tree)
332332

333+
def find[T](pred: (tpd.Tree) => Boolean): Option[tpd.Tree] =
334+
shallowFold[Option[tpd.Tree]](None)((accum, tree) => if (pred(tree)) Some(tree) else accum)
335+
333336
def subst(from: List[Symbol], to: List[Symbol])(implicit ctx: Context): ThisTree =
334337
new TreeMapper(typeMap = new ctx.SubstSymMap(from, to)).apply(tree)
335338

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package dotty.tools.dotc
2+
package ast
3+
4+
import org.junit.Test
5+
import test.DottyTest
6+
import core.Names._
7+
import core.Types._
8+
import core.Symbols._
9+
import org.junit.Assert._
10+
11+
class TreeInfoTest extends DottyTest {
12+
13+
import tpd._
14+
15+
@Test
16+
def testDefPath = checkCompile("frontend", "class A { def bar = { val x = { val z = 0; 0} }} ") {
17+
(tree, context) =>
18+
implicit val ctx = context
19+
val xTree = tree.find(tree => tree.symbol.name == termName("x")).get
20+
val path = defPath(xTree.symbol, tree)
21+
assertEquals(List(
22+
("PackageDef", EMPTY_PACKAGE),
23+
("TypeDef", typeName("A")),
24+
("Template", termName("<local A>")),
25+
("DefDef", termName("bar")),
26+
("Block", NoSymbol.name),
27+
("ValDef", termName("x"))
28+
), path.map(x => (x.productPrefix, x.symbol.name)))
29+
}
30+
}

0 commit comments

Comments
 (0)