Update tutorial.
[docutils.git] / tools / editors / emacs / tests / toc.el
blob59e95caa89ec5e0169de68cd954795209d985531
1 ;; Tests for operations on toc
3 (add-to-list 'load-path ".")
4 (load "init" nil t)
5 (init-rst-ert t)
7 (ert-deftest toc-asserts ()
8 "Check some assertions."
9 (should (equal ert-Buf-point-char "\^@"))
10 (should (equal ert-Buf-mark-char "\^?"))
13 (ert-deftest rst-toc-insert ()
14 "Tests `rst-toc-insert'."
15 (let ((title "=====
16 Title
17 =====
20 (headers "Header A
21 ========
23 Header B
24 ========
26 Subheader B.a
27 -------------
29 SubSubheader B.a.1
30 ~~~~~~~~~~~~~~~~~~
32 Subheader B.b
33 -------------
35 Header C
36 ========"))
37 ;; Set customizable variables to defaults
38 (let ((rst-toc-indent 2)
39 (rst-toc-insert-style 'fixed)
40 (rst-toc-insert-number-separator " ")
41 (rst-toc-insert-max-level nil))
42 ;; Can't identify a title so do nothing - that's actually a (MIS-)FEATURE
43 (should (ert-equal-buffer
44 (rst-toc-insert)
45 (concat "\^@" headers)
46 t))
47 ;; Won't work on a section title
48 (should (ert-equal-buffer
49 (rst-toc-insert)
50 (concat title "\^@" headers)
51 t))
52 ;; No indentation
53 (should (ert-equal-buffer
54 (rst-toc-insert)
55 (concat title "\^@\n\n" headers)
56 (concat title "1 Header A
57 2 Header B
58 2.1 Subheader B.a
59 2.1.1 SubSubheader B.a.1
60 2.2 Subheader B.b
61 3 Header C\^@
63 " headers)))
64 ;; Indentation
65 (should (ert-equal-buffer
66 (rst-toc-insert)
67 (concat title " \^@\n\n" headers)
68 (concat title " 1 Header A
69 2 Header B
70 2.1 Subheader B.a
71 2.1.1 SubSubheader B.a.1
72 2.2 Subheader B.b
73 3 Header C\^@
75 " headers)))
76 ;; Only first level
77 (should (ert-equal-buffer
78 (rst-toc-insert 1)
79 (concat title " \^@\n\n" headers)
80 (concat title " 1 Header A
81 2 Header B
82 3 Header C\^@
84 " headers)))
85 ;; Prefix and indentation
86 (should (ert-equal-buffer
87 (rst-toc-insert)
88 (concat title ".. \^@\n\n" headers)
89 (concat title ".. 1 Header A
90 2 Header B
91 2.1 Subheader B.a
92 2.1.1 SubSubheader B.a.1
93 2.2 Subheader B.b
94 3 Header C\^@
96 " headers)))
100 (ert-deftest rst-toc-update ()
101 "Tests `rst-toc-update'."
102 (let ((title "=====
103 Title
104 =====
107 (headers "Header A
108 ========
110 Header B
111 ========
113 Subheader B.a
114 -------------
116 SubSubheader B.a.1
117 ~~~~~~~~~~~~~~~~~~
119 Subheader B.b
120 -------------
122 Header C
123 ========")
124 (contents ".. contents:: Inhalt\n")
125 (fields " :bla: blub\n :local:\n")
126 (old ".. 1 Header A
127 2 Header B
128 3 Header C")
129 (new "..
130 1 Header A
131 2 Header B
132 2.1 Subheader B.a
133 2.1.1 SubSubheader B.a.1
134 2.2 Subheader B.b
135 3 Header C")
137 ;; Set customizable variables to defaults
138 (let ((rst-toc-indent 2)
139 (rst-toc-insert-style 'fixed)
140 (rst-toc-insert-number-separator " ")
141 (rst-toc-insert-max-level nil))
142 (should (ert-equal-buffer
143 (rst-toc-update)
144 (concat title contents fields old "\n\n" headers "\^@")
145 (concat title contents fields new "\n\n" headers "\^@")))
146 (should (ert-equal-buffer
147 (rst-toc-update)
148 (concat title contents old "\n\n" headers "\^@")
149 (concat title contents new "\n\n" headers "\^@")))
153 (defun toc ()
154 "Call `rst-toc' and copy special buffer to target buffer."
155 (let ((wincfg (current-window-configuration))
156 txt pt mrk)
157 (if (get-buffer rst-toc-buffer-name)
158 (kill-buffer rst-toc-buffer-name))
159 (rst-toc)
160 (with-current-buffer rst-toc-buffer-name
161 (setq txt (buffer-substring-no-properties (point-min) (point-max)))
162 (setq pt (point))
163 (setq mrk (mark t)))
164 (set-window-configuration wincfg)
165 (kill-buffer rst-toc-buffer-name)
166 (delete-region (point-min) (point-max))
167 (insert txt)
168 (set-mark mrk)
169 (goto-char pt)))
171 (ert-deftest rst-toc ()
172 "Tests `rst-toc'."
173 ;; Set customizable variables to defaults
174 (let ((rst-toc-indent 2))
175 (should (ert-equal-buffer
176 (toc)
177 "=====
178 Title
179 =====
181 Header A
182 ========
184 Header B
185 ========
187 Subheader B.a
188 -------------
190 SubSubheader B.a.1
191 ~~~~~~~~~~~~~~~~~~
193 Subheader B.b
194 -------------
196 Header C
197 ========
198 \^@"
199 "Table of Contents:
200 Title
201 Header A
202 Header B
203 Subheader B.a
204 SubSubheader B.a.1
205 Subheader B.b
206 \^@ Header C
209 (should (ert-equal-buffer
210 (toc)
211 "=====
212 Title
213 =====
215 Header A
216 ========
218 Header B
219 ========
221 Subh\^@eader B.a
222 -------------
224 SubSubheader B.a.1
225 ~~~~~~~~~~~~~~~~~~
227 Subheader B.b
228 -------------
230 Header C
231 ========
233 "Table of Contents:
234 Title
235 Header A
236 Header B
237 \^@ Subheader B.a
238 SubSubheader B.a.1
239 Subheader B.b
240 Header C
243 (should (ert-equal-buffer
244 (toc)
245 "\^@
247 =====
248 Title
249 =====
251 Header A
252 ========
254 Header B
255 ========
257 Subheader B.a
258 -------------
260 SubSubheader B.a.1
261 ~~~~~~~~~~~~~~~~~~
263 Subheader B.b
264 -------------
266 Header C
267 ========
269 "\^@Table of Contents:
270 Title
271 Header A
272 Header B
273 Subheader B.a
274 SubSubheader B.a.1
275 Subheader B.b
276 Header C
279 (should (ert-equal-buffer
280 (toc)
281 "=====
282 Title
283 =====
285 Header A
286 ========
288 Header B
289 ========
291 Subheader B.a
292 -------------
294 SubSubheader B.a.1
295 ~~~~~~~~~~~~~~~~~~
297 Subheader B.b
298 -------------
300 Header C
301 ========
303 "Table of Contents:
304 Title
305 Header A
306 Header B
307 Subheader B.a
308 \^@ SubSubheader B.a.1
309 Subheader B.b
310 Header C
315 ;; FIXME: More functions to test:
316 ;; * rst-toc-mode-goto-section