Skip to content

Commit 0c555cc

Browse files
committed
Add tests for Dottydoc from tasty
1 parent 28023b4 commit 0c555cc

File tree

12 files changed

+149
-53
lines changed

12 files changed

+149
-53
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dotty.tools
2+
3+
import java.nio.file.{FileSystems, FileVisitOption, FileVisitResult, FileVisitor, Files, Path}
4+
import java.nio.file.attribute.BasicFileAttributes
5+
import java.util.EnumSet
6+
7+
import scala.collection.JavaConverters.asScalaIteratorConverter
8+
9+
object IOUtils {
10+
11+
def inTempDirectory[T](fn: Path => T): T = {
12+
val temp = Files.createTempDirectory("temp")
13+
try fn(temp)
14+
finally {
15+
val allFiles = getAll(temp, "glob:**").sortBy(_.toAbsolutePath.toString).reverse
16+
allFiles.foreach(Files.delete(_))
17+
}
18+
}
19+
20+
def getAll(base: Path, pattern: String): List[Path] = {
21+
val paths = Files.walk(base)
22+
val matcher = FileSystems.getDefault.getPathMatcher(pattern)
23+
try paths.filter(matcher.matches).iterator().asScala.toList
24+
finally paths.close()
25+
}
26+
27+
}

compiler/test/dotty/tools/dotc/core/tasty/CommentPicklingTest.scala

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@ import dotty.tools.dotc.core.Mode
1010
import dotty.tools.dotc.core.Names.Name
1111
import dotty.tools.dotc.interfaces.Diagnostic.ERROR
1212
import dotty.tools.dotc.reporting.TestReporter
13+
import dotty.tools.IOUtils
1314

14-
import dotty.tools.vulpix.{TestConfiguration, ParallelTesting}
15+
import dotty.tools.vulpix.TestConfiguration
1516

16-
import java.io.IOException
17-
import java.nio.file.{FileSystems, FileVisitOption, FileVisitResult, FileVisitor, Files, Path}
18-
import java.nio.file.attribute.BasicFileAttributes
19-
import java.util.EnumSet
20-
21-
import scala.collection.JavaConverters.asScalaIteratorConverter
22-
import scala.concurrent.duration.{Duration, DurationInt}
17+
import java.nio.file.{Files, Path}
2318

2419
import org.junit.Test
2520
import org.junit.Assert.{assertEquals, assertFalse, fail}
@@ -86,7 +81,7 @@ class CommentPicklingTest {
8681
}
8782

8883
private def compileAndUnpickle[T](sources: List[String])(fn: (List[tpd.Tree], Context) => T) = {
89-
inTempDirectory { tmp =>
84+
IOUtils.inTempDirectory { tmp =>
9085
val sourceFiles = sources.zipWithIndex.map {
9186
case (src, id) =>
9287
val path = tmp.resolve(s"Src$id.scala").toAbsolutePath
@@ -102,7 +97,7 @@ class CommentPicklingTest {
10297
Main.process(options.all, reporter)
10398
assertFalse("Compilation failed.", reporter.hasErrors)
10499

105-
val tastyFiles = getAll(tmp, "glob:**.tasty")
100+
val tastyFiles = IOUtils.getAll(tmp, "glob:**.tasty")
106101
val unpicklingOptions = unpickleOptions
107102
.withClasspath(out.toAbsolutePath.toString)
108103
.and("dummy") // Need to pass a dummy source file name
@@ -126,20 +121,4 @@ class CommentPicklingTest {
126121
}
127122
}
128123

129-
private def inTempDirectory[T](fn: Path => T): T = {
130-
val temp = Files.createTempDirectory("temp")
131-
try fn(temp)
132-
finally {
133-
val allFiles = getAll(temp, "glob:**").sortBy(_.toAbsolutePath.toString).reverse
134-
allFiles.foreach(Files.delete(_))
135-
}
136-
}
137-
138-
private def getAll(base: Path, pattern: String): List[Path] = {
139-
val paths = Files.walk(base)
140-
val matcher = FileSystems.getDefault.getPathMatcher(pattern)
141-
try paths.filter(matcher.matches).iterator().asScala.toList
142-
finally paths.close()
143-
}
144-
145124
}

doc-tool/test/ConstructorTest.scala

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import model._
99
import model.internal._
1010
import model.references._
1111

12-
class Constructors extends DottyDocTest {
12+
class ConstructorsFromSourceTest extends ConstructorsBase with CheckFromSource
13+
class ConstructorsFromTastyTest extends ConstructorsBase with CheckFromTasty
14+
15+
abstract class ConstructorsBase extends DottyDocTest {
1316
@Test def singleClassConstructor = {
1417
val source = new SourceFile (
1518
"Class.scala",
@@ -20,7 +23,9 @@ class Constructors extends DottyDocTest {
2023
""".stripMargin
2124
)
2225

23-
checkSources(source :: Nil) { packages =>
26+
val className = "scala.Class"
27+
28+
check(className :: Nil, source :: Nil) { packages =>
2429
packages("scala") match {
2530
case PackageImpl(_, _, _, List(cls: Class), _, _, _, _) =>
2631
cls.constructors.headOption match {
@@ -42,7 +47,9 @@ class Constructors extends DottyDocTest {
4247
""".stripMargin
4348
)
4449

45-
checkSources(source :: Nil) { packages =>
50+
val className = "scala.Class"
51+
52+
check(className :: Nil, source :: Nil) { packages =>
4653
packages("scala") match {
4754
case PackageImpl(_, _, _, List(cls: Class), _, _, _, _) =>
4855
cls.constructors match {
@@ -67,7 +74,9 @@ class Constructors extends DottyDocTest {
6774
""".stripMargin
6875
)
6976

70-
checkSources(source :: Nil) { packages =>
77+
val className = "scala.Class"
78+
79+
check(className :: Nil, source :: Nil) { packages =>
7180
packages("scala") match {
7281
case PackageImpl(_, _, _, List(cls: Class), _, _, _, _) =>
7382
cls.constructors match {
@@ -99,7 +108,9 @@ class Constructors extends DottyDocTest {
99108
""".stripMargin
100109
)
101110

102-
checkSources(source :: Nil) { packages =>
111+
val className = "scala.Class"
112+
113+
check(className :: Nil, source :: Nil) { packages =>
103114
packages("scala") match {
104115
case PackageImpl(_, _, _, List(cls: Class), _, _, _, _) =>
105116
cls.constructors match {
@@ -137,7 +148,9 @@ class Constructors extends DottyDocTest {
137148
""".stripMargin
138149
)
139150

140-
checkSources(source :: Nil) { packages =>
151+
val className = "scala.Class"
152+
153+
check(className :: Nil, source :: Nil) { packages =>
141154
packages("scala") match {
142155
case PackageImpl(_, _, _, List(cls: CaseClass, obj: Object), _, _, _, _) =>
143156
cls.constructors match {
@@ -170,7 +183,9 @@ class Constructors extends DottyDocTest {
170183
""".stripMargin
171184
)
172185

173-
checkSources(source :: Nil) { packages =>
186+
val className = "scala.Trait"
187+
188+
check(className :: Nil, source :: Nil) { packages =>
174189
packages("scala") match {
175190
case PackageImpl(_, _, _, List(trt: Trait), _, _, _, _) =>
176191
trt.traitParams match {

doc-tool/test/DottyDocTest.scala

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package dottydoc
33

44
import vulpix.TestConfiguration
55

6+
import dotc.Compiler
67
import dotc.core.Contexts.{ Context, ContextBase, FreshContext }
78
import dotc.core.Comments.{ ContextDoc, ContextDocstrings }
89
import dotc.util.SourceFile
910
import dotc.core.Phases.Phase
11+
import dotty.tools.io.AbstractFile
1012
import dotc.typer.FrontEnd
1113
import dottydoc.core.{ DocASTPhase, ContextDottydoc }
1214
import model.Package
@@ -16,11 +18,12 @@ import dotc.interfaces.Diagnostic.ERROR
1618
import org.junit.Assert.fail
1719

1820
import java.io.{ BufferedWriter, OutputStreamWriter }
21+
import java.nio.file.Files
1922

2023
trait DottyDocTest extends MessageRendering {
2124
dotty.tools.dotc.parsing.Scanners // initialize keywords
2225

23-
implicit val ctx: FreshContext = {
26+
private def freshCtx(extraClasspath: List[String]): FreshContext = {
2427
val base = new ContextBase
2528
import base.settings._
2629
val ctx = base.initialCtx.fresh
@@ -32,12 +35,13 @@ trait DottyDocTest extends MessageRendering {
3235
ctx.setProperty(ContextDoc, new ContextDottydoc)
3336
ctx.setSetting(
3437
ctx.settings.classpath,
35-
TestConfiguration.basicClasspath
38+
(TestConfiguration.basicClasspath :: extraClasspath).mkString(java.io.File.pathSeparator)
3639
)
3740
ctx.setReporter(new StoreReporter(ctx.reporter))
3841
base.initialize()(ctx)
3942
ctx
4043
}
44+
implicit val ctx: FreshContext = freshCtx(Nil)
4145

4246
private def compilerWithChecker(assertion: Map[String, Package] => Unit) = new DocCompiler {
4347
private[this] val assertionPhase: List[List[Phase]] =
@@ -93,9 +97,50 @@ trait DottyDocTest extends MessageRendering {
9397
run.compile(sources)
9498
}
9599

96-
def checkSources(sourceFiles: List[SourceFile])(assertion: Map[String, Package] => Unit): Unit = {
100+
def checkFromSource(sourceFiles: List[SourceFile])(assertion: Map[String, Package] => Unit): Unit = {
97101
val c = compilerWithChecker(assertion)
98102
val run = c.newRun
99103
run.compileSources(sourceFiles)
100104
}
105+
106+
def checkFromTasty(classNames: List[String], sources: List[SourceFile])(assertion: Map[String, Package] => Unit): Unit = {
107+
IOUtils.inTempDirectory { tmp =>
108+
val ctx = "shadow ctx"
109+
val out = tmp.resolve("out")
110+
Files.createDirectories(out)
111+
112+
val dotcCtx = {
113+
val ctx = freshCtx(out.toString :: Nil)
114+
ctx.setSetting(ctx.settings.outputDir, AbstractFile.getDirectory(out))
115+
}
116+
val dotc = new Compiler
117+
val run = dotc.newRun(dotcCtx)
118+
run.compileSources(sources)
119+
assert(!dotcCtx.reporter.hasErrors)
120+
121+
val fromTastyCtx = {
122+
val ctx = freshCtx(out.toString :: Nil)
123+
ctx.setSetting(ctx.settings.fromTasty, true)
124+
}
125+
val fromTastyCompiler = compilerWithChecker(assertion)
126+
val fromTastyRun = fromTastyCompiler.newRun(fromTastyCtx)
127+
fromTastyRun.compile(classNames)
128+
assert(!fromTastyCtx.reporter.hasErrors)
129+
}
130+
}
131+
132+
def check(classNames: List[String], sources: List[SourceFile])(assertion: Map[String, Package] => Unit): Unit
133+
134+
}
135+
136+
trait CheckFromSource extends DottyDocTest {
137+
override def check(classNames: List[String], sources: List[SourceFile])(assertion: Map[String, Package] => Unit): Unit = {
138+
checkFromSource(sources)(assertion)
139+
}
140+
}
141+
142+
trait CheckFromTasty extends DottyDocTest {
143+
override def check(classNames: List[String], sources: List[SourceFile])(assertion: Map[String, Package] => Unit): Unit = {
144+
checkFromTasty(classNames, sources)(assertion)
145+
}
101146
}

doc-tool/test/MarkdownTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import dotc.core.Contexts.{ Context, ContextBase, FreshContext }
1010
import dotc.core.Comments.{ ContextDoc, ContextDocstrings }
1111
import dottydoc.core.ContextDottydoc
1212

13-
class MarkdownTests extends DottyDocTest {
13+
class MarkdownTests extends DottyDocTest with CheckFromSource {
1414
override implicit val ctx: FreshContext = {
1515
// TODO: check if can reuse parent instead of copy-paste
1616
val base = new ContextBase

doc-tool/test/PackageStructure.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import org.junit.Assert._
77
import dotc.util.SourceFile
88
import model.internal._
99

10-
class PackageStructure extends DottyDocTest {
10+
class PackageStructureFromSourceTest extends PackageStructureBase with CheckFromSource
11+
class PackageStructureFromTastyTest extends PackageStructureBase with CheckFromTasty
12+
13+
abstract class PackageStructureBase extends DottyDocTest {
1114
@Test def multipleCompilationUnits = {
1215
val source1 = new SourceFile(
1316
"TraitA.scala",
@@ -27,7 +30,9 @@ class PackageStructure extends DottyDocTest {
2730
""".stripMargin
2831
)
2932

30-
checkSources(source1 :: source2 :: Nil) { packages =>
33+
val classNames = "scala.A" :: "scala.B" :: Nil
34+
35+
check(classNames, source1 :: source2 :: Nil) { packages =>
3136
packages("scala") match {
3237
case PackageImpl(_, _, _, List(tA, tB), _, _, _, _) =>
3338
assert(
@@ -59,7 +64,9 @@ class PackageStructure extends DottyDocTest {
5964
|trait B
6065
""".stripMargin)
6166

62-
checkSources(source1 :: source2 :: Nil) { packages =>
67+
val classNames = "scala.collection.A" :: "scala.collection.B" :: Nil
68+
69+
check(classNames, source1 :: source2 :: Nil) { packages =>
6370
packages("scala.collection") match {
6471
case PackageImpl(_, _, "scala.collection", List(tA, tB), _, _, _, _) =>
6572
assert(

doc-tool/test/SimpleComments.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package dotty.tools
22
package dottydoc
33

44
import model.internal._
5+
import dotc.util.SourceFile
56

67
import org.junit.Test
78
import org.junit.Assert._
89

9-
class TestSimpleComments extends DottyDocTest {
10+
class SimpleCommentsFromSourceTest extends SimpleCommentsBase with CheckFromSource
11+
class SimpleCommentsFromTastyTest extends SimpleCommentsBase with CheckFromTasty
12+
13+
abstract class SimpleCommentsBase extends DottyDocTest {
1014

1115
@Test def cookCommentEmptyClass = {
1216
val source =
@@ -31,15 +35,19 @@ class TestSimpleComments extends DottyDocTest {
3135
}
3236

3337
@Test def simpleComment = {
34-
val source =
38+
val source = new SourceFile(
39+
"HelloWorld.scala",
3540
"""
3641
|package scala
3742
|
3843
|/** Hello, world! */
3944
|trait HelloWorld
4045
""".stripMargin
46+
)
4147

42-
checkSource(source) { packages =>
48+
val className = "scala.HelloWorld"
49+
50+
check(className :: Nil, source :: Nil) { packages =>
4351
val traitCmt =
4452
packages("scala")
4553
.children.find(_.path.mkString(".") == "scala.HelloWorld")

doc-tool/test/TemplateErrorTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package staticsite
55
import org.junit.Test
66
import org.junit.Assert._
77

8-
class TemplateErrorTests extends DottyDocTest with SourceFileOps {
8+
class TemplateErrorTests extends DottyDocTest with SourceFileOps with CheckFromSource {
99
@Test def unclosedTag: Unit = {
1010
htmlPage(
1111
"""|Yo dawg:

0 commit comments

Comments
 (0)