aboutsummaryrefslogtreecommitdiff
path: root/doc-tool/test/dotty/tools/dottydoc/staticsite/PageTests.scala
blob: 14886b681c46c9d8f0e687d601b0ed3a2752b75f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package dotty.tools
package dottydoc
package staticsite

import org.junit.Test
import org.junit.Assert._

class PageTests extends DottyDocTest {
  import scala.collection.JavaConverters._

  @Test def mdHas1Key = {
    val page = new MarkdownPage(
      """|---
         |key:
         |---
         |
         |great""".stripMargin,
      Map.empty,
      Map.empty,
      Map.empty
    )

    assert(
      page.yaml == Map("key" -> List.empty.asJava),
      s"""incorrect yaml, expected "key:" without key in: ${page.yaml}"""
    )

    assertEquals("<p>great</p>\n", page.html)
  }

  @Test def yamlPreservesLiquidTags = {
    val page1 = new MarkdownPage(
      """|---
         |key:
         |---
         |
         |{{ content }}""".stripMargin,
       Map("content" -> "Hello, world!"),
       Map.empty,
       Map.empty
    )

    assert(
      page1.yaml == Map("key" -> List.empty.asJava),
      s"""incorrect yaml, expected "key:" without key in: ${page1.yaml}"""
    )

    assertEquals("<p>Hello, world!</p>\n", page1.html)

    val page2 = new MarkdownPage(
      """|{{ content }}""".stripMargin,
      Map("content" -> "hello"),
      Map.empty,
      Map.empty
    )
    assert(
      page2.yaml == Map(),
      s"""incorrect yaml, expected "key:" without key in: ${page2.yaml}"""
    )
    assertEquals("<p>hello</p>\n", page2.html)

    val page3 = new MarkdownPage(
      """|{% if product.title == "Awesome Shoes" %}
         |These shoes are awesome!
         |{% endif %}""".stripMargin,
      Map("product" -> Map("title" -> "Awesome Shoes").asJava),
      Map.empty,
      Map.empty
    )

    assertEquals(
      "<p>These shoes are awesome!</p>\n",
      page3.html
    )
  }

  @Test def simpleHtmlPage = {
    val p1 = new HtmlPage("""<h1>{{ "hello, world!" }}</h1>""", Map.empty, Map.empty)
    assert(p1.yaml == Map(), "non-empty yaml found")
    assertEquals("<h1>hello, world!</h1>", p1.html)
  }

  @Test def htmlPageHasNoYaml = {
    val page = new HtmlPage(
      """|---
         |layout: main
         |---
         |
         |Hello, world!""".stripMargin,
      Map.empty,
      Map.empty
    )

    assert(!page.html.contains("---\nlayout: main\n---"),
           s"page still contains yaml:\n${page.html}")
  }

  @Test def illegalYamlFrontMatter = try {
    val page = new HtmlPage(
      """|---
         |layout: main
         |
         |
         |Hello, world!""".stripMargin,
      Map.empty,
      Map.empty
    )

    page.html
    fail("illegal front matter didn't throw exception")
  } catch {
    case IllegalFrontMatter(x) => // success!
    case t: Throwable => throw t
  }
}