diff options
author | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2013-08-06 08:03:17 -0700 |
---|---|---|
committer | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2013-08-06 08:03:17 -0700 |
commit | 4c62f7db6f3913eedd92d85daf8f631149cc97b2 (patch) | |
tree | c0f8381276e4f7fc1726d1eac3a3838f5c6a95ce /test | |
parent | 46616ea2e94fa6ac7100b1cde66295f68338e18e (diff) | |
parent | d5e0f728c322a3beec5c62a9f4e28b3a3f86e84d (diff) | |
download | scala-4c62f7db6f3913eedd92d85daf8f631149cc97b2.tar.gz scala-4c62f7db6f3913eedd92d85daf8f631149cc97b2.tar.bz2 scala-4c62f7db6f3913eedd92d85daf8f631149cc97b2.zip |
Merge pull request #2776 from gkossakowski/symbolTable-refactorings
Refactor the cake so SymbolTable does not depend on Global
Diffstat (limited to 'test')
-rwxr-xr-x | test/files/presentation/doc/doc.scala | 4 | ||||
-rw-r--r-- | test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala | 89 | ||||
-rw-r--r-- | test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala | 50 |
3 files changed, 139 insertions, 4 deletions
diff --git a/test/files/presentation/doc/doc.scala b/test/files/presentation/doc/doc.scala index c884b6425b..f2233f1828 100755 --- a/test/files/presentation/doc/doc.scala +++ b/test/files/presentation/doc/doc.scala @@ -51,10 +51,6 @@ object Test extends InteractiveTest { new Typer(context) with InteractiveTyper with ScaladocTyper } - override lazy val loaders = new scala.tools.nsc.symtab.SymbolLoaders { - val global: outer.type = outer - } - def chooseLink(links: List[LinkTo]): LinkTo = links.head def internalLink(sym: Symbol, site: Symbol) = None def toString(link: LinkTo) = link.toString diff --git a/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala b/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala new file mode 100644 index 0000000000..285e87e3b2 --- /dev/null +++ b/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala @@ -0,0 +1,89 @@ +package scala.tools.nsc +package symtab + +import scala.reflect.internal.{Phase, NoPhase, SomePhase} +import scala.tools.util.PathResolver +import util.ClassPath +import io.AbstractFile + +/** + * A complete SymbolTable implementation designed to be used in JUnit tests. + * + * It enables `usejavacp` setting so classpath of JUnit runner is being used + * for symbol table's classpath. + * + * This class contains enough of logic implemented to make it possible to + * initialize definitions and inspect symbols. + */ +class SymbolTableForUnitTesting extends SymbolTable { + // Members declared in scala.reflect.api.Trees + override def newStrictTreeCopier: TreeCopier = new StrictTreeCopier + override def newLazyTreeCopier: TreeCopier = new LazyTreeCopier + trait TreeCopier extends InternalTreeCopierOps + // these should be mocks + class StrictTreeCopier extends super.StrictTreeCopier with TreeCopier + class LazyTreeCopier extends super.LazyTreeCopier with TreeCopier + + override def isCompilerUniverse: Boolean = true + def classPath = new PathResolver(settings).result + + object platform extends backend.Platform { + val symbolTable: SymbolTableForUnitTesting.this.type = SymbolTableForUnitTesting.this + lazy val loaders: SymbolTableForUnitTesting.this.loaders.type = SymbolTableForUnitTesting.this.loaders + def platformPhases: List[SubComponent] = Nil + val classPath: ClassPath[AbstractFile] = new PathResolver(settings).result + def doLoad(cls: ClassPath[AbstractFile]#ClassRep): Boolean = true + def isMaybeBoxed(sym: Symbol): Boolean = ??? + def needCompile(bin: AbstractFile, src: AbstractFile): Boolean = ??? + def externalEquals: Symbol = ??? + def updateClassPath(subst: Map[ClassPath[AbstractFile], ClassPath[AbstractFile]]): Unit = ??? + } + + object loaders extends symtab.SymbolLoaders { + val symbolTable: SymbolTableForUnitTesting.this.type = SymbolTableForUnitTesting.this + lazy val platform: symbolTable.platform.type = symbolTable.platform + def lookupMemberAtTyperPhaseIfPossible(sym: Symbol, name: Name): Symbol = + sym.info.member(name) + protected override def compileLate(srcfile: AbstractFile): Unit = + sys.error(s"We do not expect compileLate to be called in SymbolTableTest. The srcfile passed in is $srcfile") + } + + class GlobalMirror extends Roots(NoSymbol) { + val universe: SymbolTableForUnitTesting.this.type = SymbolTableForUnitTesting.this + def rootLoader: LazyType = new loaders.PackageLoader(classPath) + override def toString = "compiler mirror" + } + + lazy val rootMirror: Mirror = { + val rm = new GlobalMirror + rm.init() + rm.asInstanceOf[Mirror] + } + + def settings: Settings = { + val s = new Settings + // initialize classpath using java classpath + s.usejavacp.value = true + s + } + + // Members declared in scala.reflect.internal.Required + def picklerPhase: scala.reflect.internal.Phase = SomePhase + + // Members declared in scala.reflect.internal.SymbolTable + def currentRunId: Int = 1 + def log(msg: => AnyRef): Unit = println(msg) + def mirrorThatLoaded(sym: Symbol): Mirror = rootMirror + val phases: Seq[Phase] = List(NoPhase, SomePhase) + val phaseWithId: Array[Phase] = { + val maxId = phases.map(_.id).max + val phasesArray = Array.ofDim[Phase](maxId+1) + phases foreach { phase => + phasesArray(phase.id) = phase + } + phasesArray + } + lazy val treeInfo: scala.reflect.internal.TreeInfo{val global: SymbolTableForUnitTesting.this.type} = ??? + + phase = SomePhase +} diff --git a/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala b/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala new file mode 100644 index 0000000000..537cb93ef3 --- /dev/null +++ b/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala @@ -0,0 +1,50 @@ +package scala.tools.nsc +package symtab + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class SymbolTableTest { + private def createSymbolTable: SymbolTable = new SymbolTableForUnitTesting + + @Test + def initDefinitions = { + val symbolTable = createSymbolTable + symbolTable.definitions.init() + } + + @Test + def basicSubTypeCheck = { + val symbolTable = createSymbolTable + symbolTable.definitions.init() + val listClassTpe = symbolTable.definitions.ListClass.tpe + val seqClassTpe = symbolTable.definitions.SeqClass.tpe + assertTrue("List should be subclass of Seq", listClassTpe <:< seqClassTpe) + } + + /** + * Demonstrates how one can create symbols and type completely + * from scratch and perform sub type check. + */ + @Test + def customClassesSubTypeCheck: Unit = { + val symbolTable = createSymbolTable + import symbolTable._ + symbolTable.definitions.init() + val rootClass = symbolTable.rootMirror.RootClass + val fooSymbol = rootClass.newClassSymbol("Foo": TypeName, NoPosition, 0) + val fooType = new ClassInfoType(Nil, EmptyScope, fooSymbol) + fooSymbol.info = fooType + val barSymbol = rootClass.newClassSymbol("Bar": TypeName, NoPosition, 0) + val fooTypeRef = TypeRef(fooSymbol.owner.tpe, fooSymbol, Nil) + val barType = new ClassInfoType(List(fooTypeRef), EmptyScope, barSymbol) + barSymbol.info = barType + assertTrue("Bar should be subclass of Foo", barSymbol.tpe <:< fooSymbol.tpe) + assertFalse("Foo should be a superclass of Foo", fooSymbol.tpe <:< barSymbol.tpe) + } + +} |