org.el (org-make-org-heading-search-string): Rewrite using org-element.el
[org-mode.git] / testing / lisp / test-org.el
blob33bec7e96e4a68d4e0a2e6d8915a625c02f018e6
1 ;;; test-org.el --- tests for org.el
3 ;; Copyright (c) David Maus
4 ;; Authors: David Maus
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Comments:
23 ;; Template test file for Org-mode tests
25 ;;; Code:
28 ;;; Comments
30 (ert-deftest test-org/comment-dwim ()
31 "Test `comment-dwim' behaviour in an Org buffer."
32 ;; No region selected, no comment on current line and line not
33 ;; empty: insert comment on line above.
34 (should
35 (equal "# \nComment"
36 (org-test-with-temp-text "Comment"
37 (progn (call-interactively 'comment-dwim)
38 (buffer-string)))))
39 ;; No region selected, no comment on current line and line empty:
40 ;; insert comment on this line.
41 (should
42 (equal "# \nParagraph"
43 (org-test-with-temp-text "\nParagraph"
44 (progn (call-interactively 'comment-dwim)
45 (buffer-string)))))
46 ;; No region selected, and a comment on this line: indent it.
47 (should
48 (equal "* Headline\n # Comment"
49 (org-test-with-temp-text "* Headline\n# Comment"
50 (progn (forward-line)
51 (let ((org-adapt-indentation t))
52 (call-interactively 'comment-dwim))
53 (buffer-string)))))
54 ;; Also recognize single # at column 0 as comments.
55 (should
56 (equal "# Comment"
57 (org-test-with-temp-text "# Comment"
58 (progn (forward-line)
59 (call-interactively 'comment-dwim)
60 (buffer-string)))))
61 ;; Region selected and only comments and blank lines within it:
62 ;; un-comment all commented lines.
63 (should
64 (equal "Comment 1\n\nComment 2"
65 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
66 (progn
67 (transient-mark-mode 1)
68 (push-mark (point) t t)
69 (goto-char (point-max))
70 (call-interactively 'comment-dwim)
71 (buffer-string)))))
72 ;; Region selected without comments: comment all non-blank lines.
73 (should
74 (equal "# Comment 1\n\n# Comment 2"
75 (org-test-with-temp-text "Comment 1\n\nComment 2"
76 (progn
77 (transient-mark-mode 1)
78 (push-mark (point) t t)
79 (goto-char (point-max))
80 (call-interactively 'comment-dwim)
81 (buffer-string)))))
82 ;; In front of a keyword without region, insert a new comment.
83 (should
84 (equal "# \n#+KEYWORD: value"
85 (org-test-with-temp-text "#+KEYWORD: value"
86 (progn (call-interactively 'comment-dwim)
87 (buffer-string))))))
91 ;;; Date Analysis
93 (ert-deftest test-org/org-read-date ()
94 "Test `org-read-date' specifications."
95 ;; Parse ISO date with abbreviated year and month.
96 (should (equal "2012-03-29 16:40"
97 (let ((org-time-was-given t))
98 (org-read-date t nil "12-3-29 16:40"))))
99 ;; Parse Europeans dates.
100 (should (equal "2012-03-29 16:40"
101 (let ((org-time-was-given t))
102 (org-read-date t nil "29.03.2012 16:40"))))
103 ;; Parse Europeans dates without year.
104 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
105 (let ((org-time-was-given t))
106 (org-read-date t nil "29.03. 16:40")))))
110 ;;; Filling
112 (ert-deftest test-org/fill-paragraph ()
113 "Test `org-fill-paragraph' specifications."
114 ;; At an Org table, align it.
115 (org-test-with-temp-text "|a|"
116 (org-fill-paragraph)
117 (should (equal (buffer-string) "| a |\n")))
118 ;; At a paragraph, preserve line breaks.
119 (org-test-with-temp-text "some \\\\\nlong\ntext"
120 (let ((fill-column 20))
121 (org-fill-paragraph)
122 (should (equal (buffer-string) "some \\\\\nlong text"))))
123 ;; Correctly fill a paragraph when point is at its very end.
124 (should
125 (equal "A B"
126 (org-test-with-temp-text "A\nB"
127 (let ((fill-column 20))
128 (goto-char (point-max))
129 (org-fill-paragraph)
130 (buffer-string)))))
131 ;; Correctly fill the last paragraph of a greater element.
132 (should
133 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
134 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
135 (let ((fill-column 8))
136 (forward-line)
137 (end-of-line)
138 (org-fill-paragraph)
139 (buffer-string)))))
140 ;; Correctly fill an element in a narrowed buffer.
141 (should
142 (equal "01234\n6"
143 (org-test-with-temp-text "01234 6789"
144 (let ((fill-column 5))
145 (narrow-to-region 1 8)
146 (org-fill-paragraph)
147 (buffer-string)))))
148 ;; Special case: Fill first paragraph when point is at an item or
149 ;; a plain-list or a footnote reference.
150 (should
151 (equal "- A B"
152 (org-test-with-temp-text "- A\n B"
153 (let ((fill-column 20))
154 (org-fill-paragraph)
155 (buffer-string)))))
156 (should
157 (equal "[fn:1] A B"
158 (org-test-with-temp-text "[fn:1] A\nB"
159 (let ((fill-column 20))
160 (org-fill-paragraph)
161 (buffer-string)))))
162 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
163 (let ((fill-column 20))
164 (org-fill-paragraph)
165 (should (equal (buffer-string)
166 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
167 ;; Fill contents of `comment-block' elements.
168 (should
169 (equal
170 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
171 (let ((fill-column 20))
172 (forward-line)
173 (org-fill-paragraph)
174 (buffer-string)))
175 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
176 ;; Fill `comment' elements.
177 (should
178 (equal " # A B"
179 (org-test-with-temp-text " # A\n # B"
180 (let ((fill-column 20))
181 (org-fill-paragraph)
182 (buffer-string)))))
183 ;; Do nothing at affiliated keywords.
184 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
185 (let ((fill-column 20))
186 (org-fill-paragraph)
187 (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
189 (ert-deftest test-org/auto-fill-function ()
190 "Test auto-filling features."
191 ;; Auto fill paragraph.
192 (should
193 (equal "12345\n7890"
194 (org-test-with-temp-text "12345 7890"
195 (let ((fill-column 5))
196 (end-of-line)
197 (org-auto-fill-function)
198 (buffer-string)))))
199 ;; Auto fill first paragraph in an item.
200 (should
201 (equal "- 12345\n 7890"
202 (org-test-with-temp-text "- 12345 7890"
203 (let ((fill-column 7))
204 (end-of-line)
205 (org-auto-fill-function)
206 (buffer-string)))))
207 ;; Auto fill comments.
208 (should
209 (equal " # 12345\n # 7890"
210 (org-test-with-temp-text " # 12345 7890"
211 (let ((fill-column 10))
212 (end-of-line)
213 (org-auto-fill-function)
214 (buffer-string)))))
215 ;; A hash within a line isn't a comment.
216 (should-not
217 (equal "12345 # 7890\n# 1"
218 (org-test-with-temp-text "12345 # 7890 1"
219 (let ((fill-column 12))
220 (end-of-line)
221 (org-auto-fill-function)
222 (buffer-string)))))
223 ;; Correctly interpret empty prefix.
224 (should-not
225 (equal "# a\n# b\nRegular\n# paragraph"
226 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
227 (let ((fill-column 12))
228 (end-of-line 3)
229 (org-auto-fill-function)
230 (buffer-string)))))
231 ;; Comment block: auto fill contents.
232 (should
233 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
234 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
235 (let ((fill-column 5))
236 (forward-line)
237 (end-of-line)
238 (org-auto-fill-function)
239 (buffer-string)))))
240 (should
241 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
242 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
243 (let ((fill-column 5))
244 (forward-line)
245 (end-of-line)
246 (org-auto-fill-function)
247 (buffer-string)))))
248 ;; Do not fill if a new item could be created.
249 (should-not
250 (equal "12345\n- 90"
251 (org-test-with-temp-text "12345 - 90"
252 (let ((fill-column 5))
253 (end-of-line)
254 (org-auto-fill-function)
255 (buffer-string)))))
256 ;; Do not fill if a line break could be introduced.
257 (should-not
258 (equal "123\\\\\n7890"
259 (org-test-with-temp-text "123\\\\ 7890"
260 (let ((fill-column 6))
261 (end-of-line)
262 (org-auto-fill-function)
263 (buffer-string)))))
264 ;; Do not fill affiliated keywords.
265 (should-not
266 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
267 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
268 (let ((fill-column 20))
269 (end-of-line)
270 (org-auto-fill-function)
271 (buffer-string))))))
275 ;;; Links
277 ;;;; Fuzzy Links
279 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
280 ;; a target keyword (aka an invisible target: #+TARGET: text), to
281 ;; a named element (#+name: text) and to headlines (* Text).
283 (ert-deftest test-org/fuzzy-links ()
284 "Test fuzzy links specifications."
285 ;; 1. Fuzzy link goes in priority to a matching target.
286 (org-test-with-temp-text
287 "#+TARGET: Test\n#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
288 (goto-line 6)
289 (org-open-at-point)
290 (should (looking-at "<<Test>>")))
291 ;; 2. Fuzzy link should then go to a matching target keyword.
292 (org-test-with-temp-text
293 "#+NAME: Test\n|a|b|\n#+TARGET: Test\n* Test\n[[Test]]"
294 (goto-line 5)
295 (org-open-at-point)
296 (should (looking-at "#\\+TARGET: Test")))
297 ;; 3. Then fuzzy link points to an element with a given name.
298 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
299 (goto-line 5)
300 (org-open-at-point)
301 (should (looking-at "#\\+NAME: Test")))
302 ;; 4. A target still lead to a matching headline otherwise.
303 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
304 (goto-line 4)
305 (org-open-at-point)
306 (should (looking-at "\\* Head2")))
307 ;; 5. With a leading star in link, enforce heading match.
308 (org-test-with-temp-text "#+TARGET: Test\n* Test\n<<Test>>\n[[*Test]]"
309 (goto-line 4)
310 (org-open-at-point)
311 (should (looking-at "\\* Test"))))
314 ;;;; Link Escaping
316 (ert-deftest test-org/org-link-escape-ascii-character ()
317 "Escape an ascii character."
318 (should
319 (string=
320 "%5B"
321 (org-link-escape "["))))
323 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
324 "Escape an ascii control character."
325 (should
326 (string=
327 "%09"
328 (org-link-escape "\t"))))
330 (ert-deftest test-org/org-link-escape-multibyte-character ()
331 "Escape an unicode multibyte character."
332 (should
333 (string=
334 "%E2%82%AC"
335 (org-link-escape "€"))))
337 (ert-deftest test-org/org-link-escape-custom-table ()
338 "Escape string with custom character table."
339 (should
340 (string=
341 "Foo%3A%42ar%0A"
342 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
344 (ert-deftest test-org/org-link-escape-custom-table-merge ()
345 "Escape string with custom table merged with default table."
346 (should
347 (string=
348 "%5BF%6F%6F%3A%42ar%0A%5D"
349 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
351 (ert-deftest test-org/org-link-unescape-ascii-character ()
352 "Unescape an ascii character."
353 (should
354 (string=
356 (org-link-unescape "%5B"))))
358 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
359 "Unescpae an ascii control character."
360 (should
361 (string=
362 "\n"
363 (org-link-unescape "%0A"))))
365 (ert-deftest test-org/org-link-unescape-multibyte-character ()
366 "Unescape unicode multibyte character."
367 (should
368 (string=
369 "€"
370 (org-link-unescape "%E2%82%AC"))))
372 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
373 "Unescape old style percent escaped character."
374 (should
375 (string=
376 "àâçèéêîôùû"
377 (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
379 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
380 "Escape and unscape a URL that includes an escaped char.
381 http://article.gmane.org/gmane.emacs.orgmode/21459/"
382 (should
383 (string=
384 "http://some.host.com/form?&id=blah%2Bblah25"
385 (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
389 ;;; Macros
391 (ert-deftest test-org/macro-replace-all ()
392 "Test `org-macro-replace-all' specifications."
393 ;; Standard test.
394 (should
395 (equal
396 "#+MACRO: A B\n1 B 3"
397 (org-test-with-temp-text "#+MACRO: A B\n1 {{{A}}} 3"
398 (progn (org-macro-initialize-templates)
399 (org-macro-replace-all org-macro-templates)
400 (buffer-string)))))
401 ;; Macro with arguments.
402 (should
403 (equal
404 "#+MACRO: macro $1 $2\nsome text"
405 (org-test-with-temp-text "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
406 (progn (org-macro-initialize-templates)
407 (org-macro-replace-all org-macro-templates)
408 (buffer-string)))))
409 ;; Macro with "eval".
410 (should
411 (equal
412 "#+MACRO: add (eval (+ $1 $2))\n3"
413 (org-test-with-temp-text "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
414 (progn (org-macro-initialize-templates)
415 (org-macro-replace-all org-macro-templates)
416 (buffer-string)))))
417 ;; Nested macros.
418 (should
419 (equal
420 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\ninner outer"
421 (org-test-with-temp-text
422 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
423 (progn (org-macro-initialize-templates)
424 (org-macro-replace-all org-macro-templates)
425 (buffer-string)))))
426 ;; Error out when macro expansion is circular.
427 (should-error
428 (org-test-with-temp-text
429 "#+MACRO: mac1 {{{mac2}}}\n#+MACRO: mac2 {{{mac1}}}\n{{{mac1}}}"
430 (org-macro-initialize-templates)
431 (org-macro-replace-all org-macro-templates))))
435 ;;; Node Properties
437 (ert-deftest test-org/accumulated-properties-in-drawers ()
438 "Ensure properties accumulate in subtree drawers."
439 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
440 (org-babel-next-src-block)
441 (should (equal '(2 1) (org-babel-execute-src-block)))))
445 ;;; Mark Region
447 (ert-deftest test-org/mark-subtree ()
448 "Test `org-mark-subtree' specifications."
449 ;; Error when point is before first headline.
450 (should-error
451 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
452 (progn (transient-mark-mode 1)
453 (org-mark-subtree))))
454 ;; Without argument, mark current subtree.
455 (should
456 (equal
457 '(12 32)
458 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
459 (progn (transient-mark-mode 1)
460 (forward-line 2)
461 (org-mark-subtree)
462 (list (region-beginning) (region-end))))))
463 ;; With an argument, move ARG up.
464 (should
465 (equal
466 '(1 32)
467 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
468 (progn (transient-mark-mode 1)
469 (forward-line 2)
470 (org-mark-subtree 1)
471 (list (region-beginning) (region-end))))))
472 ;; Do not get fooled by inlinetasks.
473 (when (featurep 'org-inlinetask)
474 (should
475 (= 1
476 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
477 (progn (transient-mark-mode 1)
478 (forward-line 1)
479 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
480 (region-beginning)))))))
484 ;;; Navigation
486 (ert-deftest test-org/beginning-of-line ()
487 "Test `org-beginning-of-line' specifications."
488 ;; Standard test.
489 (should
490 (org-test-with-temp-text "Some text\nSome other text"
491 (progn (org-beginning-of-line) (bolp))))
492 ;; Standard test with `visual-line-mode'.
493 (should-not
494 (org-test-with-temp-text "A long line of text\nSome other text"
495 (progn (visual-line-mode)
496 (forward-char 2)
497 (dotimes (i 1000) (insert "very "))
498 (org-beginning-of-line)
499 (bolp))))
500 ;; At an headline with special movement.
501 (should
502 (org-test-with-temp-text "* TODO Headline"
503 (let ((org-special-ctrl-a/e t))
504 (org-end-of-line)
505 (and (progn (org-beginning-of-line) (looking-at "Headline"))
506 (progn (org-beginning-of-line) (bolp))
507 (progn (org-beginning-of-line) (looking-at "Headline")))))))
509 (ert-deftest test-org/end-of-line ()
510 "Test `org-end-of-line' specifications."
511 ;; Standard test.
512 (should
513 (org-test-with-temp-text "Some text\nSome other text"
514 (progn (org-end-of-line) (eolp))))
515 ;; Standard test with `visual-line-mode'.
516 (should-not
517 (org-test-with-temp-text "A long line of text\nSome other text"
518 (progn (visual-line-mode)
519 (forward-char 2)
520 (dotimes (i 1000) (insert "very "))
521 (goto-char (point-min))
522 (org-end-of-line)
523 (eolp))))
524 ;; At an headline with special movement.
525 (should
526 (org-test-with-temp-text "* Headline1 :tag:\n"
527 (let ((org-special-ctrl-a/e t))
528 (and (progn (org-end-of-line) (looking-at " :tag:"))
529 (progn (org-end-of-line) (eolp))
530 (progn (org-end-of-line) (looking-at " :tag:"))))))
531 ;; At an headline without special movement.
532 (should
533 (org-test-with-temp-text "* Headline2 :tag:\n"
534 (let ((org-special-ctrl-a/e nil))
535 (and (progn (org-end-of-line) (eolp))
536 (progn (org-end-of-line) (eolp))))))
537 ;; At an headline, with reversed movement.
538 (should
539 (org-test-with-temp-text "* Headline3 :tag:\n"
540 (let ((org-special-ctrl-a/e 'reversed)
541 (this-command last-command))
542 (and (progn (org-end-of-line) (eolp))
543 (progn (org-end-of-line) (looking-at " :tag:"))))))
544 ;; At a block without hidden contents.
545 (should
546 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
547 (progn (org-end-of-line) (eolp))))
548 ;; At a block with hidden contents.
549 (should-not
550 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
551 (let ((org-special-ctrl-a/e t))
552 (org-hide-block-toggle)
553 (org-end-of-line)
554 (eobp)))))
556 (ert-deftest test-org/forward-element ()
557 "Test `org-forward-element' specifications."
558 ;; 1. At EOB: should error.
559 (org-test-with-temp-text "Some text\n"
560 (goto-char (point-max))
561 (should-error (org-forward-element)))
562 ;; 2. Standard move: expected to ignore blank lines.
563 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
564 (org-forward-element)
565 (should (looking-at "Second paragraph.")))
566 ;; 3. Headline tests.
567 (org-test-with-temp-text "
568 * Head 1
569 ** Head 1.1
570 *** Head 1.1.1
571 ** Head 1.2"
572 ;; 3.1. At an headline beginning: move to next headline at the
573 ;; same level.
574 (goto-line 3)
575 (org-forward-element)
576 (should (looking-at "** Head 1.2"))
577 ;; 3.2. At an headline beginning: move to parent headline if no
578 ;; headline at the same level.
579 (goto-line 3)
580 (org-forward-element)
581 (should (looking-at "** Head 1.2")))
582 ;; 4. Greater element tests.
583 (org-test-with-temp-text
584 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
585 ;; 4.1. At a greater element: expected to skip contents.
586 (org-forward-element)
587 (should (looking-at "Outside."))
588 ;; 4.2. At the end of greater element contents: expected to skip
589 ;; to the end of the greater element.
590 (goto-line 2)
591 (org-forward-element)
592 (should (looking-at "Outside.")))
593 ;; 5. List tests.
594 (org-test-with-temp-text "
595 - item1
597 - sub1
599 - sub2
601 - sub3
603 Inner paragraph.
605 - item2
607 Outside."
608 ;; 5.1. At list top point: expected to move to the element after
609 ;; the list.
610 (goto-line 2)
611 (org-forward-element)
612 (should (looking-at "Outside."))
613 ;; 5.2. Special case: at the first line of a sub-list, but not at
614 ;; beginning of line, move to next item.
615 (goto-line 2)
616 (forward-char)
617 (org-forward-element)
618 (should (looking-at "- item2"))
619 (goto-line 4)
620 (forward-char)
621 (org-forward-element)
622 (should (looking-at " - sub2"))
623 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
624 (goto-line 4)
625 (org-forward-element)
626 (should (looking-at " Inner paragraph."))
627 ;; 5.4. At sub-list end: expected to move outside the sub-list.
628 (goto-line 8)
629 (org-forward-element)
630 (should (looking-at " Inner paragraph."))
631 ;; 5.5. At an item: expected to move to next item, if any.
632 (goto-line 6)
633 (org-forward-element)
634 (should (looking-at " - sub3"))))
636 (ert-deftest test-org/backward-element ()
637 "Test `org-backward-element' specifications."
638 ;; 1. Should error at BOB.
639 (org-test-with-temp-text " \nParagraph."
640 (should-error (org-backward-element)))
641 ;; 2. Should move at BOB when called on the first element in buffer.
642 (should
643 (org-test-with-temp-text "\n#+TITLE: test"
644 (progn (forward-line)
645 (org-backward-element)
646 (bobp))))
647 ;; 3. Not at the beginning of an element: move at its beginning.
648 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
649 (goto-line 3)
650 (end-of-line)
651 (org-backward-element)
652 (should (looking-at "Paragraph2.")))
653 ;; 4. Headline tests.
654 (org-test-with-temp-text "
655 * Head 1
656 ** Head 1.1
657 *** Head 1.1.1
658 ** Head 1.2"
659 ;; 4.1. At an headline beginning: move to previous headline at the
660 ;; same level.
661 (goto-line 5)
662 (org-backward-element)
663 (should (looking-at "** Head 1.1"))
664 ;; 4.2. At an headline beginning: move to parent headline if no
665 ;; headline at the same level.
666 (goto-line 3)
667 (org-backward-element)
668 (should (looking-at "* Head 1"))
669 ;; 4.3. At the first top-level headline: should error.
670 (goto-line 2)
671 (should-error (org-backward-element)))
672 ;; 5. At beginning of first element inside a greater element:
673 ;; expected to move to greater element's beginning.
674 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
675 (goto-line 3)
676 (org-backward-element)
677 (should (looking-at "#\\+BEGIN_CENTER")))
678 ;; 6. At the beginning of the first element in a section: should
679 ;; move back to headline, if any.
680 (should
681 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
682 (progn (goto-char (point-max))
683 (beginning-of-line)
684 (org-backward-element)
685 (org-at-heading-p))))
686 ;; 7. List tests.
687 (org-test-with-temp-text "
688 - item1
690 - sub1
692 - sub2
694 - sub3
696 Inner paragraph.
698 - item2
701 Outside."
702 ;; 7.1. At beginning of sub-list: expected to move to the
703 ;; paragraph before it.
704 (goto-line 4)
705 (org-backward-element)
706 (should (looking-at "item1"))
707 ;; 7.2. At an item in a list: expected to move at previous item.
708 (goto-line 8)
709 (org-backward-element)
710 (should (looking-at " - sub2"))
711 (goto-line 12)
712 (org-backward-element)
713 (should (looking-at "- item1"))
714 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
715 ;; beginning.
716 (goto-line 10)
717 (org-backward-element)
718 (should (looking-at " - sub1"))
719 (goto-line 15)
720 (org-backward-element)
721 (should (looking-at "- item1"))
722 ;; 7.4. At blank-lines before list end: expected to move to top
723 ;; item.
724 (goto-line 14)
725 (org-backward-element)
726 (should (looking-at "- item1"))))
728 (ert-deftest test-org/up-element ()
729 "Test `org-up-element' specifications."
730 ;; 1. At BOB or with no surrounding element: should error.
731 (org-test-with-temp-text "Paragraph."
732 (should-error (org-up-element)))
733 (org-test-with-temp-text "* Head1\n* Head2"
734 (goto-line 2)
735 (should-error (org-up-element)))
736 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
737 (goto-line 3)
738 (should-error (org-up-element)))
739 ;; 2. At an headline: move to parent headline.
740 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
741 (goto-line 3)
742 (org-up-element)
743 (should (looking-at "\\* Head1")))
744 ;; 3. Inside a greater element: move to greater element beginning.
745 (org-test-with-temp-text
746 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
747 (goto-line 3)
748 (org-up-element)
749 (should (looking-at "#\\+BEGIN_CENTER")))
750 ;; 4. List tests.
751 (org-test-with-temp-text "* Top
752 - item1
754 - sub1
756 - sub2
758 Paragraph within sub2.
760 - item2"
761 ;; 4.1. Within an item: move to the item beginning.
762 (goto-line 8)
763 (org-up-element)
764 (should (looking-at " - sub2"))
765 ;; 4.2. At an item in a sub-list: move to parent item.
766 (goto-line 4)
767 (org-up-element)
768 (should (looking-at "- item1"))
769 ;; 4.3. At an item in top list: move to beginning of whole list.
770 (goto-line 10)
771 (org-up-element)
772 (should (looking-at "- item1"))
773 ;; 4.4. Special case. At very top point: should move to parent of
774 ;; list.
775 (goto-line 2)
776 (org-up-element)
777 (should (looking-at "\\* Top"))))
779 (ert-deftest test-org/down-element ()
780 "Test `org-down-element' specifications."
781 ;; Error when the element hasn't got a recursive type.
782 (org-test-with-temp-text "Paragraph."
783 (should-error (org-down-element)))
784 ;; Error when the element has no contents
785 (org-test-with-temp-text "* Headline"
786 (should-error (org-down-element)))
787 ;; When at a plain-list, move to first item.
788 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
789 (goto-line 2)
790 (org-down-element)
791 (should (looking-at " - Item 1.1")))
792 (org-test-with-temp-text "#+NAME: list\n- Item 1"
793 (org-down-element)
794 (should (looking-at " Item 1")))
795 ;; When at a table, move to first row
796 (org-test-with-temp-text "#+NAME: table\n| a | b |"
797 (org-down-element)
798 (should (looking-at " a | b |")))
799 ;; Otherwise, move inside the greater element.
800 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
801 (org-down-element)
802 (should (looking-at "Paragraph"))))
804 (ert-deftest test-org/drag-element-backward ()
805 "Test `org-drag-element-backward' specifications."
806 ;; 1. Error when trying to move first element of buffer.
807 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
808 (should-error (org-drag-element-backward)))
809 ;; 2. Error when trying to swap nested elements.
810 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
811 (forward-line)
812 (should-error (org-drag-element-backward)))
813 ;; 3. Error when trying to swap an headline element and
814 ;; a non-headline element.
815 (org-test-with-temp-text "Test.\n* Head 1"
816 (forward-line)
817 (should-error (org-drag-element-backward)))
818 ;; 4. Otherwise, swap elements, preserving column and blank lines
819 ;; between elements.
820 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
821 (search-forward "graph")
822 (org-drag-element-backward)
823 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
824 (should (looking-at " 2")))
825 ;; 5. Preserve visibility of elements and their contents.
826 (org-test-with-temp-text "
827 #+BEGIN_CENTER
828 Text.
829 #+END_CENTER
830 - item 1
831 #+BEGIN_QUOTE
832 Text.
833 #+END_QUOTE"
834 (while (search-forward "BEGIN_" nil t) (org-cycle))
835 (search-backward "- item 1")
836 (org-drag-element-backward)
837 (should
838 (equal
839 '((63 . 82) (26 . 48))
840 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
841 (overlays-in (point-min) (point-max)))))))
843 (ert-deftest test-org/drag-element-forward ()
844 "Test `org-drag-element-forward' specifications."
845 ;; 1. Error when trying to move first element of buffer.
846 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
847 (goto-line 3)
848 (should-error (org-drag-element-forward)))
849 ;; 2. Error when trying to swap nested elements.
850 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
851 (forward-line)
852 (should-error (org-drag-element-forward)))
853 ;; 3. Error when trying to swap a non-headline element and an
854 ;; headline.
855 (org-test-with-temp-text "Test.\n* Head 1"
856 (should-error (org-drag-element-forward)))
857 ;; 4. Otherwise, swap elements, preserving column and blank lines
858 ;; between elements.
859 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
860 (search-forward "graph")
861 (org-drag-element-forward)
862 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
863 (should (looking-at " 1")))
864 ;; 5. Preserve visibility of elements and their contents.
865 (org-test-with-temp-text "
866 #+BEGIN_CENTER
867 Text.
868 #+END_CENTER
869 - item 1
870 #+BEGIN_QUOTE
871 Text.
872 #+END_QUOTE"
873 (while (search-forward "BEGIN_" nil t) (org-cycle))
874 (search-backward "#+BEGIN_CENTER")
875 (org-drag-element-forward)
876 (should
877 (equal
878 '((63 . 82) (26 . 48))
879 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
880 (overlays-in (point-min) (point-max)))))))
884 ;;; Targets and Radio Targets
886 (ert-deftest test-org/all-targets ()
887 "Test `org-all-targets' specifications."
888 ;; Without an argument.
889 (should
890 (equal '("radio-target" "target")
891 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
892 (org-all-targets))))
893 (should
894 (equal '("radio-target")
895 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
896 ;; With argument.
897 (should
898 (equal '("radio-target")
899 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
900 (org-all-targets t)))))
903 (provide 'test-org)
905 ;;; test-org.el ends here