Skip to content

Commit

Permalink
Test that we can resolve all the symbols in test-sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjrd committed Oct 10, 2023
1 parent b6cc48f commit f29a740
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion tasty-query/shared/src/test/scala/tastyquery/TypeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import scala.collection.mutable

import tastyquery.Constants.*
import tastyquery.Contexts.*
import tastyquery.Exceptions.*
import tastyquery.Modifiers.*
import tastyquery.Names.*
import tastyquery.Symbols.*
import tastyquery.Traversers.TreeTraverser
import tastyquery.Trees.*
import tastyquery.Types.*

import TestUtils.*
import tastyquery.Traversers.TreeTraverser

class TypeSuite extends UnrestrictedUnpicklingSuite {
extension [T](elems: List[T])
Expand Down Expand Up @@ -3042,4 +3043,63 @@ class TypeSuite extends UnrestrictedUnpicklingSuite {
)
)
}

testWithContext("all-symbol-resolutions") {
/* Test that we can resolve the `.symbol` of all the `Ident`s and `Select`s
* within the test-sources.
*/

var successCount = 0
val errorsBuilder = List.newBuilder[String]

def testSelect(tree: TermReferenceTree): Unit =
try
tree.symbol
tree.referenceType match
case tpe: NamedType if tpe.prefix != NoPrefix =>
successCount += 1
case _ =>
// too "easy"; don't count that as a success
()
catch
case ex: MemberNotFoundException =>
val displayPos =
if tree.pos.hasLineColumnInformation then s":${tree.pos.pointLine}:${tree.pos.pointColumn}"
else ""
val prefixDetails = ex.prefix match
case prefix: SingletonType => s" (${prefix.showBasic}: ${prefix.superType.showBasic})"
case prefix: Type => s" (${prefix.showBasic})"
case _ => ""

errorsBuilder += s"${tree.pos.sourceFile.path}$displayPos: ${ex.getMessage()}$prefixDetails"
end testSelect

def walkTree(tree: Tree): Unit =
new Traversers.TreeTraverser {
override def traverse(tree: Tree): Unit = tree match
case tree: TermReferenceTree =>
super.traverse(tree)
testSelect(tree)
case _ =>
super.traverse(tree)
}.traverse(tree)
end walkTree

def walk(pkg: PackageSymbol): Unit =
for sym <- pkg.declarations do
sym match
case sym: PackageSymbol => walk(sym)
case sym: ClassSymbol => sym.tree.foreach(walkTree(_))
case _ => ()
end walk

walk(ctx.findPackage("simple_trees"))

val errors = errorsBuilder.result()
if errors.nonEmpty then
fail(errors.mkString("Could not resolved the `symbol` of some trees in the test-sources:\n", "\n", "\n"))

// As of this writing, there were 788 successes
assert(clue(successCount) > 700)
}
}

0 comments on commit f29a740

Please sign in to comment.