Merge branch 'maint'
[org-mode.git] / testing / lisp / test-org.el
bloba2f696bd91afcf7bcbbe29ef49129dc90b613723
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:
14 (ert-deftest test-org/org-link-escape-ascii-character ()
15 "Escape an ascii character."
16 (should
17 (string=
18 "%5B"
19 (org-link-escape "["))))
21 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
22 "Escape an ascii control character."
23 (should
24 (string=
25 "%09"
26 (org-link-escape "\t"))))
28 (ert-deftest test-org/org-link-escape-multibyte-character ()
29 "Escape an unicode multibyte character."
30 (should
31 (string=
32 "%E2%82%AC"
33 (org-link-escape "€"))))
35 (ert-deftest test-org/org-link-escape-custom-table ()
36 "Escape string with custom character table."
37 (should
38 (string=
39 "Foo%3A%42ar%0A"
40 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
42 (ert-deftest test-org/org-link-escape-custom-table-merge ()
43 "Escape string with custom table merged with default table."
44 (should
45 (string=
46 "%5BF%6F%6F%3A%42ar%0A%5D"
47 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
49 (ert-deftest test-org/org-link-unescape-ascii-character ()
50 "Unescape an ascii character."
51 (should
52 (string=
53 "["
54 (org-link-unescape "%5B"))))
56 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
57 "Unescpae an ascii control character."
58 (should
59 (string=
60 "\n"
61 (org-link-unescape "%0A"))))
63 (ert-deftest test-org/org-link-unescape-multibyte-character ()
64 "Unescape unicode multibyte character."
65 (should
66 (string=
67 "€"
68 (org-link-unescape "%E2%82%AC"))))
70 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
71 "Unescape old style percent escaped character."
72 (should
73 (string=
74 "àâçèéêîôùû"
75 (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
77 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
78 "Escape and unscape a URL that includes an escaped char.
79 http://article.gmane.org/gmane.emacs.orgmode/21459/"
80 (should
81 (string=
82 "http://some.host.com/form?&id=blah%2Bblah25"
83 (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
85 (ert-deftest test-org/accumulated-properties-in-drawers ()
86 "Ensure properties accumulate in subtree drawers."
87 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
88 (org-babel-next-src-block)
89 (should (equal '(2 1) (org-babel-execute-src-block)))))
93 ;;; Links
95 ;;;; Fuzzy links
97 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
98 ;; a target keyword (aka an invisible target: #+TARGET: text), to
99 ;; a named element (#+name: text) and to headlines (* Text).
101 (ert-deftest test-org/fuzzy-links ()
102 "Test fuzzy links specifications."
103 ;; 1. Fuzzy link goes in priority to a matching target.
104 (org-test-with-temp-text
105 "#+TARGET: Test\n#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
106 (goto-line 6)
107 (org-open-at-point)
108 (should (looking-at "<<Test>>")))
109 ;; 2. Fuzzy link should then go to a matching target keyword.
110 (org-test-with-temp-text
111 "#+NAME: Test\n|a|b|\n#+TARGET: Test\n* Test\n[[Test]]"
112 (goto-line 5)
113 (org-open-at-point)
114 (should (looking-at "#\\+TARGET: Test")))
115 ;; 3. Then fuzzy link points to an element with a given name.
116 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
117 (goto-line 5)
118 (org-open-at-point)
119 (should (looking-at "#\\+NAME: Test")))
120 ;; 4. A target still lead to a matching headline otherwise.
121 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
122 (goto-line 4)
123 (org-open-at-point)
124 (should (looking-at "\\* Head2")))
125 ;; 5. With a leading star in link, enforce heading match.
126 (org-test-with-temp-text "#+TARGET: Test\n* Test\n<<Test>>\n[[*Test]]"
127 (goto-line 4)
128 (org-open-at-point)
129 (should (looking-at "\\* Test"))))
133 ;;; Macros
135 (ert-deftest test-org/macro-replace-all ()
136 "Test `org-macro-replace-all' specifications."
137 ;; Standard test.
138 (should
139 (equal
140 "#+MACRO: A B\n1 B 3"
141 (org-test-with-temp-text "#+MACRO: A B\n1 {{{A}}} 3"
142 (progn (org-macro-initialize-templates)
143 (org-macro-replace-all)
144 (buffer-string)))))
145 ;; Macro with arguments.
146 (should
147 (equal
148 "#+MACRO: macro $1 $2\nsome text"
149 (org-test-with-temp-text "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
150 (progn (org-macro-initialize-templates)
151 (org-macro-replace-all)
152 (buffer-string)))))
153 ;; Macro with "eval".
154 (should
155 (equal
156 "#+MACRO: add (eval (+ $1 $2))\n3"
157 (org-test-with-temp-text "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
158 (progn (org-macro-initialize-templates)
159 (org-macro-replace-all)
160 (buffer-string)))))
161 ;; Nested macros.
162 (should
163 (equal
164 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\ninner outer"
165 (org-test-with-temp-text
166 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
167 (progn (org-macro-initialize-templates)
168 (org-macro-replace-all)
169 (buffer-string))))))
173 ;;; Filling
175 (ert-deftest test-org/fill-paragraph ()
176 "Test `org-fill-paragraph' specifications."
177 ;; At an Org table, align it.
178 (org-test-with-temp-text "|a|"
179 (org-fill-paragraph)
180 (should (equal (buffer-string) "| a |\n")))
181 ;; At a paragraph, preserve line breaks.
182 (org-test-with-temp-text "some \\\\\nlong\ntext"
183 (let ((fill-column 20))
184 (org-fill-paragraph)
185 (should (equal (buffer-string) "some \\\\\nlong text"))))
186 ;; Correctly fill a paragraph when point is at its very end.
187 (should
188 (equal "A B"
189 (org-test-with-temp-text "A\nB"
190 (let ((fill-column 20))
191 (goto-char (point-max))
192 (org-fill-paragraph)
193 (buffer-string)))))
194 ;; Correctly fill the last paragraph of a greater element.
195 (should
196 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
197 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
198 (let ((fill-column 8))
199 (forward-line)
200 (end-of-line)
201 (org-fill-paragraph)
202 (buffer-string)))))
203 ;; Correctly fill an element in a narrowed buffer.
204 (should
205 (equal "01234\n6"
206 (org-test-with-temp-text "01234 6789"
207 (let ((fill-column 5))
208 (narrow-to-region 1 8)
209 (org-fill-paragraph)
210 (buffer-string)))))
211 ;; Special case: Fill first paragraph when point is at an item or
212 ;; a plain-list or a footnote reference.
213 (should
214 (equal "- A B"
215 (org-test-with-temp-text "- A\n B"
216 (let ((fill-column 20))
217 (org-fill-paragraph)
218 (buffer-string)))))
219 (should
220 (equal "[fn:1] A B"
221 (org-test-with-temp-text "[fn:1] A\nB"
222 (let ((fill-column 20))
223 (org-fill-paragraph)
224 (buffer-string)))))
225 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
226 (let ((fill-column 20))
227 (org-fill-paragraph)
228 (should (equal (buffer-string)
229 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
230 ;; Fill contents of `comment-block' elements.
231 (should
232 (equal
233 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
234 (let ((fill-column 20))
235 (forward-line)
236 (org-fill-paragraph)
237 (buffer-string)))
238 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
239 ;; Fill `comment' elements.
240 (should
241 (equal " # A B"
242 (org-test-with-temp-text " # A\n # B"
243 (let ((fill-column 20))
244 (org-fill-paragraph)
245 (buffer-string)))))
246 ;; Do nothing at affiliated keywords.
247 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
248 (let ((fill-column 20))
249 (org-fill-paragraph)
250 (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
252 (ert-deftest test-org/auto-fill-function ()
253 "Test auto-filling features."
254 ;; Auto fill paragraph.
255 (should
256 (equal "12345\n7890"
257 (org-test-with-temp-text "12345 7890"
258 (let ((fill-column 5))
259 (end-of-line)
260 (org-auto-fill-function)
261 (buffer-string)))))
262 ;; Auto fill first paragraph in an item.
263 (should
264 (equal "- 12345\n 7890"
265 (org-test-with-temp-text "- 12345 7890"
266 (let ((fill-column 7))
267 (end-of-line)
268 (org-auto-fill-function)
269 (buffer-string)))))
270 ;; Auto fill comments.
271 (should
272 (equal " # 12345\n # 7890"
273 (org-test-with-temp-text " # 12345 7890"
274 (let ((fill-column 10))
275 (end-of-line)
276 (org-auto-fill-function)
277 (buffer-string)))))
278 ;; Comment block: auto fill contents.
279 (should
280 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
281 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
282 (let ((fill-column 5))
283 (forward-line)
284 (end-of-line)
285 (org-auto-fill-function)
286 (buffer-string)))))
287 (should
288 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
289 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
290 (let ((fill-column 5))
291 (forward-line)
292 (end-of-line)
293 (org-auto-fill-function)
294 (buffer-string)))))
295 ;; Do not fill if a new item could be created.
296 (should-not
297 (equal "12345\n- 90"
298 (org-test-with-temp-text "12345 - 90"
299 (let ((fill-column 5))
300 (end-of-line)
301 (org-auto-fill-function)
302 (buffer-string)))))
303 ;; Do not fill if a line break could be introduced.
304 (should-not
305 (equal "123\\\\\n7890"
306 (org-test-with-temp-text "123\\\\ 7890"
307 (let ((fill-column 6))
308 (end-of-line)
309 (org-auto-fill-function)
310 (buffer-string)))))
311 ;; Do not fill affiliated keywords.
312 (should-not
313 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
314 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
315 (let ((fill-column 20))
316 (end-of-line)
317 (org-auto-fill-function)
318 (buffer-string))))))
322 ;;; Comments
324 (ert-deftest test-org/comment-dwim ()
325 "Test `comment-dwim' behaviour in an Org buffer."
326 ;; No region selected, no comment on current line and line not
327 ;; empty: insert comment on line above.
328 (should
329 (equal "# \nComment"
330 (org-test-with-temp-text "Comment"
331 (progn (call-interactively 'comment-dwim)
332 (buffer-string)))))
333 ;; No region selected, no comment on current line and line empty:
334 ;; insert comment on this line.
335 (should
336 (equal "# \nParagraph"
337 (org-test-with-temp-text "\nParagraph"
338 (progn (call-interactively 'comment-dwim)
339 (buffer-string)))))
340 ;; No region selected, and a comment on this line: indent it.
341 (should
342 (equal "* Headline\n # Comment"
343 (org-test-with-temp-text "* Headline\n# Comment"
344 (progn (forward-line)
345 (let ((org-adapt-indentation t))
346 (call-interactively 'comment-dwim))
347 (buffer-string)))))
348 ;; Also recognize single # at column 0 as comments.
349 (should
350 (equal "# Comment"
351 (org-test-with-temp-text "# Comment"
352 (progn (forward-line)
353 (call-interactively 'comment-dwim)
354 (buffer-string)))))
355 ;; Region selected and only comments and blank lines within it:
356 ;; un-comment all commented lines.
357 (should
358 (equal "Comment 1\n\nComment 2"
359 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
360 (progn
361 (transient-mark-mode 1)
362 (push-mark (point) t t)
363 (goto-char (point-max))
364 (call-interactively 'comment-dwim)
365 (buffer-string)))))
366 ;; Region selected without comments: comment all non-blank lines.
367 (should
368 (equal "# Comment 1\n\n# Comment 2"
369 (org-test-with-temp-text "Comment 1\n\nComment 2"
370 (progn
371 (transient-mark-mode 1)
372 (push-mark (point) t t)
373 (goto-char (point-max))
374 (call-interactively 'comment-dwim)
375 (buffer-string)))))
376 ;; In front of a keyword without region, insert a new comment.
377 (should
378 (equal "# \n#+KEYWORD: value"
379 (org-test-with-temp-text "#+KEYWORD: value"
380 (progn (call-interactively 'comment-dwim)
381 (buffer-string))))))
385 ;;; Mark region
387 (ert-deftest test-org/mark-subtree ()
388 "Test `org-mark-subtree' specifications."
389 ;; Error when point is before first headline.
390 (should-error
391 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
392 (progn (transient-mark-mode 1)
393 (org-mark-subtree))))
394 ;; Without argument, mark current subtree.
395 (should
396 (equal
397 '(12 32)
398 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
399 (progn (transient-mark-mode 1)
400 (forward-line 2)
401 (org-mark-subtree)
402 (list (region-beginning) (region-end))))))
403 ;; With an argument, move ARG up.
404 (should
405 (equal
406 '(1 32)
407 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
408 (progn (transient-mark-mode 1)
409 (forward-line 2)
410 (org-mark-subtree 1)
411 (list (region-beginning) (region-end))))))
412 ;; Do not get fooled with inlinetasks.
413 (when (featurep 'org-inlinetask)
414 (should
415 (= 1
416 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
417 (progn (transient-mark-mode 1)
418 (forward-line 1)
419 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
420 (region-beginning)))))))
424 ;; Navigation
426 (ert-deftest test-org/forward-element ()
427 "Test `org-forward-element' specifications."
428 ;; 1. At EOB: should error.
429 (org-test-with-temp-text "Some text\n"
430 (goto-char (point-max))
431 (should-error (org-forward-element)))
432 ;; 2. Standard move: expected to ignore blank lines.
433 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
434 (org-forward-element)
435 (should (looking-at "Second paragraph.")))
436 ;; 3. Headline tests.
437 (org-test-with-temp-text "
438 * Head 1
439 ** Head 1.1
440 *** Head 1.1.1
441 ** Head 1.2"
442 ;; 3.1. At an headline beginning: move to next headline at the
443 ;; same level.
444 (goto-line 3)
445 (org-forward-element)
446 (should (looking-at "** Head 1.2"))
447 ;; 3.2. At an headline beginning: move to parent headline if no
448 ;; headline at the same level.
449 (goto-line 3)
450 (org-forward-element)
451 (should (looking-at "** Head 1.2")))
452 ;; 4. Greater element tests.
453 (org-test-with-temp-text
454 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
455 ;; 4.1. At a greater element: expected to skip contents.
456 (org-forward-element)
457 (should (looking-at "Outside."))
458 ;; 4.2. At the end of greater element contents: expected to skip
459 ;; to the end of the greater element.
460 (goto-line 2)
461 (org-forward-element)
462 (should (looking-at "Outside.")))
463 ;; 5. List tests.
464 (org-test-with-temp-text "
465 - item1
467 - sub1
469 - sub2
471 - sub3
473 Inner paragraph.
475 - item2
477 Outside."
478 ;; 5.1. At list top point: expected to move to the element after
479 ;; the list.
480 (goto-line 2)
481 (org-forward-element)
482 (should (looking-at "Outside."))
483 ;; 5.2. Special case: at the first line of a sub-list, but not at
484 ;; beginning of line, move to next item.
485 (goto-line 2)
486 (forward-char)
487 (org-forward-element)
488 (should (looking-at "- item2"))
489 (goto-line 4)
490 (forward-char)
491 (org-forward-element)
492 (should (looking-at " - sub2"))
493 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
494 (goto-line 4)
495 (org-forward-element)
496 (should (looking-at " Inner paragraph."))
497 ;; 5.4. At sub-list end: expected to move outside the sub-list.
498 (goto-line 8)
499 (org-forward-element)
500 (should (looking-at " Inner paragraph."))
501 ;; 5.5. At an item: expected to move to next item, if any.
502 (goto-line 6)
503 (org-forward-element)
504 (should (looking-at " - sub3"))))
506 (ert-deftest test-org/backward-element ()
507 "Test `org-backward-element' specifications."
508 ;; 1. Should error at BOB.
509 (org-test-with-temp-text " \nParagraph."
510 (should-error (org-backward-element)))
511 ;; 2. Should move at BOB when called on the first element in buffer.
512 (should
513 (org-test-with-temp-text "\n#+TITLE: test"
514 (progn (forward-line)
515 (org-backward-element)
516 (bobp))))
517 ;; 3. Not at the beginning of an element: move at its beginning.
518 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
519 (goto-line 3)
520 (end-of-line)
521 (org-backward-element)
522 (should (looking-at "Paragraph2.")))
523 ;; 4. Headline tests.
524 (org-test-with-temp-text "
525 * Head 1
526 ** Head 1.1
527 *** Head 1.1.1
528 ** Head 1.2"
529 ;; 4.1. At an headline beginning: move to previous headline at the
530 ;; same level.
531 (goto-line 5)
532 (org-backward-element)
533 (should (looking-at "** Head 1.1"))
534 ;; 4.2. At an headline beginning: move to parent headline if no
535 ;; headline at the same level.
536 (goto-line 3)
537 (org-backward-element)
538 (should (looking-at "* Head 1"))
539 ;; 4.3. At the first top-level headline: should error.
540 (goto-line 2)
541 (should-error (org-backward-element)))
542 ;; 5. At beginning of first element inside a greater element:
543 ;; expected to move to greater element's beginning.
544 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
545 (goto-line 3)
546 (org-backward-element)
547 (should (looking-at "#\\+BEGIN_CENTER")))
548 ;; 6. At the beginning of the first element in a section: should
549 ;; move back to headline, if any.
550 (should
551 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
552 (progn (goto-char (point-max))
553 (beginning-of-line)
554 (org-backward-element)
555 (org-at-heading-p))))
556 ;; 7. List tests.
557 (org-test-with-temp-text "
558 - item1
560 - sub1
562 - sub2
564 - sub3
566 Inner paragraph.
568 - item2
571 Outside."
572 ;; 7.1. At beginning of sub-list: expected to move to the
573 ;; paragraph before it.
574 (goto-line 4)
575 (org-backward-element)
576 (should (looking-at "item1"))
577 ;; 7.2. At an item in a list: expected to move at previous item.
578 (goto-line 8)
579 (org-backward-element)
580 (should (looking-at " - sub2"))
581 (goto-line 12)
582 (org-backward-element)
583 (should (looking-at "- item1"))
584 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
585 ;; beginning.
586 (goto-line 10)
587 (org-backward-element)
588 (should (looking-at " - sub1"))
589 (goto-line 15)
590 (org-backward-element)
591 (should (looking-at "- item1"))
592 ;; 7.4. At blank-lines before list end: expected to move to top
593 ;; item.
594 (goto-line 14)
595 (org-backward-element)
596 (should (looking-at "- item1"))))
598 (ert-deftest test-org/up-element ()
599 "Test `org-up-element' specifications."
600 ;; 1. At BOB or with no surrounding element: should error.
601 (org-test-with-temp-text "Paragraph."
602 (should-error (org-up-element)))
603 (org-test-with-temp-text "* Head1\n* Head2"
604 (goto-line 2)
605 (should-error (org-up-element)))
606 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
607 (goto-line 3)
608 (should-error (org-up-element)))
609 ;; 2. At an headline: move to parent headline.
610 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
611 (goto-line 3)
612 (org-up-element)
613 (should (looking-at "\\* Head1")))
614 ;; 3. Inside a greater element: move to greater element beginning.
615 (org-test-with-temp-text
616 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
617 (goto-line 3)
618 (org-up-element)
619 (should (looking-at "#\\+BEGIN_CENTER")))
620 ;; 4. List tests.
621 (org-test-with-temp-text "* Top
622 - item1
624 - sub1
626 - sub2
628 Paragraph within sub2.
630 - item2"
631 ;; 4.1. Within an item: move to the item beginning.
632 (goto-line 8)
633 (org-up-element)
634 (should (looking-at " - sub2"))
635 ;; 4.2. At an item in a sub-list: move to parent item.
636 (goto-line 4)
637 (org-up-element)
638 (should (looking-at "- item1"))
639 ;; 4.3. At an item in top list: move to beginning of whole list.
640 (goto-line 10)
641 (org-up-element)
642 (should (looking-at "- item1"))
643 ;; 4.4. Special case. At very top point: should move to parent of
644 ;; list.
645 (goto-line 2)
646 (org-up-element)
647 (should (looking-at "\\* Top"))))
649 (ert-deftest test-org/down-element ()
650 "Test `org-down-element' specifications."
651 ;; Error when the element hasn't got a recursive type.
652 (org-test-with-temp-text "Paragraph."
653 (should-error (org-down-element)))
654 ;; Error when the element has no contents
655 (org-test-with-temp-text "* Headline"
656 (should-error (org-down-element)))
657 ;; When at a plain-list, move to first item.
658 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
659 (goto-line 2)
660 (org-down-element)
661 (should (looking-at " - Item 1.1")))
662 (org-test-with-temp-text "#+NAME: list\n- Item 1"
663 (org-down-element)
664 (should (looking-at " Item 1")))
665 ;; When at a table, move to first row
666 (org-test-with-temp-text "#+NAME: table\n| a | b |"
667 (org-down-element)
668 (should (looking-at " a | b |")))
669 ;; Otherwise, move inside the greater element.
670 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
671 (org-down-element)
672 (should (looking-at "Paragraph"))))
674 (ert-deftest test-org/drag-element-backward ()
675 "Test `org-drag-element-backward' specifications."
676 ;; 1. Error when trying to move first element of buffer.
677 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
678 (should-error (org-drag-element-backward)))
679 ;; 2. Error when trying to swap nested elements.
680 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
681 (forward-line)
682 (should-error (org-drag-element-backward)))
683 ;; 3. Error when trying to swap an headline element and
684 ;; a non-headline element.
685 (org-test-with-temp-text "Test.\n* Head 1"
686 (forward-line)
687 (should-error (org-drag-element-backward)))
688 ;; 4. Otherwise, swap elements, preserving column and blank lines
689 ;; between elements.
690 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
691 (search-forward "graph")
692 (org-drag-element-backward)
693 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
694 (should (looking-at " 2")))
695 ;; 5. Preserve visibility of elements and their contents.
696 (org-test-with-temp-text "
697 #+BEGIN_CENTER
698 Text.
699 #+END_CENTER
700 - item 1
701 #+BEGIN_QUOTE
702 Text.
703 #+END_QUOTE"
704 (while (search-forward "BEGIN_" nil t) (org-cycle))
705 (search-backward "- item 1")
706 (org-drag-element-backward)
707 (should
708 (equal
709 '((63 . 82) (26 . 48))
710 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
711 (overlays-in (point-min) (point-max)))))))
713 (ert-deftest test-org/drag-element-forward ()
714 "Test `org-drag-element-forward' specifications."
715 ;; 1. Error when trying to move first element of buffer.
716 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
717 (goto-line 3)
718 (should-error (org-drag-element-forward)))
719 ;; 2. Error when trying to swap nested elements.
720 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
721 (forward-line)
722 (should-error (org-drag-element-forward)))
723 ;; 3. Error when trying to swap a non-headline element and an
724 ;; headline.
725 (org-test-with-temp-text "Test.\n* Head 1"
726 (should-error (org-drag-element-forward)))
727 ;; 4. Otherwise, swap elements, preserving column and blank lines
728 ;; between elements.
729 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
730 (search-forward "graph")
731 (org-drag-element-forward)
732 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
733 (should (looking-at " 1")))
734 ;; 5. Preserve visibility of elements and their contents.
735 (org-test-with-temp-text "
736 #+BEGIN_CENTER
737 Text.
738 #+END_CENTER
739 - item 1
740 #+BEGIN_QUOTE
741 Text.
742 #+END_QUOTE"
743 (while (search-forward "BEGIN_" nil t) (org-cycle))
744 (search-backward "#+BEGIN_CENTER")
745 (org-drag-element-forward)
746 (should
747 (equal
748 '((63 . 82) (26 . 48))
749 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
750 (overlays-in (point-min) (point-max)))))))
753 (provide 'test-org)
755 ;;; test-org.el ends here