Another fix for broken commit abbea59611
[org-mode/org-tableheadings.git] / testing / lisp / test-org-list.el
blob41721a563523cd3641f985ac2dd8e5c2f6ee73e0
1 ;;; test-org-list.el --- Tests for org-list.el
3 ;; Copyright (C) 2012 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Code:
22 (ert-deftest test-org-list/list-ending ()
23 "Test if lists end at the right place."
24 ;; With two blank lines.
25 (org-test-with-temp-text "- item\n\n\n Text"
26 (goto-line 4)
27 (should-not (org-in-item-p)))
28 ;; With text less indented than top items.
29 (org-test-with-temp-text "- item\nText"
30 (goto-line 2)
31 (should-not (org-in-item-p)))
32 ;; Though, blank lines and text indentation is ignored in blocks.
33 (org-test-with-temp-text
34 "- item\n #+begin_quote\n\n\nText at column 0\n #+end_quote\n Text"
35 (goto-line 7)
36 (should (org-in-item-p))))
38 (ert-deftest test-org-list/list-navigation ()
39 "Test list navigation specifications."
40 (org-test-with-temp-text "
41 - item A
42 - item B
45 - item 1
46 - item 1.1
47 - item 1.2
48 - item 1.3
49 - item 2
52 - item X
53 - item Y"
54 (let ((org-list-use-circular-motion nil))
55 ;; 1. Test `org-next-item'.
57 ;; 1.1. Should return an error if at last item in
58 ;; a list/sub-list, unless `org-list-use-circular-motion'
59 ;; is non-nil.
60 (goto-line 9)
61 (should-error (org-next-item))
62 (let ((org-list-use-circular-motion t))
63 (should (progn (org-next-item) t)))
64 (goto-line 14)
65 (should-error (org-next-item))
66 (let ((org-list-use-circular-motion t))
67 (should (progn (org-next-item) t)))
68 ;; 1.2. Should jump over sub-lists.
69 (goto-line 6)
70 (org-next-item)
71 (should (looking-at "- item 2"))
72 ;; 1.3. Shouldn't move to another list.
73 (goto-line 3)
74 (should-error (org-next-item))
75 (should-not (looking-at "- item 1"))
76 ;; 1.4. Should move to the list/sub-list first item when
77 ;; `org-list-use-circular-motion' is non-nil.
78 (let ((org-list-use-circular-motion t))
79 (goto-line 10)
80 (org-next-item)
81 (should (looking-at "- item 1"))
82 (goto-line 9)
83 (org-next-item)
84 (should (looking-at " - item 1.1")))
85 ;; 2. Test `org-previous-item'.
87 ;; 2.1. Should return an error if at first item in
88 ;; a list/sub-list, unless `org-list-use-circular-motion is
89 ;; non-nil.
90 (goto-line 7)
91 (should-error (org-previous-item))
92 (let ((org-list-use-circular-motion t))
93 (should (progn (org-previous-item) t)))
94 (goto-line 13)
95 (should-error (org-previous-item))
96 (let ((org-list-use-circular-motion t))
97 (should (progn (org-previous-item) t)))
98 ;; 2.2. Should ignore sub-lists.
99 (goto-line 10)
100 (org-previous-item)
101 (should (looking-at "- item 1"))
102 ;; 2.3. Shouldn't move to another list.
103 (goto-line 6)
104 (should-error (org-previous-item))
105 (should-not (looking-at "- item B"))
106 ;; 2.4. Should move to the list/sub-list last item when
107 ;; `org-list-use-circular-motion' is non-nil.
108 (let ((org-list-use-circular-motion t))
109 (goto-line 6)
110 (org-previous-item)
111 (should (looking-at "- item 2"))
112 (goto-line 7)
113 (org-previous-item)
114 (should (looking-at " - item 1.3"))))))
116 (ert-deftest test-org-list/indent-item ()
117 "Test `org-indent-item' specifications."
118 ;; 1. Error when not at an item.
119 (org-test-with-temp-text "Paragraph."
120 (should-error (org-indent-item)))
121 ;; 2. Error when trying to move first item of a list.
122 (org-test-with-temp-text "
123 - Item 1
124 - Item 2"
125 (forward-line)
126 (should-error (org-indent-item)))
127 ;; 3. Indent a single item, not its children.
128 (org-test-with-temp-text "
129 - Item 1
130 - Item 2
131 - Item 2.1"
132 (search-forward "- Item 2")
133 (let (org-list-demote-modify-bullet) (org-indent-item))
134 (should (equal (buffer-string)
136 - Item 1
137 - Item 2
138 - Item 2.1")))
139 ;; 4. Follow `org-list-demote-modify-bullet' specifications.
141 ;; 4.1. With unordered lists.
142 (org-test-with-temp-text "
143 - Item 1
144 - Item 2"
145 (search-forward "- Item 2")
146 (let ((org-list-demote-modify-bullet '(("-" . "+")))) (org-indent-item))
147 (should (equal (buffer-string)
149 - Item 1
150 + Item 2")))
151 ;; 4.2. and ordered lists.
152 (org-test-with-temp-text "
153 1. Item 1
154 2. Item 2"
155 (search-forward "2. Item 2")
156 (let ((org-plain-list-ordered-item-terminator t)
157 (org-list-demote-modify-bullet '(("1." . "+"))))
158 (org-indent-item))
159 (should (equal (buffer-string)
161 1. Item 1
162 + Item 2")))
163 ;; 5. When a region is selected, indent every item within.
164 (org-test-with-temp-text "
165 - Item 1
166 - Item 2
167 - Item 3
169 (search-forward "- Item 2")
170 (beginning-of-line)
171 (transient-mark-mode 1)
172 (push-mark (point) t t)
173 (goto-char (point-max))
174 (let (org-list-demote-modify-bullet) (org-indent-item))
175 (should (equal (buffer-string)
177 - Item 1
178 - Item 2
179 - Item 3
180 "))))
182 (ert-deftest test-org-list/indent-item-tree ()
183 "Test `org-indent-item-tree' specifications."
184 ;; 1. Error when not at an item.
185 (org-test-with-temp-text "Paragraph."
186 (should-error (org-indent-item-tree)))
187 ;; 2. Indent item along with its children.
188 (org-test-with-temp-text "
189 - Item 1
190 - Item 2
191 - Item 2.1"
192 (search-forward "- Item 2")
193 (let (org-list-demote-modify-bullet) (org-indent-item-tree))
194 (should (equal (buffer-string)
196 - Item 1
197 - Item 2
198 - Item 2.1")))
199 ;; 3. Special case: When indenting top item, move the whole list.
200 (org-test-with-temp-text "
201 - Item 1
202 - Item 2"
203 (search-forward "- Item 1")
204 (let (org-list-demote-modify-bullet org-odd-levels-only)
205 (org-indent-item-tree))
206 (should (equal (buffer-string)
208 - Item 1
209 - Item 2")))
210 ;; 4. Follow `org-list-demote-modify-bullet' specifications.
212 ;; 4.1. With unordered lists.
213 (org-test-with-temp-text "
214 - Item 1
215 - Item 2
216 + Item 2.1"
217 (search-forward "- Item 2")
218 (let ((org-list-demote-modify-bullet '(("-" . "+") ("+" . "-"))))
219 (org-indent-item-tree))
220 (should (equal (buffer-string)
222 - Item 1
223 + Item 2
224 - Item 2.1")))
225 ;; 4.2. and ordered lists.
226 (org-test-with-temp-text "
227 1. Item 1
228 2. Item 2
229 + Item 2.1"
230 (search-forward "2. Item 2")
231 (let ((org-plain-list-ordered-item-terminator t)
232 (org-list-demote-modify-bullet '(("1." . "+") ("+" . "1."))))
233 (org-indent-item-tree))
234 (should (equal (buffer-string)
236 1. Item 1
237 + Item 2
238 1. Item 2.1")))
239 ;; 5. When a region is selected, indent every item within.
240 (org-test-with-temp-text "
241 - Item 1
242 - Item 2
243 - Item 2.1
244 - Item 3
245 - Item 3.1
247 (search-forward "- Item 2")
248 (beginning-of-line)
249 (transient-mark-mode 1)
250 (push-mark (point) t t)
251 (goto-char (point-max))
252 (let (org-list-demote-modify-bullet) (org-indent-item-tree))
253 (should (equal (buffer-string)
255 - Item 1
256 - Item 2
257 - Item 2.1
258 - Item 3
259 - Item 3.1
260 "))))
262 (ert-deftest test-org-list/outdent-item ()
263 "Test `org-outdent-item' specifications."
264 ;; 1. Error when not at an item.
265 (org-test-with-temp-text "Paragraph."
266 (should-error (org-outdent-item)))
267 ;; 2. Error when trying to move first item of a list.
268 (org-test-with-temp-text "
269 - Item 1
270 - Item 2"
271 (forward-line)
272 (should-error (org-outdent-item)))
273 ;; 3. Error when trying to outdent an item without its children.
274 (org-test-with-temp-text "
275 - Item 1
276 - Item 1.1
277 - Item 1.1.1"
278 (search-forward "- Item 1.1")
279 (should-error (org-outdent-item)))
280 ;; 4. Error when trying to outdent before top item.
281 (org-test-with-temp-text "
282 - Item 1
283 - Item 2"
284 (search-forward "- Item 2")
285 (should-error (org-outdent-item)))
286 ;; 5. When a region is selected, outdent every item within.
287 (org-test-with-temp-text "
288 - Item 1
289 - Item 2
290 - Item 3
292 (search-forward "- Item 2")
293 (beginning-of-line)
294 (transient-mark-mode 1)
295 (push-mark (point) t t)
296 (goto-char (point-max))
297 (let (org-list-demote-modify-bullet) (org-outdent-item))
298 (should (equal (buffer-string)
300 - Item 1
301 - Item 2
302 - Item 3
303 "))))
305 (ert-deftest test-org-list/outdent-item-tree ()
306 "Test `org-outdent-item-tree' specifications."
307 ;; 1. Error when not at an item.
308 (org-test-with-temp-text "Paragraph."
309 (should-error (org-outdent-item-tree)))
310 ;; 2. Error when trying to outdent before top item.
311 (org-test-with-temp-text "
312 - Item 1
313 - Item 2"
314 (search-forward "- Item 2")
315 (should-error (org-outdent-item-tree)))
316 ;; 3. Outdent item along with its children.
317 (org-test-with-temp-text "
318 - Item 1
319 - Item 2
320 - Item 2.1"
321 (search-forward "- Item 2")
322 (org-outdent-item-tree)
323 (should (equal (buffer-string)
325 - Item 1
326 - Item 2
327 - Item 2.1")))
328 ;; 3. Special case: When outdenting top item, move the whole list.
329 (org-test-with-temp-text "
330 - Item 1
331 - Item 2"
332 (search-forward "- Item 1")
333 (let (org-odd-levels-only) (org-outdent-item-tree))
334 (should (equal (buffer-string)
336 - Item 1
337 - Item 2")))
338 ;; 5. When a region is selected, outdent every item within.
339 (org-test-with-temp-text "
340 - Item 1
341 - Item 2
342 - Item 2.1
343 - Item 3
344 - Item 3.1
346 (search-forward "- Item 2")
347 (beginning-of-line)
348 (transient-mark-mode 1)
349 (push-mark (point) t t)
350 (goto-char (point-max))
351 (org-outdent-item-tree)
352 (should (equal (buffer-string)
354 - Item 1
355 - Item 2
356 - Item 2.1
357 - Item 3
358 - Item 3.1
359 "))))
361 (ert-deftest test-org-list/move-item-down ()
362 "Test `org-move-item-down' specifications."
363 ;; Standard test.
364 (org-test-with-temp-text "- item 1\n- item 2"
365 (org-move-item-down)
366 (should (equal (buffer-string)
367 "- item 2\n- item 1")))
368 ;; Keep same column in item.
369 (org-test-with-temp-text "- item 1\n- item 2"
370 (forward-char 4)
371 (org-move-item-down)
372 (should (looking-at "em 1")))
373 ;; Move sub-items.
374 (org-test-with-temp-text "- item 1\n - sub-item 1\n- item 2"
375 (org-move-item-down)
376 (should (equal (buffer-string)
377 "- item 2\n- item 1\n - sub-item 1")))
378 ;; Preserve blank lines.
379 (org-test-with-temp-text "- item 1\n\n- item 2"
380 (let ((org-empty-line-terminates-plain-lists nil)) (org-move-item-down))
381 (should (equal (buffer-string) "- item 2\n\n- item 1")))
382 ;; Error when trying to move the last item...
383 (org-test-with-temp-text "- item 1\n- item 2"
384 (forward-line)
385 (should-error (org-move-item-down)))
386 ;; ... unless `org-list-use-circular-motion' is non-nil. In this
387 ;; case, move to the first item.
388 (org-test-with-temp-text "- item 1\n- item 2\n- item 3"
389 (forward-line 2)
390 (let ((org-list-use-circular-motion t)) (org-move-item-down))
391 (should (equal (buffer-string) "- item 3\n- item 1\n- item 2\n")))
392 ;; Preserve item visibility.
393 (org-test-with-temp-text "* Headline\n- item 1\n body 1\n- item 2\n body 2"
394 (let ((org-cycle-include-plain-lists t))
395 (search-forward "- item 1")
396 (org-cycle)
397 (search-forward "- item 2")
398 (org-cycle))
399 (search-backward "- item 1")
400 (org-move-item-down)
401 (forward-line)
402 (should (org-invisible-p2))
403 (search-backward " body 2")
404 (should (org-invisible-p2)))
405 ;; Preserve children visibility.
406 (org-test-with-temp-text "* Headline
407 - item 1
408 - sub-item 1
409 sub-body 1
410 - item 2
411 - sub-item 2
412 sub-body 2"
413 (let ((org-cycle-include-plain-lists t))
414 (search-forward "- sub-item 1")
415 (org-cycle)
416 (search-forward "- sub-item 2")
417 (org-cycle))
418 (search-backward "- item 1")
419 (org-move-item-down)
420 (search-forward "sub-body 1")
421 (should (org-invisible-p2))
422 (search-backward "sub-body 2")
423 (should (org-invisible-p2)))
424 ;; Preserve contents visibility.
425 (org-test-with-temp-text "
426 - item 1
427 #+BEGIN_CENTER
428 Text1
429 #+END_CENTER
430 - item 2
431 #+BEGIN_CENTER
432 Text2
433 #+END_CENTER"
434 (org-hide-block-all)
435 (search-forward "- item 1")
436 (org-move-item-down)
437 (search-forward "Text1")
438 (should (org-invisible-p2))
439 (search-backward "Text2")
440 (should (org-invisible-p2))))
442 (ert-deftest test-org-list/move-item-up ()
443 "Test `org-move-item-up' specifications."
444 ;; Standard test.
445 (org-test-with-temp-text "- item 1\n- item 2"
446 (forward-line)
447 (org-move-item-up)
448 (should (equal (buffer-string)
449 "- item 2\n- item 1")))
450 ;; Keep same column in item.
451 (org-test-with-temp-text "- item 1\n- item 2"
452 (forward-line)
453 (forward-char 4)
454 (org-move-item-up)
455 (should (looking-at "em 2")))
456 ;; Move sub-items.
457 (org-test-with-temp-text "- item 1\n- item 2\n - sub-item 2"
458 (forward-line)
459 (org-move-item-up)
460 (should (equal (buffer-string)
461 "- item 2\n - sub-item 2\n- item 1")))
462 ;; Preserve blank lines.
463 (org-test-with-temp-text "- item 1\n\n- item 2"
464 (search-forward "- item 2")
465 (let ((org-empty-line-terminates-plain-lists nil)) (org-move-item-up))
466 (should (equal (buffer-string) "- item 2\n\n- item 1")))
467 ;; Error when trying to move the first item...
468 (org-test-with-temp-text "- item 1\n- item 2"
469 (should-error (org-move-item-up)))
470 ;; ... unless `org-list-use-circular-motion' is non-nil. In this
471 ;; case, move to the first item.
472 (org-test-with-temp-text "- item 1\n- item 2\n- item 3"
473 (let ((org-list-use-circular-motion t)) (org-move-item-up))
474 (should (equal (buffer-string) "- item 2\n- item 3\n- item 1")))
475 ;; Preserve item visibility.
476 (org-test-with-temp-text "* Headline\n- item 1\n body 1\n- item 2\n body 2"
477 (let ((org-cycle-include-plain-lists t))
478 (search-forward "- item 1")
479 (org-cycle)
480 (search-forward "- item 2")
481 (org-cycle))
482 (org-move-item-up)
483 (forward-line)
484 (should (org-invisible-p2))
485 (search-forward " body 1")
486 (should (org-invisible-p2)))
487 ;; Preserve children visibility.
488 (org-test-with-temp-text "* Headline
489 - item 1
490 - sub-item 1
491 sub-body 1
492 - item 2
493 - sub-item 2
494 sub-body 2"
495 (let ((org-cycle-include-plain-lists t))
496 (search-forward "- sub-item 1")
497 (org-cycle)
498 (search-forward "- sub-item 2")
499 (org-cycle))
500 (search-backward "- item 2")
501 (org-move-item-up)
502 (search-forward "sub-body 2")
503 (should (org-invisible-p2))
504 (search-forward "sub-body 1")
505 (should (org-invisible-p2)))
506 ;; Preserve contents visibility.
507 (org-test-with-temp-text "
508 - item 1
509 #+BEGIN_CENTER
510 Text1
511 #+END_CENTER
512 - item 2
513 #+BEGIN_CENTER
514 Text2
515 #+END_CENTER"
516 (org-hide-block-all)
517 (search-forward "- item 2")
518 (org-move-item-up)
519 (search-forward "Text2")
520 (should (org-invisible-p2))
521 (search-forward "Text1")
522 (should (org-invisible-p2))))
525 (provide 'test-org-list)
526 ;;; test-org-list.el ends here