| 1 |
;; Tests for operations on toc -*- lexical-binding: t -*- |
| 2 |
|
| 3 |
(add-to-list 'load-path ".") |
| 4 |
(load "init" nil t) |
| 5 |
(init-rst-ert t) |
| 6 |
|
| 7 |
(ert-deftest toc-asserts () |
| 8 |
"Check some assertions." |
| 9 |
(should (equal ert-Buf-point-char "\^@")) |
| 10 |
(should (equal ert-Buf-mark-char "\^?")) |
| 11 |
) |
| 12 |
|
| 13 |
(setq title "===== |
| 14 |
Title |
| 15 |
===== |
| 16 |
|
| 17 |
" |
| 18 |
headers "Header A |
| 19 |
======== |
| 20 |
|
| 21 |
Header B |
| 22 |
======== |
| 23 |
|
| 24 |
Subheader B.a |
| 25 |
------------- |
| 26 |
|
| 27 |
SubSubheader B.a.1 |
| 28 |
~~~~~~~~~~~~~~~~~~ |
| 29 |
|
| 30 |
Subheader B.b |
| 31 |
------------- |
| 32 |
|
| 33 |
Header C |
| 34 |
========") |
| 35 |
|
| 36 |
(ert-deftest rst-toc-insert () |
| 37 |
"Tests `rst-toc-insert'." |
| 38 |
;; Set customizable variables to defaults |
| 39 |
(let ((rst-toc-indent 2) |
| 40 |
(rst-toc-insert-style 'fixed) |
| 41 |
(rst-toc-insert-number-separator " - ") |
| 42 |
(rst-toc-insert-max-level nil) |
| 43 |
(indent-tabs-mode nil)) |
| 44 |
(should (ert-equal-buffer |
| 45 |
'(rst-toc-insert) |
| 46 |
"" |
| 47 |
t)) |
| 48 |
;; Can't identify a title so do nothing - that's actually a (MIS-)FEATURE |
| 49 |
(should (ert-equal-buffer |
| 50 |
'(rst-toc-insert) |
| 51 |
(concat "\^@" headers) |
| 52 |
t)) |
| 53 |
;; Can't identify a title so do nothing - that's actually a (MIS-)FEATURE |
| 54 |
(should (ert-equal-buffer |
| 55 |
(rst-toc-insert) |
| 56 |
(concat "\^@\n" headers) |
| 57 |
t)) |
| 58 |
;; Won't work on a section title |
| 59 |
(should (ert-equal-buffer |
| 60 |
'(rst-toc-insert) |
| 61 |
(concat title "\^@" headers) |
| 62 |
t)) |
| 63 |
;; No indentation |
| 64 |
(let ((rst-toc-indent 1)) |
| 65 |
(should (ert-equal-buffer |
| 66 |
'(rst-toc-insert) |
| 67 |
(concat title "\^@THIS IS DELETED\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\^@ |
| 74 |
|
| 75 |
" headers)))) |
| 76 |
;; Indentation |
| 77 |
(let ((rst-toc-insert-style 'listed) |
| 78 |
(rst-preferred-bullets '(?+)) |
| 79 |
(rst-toc-indent 1)) |
| 80 |
(should (ert-equal-buffer |
| 81 |
'(rst-toc-insert) |
| 82 |
(concat title " \^@\n\n" headers) |
| 83 |
(concat title " + Header A |
| 84 |
+ Header B |
| 85 |
+ Subheader B.a |
| 86 |
+ SubSubheader B.a.1 |
| 87 |
+ Subheader B.b |
| 88 |
+ Header C\^@ |
| 89 |
|
| 90 |
" headers)))) |
| 91 |
;; Only first level |
| 92 |
(let ((rst-toc-insert-style 'plain)) |
| 93 |
(should (ert-equal-buffer |
| 94 |
'(rst-toc-insert 1) |
| 95 |
(concat title " \^@\n\n" headers) |
| 96 |
(concat title " Header A |
| 97 |
Header B |
| 98 |
Header C\^@ |
| 99 |
|
| 100 |
" headers)))) |
| 101 |
;; Prefix and indentation |
| 102 |
(let ((rst-toc-insert-style 'aligned) |
| 103 |
(headers-add " |
| 104 |
|
| 105 |
C_1 |
| 106 |
--- |
| 107 |
|
| 108 |
C_2 |
| 109 |
--- |
| 110 |
|
| 111 |
C_3 |
| 112 |
--- |
| 113 |
|
| 114 |
C_4 |
| 115 |
--- |
| 116 |
|
| 117 |
C_5 |
| 118 |
--- |
| 119 |
|
| 120 |
C_6 |
| 121 |
--- |
| 122 |
|
| 123 |
C_7 |
| 124 |
--- |
| 125 |
|
| 126 |
C_8 |
| 127 |
--- |
| 128 |
|
| 129 |
C_9 |
| 130 |
--- |
| 131 |
|
| 132 |
C_10 |
| 133 |
---- |
| 134 |
")) |
| 135 |
(should (ert-equal-buffer |
| 136 |
'(rst-toc-insert) |
| 137 |
(concat title ".. \^@\n\n" headers headers-add) |
| 138 |
(concat title ".. 1 - Header A |
| 139 |
2 - Header B |
| 140 |
2.1 - Subheader B.a |
| 141 |
2.1.1 - SubSubheader B.a.1 |
| 142 |
2.2 - Subheader B.b |
| 143 |
3 - Header C |
| 144 |
3. 1 - C_1 |
| 145 |
3. 2 - C_2 |
| 146 |
3. 3 - C_3 |
| 147 |
3. 4 - C_4 |
| 148 |
3. 5 - C_5 |
| 149 |
3. 6 - C_6 |
| 150 |
3. 7 - C_7 |
| 151 |
3. 8 - C_8 |
| 152 |
3. 9 - C_9 |
| 153 |
3.10 - C_10\^@ |
| 154 |
|
| 155 |
" headers headers-add)))) |
| 156 |
) |
| 157 |
) |
| 158 |
|
| 159 |
(ert-deftest rst-toc-update () |
| 160 |
"Tests `rst-toc-update'." |
| 161 |
(let ((contents ".. contents:: Inhalt\n") |
| 162 |
(fields " :bla: blub\n :local:\n") |
| 163 |
(old ".. 1 Header A |
| 164 |
2 Header B |
| 165 |
3 Header C") |
| 166 |
(new ".. |
| 167 |
1 Header A |
| 168 |
2 Header B |
| 169 |
2.1 Subheader B.a |
| 170 |
2.1.1 SubSubheader B.a.1 |
| 171 |
2.2 Subheader B.b |
| 172 |
3 Header C") |
| 173 |
(indent-tabs-mode nil)) |
| 174 |
;; Set customizable variables to defaults |
| 175 |
(let ((rst-toc-indent 2) |
| 176 |
(rst-toc-insert-style 'fixed) |
| 177 |
(rst-toc-insert-number-separator " ") |
| 178 |
(rst-toc-insert-max-level nil)) |
| 179 |
(should (ert-equal-buffer |
| 180 |
'(rst-toc-update) |
| 181 |
(concat title contents fields old "\n\n" headers "\^@") |
| 182 |
(concat title contents fields new "\n\n" headers "\^@"))) |
| 183 |
(should (ert-equal-buffer |
| 184 |
'(rst-toc-update) |
| 185 |
(concat title contents old "\n\n" headers "\^@") |
| 186 |
(concat title contents new "\n\n" headers "\^@"))) |
| 187 |
(should (ert-equal-buffer |
| 188 |
'(rst-toc-update) |
| 189 |
(concat title contents ".." "\n\n" headers "\^@") |
| 190 |
(concat title contents new "\n\n" headers "\^@"))) |
| 191 |
(should (ert-equal-buffer |
| 192 |
'(rst-toc-update) |
| 193 |
(concat title contents ".." "\^@") |
| 194 |
(concat title contents ".." "\^@"))) |
| 195 |
) |
| 196 |
)) |
| 197 |
|
| 198 |
(defun toc () |
| 199 |
"Call `rst-toc' and copy special buffer to target buffer. |
| 200 |
Return line number link is pointing to in original buffer or nil for no link." |
| 201 |
(rst-toc) |
| 202 |
(let ((txt (buffer-substring-no-properties (point-min) (point-max))) |
| 203 |
(pt (point)) |
| 204 |
(mrk (mark t)) |
| 205 |
(lnk (condition-case nil |
| 206 |
(progn |
| 207 |
(rst-toc-mode-follow-link-kill) |
| 208 |
t) |
| 209 |
(error nil)))) |
| 210 |
(setq lnk (and lnk (line-number-at-pos))) |
| 211 |
(delete-region (point-min) (point-max)) |
| 212 |
(insert txt) |
| 213 |
(set-mark mrk) |
| 214 |
(goto-char pt) |
| 215 |
lnk)) |
| 216 |
|
| 217 |
(ert-deftest rst-toc () |
| 218 |
"Tests `rst-toc'." |
| 219 |
;; Set customizable variables to defaults |
| 220 |
(let ((rst-toc-indent 2) |
| 221 |
(indent-tabs-mode nil)) |
| 222 |
(should (ert-equal-buffer |
| 223 |
'(toc) |
| 224 |
"No section title at all |
| 225 |
\^@" |
| 226 |
"\^@" |
| 227 |
)) |
| 228 |
(should (ert-equal-buffer-return |
| 229 |
'(toc) |
| 230 |
(concat title headers "\n\^@") |
| 231 |
"Title |
| 232 |
Header A |
| 233 |
Header B |
| 234 |
Subheader B.a |
| 235 |
SubSubheader B.a.1 |
| 236 |
Subheader B.b |
| 237 |
\^@ Header C |
| 238 |
" |
| 239 |
20)) |
| 240 |
(should (ert-equal-buffer-return |
| 241 |
'(toc) |
| 242 |
(concat title "Header A |
| 243 |
======== |
| 244 |
|
| 245 |
Header B |
| 246 |
======== |
| 247 |
|
| 248 |
Subh\^@eader B.a |
| 249 |
------------- |
| 250 |
|
| 251 |
SubSubheader B.a.1 |
| 252 |
~~~~~~~~~~~~~~~~~~ |
| 253 |
|
| 254 |
Subheader B.b |
| 255 |
------------- |
| 256 |
|
| 257 |
Header C |
| 258 |
======== |
| 259 |
") |
| 260 |
"Title |
| 261 |
Header A |
| 262 |
Header B |
| 263 |
\^@ Subheader B.a |
| 264 |
SubSubheader B.a.1 |
| 265 |
Subheader B.b |
| 266 |
Header C |
| 267 |
" |
| 268 |
11)) |
| 269 |
(should (ert-equal-buffer-return |
| 270 |
'(toc) |
| 271 |
(concat "\^@\n\n" title headers "\n") |
| 272 |
"\^@Title |
| 273 |
Header A |
| 274 |
Header B |
| 275 |
Subheader B.a |
| 276 |
SubSubheader B.a.1 |
| 277 |
Subheader B.b |
| 278 |
Header C |
| 279 |
" |
| 280 |
4)) |
| 281 |
(should (ert-equal-buffer-return |
| 282 |
'(toc) |
| 283 |
(concat title "Header A |
| 284 |
======== |
| 285 |
|
| 286 |
Header B |
| 287 |
======== |
| 288 |
|
| 289 |
Subheader B.a |
| 290 |
------------- |
| 291 |
|
| 292 |
SubSubheader B.a.1 |
| 293 |
~~~~~~~~~~~~~~~~~~ |
| 294 |
\^@ |
| 295 |
Subheader B.b |
| 296 |
------------- |
| 297 |
|
| 298 |
Header C |
| 299 |
======== |
| 300 |
") |
| 301 |
"Title |
| 302 |
Header A |
| 303 |
Header B |
| 304 |
Subheader B.a |
| 305 |
\^@ SubSubheader B.a.1 |
| 306 |
Subheader B.b |
| 307 |
Header C |
| 308 |
" |
| 309 |
14)) |
| 310 |
)) |
| 311 |
|
| 312 |
(defun toc-follow (level) |
| 313 |
(rst-toc-insert level) |
| 314 |
(back-to-indentation) |
| 315 |
(call-interactively #'rst-toc-follow-link)) |
| 316 |
|
| 317 |
(ert-deftest rst-toc-follow-link () |
| 318 |
"Tests `rst-toc-follow-link'." |
| 319 |
(let ((rst-toc-insert-style 'plain)) |
| 320 |
(should-error (ert-equal-buffer |
| 321 |
'(toc-follow 1) |
| 322 |
"" |
| 323 |
"") |
| 324 |
:type 'error) |
| 325 |
(should (ert-equal-buffer |
| 326 |
'(toc-follow 1) |
| 327 |
(concat title " \^@\n\n" headers) |
| 328 |
(concat title " Header A |
| 329 |
Header B |
| 330 |
Header C |
| 331 |
|
| 332 |
Header A |
| 333 |
======== |
| 334 |
|
| 335 |
Header B |
| 336 |
======== |
| 337 |
|
| 338 |
Subheader B.a |
| 339 |
------------- |
| 340 |
|
| 341 |
SubSubheader B.a.1 |
| 342 |
~~~~~~~~~~~~~~~~~~ |
| 343 |
|
| 344 |
Subheader B.b |
| 345 |
------------- |
| 346 |
|
| 347 |
\^@Header C |
| 348 |
========"))) |
| 349 |
)) |
| 350 |
|
| 351 |
(defun toc-mode-return (kill) |
| 352 |
(rst-toc) |
| 353 |
(rst-toc-mode-return kill) |
| 354 |
(let ((buf (get-buffer rst-toc-buffer-name))) |
| 355 |
(when buf |
| 356 |
(kill-buffer buf) |
| 357 |
t))) |
| 358 |
|
| 359 |
(ert-deftest rst-toc-mode-return () |
| 360 |
"Tests `rst-toc-mode-return'." |
| 361 |
(should-error (ert-equal-buffer |
| 362 |
'(rst-toc-mode-return) |
| 363 |
"" |
| 364 |
t) |
| 365 |
:type 'error) |
| 366 |
(should (ert-equal-buffer-return |
| 367 |
'(toc-mode-return nil) |
| 368 |
(concat title " \^@\n\n" headers) |
| 369 |
t |
| 370 |
t)) |
| 371 |
(should (ert-equal-buffer-return |
| 372 |
'(toc-mode-return t) |
| 373 |
(concat title " \^@\n\n" headers) |
| 374 |
t |
| 375 |
nil)) |
| 376 |
) |