Skip to content

Add checkTypes to support type unit testing #5245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions compiler/test/dotty/tools/CheckTypesTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dotty.tools

import org.junit.Test
import org.junit.Assert.{ assertFalse, assertTrue, fail }

import dotc.ast.Trees._
import dotc.core.Decorators._

class CheckTypeTest extends DottyTest {
@Test
def checkTypesTest: Unit = {
val source = """
|class A
|class B extends A
""".stripMargin

val types = List(
"A",
"B",
"List[_]",
"List[Int]",
"List[AnyRef]",
"List[String]",
"List[A]",
"List[B]"
)

checkTypes(source, types: _*) {
case (List(a, b, lu, li, lr, ls, la, lb), context) =>
implicit val ctx = context

assertTrue ( b <:< a)
assertTrue (li <:< lu)
assertFalse (li <:< lr)
assertTrue (ls <:< lr)
assertTrue (lb <:< la)
assertFalse (la <:< lb)

case _ => fail
}
}

@Test
def checkTypessTest: Unit = {
val source = """
|class A
|class B extends A
""".stripMargin

val typesA = List(
"A",
"List[A]"
)

val typesB = List(
"B",
"List[B]"
)

checkTypes(source, List(typesA, typesB)) {
case (List(sups, subs), context) =>
implicit val ctx = context

(sups, subs).zipped.foreach { (sup, sub) => assertTrue(sub <:< sup) }

case _ => fail
}
}
}
29 changes: 29 additions & 0 deletions compiler/test/dotty/tools/DottyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ trait DottyTest extends ContextEscapeDetection {
run.runContext
}

def checkTypes(source: String, typeStrings: String*)(assertion: (List[Type], Context) => Unit): Unit =
checkTypes(source, List(typeStrings.toList)) { (tpess, ctx) => (tpess: @unchecked) match {
case List(tpes) => assertion(tpes, ctx)
}}

def checkTypes(source: String, typeStringss: List[List[String]])(assertion: (List[List[Type]], Context) => Unit): Unit = {
val dummyName = "x_x_x"
val vals = typeStringss.flatten.zipWithIndex.map{case (s, x)=> s"val ${dummyName}$x: $s = ???"}.mkString("\n")
val gatheredSource = s" ${source}\n object A$dummyName {$vals}"
checkCompile("frontend", gatheredSource) {
(tree, context) =>
implicit val ctx = context
val findValDef: (List[tpd.ValDef], tpd.Tree) => List[tpd.ValDef] =
(acc , tree) => { tree match {
case t: tpd.ValDef if t.name.startsWith(dummyName) => t :: acc
case _ => acc
}
}
val d = new tpd.DeepFolder[List[tpd.ValDef]](findValDef).foldOver(Nil, tree)
val tpes = d.map(_.tpe.widen).reverse
val tpess = typeStringss.foldLeft[(List[Type], List[List[Type]])]((tpes, Nil)) {
case ((rest, result), typeStrings) =>
val (prefix, suffix) = rest.splitAt(typeStrings.length)
(suffix, prefix :: result)
}._2.reverse
assertion(tpess, context)
}
}

def methType(names: String*)(paramTypes: Type*)(resultType: Type = defn.UnitType) =
MethodType(names.toList map (_.toTermName), paramTypes.toList, resultType)
}