1 ;;; test-org-list.el --- Tests for org-list.el
3 ;; Copyright (C) 2012, 2013, 2014 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/>.
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"
27 (should-not (org-in-item-p)))
28 ;; With text less indented than top items.
29 (org-test-with-temp-text "- item\nText"
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"
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 "
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'
61 (should-error (org-next-item))
62 (let ((org-list-use-circular-motion t
))
63 (should (progn (org-next-item) t
)))
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.
71 (should (looking-at "- item 2"))
72 ;; 1.3. Shouldn't move to another list.
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
))
81 (should (looking-at "- item 1"))
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
91 (should-error (org-previous-item))
92 (let ((org-list-use-circular-motion t
))
93 (should (progn (org-previous-item) t
)))
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.
101 (should (looking-at "- item 1"))
102 ;; 2.3. Shouldn't move to another list.
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
))
111 (should (looking-at "- item 2"))
114 (should (looking-at " - item 1.3"))))))
116 (ert-deftest test-org-list
/cycle-bullet
()
117 "Test `org-cycle-list-bullet' specifications."
118 ;; Error when not at an item.
120 (org-test-with-temp-text "Paragraph"
121 (org-cycle-list-bullet)))
122 ;; Cycle through "-", "+", "*", "1.", "1)".
123 (org-test-with-temp-text " - item"
124 (org-cycle-list-bullet)
125 (should (looking-at "[ \t]+\\+"))
126 (org-cycle-list-bullet)
127 (should (looking-at "[ \t]+\\*"))
128 (let ((org-plain-list-ordered-item-terminator t
))
129 (org-cycle-list-bullet))
130 (should (looking-at "[ \t]+1\\."))
131 (let ((org-plain-list-ordered-item-terminator t
))
132 (org-cycle-list-bullet))
133 (should (looking-at "[ \t]+1)")))
134 ;; Argument is a valid bullet: cycle to that bullet directly.
137 (org-test-with-temp-text "- item"
138 (let ((org-plain-list-ordered-item-terminator t
))
139 (org-cycle-list-bullet "1.")
141 ;; Argument is an integer N: cycle to the Nth allowed bullet.
144 (org-test-with-temp-text "1. item"
145 (let ((org-plain-list-ordered-item-terminator t
))
146 (org-cycle-list-bullet 1)
148 ;; Argument is `previous': cycle backwards.
151 (org-test-with-temp-text "+ item"
152 (let ((org-plain-list-ordered-item-terminator t
))
153 (org-cycle-list-bullet 'previous
)
155 ;; Do not cycle to "*" bullets when item is at column 0.
158 (org-test-with-temp-text "+ item"
159 (let ((org-plain-list-ordered-item-terminator t
))
160 (org-cycle-list-bullet)
162 ;; Do not cycle to numbered bullets in a description list.
164 (equal "1. tag :: item"
165 (org-test-with-temp-text "+ tag :: item"
166 (let ((org-plain-list-ordered-item-terminator t
))
167 (org-cycle-list-bullet)
169 ;; Do not cycle to ordered item terminators if they are not allowed
170 ;; in `org-plain-list-ordered-item-terminator'.
173 (org-test-with-temp-text " * item"
174 (let ((org-plain-list-ordered-item-terminator 41))
175 (org-cycle-list-bullet)
177 ;; When `org-list-allow-alphabetical' is non-nil, cycle to alpha bullets.
180 (org-test-with-temp-text "1) item"
181 (let ((org-plain-list-ordered-item-terminator t
)
182 (org-list-allow-alphabetical t
))
183 (org-cycle-list-bullet)
185 ;; Do not cycle to alpha bullets when list has more than 26
189 (org-test-with-temp-text "1) item 1
216 (let ((org-plain-list-ordered-item-terminator t
)
217 (org-list-allow-alphabetical t
))
218 (org-cycle-list-bullet)
219 (buffer-substring (point) (line-end-position)))))))
221 (ert-deftest test-org-list
/indent-item
()
222 "Test `org-indent-item' specifications."
223 ;; 1. Error when not at an item.
224 (org-test-with-temp-text "Paragraph."
225 (should-error (org-indent-item)))
226 ;; 2. Error when trying to move first item of a list.
227 (org-test-with-temp-text "
231 (should-error (org-indent-item)))
232 ;; 3. Indent a single item, not its children.
233 (org-test-with-temp-text "
237 (search-forward "- Item 2")
238 (let (org-list-demote-modify-bullet) (org-indent-item))
239 (should (equal (buffer-string)
244 ;; 4. Follow `org-list-demote-modify-bullet' specifications.
246 ;; 4.1. With unordered lists.
247 (org-test-with-temp-text "
250 (search-forward "- Item 2")
251 (let ((org-list-demote-modify-bullet '(("-" .
"+")))) (org-indent-item))
252 (should (equal (buffer-string)
256 ;; 4.2. and ordered lists.
257 (org-test-with-temp-text "
260 (search-forward "2. Item 2")
261 (let ((org-plain-list-ordered-item-terminator t
)
262 (org-list-demote-modify-bullet '(("1." .
"+"))))
264 (should (equal (buffer-string)
268 ;; 5. When a region is selected, indent every item within.
269 (org-test-with-temp-text "
274 (search-forward "- Item 2")
276 (transient-mark-mode 1)
277 (push-mark (point) t t
)
278 (goto-char (point-max))
279 (let (org-list-demote-modify-bullet) (org-indent-item))
280 (should (equal (buffer-string)
287 (ert-deftest test-org-list
/indent-item-tree
()
288 "Test `org-indent-item-tree' specifications."
289 ;; 1. Error when not at an item.
290 (org-test-with-temp-text "Paragraph."
291 (should-error (org-indent-item-tree)))
292 ;; 2. Indent item along with its children.
293 (org-test-with-temp-text "
297 (search-forward "- Item 2")
298 (let (org-list-demote-modify-bullet) (org-indent-item-tree))
299 (should (equal (buffer-string)
304 ;; 3. Special case: When indenting top item, move the whole list.
305 (org-test-with-temp-text "
308 (search-forward "- Item 1")
309 (let (org-list-demote-modify-bullet org-odd-levels-only
)
310 (org-indent-item-tree))
311 (should (equal (buffer-string)
315 ;; 4. Follow `org-list-demote-modify-bullet' specifications.
317 ;; 4.1. With unordered lists.
318 (org-test-with-temp-text "
322 (search-forward "- Item 2")
323 (let ((org-list-demote-modify-bullet '(("-" .
"+") ("+" .
"-"))))
324 (org-indent-item-tree))
325 (should (equal (buffer-string)
330 ;; 4.2. and ordered lists.
331 (org-test-with-temp-text "
335 (search-forward "2. Item 2")
336 (let ((org-plain-list-ordered-item-terminator t
)
337 (org-list-demote-modify-bullet '(("1." .
"+") ("+" .
"1."))))
338 (org-indent-item-tree))
339 (should (equal (buffer-string)
344 ;; 5. When a region is selected, indent every item within.
345 (org-test-with-temp-text "
352 (search-forward "- Item 2")
354 (transient-mark-mode 1)
355 (push-mark (point) t t
)
356 (goto-char (point-max))
357 (let (org-list-demote-modify-bullet) (org-indent-item-tree))
358 (should (equal (buffer-string)
367 (ert-deftest test-org-list
/outdent-item
()
368 "Test `org-outdent-item' specifications."
369 ;; 1. Error when not at an item.
370 (org-test-with-temp-text "Paragraph."
371 (should-error (org-outdent-item)))
372 ;; 2. Error when trying to move first item of a list.
373 (org-test-with-temp-text "
377 (should-error (org-outdent-item)))
378 ;; 3. Error when trying to outdent an item without its children.
379 (org-test-with-temp-text "
383 (search-forward "- Item 1.1")
384 (should-error (org-outdent-item)))
385 ;; 4. Error when trying to outdent before top item.
386 (org-test-with-temp-text "
389 (search-forward "- Item 2")
390 (should-error (org-outdent-item)))
391 ;; 5. When a region is selected, outdent every item within.
392 (org-test-with-temp-text "
397 (search-forward "- Item 2")
399 (transient-mark-mode 1)
400 (push-mark (point) t t
)
401 (goto-char (point-max))
402 (let (org-list-demote-modify-bullet) (org-outdent-item))
403 (should (equal (buffer-string)
410 (ert-deftest test-org-list
/outdent-item-tree
()
411 "Test `org-outdent-item-tree' specifications."
412 ;; 1. Error when not at an item.
413 (org-test-with-temp-text "Paragraph."
414 (should-error (org-outdent-item-tree)))
415 ;; 2. Error when trying to outdent before top item.
416 (org-test-with-temp-text "
419 (search-forward "- Item 2")
420 (should-error (org-outdent-item-tree)))
421 ;; 3. Outdent item along with its children.
422 (org-test-with-temp-text "
426 (search-forward "- Item 2")
427 (org-outdent-item-tree)
428 (should (equal (buffer-string)
433 ;; 3. Special case: When outdenting top item, move the whole list.
434 (org-test-with-temp-text "
437 (search-forward "- Item 1")
438 (let (org-odd-levels-only) (org-outdent-item-tree))
439 (should (equal (buffer-string)
443 ;; 5. When a region is selected, outdent every item within.
444 (org-test-with-temp-text "
451 (search-forward "- Item 2")
453 (transient-mark-mode 1)
454 (push-mark (point) t t
)
455 (goto-char (point-max))
456 (org-outdent-item-tree)
457 (should (equal (buffer-string)
466 (ert-deftest test-org-list
/move-item-down
()
467 "Test `org-move-item-down' specifications."
469 (org-test-with-temp-text "- item 1\n- item 2"
471 (should (equal (buffer-string)
472 "- item 2\n- item 1")))
473 ;; Keep same column in item.
474 (org-test-with-temp-text "- item 1\n- item 2"
477 (should (looking-at "em 1")))
479 (org-test-with-temp-text "- item 1\n - sub-item 1\n- item 2"
481 (should (equal (buffer-string)
482 "- item 2\n- item 1\n - sub-item 1")))
483 ;; Preserve blank lines.
484 (org-test-with-temp-text "- item 1\n\n- item 2"
485 (let ((org-list-empty-line-terminates-plain-lists nil
)) (org-move-item-down))
486 (should (equal (buffer-string) "- item 2\n\n- item 1")))
487 ;; Error when trying to move the last item...
488 (org-test-with-temp-text "- item 1\n- item 2"
490 (should-error (org-move-item-down)))
491 ;; ... unless `org-list-use-circular-motion' is non-nil. In this
492 ;; case, move to the first item.
493 (org-test-with-temp-text "- item 1\n- item 2\n- item 3"
495 (let ((org-list-use-circular-motion t
)) (org-move-item-down))
496 (should (equal (buffer-string) "- item 3\n- item 1\n- item 2\n")))
497 ;; Preserve item visibility.
498 (org-test-with-temp-text "* Headline\n- item 1\n body 1\n- item 2\n body 2"
499 (let ((org-cycle-include-plain-lists t
))
500 (search-forward "- item 1")
502 (search-forward "- item 2")
504 (search-backward "- item 1")
507 (should (org-invisible-p2))
508 (search-backward " body 2")
509 (should (org-invisible-p2)))
510 ;; Preserve children visibility.
511 (org-test-with-temp-text "* Headline
518 (let ((org-cycle-include-plain-lists t
))
519 (search-forward "- sub-item 1")
521 (search-forward "- sub-item 2")
523 (search-backward "- item 1")
525 (search-forward "sub-body 1")
526 (should (org-invisible-p2))
527 (search-backward "sub-body 2")
528 (should (org-invisible-p2)))
529 ;; Preserve contents visibility.
530 (org-test-with-temp-text "
540 (search-forward "- item 1")
542 (search-forward "Text1")
543 (should (org-invisible-p2))
544 (search-backward "Text2")
545 (should (org-invisible-p2))))
547 (ert-deftest test-org-list
/move-item-up
()
548 "Test `org-move-item-up' specifications."
550 (org-test-with-temp-text "- item 1\n- item 2"
553 (should (equal (buffer-string)
554 "- item 2\n- item 1")))
555 ;; Keep same column in item.
556 (org-test-with-temp-text "- item 1\n- item 2"
560 (should (looking-at "em 2")))
562 (org-test-with-temp-text "- item 1\n- item 2\n - sub-item 2"
565 (should (equal (buffer-string)
566 "- item 2\n - sub-item 2\n- item 1")))
567 ;; Preserve blank lines.
568 (org-test-with-temp-text "- item 1\n\n- item 2"
569 (search-forward "- item 2")
570 (let ((org-list-empty-line-terminates-plain-lists nil
)) (org-move-item-up))
571 (should (equal (buffer-string) "- item 2\n\n- item 1")))
572 ;; Error when trying to move the first item...
573 (org-test-with-temp-text "- item 1\n- item 2"
574 (should-error (org-move-item-up)))
575 ;; ... unless `org-list-use-circular-motion' is non-nil. In this
576 ;; case, move to the first item.
577 (org-test-with-temp-text "- item 1\n- item 2\n- item 3"
578 (let ((org-list-use-circular-motion t
)) (org-move-item-up))
579 (should (equal (buffer-string) "- item 2\n- item 3\n- item 1")))
580 ;; Preserve item visibility.
581 (org-test-with-temp-text "* Headline\n- item 1\n body 1\n- item 2\n body 2"
582 (let ((org-cycle-include-plain-lists t
))
583 (search-forward "- item 1")
585 (search-forward "- item 2")
589 (should (org-invisible-p2))
590 (search-forward " body 1")
591 (should (org-invisible-p2)))
592 ;; Preserve children visibility.
593 (org-test-with-temp-text "* Headline
600 (let ((org-cycle-include-plain-lists t
))
601 (search-forward "- sub-item 1")
603 (search-forward "- sub-item 2")
605 (search-backward "- item 2")
607 (search-forward "sub-body 2")
608 (should (org-invisible-p2))
609 (search-forward "sub-body 1")
610 (should (org-invisible-p2)))
611 ;; Preserve contents visibility.
612 (org-test-with-temp-text "
622 (search-forward "- item 2")
624 (search-forward "Text2")
625 (should (org-invisible-p2))
626 (search-forward "Text1")
627 (should (org-invisible-p2))))
629 (ert-deftest test-org-list
/insert-item
()
630 "Test item insertion."
631 ;; Blank lines specifications.
633 ;; Non-nil `org-blank-before-new-entry': insert a blank line, unless
634 ;; `org-list-empty-line-terminates-plain-lists' is non-nil.
636 (org-test-with-temp-text "- a"
637 (let ((org-list-empty-line-terminates-plain-lists nil
)
638 (org-blank-before-new-entry '((plain-list-item . t
))))
644 (org-test-with-temp-text "- a"
645 (let ((org-list-empty-line-terminates-plain-lists t
)
646 (org-blank-before-new-entry '((plain-list-item . t
))))
651 ;; Nil `org-blank-before-new-entry': do not insert a blank line.
653 (org-test-with-temp-text "- a"
654 (let ((org-list-empty-line-terminates-plain-lists nil
)
655 (org-blank-before-new-entry '((plain-list-item . nil
))))
660 ;; `org-blank-before-new-entry' set to auto: if there's no blank
661 ;; line already in the sole item, do not insert one.
663 (org-test-with-temp-text "- a"
664 (let ((org-list-empty-line-terminates-plain-lists nil
)
665 (org-blank-before-new-entry '((plain-list-item . auto
))))
670 ;; `org-blank-before-new-entry' set to `auto': if there's a blank
671 ;; line in the sole item, insert another one.
673 (org-test-with-temp-text "- a\n\n b"
674 (let ((org-list-empty-line-terminates-plain-lists nil
)
675 (org-blank-before-new-entry '((plain-list-item . auto
))))
676 (goto-char (point-max))
680 ;; `org-blank-before-new-entry' set to `auto': if the user specified
681 ;; a blank line, preserve it.
683 (org-test-with-temp-text "- a\n\n"
684 (let ((org-list-empty-line-terminates-plain-lists nil
)
685 (org-blank-before-new-entry '((plain-list-item . auto
))))
686 (goto-char (point-max))
690 ;; `org-blank-before-new-entry' set to `auto': if some items in list
691 ;; are already separated by blank lines, insert one.
693 (org-test-with-temp-text "- a\n\n- b"
694 (let ((org-list-empty-line-terminates-plain-lists nil
)
695 (org-blank-before-new-entry '((plain-list-item . auto
))))
696 (goto-char (point-max))
701 (org-test-with-temp-text "- a\n\n- b"
702 (let ((org-list-empty-line-terminates-plain-lists nil
)
703 (org-blank-before-new-entry '((plain-list-item . auto
))))
708 (org-test-with-temp-text "- a\n #+BEGIN_EXAMPLE\n\n x\n #+END_EXAMPLE"
709 (let ((org-list-empty-line-terminates-plain-lists nil
)
710 (org-blank-before-new-entry '((plain-list-item . auto
))))
711 (goto-char (point-max))
716 (ert-deftest test-org-list
/repair
()
717 "Test `org-list-repair' specifications."
718 ;; Repair indentation.
720 (equal "- item\n - child"
721 (org-test-with-temp-text "- item\n - child"
722 (let ((org-list-indent-offset 0)) (org-list-repair))
724 ;; Repair bullets and numbering.
727 (org-test-with-temp-text "- a\n+ b"
728 (let ((org-list-indent-offset 0))
733 (org-test-with-temp-text "1. a\n1. b"
734 (let ((org-list-indent-offset 0)
735 (org-plain-list-ordered-item-terminator t
))
738 ;; Repair check-boxes.
740 (equal "- [X] item\n - [X] child"
741 (org-test-with-temp-text "- [ ] item\n - [X] child"
742 (let ((org-list-indent-offset 0))
745 ;; Special case: do not move contents of an item within its child.
747 (equal "- item\n - child\n within item"
748 (org-test-with-temp-text "- item\n - child\n within item"
749 (let ((org-list-indent-offset 0)) (org-list-repair))
756 (ert-deftest test-org-list
/send-list
()
757 "Test various checks for `org-list-send-list'."
758 ;; Error when not at a list item.
760 (org-test-with-temp-text "Not a list item"
761 (org-list-send-list)))
762 ;; Error when ORGLST line is not provided.
764 (org-test-with-temp-text "- item"
765 (org-list-send-list)))
766 ;; Error when transformation function is unknown.
768 (org-test-with-temp-text "@ignore
769 #+ORGLST: SEND list unknown-function
773 (org-list-send-list)))
774 ;; Error when receiving location is not defined.
776 (org-test-with-temp-text "@ignore
777 #+ORGLST: SEND list org-list-to-texinfo
781 (org-list-send-list)))
782 ;; Error when insertion region is ill-formed.
784 (org-test-with-temp-text "@c BEGIN RECEIVE ORGLST list
786 #+ORGLST: SEND list org-list-to-texinfo
790 (org-list-send-list))))
792 (ert-deftest test-org-list
/to-html
()
793 "Test `org-list-to-html' specifications."
795 (equal "<ul class=\"org-ul\">\n<li>a\n</li>\n</ul>"
796 (let (org-html-indent)
798 (insert "<!-- BEGIN RECEIVE ORGLST name -->
799 <!-- END RECEIVE ORGLST name -->
801 #+ORGLST: SEND name org-list-to-html
804 (goto-char (point-min))
805 (re-search-forward "^- a" nil t
)
809 (buffer-substring-no-properties
811 (progn (re-search-forward "^<!-- END" nil t
)
813 (skip-chars-backward " \r\t\n")
816 (ert-deftest test-org-list
/to-latex
()
817 "Test `org-list-to-latex' specifications."
819 (equal "\\begin{itemize}\n\\item a\n\\end{itemize}"
821 (insert "% BEGIN RECEIVE ORGLST name
822 % END RECEIVE ORGLST name
824 #+ORGLST: SEND name org-list-to-latex
827 (goto-char (point-min))
828 (re-search-forward "^- a" nil t
)
832 (buffer-substring-no-properties
834 (progn (re-search-forward "^% END" nil t
)
836 (skip-chars-backward " \r\t\n")
839 (ert-deftest test-org-list
/to-texinfo
()
840 "Test `org-list-to-texinfo' specifications."
842 (equal "@itemize\n@item\na\n@end itemize"
844 (insert "@c BEGIN RECEIVE ORGLST name
845 @c END RECEIVE ORGLST name
847 #+ORGLST: SEND name org-list-to-texinfo
850 (goto-char (point-min))
851 (re-search-forward "^- a" nil t
)
855 (buffer-substring-no-properties
857 (progn (re-search-forward "^@c END" nil t
)
859 (skip-chars-backward " \r\t\n")
863 (provide 'test-org-list
)
864 ;;; test-org-list.el ends here