tableofcontents_test.go (3856B)
1 // Copyright 2019 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package tableofcontents
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 )
21
22 func TestToc(t *testing.T) {
23 c := qt.New(t)
24
25 toc := &Root{}
26
27 toc.AddAt(Heading{Text: "Heading 1", ID: "h1-1"}, 0, 0)
28 toc.AddAt(Heading{Text: "1-H2-1", ID: "1-h2-1"}, 0, 1)
29 toc.AddAt(Heading{Text: "1-H2-2", ID: "1-h2-2"}, 0, 1)
30 toc.AddAt(Heading{Text: "1-H3-1", ID: "1-h2-2"}, 0, 2)
31 toc.AddAt(Heading{Text: "Heading 2", ID: "h1-2"}, 1, 0)
32
33 got := toc.ToHTML(1, -1, false)
34 c.Assert(got, qt.Equals, `<nav id="TableOfContents">
35 <ul>
36 <li><a href="#h1-1">Heading 1</a>
37 <ul>
38 <li><a href="#1-h2-1">1-H2-1</a></li>
39 <li><a href="#1-h2-2">1-H2-2</a>
40 <ul>
41 <li><a href="#1-h2-2">1-H3-1</a></li>
42 </ul>
43 </li>
44 </ul>
45 </li>
46 <li><a href="#h1-2">Heading 2</a></li>
47 </ul>
48 </nav>`, qt.Commentf(got))
49
50 got = toc.ToHTML(1, 1, false)
51 c.Assert(got, qt.Equals, `<nav id="TableOfContents">
52 <ul>
53 <li><a href="#h1-1">Heading 1</a></li>
54 <li><a href="#h1-2">Heading 2</a></li>
55 </ul>
56 </nav>`, qt.Commentf(got))
57
58 got = toc.ToHTML(1, 2, false)
59 c.Assert(got, qt.Equals, `<nav id="TableOfContents">
60 <ul>
61 <li><a href="#h1-1">Heading 1</a>
62 <ul>
63 <li><a href="#1-h2-1">1-H2-1</a></li>
64 <li><a href="#1-h2-2">1-H2-2</a></li>
65 </ul>
66 </li>
67 <li><a href="#h1-2">Heading 2</a></li>
68 </ul>
69 </nav>`, qt.Commentf(got))
70
71 got = toc.ToHTML(2, 2, false)
72 c.Assert(got, qt.Equals, `<nav id="TableOfContents">
73 <ul>
74 <li><a href="#1-h2-1">1-H2-1</a></li>
75 <li><a href="#1-h2-2">1-H2-2</a></li>
76 </ul>
77 </nav>`, qt.Commentf(got))
78
79 got = toc.ToHTML(1, -1, true)
80 c.Assert(got, qt.Equals, `<nav id="TableOfContents">
81 <ol>
82 <li><a href="#h1-1">Heading 1</a>
83 <ol>
84 <li><a href="#1-h2-1">1-H2-1</a></li>
85 <li><a href="#1-h2-2">1-H2-2</a>
86 <ol>
87 <li><a href="#1-h2-2">1-H3-1</a></li>
88 </ol>
89 </li>
90 </ol>
91 </li>
92 <li><a href="#h1-2">Heading 2</a></li>
93 </ol>
94 </nav>`, qt.Commentf(got))
95 }
96
97 func TestTocMissingParent(t *testing.T) {
98 c := qt.New(t)
99
100 toc := &Root{}
101
102 toc.AddAt(Heading{Text: "H2", ID: "h2"}, 0, 1)
103 toc.AddAt(Heading{Text: "H3", ID: "h3"}, 1, 2)
104 toc.AddAt(Heading{Text: "H3", ID: "h3"}, 1, 2)
105
106 got := toc.ToHTML(1, -1, false)
107 c.Assert(got, qt.Equals, `<nav id="TableOfContents">
108 <ul>
109 <li>
110 <ul>
111 <li><a href="#h2">H2</a></li>
112 </ul>
113 </li>
114 <li>
115 <ul>
116 <li>
117 <ul>
118 <li><a href="#h3">H3</a></li>
119 <li><a href="#h3">H3</a></li>
120 </ul>
121 </li>
122 </ul>
123 </li>
124 </ul>
125 </nav>`, qt.Commentf(got))
126
127 got = toc.ToHTML(3, 3, false)
128 c.Assert(got, qt.Equals, `<nav id="TableOfContents">
129 <ul>
130 <li><a href="#h3">H3</a></li>
131 <li><a href="#h3">H3</a></li>
132 </ul>
133 </nav>`, qt.Commentf(got))
134
135 got = toc.ToHTML(1, -1, true)
136 c.Assert(got, qt.Equals, `<nav id="TableOfContents">
137 <ol>
138 <li>
139 <ol>
140 <li><a href="#h2">H2</a></li>
141 </ol>
142 </li>
143 <li>
144 <ol>
145 <li>
146 <ol>
147 <li><a href="#h3">H3</a></li>
148 <li><a href="#h3">H3</a></li>
149 </ol>
150 </li>
151 </ol>
152 </li>
153 </ol>
154 </nav>`, qt.Commentf(got))
155 }