Merge branch 'maint'
[org-mode.git] / testing / lisp / test-org.el
blob80822d01a375bae0bd495bc745cc2e5aeb84bc0b
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 ;; Comment block: auto fill contents.
204 (should
205 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
206 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
207 (let ((fill-column 5))
208 (forward-line)
209 (end-of-line)
210 (org-auto-fill-function)
211 (buffer-string)))))
212 (should
213 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
214 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
215 (let ((fill-column 5))
216 (forward-line)
217 (end-of-line)
218 (org-auto-fill-function)
219 (buffer-string)))))
220 ;; Do not fill if a new item could be created.
221 (should-not
222 (equal "12345\n- 90"
223 (org-test-with-temp-text "12345 - 90"
224 (let ((fill-column 5))
225 (end-of-line)
226 (org-auto-fill-function)
227 (buffer-string)))))
228 ;; Do not fill if a line break could be introduced.
229 (should-not
230 (equal "123\\\\\n7890"
231 (org-test-with-temp-text "123\\\\ 7890"
232 (let ((fill-column 6))
233 (end-of-line)
234 (org-auto-fill-function)
235 (buffer-string)))))
236 ;; Do not fill affiliated keywords.
237 (should-not
238 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
239 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
240 (let ((fill-column 20))
241 (end-of-line)
242 (org-auto-fill-function)
243 (buffer-string))))))
247 ;;; Links
249 ;;;; Fuzzy Links
251 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
252 ;; a target keyword (aka an invisible target: #+TARGET: text), to
253 ;; a named element (#+name: text) and to headlines (* Text).
255 (ert-deftest test-org/fuzzy-links ()
256 "Test fuzzy links specifications."
257 ;; 1. Fuzzy link goes in priority to a matching target.
258 (org-test-with-temp-text
259 "#+TARGET: Test\n#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
260 (goto-line 6)
261 (org-open-at-point)
262 (should (looking-at "<<Test>>")))
263 ;; 2. Fuzzy link should then go to a matching target keyword.
264 (org-test-with-temp-text
265 "#+NAME: Test\n|a|b|\n#+TARGET: Test\n* Test\n[[Test]]"
266 (goto-line 5)
267 (org-open-at-point)
268 (should (looking-at "#\\+TARGET: Test")))
269 ;; 3. Then fuzzy link points to an element with a given name.
270 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
271 (goto-line 5)
272 (org-open-at-point)
273 (should (looking-at "#\\+NAME: Test")))
274 ;; 4. A target still lead to a matching headline otherwise.
275 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
276 (goto-line 4)
277 (org-open-at-point)
278 (should (looking-at "\\* Head2")))
279 ;; 5. With a leading star in link, enforce heading match.
280 (org-test-with-temp-text "#+TARGET: Test\n* Test\n<<Test>>\n[[*Test]]"
281 (goto-line 4)
282 (org-open-at-point)
283 (should (looking-at "\\* Test"))))
286 ;;;; Link Escaping
288 (ert-deftest test-org/org-link-escape-ascii-character ()
289 "Escape an ascii character."
290 (should
291 (string=
292 "%5B"
293 (org-link-escape "["))))
295 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
296 "Escape an ascii control character."
297 (should
298 (string=
299 "%09"
300 (org-link-escape "\t"))))
302 (ert-deftest test-org/org-link-escape-multibyte-character ()
303 "Escape an unicode multibyte character."
304 (should
305 (string=
306 "%E2%82%AC"
307 (org-link-escape "€"))))
309 (ert-deftest test-org/org-link-escape-custom-table ()
310 "Escape string with custom character table."
311 (should
312 (string=
313 "Foo%3A%42ar%0A"
314 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
316 (ert-deftest test-org/org-link-escape-custom-table-merge ()
317 "Escape string with custom table merged with default table."
318 (should
319 (string=
320 "%5BF%6F%6F%3A%42ar%0A%5D"
321 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
323 (ert-deftest test-org/org-link-unescape-ascii-character ()
324 "Unescape an ascii character."
325 (should
326 (string=
328 (org-link-unescape "%5B"))))
330 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
331 "Unescpae an ascii control character."
332 (should
333 (string=
334 "\n"
335 (org-link-unescape "%0A"))))
337 (ert-deftest test-org/org-link-unescape-multibyte-character ()
338 "Unescape unicode multibyte character."
339 (should
340 (string=
341 "€"
342 (org-link-unescape "%E2%82%AC"))))
344 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
345 "Unescape old style percent escaped character."
346 (should
347 (string=
348 "àâçèéêîôùû"
349 (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
351 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
352 "Escape and unscape a URL that includes an escaped char.
353 http://article.gmane.org/gmane.emacs.orgmode/21459/"
354 (should
355 (string=
356 "http://some.host.com/form?&id=blah%2Bblah25"
357 (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
361 ;;; Macros
363 (ert-deftest test-org/macro-replace-all ()
364 "Test `org-macro-replace-all' specifications."
365 ;; Standard test.
366 (should
367 (equal
368 "#+MACRO: A B\n1 B 3"
369 (org-test-with-temp-text "#+MACRO: A B\n1 {{{A}}} 3"
370 (progn (org-macro-initialize-templates)
371 (org-macro-replace-all org-macro-templates)
372 (buffer-string)))))
373 ;; Macro with arguments.
374 (should
375 (equal
376 "#+MACRO: macro $1 $2\nsome text"
377 (org-test-with-temp-text "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
378 (progn (org-macro-initialize-templates)
379 (org-macro-replace-all org-macro-templates)
380 (buffer-string)))))
381 ;; Macro with "eval".
382 (should
383 (equal
384 "#+MACRO: add (eval (+ $1 $2))\n3"
385 (org-test-with-temp-text "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
386 (progn (org-macro-initialize-templates)
387 (org-macro-replace-all org-macro-templates)
388 (buffer-string)))))
389 ;; Nested macros.
390 (should
391 (equal
392 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\ninner outer"
393 (org-test-with-temp-text
394 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
395 (progn (org-macro-initialize-templates)
396 (org-macro-replace-all org-macro-templates)
397 (buffer-string))))))
401 ;;; Node Properties
403 (ert-deftest test-org/accumulated-properties-in-drawers ()
404 "Ensure properties accumulate in subtree drawers."
405 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
406 (org-babel-next-src-block)
407 (should (equal '(2 1) (org-babel-execute-src-block)))))
411 ;;; Mark Region
413 (ert-deftest test-org/mark-subtree ()
414 "Test `org-mark-subtree' specifications."
415 ;; Error when point is before first headline.
416 (should-error
417 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
418 (progn (transient-mark-mode 1)
419 (org-mark-subtree))))
420 ;; Without argument, mark current subtree.
421 (should
422 (equal
423 '(12 32)
424 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
425 (progn (transient-mark-mode 1)
426 (forward-line 2)
427 (org-mark-subtree)
428 (list (region-beginning) (region-end))))))
429 ;; With an argument, move ARG up.
430 (should
431 (equal
432 '(1 32)
433 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
434 (progn (transient-mark-mode 1)
435 (forward-line 2)
436 (org-mark-subtree 1)
437 (list (region-beginning) (region-end))))))
438 ;; Do not get fooled by inlinetasks.
439 (when (featurep 'org-inlinetask)
440 (should
441 (= 1
442 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
443 (progn (transient-mark-mode 1)
444 (forward-line 1)
445 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
446 (region-beginning)))))))
450 ;;; Navigation
452 (ert-deftest test-org/end-of-line ()
453 "Test `org-end-of-line' specifications."
454 ;; Standard test.
455 (should
456 (org-test-with-temp-text "Some text\nSome other text"
457 (progn (org-end-of-line) (eolp))))
458 ;; At an headline with special movement.
459 (should
460 (org-test-with-temp-text "* Headline :tag:"
461 (let ((org-special-ctrl-a/e t))
462 (and (progn (org-end-of-line) (looking-at " :tag:"))
463 (progn (org-end-of-line) (eolp))
464 (progn (org-end-of-line) (looking-at " :tag:"))))))
465 ;; At an headline without special movement.
466 (should
467 (org-test-with-temp-text "* Headline :tag:"
468 (let ((org-special-ctrl-a/e nil))
469 (and (progn (org-end-of-line) (eolp))
470 (progn (org-end-of-line) (eolp))))))
471 ;; At an headline, with reversed movement.
472 (should
473 (org-test-with-temp-text "* Headline :tag:"
474 (let ((org-special-ctrl-a/e 'reversed)
475 (this-command last-command))
476 (and (progn (org-end-of-line) (eolp))
477 (progn (org-end-of-line) (looking-at " :tag:"))))))
478 ;; At a block without hidden contents.
479 (should
480 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
481 (progn (org-end-of-line) (eolp))))
482 ;; At a block with hidden contents.
483 (should-not
484 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
485 (progn (org-hide-block-toggle)
486 (org-end-of-line)
487 (eolp)))))
489 (ert-deftest test-org/forward-element ()
490 "Test `org-forward-element' specifications."
491 ;; 1. At EOB: should error.
492 (org-test-with-temp-text "Some text\n"
493 (goto-char (point-max))
494 (should-error (org-forward-element)))
495 ;; 2. Standard move: expected to ignore blank lines.
496 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
497 (org-forward-element)
498 (should (looking-at "Second paragraph.")))
499 ;; 3. Headline tests.
500 (org-test-with-temp-text "
501 * Head 1
502 ** Head 1.1
503 *** Head 1.1.1
504 ** Head 1.2"
505 ;; 3.1. At an headline beginning: move to next headline at the
506 ;; same level.
507 (goto-line 3)
508 (org-forward-element)
509 (should (looking-at "** Head 1.2"))
510 ;; 3.2. At an headline beginning: move to parent headline if no
511 ;; headline at the same level.
512 (goto-line 3)
513 (org-forward-element)
514 (should (looking-at "** Head 1.2")))
515 ;; 4. Greater element tests.
516 (org-test-with-temp-text
517 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
518 ;; 4.1. At a greater element: expected to skip contents.
519 (org-forward-element)
520 (should (looking-at "Outside."))
521 ;; 4.2. At the end of greater element contents: expected to skip
522 ;; to the end of the greater element.
523 (goto-line 2)
524 (org-forward-element)
525 (should (looking-at "Outside.")))
526 ;; 5. List tests.
527 (org-test-with-temp-text "
528 - item1
530 - sub1
532 - sub2
534 - sub3
536 Inner paragraph.
538 - item2
540 Outside."
541 ;; 5.1. At list top point: expected to move to the element after
542 ;; the list.
543 (goto-line 2)
544 (org-forward-element)
545 (should (looking-at "Outside."))
546 ;; 5.2. Special case: at the first line of a sub-list, but not at
547 ;; beginning of line, move to next item.
548 (goto-line 2)
549 (forward-char)
550 (org-forward-element)
551 (should (looking-at "- item2"))
552 (goto-line 4)
553 (forward-char)
554 (org-forward-element)
555 (should (looking-at " - sub2"))
556 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
557 (goto-line 4)
558 (org-forward-element)
559 (should (looking-at " Inner paragraph."))
560 ;; 5.4. At sub-list end: expected to move outside the sub-list.
561 (goto-line 8)
562 (org-forward-element)
563 (should (looking-at " Inner paragraph."))
564 ;; 5.5. At an item: expected to move to next item, if any.
565 (goto-line 6)
566 (org-forward-element)
567 (should (looking-at " - sub3"))))
569 (ert-deftest test-org/backward-element ()
570 "Test `org-backward-element' specifications."
571 ;; 1. Should error at BOB.
572 (org-test-with-temp-text " \nParagraph."
573 (should-error (org-backward-element)))
574 ;; 2. Should move at BOB when called on the first element in buffer.
575 (should
576 (org-test-with-temp-text "\n#+TITLE: test"
577 (progn (forward-line)
578 (org-backward-element)
579 (bobp))))
580 ;; 3. Not at the beginning of an element: move at its beginning.
581 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
582 (goto-line 3)
583 (end-of-line)
584 (org-backward-element)
585 (should (looking-at "Paragraph2.")))
586 ;; 4. Headline tests.
587 (org-test-with-temp-text "
588 * Head 1
589 ** Head 1.1
590 *** Head 1.1.1
591 ** Head 1.2"
592 ;; 4.1. At an headline beginning: move to previous headline at the
593 ;; same level.
594 (goto-line 5)
595 (org-backward-element)
596 (should (looking-at "** Head 1.1"))
597 ;; 4.2. At an headline beginning: move to parent headline if no
598 ;; headline at the same level.
599 (goto-line 3)
600 (org-backward-element)
601 (should (looking-at "* Head 1"))
602 ;; 4.3. At the first top-level headline: should error.
603 (goto-line 2)
604 (should-error (org-backward-element)))
605 ;; 5. At beginning of first element inside a greater element:
606 ;; expected to move to greater element's beginning.
607 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
608 (goto-line 3)
609 (org-backward-element)
610 (should (looking-at "#\\+BEGIN_CENTER")))
611 ;; 6. At the beginning of the first element in a section: should
612 ;; move back to headline, if any.
613 (should
614 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
615 (progn (goto-char (point-max))
616 (beginning-of-line)
617 (org-backward-element)
618 (org-at-heading-p))))
619 ;; 7. List tests.
620 (org-test-with-temp-text "
621 - item1
623 - sub1
625 - sub2
627 - sub3
629 Inner paragraph.
631 - item2
634 Outside."
635 ;; 7.1. At beginning of sub-list: expected to move to the
636 ;; paragraph before it.
637 (goto-line 4)
638 (org-backward-element)
639 (should (looking-at "item1"))
640 ;; 7.2. At an item in a list: expected to move at previous item.
641 (goto-line 8)
642 (org-backward-element)
643 (should (looking-at " - sub2"))
644 (goto-line 12)
645 (org-backward-element)
646 (should (looking-at "- item1"))
647 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
648 ;; beginning.
649 (goto-line 10)
650 (org-backward-element)
651 (should (looking-at " - sub1"))
652 (goto-line 15)
653 (org-backward-element)
654 (should (looking-at "- item1"))
655 ;; 7.4. At blank-lines before list end: expected to move to top
656 ;; item.
657 (goto-line 14)
658 (org-backward-element)
659 (should (looking-at "- item1"))))
661 (ert-deftest test-org/up-element ()
662 "Test `org-up-element' specifications."
663 ;; 1. At BOB or with no surrounding element: should error.
664 (org-test-with-temp-text "Paragraph."
665 (should-error (org-up-element)))
666 (org-test-with-temp-text "* Head1\n* Head2"
667 (goto-line 2)
668 (should-error (org-up-element)))
669 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
670 (goto-line 3)
671 (should-error (org-up-element)))
672 ;; 2. At an headline: move to parent headline.
673 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
674 (goto-line 3)
675 (org-up-element)
676 (should (looking-at "\\* Head1")))
677 ;; 3. Inside a greater element: move to greater element beginning.
678 (org-test-with-temp-text
679 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
680 (goto-line 3)
681 (org-up-element)
682 (should (looking-at "#\\+BEGIN_CENTER")))
683 ;; 4. List tests.
684 (org-test-with-temp-text "* Top
685 - item1
687 - sub1
689 - sub2
691 Paragraph within sub2.
693 - item2"
694 ;; 4.1. Within an item: move to the item beginning.
695 (goto-line 8)
696 (org-up-element)
697 (should (looking-at " - sub2"))
698 ;; 4.2. At an item in a sub-list: move to parent item.
699 (goto-line 4)
700 (org-up-element)
701 (should (looking-at "- item1"))
702 ;; 4.3. At an item in top list: move to beginning of whole list.
703 (goto-line 10)
704 (org-up-element)
705 (should (looking-at "- item1"))
706 ;; 4.4. Special case. At very top point: should move to parent of
707 ;; list.
708 (goto-line 2)
709 (org-up-element)
710 (should (looking-at "\\* Top"))))
712 (ert-deftest test-org/down-element ()
713 "Test `org-down-element' specifications."
714 ;; Error when the element hasn't got a recursive type.
715 (org-test-with-temp-text "Paragraph."
716 (should-error (org-down-element)))
717 ;; Error when the element has no contents
718 (org-test-with-temp-text "* Headline"
719 (should-error (org-down-element)))
720 ;; When at a plain-list, move to first item.
721 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
722 (goto-line 2)
723 (org-down-element)
724 (should (looking-at " - Item 1.1")))
725 (org-test-with-temp-text "#+NAME: list\n- Item 1"
726 (org-down-element)
727 (should (looking-at " Item 1")))
728 ;; When at a table, move to first row
729 (org-test-with-temp-text "#+NAME: table\n| a | b |"
730 (org-down-element)
731 (should (looking-at " a | b |")))
732 ;; Otherwise, move inside the greater element.
733 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
734 (org-down-element)
735 (should (looking-at "Paragraph"))))
737 (ert-deftest test-org/drag-element-backward ()
738 "Test `org-drag-element-backward' specifications."
739 ;; 1. Error when trying to move first element of buffer.
740 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
741 (should-error (org-drag-element-backward)))
742 ;; 2. Error when trying to swap nested elements.
743 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
744 (forward-line)
745 (should-error (org-drag-element-backward)))
746 ;; 3. Error when trying to swap an headline element and
747 ;; a non-headline element.
748 (org-test-with-temp-text "Test.\n* Head 1"
749 (forward-line)
750 (should-error (org-drag-element-backward)))
751 ;; 4. Otherwise, swap elements, preserving column and blank lines
752 ;; between elements.
753 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
754 (search-forward "graph")
755 (org-drag-element-backward)
756 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
757 (should (looking-at " 2")))
758 ;; 5. Preserve visibility of elements and their contents.
759 (org-test-with-temp-text "
760 #+BEGIN_CENTER
761 Text.
762 #+END_CENTER
763 - item 1
764 #+BEGIN_QUOTE
765 Text.
766 #+END_QUOTE"
767 (while (search-forward "BEGIN_" nil t) (org-cycle))
768 (search-backward "- item 1")
769 (org-drag-element-backward)
770 (should
771 (equal
772 '((63 . 82) (26 . 48))
773 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
774 (overlays-in (point-min) (point-max)))))))
776 (ert-deftest test-org/drag-element-forward ()
777 "Test `org-drag-element-forward' specifications."
778 ;; 1. Error when trying to move first element of buffer.
779 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
780 (goto-line 3)
781 (should-error (org-drag-element-forward)))
782 ;; 2. Error when trying to swap nested elements.
783 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
784 (forward-line)
785 (should-error (org-drag-element-forward)))
786 ;; 3. Error when trying to swap a non-headline element and an
787 ;; headline.
788 (org-test-with-temp-text "Test.\n* Head 1"
789 (should-error (org-drag-element-forward)))
790 ;; 4. Otherwise, swap elements, preserving column and blank lines
791 ;; between elements.
792 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
793 (search-forward "graph")
794 (org-drag-element-forward)
795 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
796 (should (looking-at " 1")))
797 ;; 5. Preserve visibility of elements and their contents.
798 (org-test-with-temp-text "
799 #+BEGIN_CENTER
800 Text.
801 #+END_CENTER
802 - item 1
803 #+BEGIN_QUOTE
804 Text.
805 #+END_QUOTE"
806 (while (search-forward "BEGIN_" nil t) (org-cycle))
807 (search-backward "#+BEGIN_CENTER")
808 (org-drag-element-forward)
809 (should
810 (equal
811 '((63 . 82) (26 . 48))
812 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
813 (overlays-in (point-min) (point-max)))))))
817 ;;; Targets and Radio Targets
819 (ert-deftest test-org/all-targets ()
820 "Test `org-all-targets' specifications."
821 ;; Without an argument.
822 (should
823 (equal '("radio-target" "target")
824 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
825 (org-all-targets))))
826 ;; With argument.
827 (should
828 (equal '("radio-target")
829 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
830 (org-all-targets t)))))
833 (provide 'test-org)
835 ;;; test-org.el ends here