Lots of refactorings and a few minor improvements.
[docutils.git] / docutils / tools / editors / emacs / tests / apply-block.el
blob98916a1d0e749107fddd702e53b0961d4873fe75
1 ;; Tests for various functions around applying a function to an indented block -*- lexical-binding: t -*-
3 (add-to-list 'load-path ".")
4 (load "init" nil t)
5 (init-rst-ert t)
7 (ert-deftest apply-block-asserts ()
8 "Check some assertions."
9 (should (equal ert-Buf-point-char "\^@"))
10 (should (equal ert-Buf-mark-char "\^?"))
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 (defun find-leftmost-column ()
16 "Call `rst-find-leftmost-column' with current region."
17 (rst-find-leftmost-column (region-beginning) (region-end)))
19 (ert-deftest rst-find-leftmost-column ()
20 "Tests for `rst-find-leftmost-column'."
21 (should (ert-equal-buffer-return
22 '(find-leftmost-column)
23 "\^@abc
24 \^?"
26 0))
27 (should (ert-equal-buffer-return
28 '(find-leftmost-column)
30 \^@abc
31 \^?"
33 0))
34 (should (ert-equal-buffer-return
35 '(find-leftmost-column)
37 \^@ abc
38 \^?"
40 2))
41 (should (ert-equal-buffer-return
42 '(find-leftmost-column)
44 \^@ abc
45 def
46 \^?"
48 0))
49 (should (ert-equal-buffer-return
50 '(find-leftmost-column)
52 \^@ abc
53 def
54 \^?"
56 2))
57 (should (ert-equal-buffer-return
58 '(find-leftmost-column)
60 \^@ abc
61 def
62 \^?"
64 4))
65 (should (ert-equal-buffer-return
66 '(find-leftmost-column)
67 ; Empty lines contain spaces
69 \^@
71 abc
73 def
75 \^?"
77 4))
78 (should (ert-equal-buffer-return
79 '(find-leftmost-column)
80 " abc\^@
81 def\^?"
83 0))
84 (should (ert-equal-buffer-return
85 '(find-leftmost-column)
87 abc\^@
88 def
89 \^?"
91 2))
92 (should (ert-equal-buffer-return
93 '(find-leftmost-column)
94 " a\^@b\^?c
95 def"
97 2))
98 (should (ert-equal-buffer-return
99 '(find-leftmost-column)
101 \^@ abc
102 \^? def
106 (should (ert-equal-buffer-return
107 '(find-leftmost-column)
109 \^@ abc
110 \^? def
114 (should (ert-equal-buffer-return
115 '(find-leftmost-column)
117 \^@ abc
118 d\^?ef
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126 (defun bullet-list-region (all)
127 "Call `rst-bullet-list-region' with current region and ALL."
128 (rst-bullet-list-region (region-beginning) (region-end) all))
130 (ert-deftest rst-bullet-list-region ()
131 "Tests for `rst-bullet-list-region'."
132 (let ((rst-preferred-bullets '(?*)))
133 (should (ert-equal-buffer
134 '(bullet-list-region nil)
137 eins
140 zwei
142 \^?"
145 * eins
148 * zwei
150 \^?"))
151 (should (ert-equal-buffer
152 '(bullet-list-region nil)
155 eins
158 intermediate
160 zwei
162 \^?"
165 * eins
168 intermediate
170 * zwei
172 \^?"))
173 (should (ert-equal-buffer
174 '(bullet-list-region nil)
177 eins
180 zwei
181 two\^?"
184 * eins
187 * zwei
188 two\^?"))
189 (should (ert-equal-buffer
190 '(bullet-list-region t)
193 eins
194 zwei
196 drei
198 vier
199 \^?"
202 * eins
203 * zwei
205 * drei
207 vier
208 \^?"))
211 (ert-deftest rst-bullet-list-region-error ()
212 "Tests for `rst-bullet-list-region' ending in an error."
213 (let ((rst-preferred-bullets nil))
214 (should-error (ert-equal-buffer
215 '(bullet-list-region nil)
219 :type 'error)
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224 (defun enumerate-region (all)
225 "Call `rst-enumerate-region' with current region and ALL."
226 (rst-enumerate-region (region-beginning) (region-end) all))
228 (ert-deftest rst-enumerate-region ()
229 "Tests for `rst-enumerate-region'."
230 (should (ert-equal-buffer
231 '(enumerate-region nil)
233 \^@eins
236 zwei
238 \^?"
240 \^@1. eins
243 2. zwei
245 \^?"))
246 (should (ert-equal-buffer
247 '(enumerate-region nil)
249 \^@eins
252 intermediate
254 zwei
256 \^?"
258 \^@1. eins
261 intermediate
263 2. zwei
265 \^?"))
266 (should (ert-equal-buffer
267 '(enumerate-region t)
269 \^@eins
270 zwei
272 drei
273 \^?"
275 \^@1. eins
276 2. zwei
278 3. drei
279 \^?"))
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 (defun line-block-region (empty)
285 "Call `rst-line-block-region' with current region and EMPTY."
286 (rst-line-block-region (region-beginning) (region-end) empty))
288 (ert-deftest rst-line-block-region ()
289 "Tests for `rst-line-block-region'."
290 (should (ert-equal-buffer
291 '(line-block-region nil)
294 eins
297 zwei
298 \^?"
301 | eins
302 | one
304 | zwei
305 \^?"))
306 (should (ert-equal-buffer
307 '(line-block-region nil)
310 eins
313 zwei
315 \^?"
318 | eins
319 | one
321 | zwei
322 | two
323 \^?"))
324 (should (ert-equal-buffer
325 '(line-block-region nil)
328 eins
331 zwei
333 \^?"
336 | eins
337 | one
339 | zwei
340 | two
341 \^?"))
342 (should (ert-equal-buffer
343 '(line-block-region t)
346 eins
349 zwei
350 \^?"
352 \^@|
353 | eins
354 | one
356 | zwei
357 \^?"))
358 (should (ert-equal-buffer
359 '(line-block-region t)
362 eins
365 zwei
366 \^?"
368 \^@|
369 | eins
370 | one
372 | zwei
373 \^?"))
374 (should (ert-equal-buffer
375 '(line-block-region t)
378 eins
381 zwei
382 \^?"
384 \^@ |
385 | eins
386 | one
388 | zwei
389 \^?"))
392 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
394 (defun apply-indented-blocks (ind &optional limit)
395 "Call `rst-apply-indented-blocks' on current region with IND.
396 Stop after LIMIT calls if given. Return a list of all parameter
397 packs to the callback function followed by the return value."
398 (let ((called 0)
399 params r)
400 (setq r (rst-apply-indented-blocks
401 (region-beginning) (region-end) ind
402 #'(lambda (count firstp subp supp emptyp relind)
403 (setq params
404 (append
405 params
406 (list
407 (list (current-column)
408 count firstp subp supp emptyp relind))))
409 (cl-incf called)
410 (and limit (>= called limit) called))))
411 (append params (list r))))
413 (ert-deftest rst-apply-indented-blocks ()
414 "Tests for `rst-apply-indented-blocks'."
415 (should (ert-equal-buffer-return
416 '(apply-indented-blocks 0)
417 "\^@abc
418 \^?"
420 '((0 1 t nil nil nil 0)
421 nil)))
422 (should (ert-equal-buffer-return
423 '(apply-indented-blocks 0)
424 "a\^@b\^?c"
426 '((0 1 t nil nil nil 0)
427 nil)))
428 (should (ert-equal-buffer-return
429 '(apply-indented-blocks 2)
430 "\^@
432 \^?"
434 '((2 0 nil nil nil t nil)
435 (2 1 t nil nil nil 0)
436 nil)))
437 (should (ert-equal-buffer-return
438 '(apply-indented-blocks 2)
439 "\^@
443 \^?"
445 '((0 0 nil nil nil t nil)
446 (2 1 t nil nil nil 0)
447 (0 1 nil nil nil t nil)
448 (4 1 nil t nil nil 2)
449 nil)))
450 (should (ert-equal-buffer-return
451 '(apply-indented-blocks 2)
452 "\^@
458 \^?"
460 '((0 0 nil nil nil t nil)
461 (2 1 t nil nil nil 0)
462 (0 1 nil nil nil t nil)
463 (4 1 nil t nil nil 2)
464 (6 1 nil t nil nil 4)
465 (2 1 nil t nil t nil)
466 nil)))
467 (should (ert-equal-buffer-return
468 '(apply-indented-blocks 0)
469 "\^@\^?abc"
471 '(nil)))
472 (should (ert-equal-buffer-return
473 '(apply-indented-blocks 2)
474 "\^@
479 \^?"
481 '((0 0 nil nil nil t nil)
482 (2 1 t nil nil nil 0)
483 (0 1 nil nil nil t nil)
484 (2 2 t nil nil nil 0)
485 (4 2 nil t nil nil 2)
486 nil)))
487 (should (ert-equal-buffer-return
488 '(apply-indented-blocks 2)
489 "\^@
497 \^?"
499 '((0 0 nil nil nil t nil)
500 (2 1 t nil nil nil 0)
501 (0 1 nil nil nil t nil)
502 (0 1 nil nil t nil -2)
503 (0 1 nil nil t t nil)
504 (4 1 nil t nil nil 2)
505 (2 2 t nil nil nil 0)
506 (2 2 nil nil nil nil 0)
507 nil)))
508 (should (ert-equal-buffer-return
509 '(apply-indented-blocks 2 2)
510 "\^@
514 \^?"
516 '((0 0 nil nil nil t nil)
517 (2 1 t nil nil nil 0)
518 2)))