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