Skip to content

Commit 17ed48b

Browse files
committed
Migrate template tests from dokka-site
1 parent aafbe91 commit 17ed48b

File tree

4 files changed

+225
-5
lines changed

4 files changed

+225
-5
lines changed

scala3doc/src/dotty/dokka/model/api/api.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ object Annotation:
8787

8888
// TODO (longterm) properly represent signatures
8989
case class Link(name: String, dri: DRI)
90-
type Signature = Seq[String | Link]// TODO migrate tupes to Links
90+
type Signature = Seq[String | Link]
9191

9292
object Signature:
9393
def apply(names: (String | Link)*): Signature = names // TO batter dotty shortcommings in union types

scala3doc/src/dotty/dokka/site/processors.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ class SiteResourceManager(ctx: Option[StaticSiteContext]) extends BaseStaticSite
2727

2828
override def transform(input: RootPageNode, ctx: StaticSiteContext): RootPageNode =
2929
val imgPath = ctx.root.toPath().resolve("images")
30-
val allImgPaths = Files.walk(imgPath)
31-
.filter(p => Files.isRegularFile(p) && p.getFileName().toString().endsWith(".svg"))
32-
val images = allImgPaths.iterator().asScala.toList.map(_.toString)
30+
val images =
31+
if !Files.exists(imgPath) then Nil
32+
else
33+
val stream = Files.walk(imgPath)filter(p => Files.isRegularFile(p) && p.getFileName().toString().endsWith(".svg"))
34+
stream.iterator().asScala.toList.map(_.toString)
3335

3436
val resources = listResources(input.getChildren.asScala.toList) ++ images
3537
val resourcePages = resources.map { path =>

scala3doc/src/dotty/dokka/site/templates.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ case class TemplateFile(
7575

7676
def resolveToHtml(ctx: StaticSiteContext): ResolvedPage = resolveInner(RenderingContext(Map(), ctx.layouts))
7777

78-
private def resolveInner(ctx: RenderingContext): ResolvedPage =
78+
private[site] def resolveInner(ctx: RenderingContext): ResolvedPage =
7979
if (ctx.resolving.contains(file.getAbsolutePath))
8080
throw new RuntimeException(s"Cycle in templates involving $file: ${ctx.resolving}")
8181

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
package dotty.dokka.site
2+
3+
import com.vladsch.flexmark.html.HtmlRenderer
4+
import com.vladsch.flexmark.parser.Parser
5+
import org.junit.Assert.assertEquals
6+
import org.junit.Test
7+
import java.nio.file.Files
8+
9+
class TemplateFileTests:
10+
private def testTemplate(code: String, ext: String = "html")(op: TemplateFile => Unit): Unit =
11+
val tmpFile = Files.createTempFile("headerTests", s".${ext}").toFile()
12+
try
13+
Files.writeString(tmpFile.toPath, code)
14+
op(loadTemplateFile(tmpFile))
15+
finally tmpFile.delete()
16+
17+
18+
private def testTemplates(
19+
props: Map[String, String],
20+
template: List[(String, String)])(
21+
op: RenderingContext => Unit
22+
) =
23+
def rec(cxt: RenderingContext, remaining: List[(String, String)]): Unit =
24+
if (remaining.isEmpty) op(cxt)
25+
else
26+
val (code, ext) = remaining.head
27+
testTemplate(code, ext) { template =>
28+
val newCtx = cxt.copy(layouts = cxt.layouts + (template.name() -> template))
29+
rec(newCtx, remaining.drop(1))
30+
}
31+
32+
rec(RenderingContext(props), template)
33+
34+
private def fullRender(template: TemplateFile, ctx: RenderingContext): String = template.resolveInner(ctx).code.trim()
35+
36+
@Test
37+
def testParsingHeaders(): Unit =
38+
testTemplate(
39+
"""---
40+
|title: myTitle
41+
|---
42+
|code""".stripMargin
43+
) { t =>
44+
assertEquals(t.rawCode, "code")
45+
assertEquals(t.title(), "myTitle")
46+
}
47+
48+
49+
@Test
50+
def testLinks(): Unit =
51+
val base =
52+
"""---
53+
|title: myTitle
54+
|name: base
55+
|---
56+
|Ala {{ content }}. {{p2}} with [link](link/target.md)!
57+
|""".stripMargin
58+
59+
val content =
60+
"""---
61+
|layout: base
62+
|name: content
63+
|---
64+
|ma kota w **{{ p1 }}** from [here](link/here.md)
65+
|""".stripMargin
66+
67+
68+
val expected = """<p>Ala ma kota w <strong>paski</strong> from <a href="link/here.md">here</a>. Hej with <a href="link/target.md">link</a>!</p>"""
69+
70+
testTemplates(
71+
Map("p1" -> "paski", "p2" -> "Hej"),
72+
List(base -> "html", content -> "md")
73+
) { it =>
74+
assertEquals(
75+
expected,
76+
fullRender(it.layouts("content"), it)
77+
)
78+
}
79+
80+
@Test
81+
def layout(): Unit =
82+
val base =
83+
"""---
84+
|title: myTitle
85+
|name: base
86+
|---
87+
|Ala {{ content }}. {{p2}}!
88+
|""".stripMargin
89+
90+
val content =
91+
"""---
92+
|layout: base
93+
|name: content
94+
|---
95+
|ma kota w **{{ p1 }}**
96+
|""".stripMargin
97+
98+
99+
val expected = """<p>Ala ma kota w <strong>paski</strong>. Hej!</p>""".stripMargin
100+
101+
testTemplates(
102+
Map("p1" -> "paski", "p2" -> "Hej"),
103+
List(base -> "html", content -> "md")
104+
) { it =>
105+
assertEquals(
106+
expected,
107+
fullRender(it.layouts("content"), it)
108+
)
109+
}
110+
111+
@Test
112+
def nestedLayout_htmlMdHtml(): Unit =
113+
val toplevel =
114+
"""---
115+
|name: toplevel
116+
|---
117+
|[div id="root"]{{ content }}[/div]
118+
|""".stripMargin
119+
120+
val basePage =
121+
"""---
122+
|layout: toplevel
123+
|name: basePage
124+
|---
125+
|# {{ pageName }}
126+
|
127+
|{{content}}
128+
|
129+
|## {{ pageName }} end
130+
|""".stripMargin
131+
132+
val content =
133+
"""---
134+
|layout: basePage
135+
|name: content
136+
|---
137+
|Hello {{ name }}!
138+
|""".stripMargin
139+
140+
141+
val expected =
142+
"""[div id="root"][h1]Test page[/h1]
143+
|[p]Hello world!![/p]
144+
|[h2]Test page end[/h2]
145+
|[/div]""".stripMargin
146+
147+
testTemplates(
148+
Map("pageName" -> "Test page", "name" -> "world!"),
149+
List(
150+
toplevel -> "html",
151+
basePage -> "md",
152+
content -> "md"
153+
)
154+
) (it => fullRender(it.layouts("content"), it))
155+
156+
@Test
157+
def nestedLayout_mdHtmlMd(): Unit =
158+
val toplevel =
159+
"""---
160+
|name: toplevel
161+
|---
162+
|<h1>The Page</h1>
163+
|{{ content }}
164+
|""".stripMargin
165+
166+
val basePage =
167+
"""---
168+
|layout: toplevel
169+
|name: basePage
170+
|---
171+
|<h2>{{ pageName }}</h2>
172+
|
173+
|{{content}}
174+
|
175+
|<h3>{{ pageName }} end</h3>
176+
|""".stripMargin
177+
178+
val content =
179+
"""---
180+
|layout: basePage
181+
|name: content
182+
|---
183+
|Hello {{ name }}!
184+
|""".stripMargin
185+
186+
187+
val expected =
188+
"""<h1>The Page</h1>
189+
|<h2>Test page</h2>
190+
|<p>Hello world!!</p>
191+
|<h3>Test page end</h3>""".stripMargin
192+
193+
testTemplates(
194+
Map("pageName" -> "Test page", "name" -> "world!"),
195+
List(
196+
toplevel -> "html",
197+
basePage -> "html",
198+
content -> "md"
199+
)
200+
) { ctx => assertEquals(expected, fullRender(ctx.layouts("content"), ctx)) }
201+
202+
@Test
203+
def markdown(): Unit =
204+
testTemplate(
205+
"""# Hello {{ msg }}!""",
206+
ext = "md"
207+
) { t =>
208+
assertEquals("# Hello there!", t.resolveInner(RenderingContext(Map("msg" -> "there"))).code.trim())
209+
}
210+
211+
@Test
212+
def mixedTemplates() : Unit =
213+
testTemplate(
214+
"""# Hello {{ msg }}!""",
215+
ext = "md"
216+
) { t =>
217+
assertEquals("# Hello there!", t.resolveInner(RenderingContext(Map("msg" -> "there"))).code.trim())
218+
}

0 commit comments

Comments
 (0)