Debian Codegraph @

tools/editors/emacs/tests/apply-block.el

raw
1 ;; Tests for various functions around applying a function to an indented block -*- lexical-binding: t -*-
2
3 (add-to-list 'load-path ".")
4 (load "init" nil t)
5 (init-rst-ert t)
6
7 (ert-deftest apply-block-asserts ()
8 "Check some assertions."
9 (should (equal ert-Buf-point-char "\^@"))
10 (should (equal ert-Buf-mark-char "\^?"))
11 )
12
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
15 (defun find-leftmost-column ()
16 "Call `rst-find-leftmost-column' with current region."
17 (rst-find-leftmost-column (region-beginning) (region-end)))
18
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 \^?"
25 t
26 0))
27 (should (ert-equal-buffer-return
28 '(find-leftmost-column)
29 "
30 \^@abc
31 \^?"
32 t
33 0))
34 (should (ert-equal-buffer-return
35 '(find-leftmost-column)
36 "
37 \^@ abc
38 \^?"
39 t
40 2))
41 (should (ert-equal-buffer-return
42 '(find-leftmost-column)
43 "
44 \^@ abc
45 def
46 \^?"
47 t
48 0))
49 (should (ert-equal-buffer-return
50 '(find-leftmost-column)
51 "
52 \^@ abc
53 def
54 \^?"
55 t
56 2))
57 (should (ert-equal-buffer-return
58 '(find-leftmost-column)
59 "
60 \^@ abc
61 def
62 \^?"
63 t
64 4))
65 (should (ert-equal-buffer-return
66 '(find-leftmost-column)
67 ; Empty lines contain spaces
68 "
69 \^@
70
71 abc
72
73 def
74
75 \^?"
76 t
77 4))
78 (should (ert-equal-buffer-return
79 '(find-leftmost-column)
80 " abc\^@
81 def\^?"
82 t
83 0))
84 (should (ert-equal-buffer-return
85 '(find-leftmost-column)
86 "
87 abc\^@
88 def
89 \^?"
90 t
91 2))
92 (should (ert-equal-buffer-return
93 '(find-leftmost-column)
94 " a\^@b\^?c
95 def"
96 t
97 2))
98 (should (ert-equal-buffer-return
99 '(find-leftmost-column)
100 "
101 \^@ abc
102 \^? def
103 "
104 t
105 4))
106 (should (ert-equal-buffer-return
107 '(find-leftmost-column)
108 "
109 \^@ abc
110 \^? def
111 "
112 t
113 2))
114 (should (ert-equal-buffer-return
115 '(find-leftmost-column)
116 "
117 \^@ abc
118 d\^?ef
119 "
120 t
121 2))
122 )
123
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125
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))
129
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)
135 "
136 \^@
137 eins
138 one
139
140 zwei
141 two
142 \^?"
143 "
144 \^@
145 * eins
146 one
147
148 * zwei
149 two
150 \^?"))
151 (should (ert-equal-buffer
152 '(bullet-list-region nil)
153 "
154 \^@
155 eins
156 one
157
158 intermediate
159
160 zwei
161 two
162 \^?"
163 "
164 \^@
165 * eins
166 one
167
168 intermediate
169
170 * zwei
171 two
172 \^?"))
173 (should (ert-equal-buffer
174 '(bullet-list-region nil)
175 "
176 \^@
177 eins
178 one
179
180 zwei
181 two\^?"
182 "
183 \^@
184 * eins
185 one
186
187 * zwei
188 two\^?"))
189 (should (ert-equal-buffer
190 '(bullet-list-region t)
191 "
192 \^@
193 eins
194 zwei
195
196 drei
197
198 vier
199 \^?"
200 "
201 \^@
202 * eins
203 * zwei
204
205 * drei
206
207 vier
208 \^?"))
209 ))
210
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)
216 ""
217 t
218 )
219 :type 'error)
220 ))
221
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223
224 (defun enumerate-region (all)
225 "Call `rst-enumerate-region' with current region and ALL."
226 (rst-enumerate-region (region-beginning) (region-end) all))
227
228 (ert-deftest rst-enumerate-region ()
229 "Tests for `rst-enumerate-region'."
230 (should (ert-equal-buffer
231 '(enumerate-region nil)
232 "
233 \^@eins
234 one
235
236 zwei
237 two
238 \^?"
239 "
240 \^@1. eins
241 one
242
243 2. zwei
244 two
245 \^?"))
246 (should (ert-equal-buffer
247 '(enumerate-region nil)
248 "
249 \^@eins
250 one
251
252 intermediate
253
254 zwei
255 two
256 \^?"
257 "
258 \^@1. eins
259 one
260
261 intermediate
262
263 2. zwei
264 two
265 \^?"))
266 (should (ert-equal-buffer
267 '(enumerate-region t)
268 "
269 \^@eins
270 zwei
271
272 drei
273 \^?"
274 "
275 \^@1. eins
276 2. zwei
277
278 3. drei
279 \^?"))
280 )
281
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283
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))
287
288 (ert-deftest rst-line-block-region ()
289 "Tests for `rst-line-block-region'."
290 (should (ert-equal-buffer
291 '(line-block-region nil)
292 "
293 \^@
294 eins
295 one
296
297 zwei
298 \^?"
299 "
300 \^@
301 | eins
302 | one
303
304 | zwei
305 \^?"))
306 (should (ert-equal-buffer
307 '(line-block-region nil)
308 "
309 \^@
310 eins
311 one
312
313 zwei
314 two
315 \^?"
316 "
317 \^@
318 | eins
319 | one
320
321 | zwei
322 | two
323 \^?"))
324 (should (ert-equal-buffer
325 '(line-block-region nil)
326 "
327 \^@
328 eins
329 one
330
331 zwei
332 two
333 \^?"
334 "
335 \^@
336 | eins
337 | one
338
339 | zwei
340 | two
341 \^?"))
342 (should (ert-equal-buffer
343 '(line-block-region t)
344 "
345 \^@
346 eins
347 one
348
349 zwei
350 \^?"
351 "
352 \^@|
353 | eins
354 | one
355 |
356 | zwei
357 \^?"))
358 (should (ert-equal-buffer
359 '(line-block-region t)
360 "
361 \^@
362 eins
363 one
364
365 zwei
366 \^?"
367 "
368 \^@|
369 | eins
370 | one
371 |
372 | zwei
373 \^?"))
374 (should (ert-equal-buffer
375 '(line-block-region t)
376 "
377 \^@
378 eins
379 one
380
381 zwei
382 \^?"
383 "
384 \^@ |
385 | eins
386 | one
387 |
388 | zwei
389 \^?"))
390 )
391
392 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
393
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))))
412
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 \^?"
419 t
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"
425 t
426 '((0 1 t nil nil nil 0)
427 nil)))
428 (should (ert-equal-buffer-return
429 '(apply-indented-blocks 2)
430 "\^@
431 abc
432 \^?"
433 t
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 "\^@
440 abc
441
442 def
443 \^?"
444 t
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 "\^@
453 abc
454
455 def
456 ghi
457
458 \^?"
459 t
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"
470 t
471 '(nil)))
472 (should (ert-equal-buffer-return
473 '(apply-indented-blocks 2)
474 "\^@
475 abc
476
477 def
478 ghi
479 \^?"
480 t
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 "\^@
490 abc
491
492 def
493
494 ghi
495 jkl
496 mno
497 \^?"
498 t
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 "\^@
511 abc
512
513 def
514 \^?"
515 t
516 '((0 0 nil nil nil t nil)
517 (2 1 t nil nil nil 0)
518 2)))
519 )