Skip to content

Commit ea2848d

Browse files
committed
Scala3doc: add cooking tests
1 parent 60a8b72 commit ea2848d

File tree

6 files changed

+92
-37
lines changed

6 files changed

+92
-37
lines changed

scala3doc-testcases/src/tests/tests.scala

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ class A {
7373
*/
7474
final def myMethod(s: String): String = s
7575

76-
/** This is a method.
77-
*
78-
* This is foo: $foo
79-
*/
76+
/** This is foo: $foo */
8077
def method(s: String): String = s
8178

8279
class AA
@@ -121,10 +118,7 @@ object A
121118
class B extends A {
122119
/** @inheritdoc */ override def method(s: String): String = s
123120

124-
/** This is a method.
125-
*
126-
* And this is my foo: $foo
127-
*/
121+
/** This is my foo: $foo */
128122
def otherMethod(s: String): String = s
129123

130124
class BB
@@ -145,7 +139,8 @@ object B {
145139
val Z: Int = 0
146140
}
147141

148-
class C {
142+
/** This is foo: $foo */
143+
class C extends A {
149144
object CC
150145
class CC
151146
}
@@ -215,6 +210,5 @@ class Methods:
215210
*/
216211
object O:
217212

218-
/** This is foo: $foo
219-
*/
213+
/** This is foo: $foo */
220214
def method(s: String) = s

scala3doc/src/dotty/dokka/tasty/ScalaDocSupport.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ trait ScaladocSupport { self: TastyParser =>
2222

2323
// println(s"Expanding comment for sym: ${tree.symbol.show}")
2424
val sym = tree.symbol.asInstanceOf[dotc.core.Symbols.Symbol]
25-
val owner =
26-
if tree.symbol.isClassDef then sym else sym.owner
2725

28-
comments.CommentExpander.cookComment(sym, owner)(using ctx)
26+
comments.CommentExpander.cookComment(sym)(using ctx)
2927
.get.asInstanceOf[Documentation]
3028
else
3129
commentPre

scala3doc/src/dotty/dokka/tasty/comments/CommentExpander.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ import reporting.ProperDefinitionNotFound
344344
}
345345

346346
object CommentExpander {
347+
348+
// TODO: handle package / non-class top-level definitions (possibly just return their comments?)
349+
def cookComment(sym: Symbol)(using Context): Option[Comment] =
350+
val owner = if sym.isClass then sym else sym.owner
351+
cookComment(sym, owner)
352+
347353
def cookComment(sym: Symbol, owner: Symbol)(using Context): Option[Comment] =
348354
ctx.docCtx.flatMap { docCtx =>
349355
expand(sym, owner)(using ctx)(using docCtx)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dotty.dokka.tasty.comments
2+
3+
import scala.quoted._
4+
5+
import org.junit.{Test, Rule}
6+
import org.junit.Assert.{assertSame, assertTrue, assertEquals}
7+
import dotty.dokka.tasty.util._
8+
import dotty.dokka.tasty.TastyParser
9+
10+
class CommentExpanderTests {
11+
def check(using quoted.Quotes)(): Unit =
12+
assertCommentEquals(
13+
qr.Symbol.requiredClass("tests.B").method("otherMethod").head,
14+
"/** This is my foo: Bar, actually. */",
15+
)
16+
assertCommentEquals(
17+
qr.Symbol.requiredClass("tests.C"),
18+
"/** This is foo: Foo expanded. */",
19+
)
20+
assertCommentEquals(
21+
qr.Symbol.requiredModule("tests.O").method("method").head,
22+
"/** This is foo: O's foo. */",
23+
)
24+
25+
26+
def assertCommentEquals(
27+
using quoted.Quotes
28+
)(
29+
rsym: quotes.reflect.Symbol,
30+
str: String
31+
): Unit =
32+
import dotty.tools.dotc
33+
given ctx as dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
34+
val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
35+
val comment = CommentExpander.cookComment(sym).get
36+
assertEquals(comment.expanded.get, str)
37+
38+
@Test
39+
def test(): Unit = {
40+
import scala.tasty.inspector.TastyInspector
41+
class Inspector extends TastyInspector:
42+
43+
def processCompilationUnit(using quoted.Quotes)(root: quotes.reflect.Tree): Unit = ()
44+
45+
override def postProcess(using quoted.Quotes): Unit =
46+
check()
47+
48+
Inspector().inspectTastyFiles(TestUtils.listOurClasses())
49+
}
50+
51+
private def qr(using quoted.Quotes): quotes.reflect.type = quotes.reflect
52+
}

scala3doc/test/dotty/dokka/tasty/comments/MemberLookupTests.scala

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.quoted.Quotes
44

55
import org.junit.{Test, Rule}
66
import org.junit.Assert.{assertSame, assertTrue}
7-
import dotty.dokka.BuildInfo
7+
import dotty.dokka.tasty.util._
88

99
class LookupTestCases[Q <: Quotes](val q: Quotes) {
1010

@@ -118,27 +118,6 @@ class MemberLookupTests {
118118
cases.testAll()
119119
}
120120

121-
Inspector().inspectTastyFiles(listOurClasses())
122-
}
123-
124-
def listOurClasses(): List[String] = {
125-
import java.io.File
126-
import scala.collection.mutable.ListBuffer
127-
128-
val classRoot = new File(BuildInfo.test_testcasesOutputDir)
129-
130-
def go(bld: ListBuffer[String])(file: File): Unit =
131-
file.listFiles.foreach { f =>
132-
if f.isFile() then
133-
if f.toString.endsWith(".tasty") then bld.append(f.toString)
134-
else go(bld)(f)
135-
}
136-
137-
if classRoot.isDirectory then
138-
val bld = new ListBuffer[String]
139-
go(bld)(classRoot)
140-
bld.result
141-
else
142-
sys.error(s"Class root could not be found: $classRoot")
121+
Inspector().inspectTastyFiles(TestUtils.listOurClasses())
143122
}
144123
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dotty.dokka.tasty.util
2+
3+
import dotty.dokka.BuildInfo
4+
5+
object TestUtils {
6+
def listOurClasses(): List[String] = {
7+
import java.io.File
8+
import scala.collection.mutable.ListBuffer
9+
10+
val classRoot = new File(BuildInfo.test_testcasesOutputDir)
11+
12+
def go(bld: ListBuffer[String])(file: File): Unit =
13+
file.listFiles.foreach { f =>
14+
if f.isFile() then
15+
if f.toString.endsWith(".tasty") then bld.append(f.toString)
16+
else go(bld)(f)
17+
}
18+
19+
if classRoot.isDirectory then
20+
val bld = new ListBuffer[String]
21+
go(bld)(classRoot)
22+
bld.result
23+
else
24+
sys.error(s"Class root could not be found: $classRoot")
25+
}
26+
}

0 commit comments

Comments
 (0)