Fix bug in comment-dwin when at a keyword
[org-mode.git] / testing / lisp / test-org.el
blobfdfd4ca34c05a88a0f0f89fc53defa8ae7cdd3a5
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 ;;; Filling
135 (ert-deftest test-org/fill-paragraph ()
136 "Test `org-fill-paragraph' specifications."
137 ;; At an Org table, align it.
138 (org-test-with-temp-text "|a|"
139 (org-fill-paragraph)
140 (should (equal (buffer-string) "| a |\n")))
141 ;; At a paragraph, preserve line breaks.
142 (org-test-with-temp-text "some \\\\\nlong\ntext"
143 (let ((fill-column 20))
144 (org-fill-paragraph)
145 (should (equal (buffer-string) "some \\\\\nlong text"))))
146 ;; Correctly fill a paragraph when point is at its very end.
147 (should
148 (equal "A B"
149 (org-test-with-temp-text "A\nB"
150 (let ((fill-column 20))
151 (goto-char (point-max))
152 (org-fill-paragraph)
153 (buffer-string)))))
154 ;; Correctly fill the last paragraph of a greater element.
155 (should
156 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
157 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
158 (let ((fill-column 8))
159 (forward-line)
160 (end-of-line)
161 (org-fill-paragraph)
162 (buffer-string)))))
163 ;; Correctly fill an element in a narrowed buffer.
164 (should
165 (equal "01234\n6"
166 (org-test-with-temp-text "01234 6789"
167 (let ((fill-column 5))
168 (narrow-to-region 1 8)
169 (org-fill-paragraph)
170 (buffer-string)))))
171 ;; Special case: Fill first paragraph when point is at an item or
172 ;; a plain-list or a footnote reference.
173 (should
174 (equal "- A B"
175 (org-test-with-temp-text "- A\n B"
176 (let ((fill-column 20))
177 (org-fill-paragraph)
178 (buffer-string)))))
179 (should
180 (equal "[fn:1] A B"
181 (org-test-with-temp-text "[fn:1] A\nB"
182 (let ((fill-column 20))
183 (org-fill-paragraph)
184 (buffer-string)))))
185 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
186 (let ((fill-column 20))
187 (org-fill-paragraph)
188 (should (equal (buffer-string)
189 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
190 ;; Fill contents of `comment-block' elements.
191 (should
192 (equal
193 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
194 (let ((fill-column 20))
195 (forward-line)
196 (org-fill-paragraph)
197 (buffer-string)))
198 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
199 ;; Fill `comment' elements.
200 (should
201 (equal " # A B"
202 (org-test-with-temp-text " # A\n # B"
203 (let ((fill-column 20))
204 (org-fill-paragraph)
205 (buffer-string)))))
206 ;; Do nothing at affiliated keywords.
207 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
208 (let ((fill-column 20))
209 (org-fill-paragraph)
210 (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
212 (ert-deftest test-org/auto-fill-function ()
213 "Test auto-filling features."
214 ;; Auto fill paragraph.
215 (should
216 (equal "12345\n7890"
217 (org-test-with-temp-text "12345 7890"
218 (let ((fill-column 5))
219 (end-of-line)
220 (org-auto-fill-function)
221 (buffer-string)))))
222 ;; Auto fill first paragraph in an item.
223 (should
224 (equal "- 12345\n 7890"
225 (org-test-with-temp-text "- 12345 7890"
226 (let ((fill-column 7))
227 (end-of-line)
228 (org-auto-fill-function)
229 (buffer-string)))))
230 ;; Auto fill comments.
231 (should
232 (equal " # 12345\n # 7890"
233 (org-test-with-temp-text " # 12345 7890"
234 (let ((fill-column 10))
235 (end-of-line)
236 (org-auto-fill-function)
237 (buffer-string)))))
238 ;; Comment block: auto fill contents.
239 (should
240 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
241 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
242 (let ((fill-column 5))
243 (forward-line)
244 (end-of-line)
245 (org-auto-fill-function)
246 (buffer-string)))))
247 (should
248 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
249 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
250 (let ((fill-column 5))
251 (forward-line)
252 (end-of-line)
253 (org-auto-fill-function)
254 (buffer-string)))))
255 ;; Do not fill if a new item could be created.
256 (should-not
257 (equal "12345\n- 90"
258 (org-test-with-temp-text "12345 - 90"
259 (let ((fill-column 5))
260 (end-of-line)
261 (org-auto-fill-function)
262 (buffer-string)))))
263 ;; Do not fill if a line break could be introduced.
264 (should-not
265 (equal "123\\\\\n7890"
266 (org-test-with-temp-text "123\\\\ 7890"
267 (let ((fill-column 6))
268 (end-of-line)
269 (org-auto-fill-function)
270 (buffer-string)))))
271 ;; Do not fill affiliated keywords.
272 (should-not
273 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
274 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
275 (let ((fill-column 20))
276 (end-of-line)
277 (org-auto-fill-function)
278 (buffer-string))))))
282 ;;; Comments
284 (ert-deftest test-org/comment-dwim ()
285 "Test `comment-dwim' behaviour in an Org buffer."
286 ;; No region selected, no comment on current line and line not
287 ;; empty: insert comment on line above.
288 (should
289 (equal "# \nComment"
290 (org-test-with-temp-text "Comment"
291 (progn (call-interactively 'comment-dwim)
292 (buffer-string)))))
293 ;; No region selected, no comment on current line and line empty:
294 ;; insert comment on this line.
295 (should
296 (equal "# \nParagraph"
297 (org-test-with-temp-text "\nParagraph"
298 (progn (call-interactively 'comment-dwim)
299 (buffer-string)))))
300 ;; No region selected, and a comment on this line: indent it.
301 (should
302 (equal "* Headline\n # Comment"
303 (org-test-with-temp-text "* Headline\n# Comment"
304 (progn (forward-line)
305 (let ((org-adapt-indentation t))
306 (call-interactively 'comment-dwim))
307 (buffer-string)))))
308 ;; Also recognize single # at column 0 as comments.
309 (should
310 (equal "# Comment"
311 (org-test-with-temp-text "# Comment"
312 (progn (forward-line)
313 (call-interactively 'comment-dwim)
314 (buffer-string)))))
315 ;; Region selected and only comments and blank lines within it:
316 ;; un-comment all commented lines.
317 (should
318 (equal "Comment 1\n\nComment 2"
319 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
320 (progn
321 (transient-mark-mode 1)
322 (push-mark (point) t t)
323 (goto-char (point-max))
324 (call-interactively 'comment-dwim)
325 (buffer-string)))))
326 ;; Region selected without comments: comment all non-blank lines.
327 (should
328 (equal "# Comment 1\n\n# Comment 2"
329 (org-test-with-temp-text "Comment 1\n\nComment 2"
330 (progn
331 (transient-mark-mode 1)
332 (push-mark (point) t t)
333 (goto-char (point-max))
334 (call-interactively 'comment-dwim)
335 (buffer-string)))))
336 ;; In front of a keyword without region, insert a new comment.
337 (should
338 (equal "# \n#+KEYWORD: value"
339 (org-test-with-temp-text "#+KEYWORD: value"
340 (progn (call-interactively 'comment-dwim)
341 (buffer-string))))))
345 ;;; Mark region
347 (ert-deftest test-org/mark-subtree ()
348 "Test `org-mark-subtree' specifications."
349 ;; Error when point is before first headline.
350 (should-error
351 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
352 (progn (transient-mark-mode 1)
353 (org-mark-subtree))))
354 ;; Without argument, mark current subtree.
355 (should
356 (equal
357 '(12 32)
358 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
359 (progn (transient-mark-mode 1)
360 (forward-line 2)
361 (org-mark-subtree)
362 (list (region-beginning) (region-end))))))
363 ;; With an argument, move ARG up.
364 (should
365 (equal
366 '(1 32)
367 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
368 (progn (transient-mark-mode 1)
369 (forward-line 2)
370 (org-mark-subtree 1)
371 (list (region-beginning) (region-end))))))
372 ;; Do not get fooled with inlinetasks.
373 (when (featurep 'org-inlinetask)
374 (should
375 (= 1
376 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
377 (progn (transient-mark-mode 1)
378 (forward-line 1)
379 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
380 (region-beginning)))))))
384 ;; Navigation
386 (ert-deftest test-org/forward-element ()
387 "Test `org-forward-element' specifications."
388 ;; 1. At EOB: should error.
389 (org-test-with-temp-text "Some text\n"
390 (goto-char (point-max))
391 (should-error (org-forward-element)))
392 ;; 2. Standard move: expected to ignore blank lines.
393 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
394 (org-forward-element)
395 (should (looking-at "Second paragraph.")))
396 ;; 3. Headline tests.
397 (org-test-with-temp-text "
398 * Head 1
399 ** Head 1.1
400 *** Head 1.1.1
401 ** Head 1.2"
402 ;; 3.1. At an headline beginning: move to next headline at the
403 ;; same level.
404 (goto-line 3)
405 (org-forward-element)
406 (should (looking-at "** Head 1.2"))
407 ;; 3.2. At an headline beginning: move to parent headline if no
408 ;; headline at the same level.
409 (goto-line 3)
410 (org-forward-element)
411 (should (looking-at "** Head 1.2")))
412 ;; 4. Greater element tests.
413 (org-test-with-temp-text
414 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
415 ;; 4.1. At a greater element: expected to skip contents.
416 (org-forward-element)
417 (should (looking-at "Outside."))
418 ;; 4.2. At the end of greater element contents: expected to skip
419 ;; to the end of the greater element.
420 (goto-line 2)
421 (org-forward-element)
422 (should (looking-at "Outside.")))
423 ;; 5. List tests.
424 (org-test-with-temp-text "
425 - item1
427 - sub1
429 - sub2
431 - sub3
433 Inner paragraph.
435 - item2
437 Outside."
438 ;; 5.1. At list top point: expected to move to the element after
439 ;; the list.
440 (goto-line 2)
441 (org-forward-element)
442 (should (looking-at "Outside."))
443 ;; 5.2. Special case: at the first line of a sub-list, but not at
444 ;; beginning of line, move to next item.
445 (goto-line 2)
446 (forward-char)
447 (org-forward-element)
448 (should (looking-at "- item2"))
449 (goto-line 4)
450 (forward-char)
451 (org-forward-element)
452 (should (looking-at " - sub2"))
453 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
454 (goto-line 4)
455 (org-forward-element)
456 (should (looking-at " Inner paragraph."))
457 ;; 5.4. At sub-list end: expected to move outside the sub-list.
458 (goto-line 8)
459 (org-forward-element)
460 (should (looking-at " Inner paragraph."))
461 ;; 5.5. At an item: expected to move to next item, if any.
462 (goto-line 6)
463 (org-forward-element)
464 (should (looking-at " - sub3"))))
466 (ert-deftest test-org/backward-element ()
467 "Test `org-backward-element' specifications."
468 ;; 1. At BOB (modulo some white spaces): should error.
469 (org-test-with-temp-text " \nParagraph."
470 (org-skip-whitespace)
471 (should-error (org-backward-element)))
472 ;; 2. Not at the beginning of an element: move at its beginning.
473 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
474 (goto-line 3)
475 (end-of-line)
476 (org-backward-element)
477 (should (looking-at "Paragraph2.")))
478 ;; 3. Headline tests.
479 (org-test-with-temp-text "
480 * Head 1
481 ** Head 1.1
482 *** Head 1.1.1
483 ** Head 1.2"
484 ;; 3.1. At an headline beginning: move to previous headline at the
485 ;; same level.
486 (goto-line 5)
487 (org-backward-element)
488 (should (looking-at "** Head 1.1"))
489 ;; 3.2. At an headline beginning: move to parent headline if no
490 ;; headline at the same level.
491 (goto-line 3)
492 (org-backward-element)
493 (should (looking-at "* Head 1"))
494 ;; 3.3. At the first top-level headline: should error.
495 (goto-line 2)
496 (should-error (org-backward-element)))
497 ;; 4. At beginning of first element inside a greater element:
498 ;; expected to move to greater element's beginning.
499 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER."
500 (goto-line 3)
501 (org-backward-element)
502 (should (looking-at "#\\+BEGIN_CENTER")))
503 ;; 5. List tests.
504 (org-test-with-temp-text "
505 - item1
507 - sub1
509 - sub2
511 - sub3
513 Inner paragraph.
515 - item2
518 Outside."
519 ;; 5.1. At beginning of sub-list: expected to move to the
520 ;; paragraph before it.
521 (goto-line 4)
522 (org-backward-element)
523 (should (looking-at "item1"))
524 ;; 5.2. At an item in a list: expected to move at previous item.
525 (goto-line 8)
526 (org-backward-element)
527 (should (looking-at " - sub2"))
528 (goto-line 12)
529 (org-backward-element)
530 (should (looking-at "- item1"))
531 ;; 5.3. At end of list/sub-list: expected to move to list/sub-list
532 ;; beginning.
533 (goto-line 10)
534 (org-backward-element)
535 (should (looking-at " - sub1"))
536 (goto-line 15)
537 (org-backward-element)
538 (should (looking-at "- item1"))
539 ;; 5.4. At blank-lines before list end: expected to move to top
540 ;; item.
541 (goto-line 14)
542 (org-backward-element)
543 (should (looking-at "- item1"))))
545 (ert-deftest test-org/up-element ()
546 "Test `org-up-element' specifications."
547 ;; 1. At BOB or with no surrounding element: should error.
548 (org-test-with-temp-text "Paragraph."
549 (should-error (org-up-element)))
550 (org-test-with-temp-text "* Head1\n* Head2"
551 (goto-line 2)
552 (should-error (org-up-element)))
553 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
554 (goto-line 3)
555 (should-error (org-up-element)))
556 ;; 2. At an headline: move to parent headline.
557 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
558 (goto-line 3)
559 (org-up-element)
560 (should (looking-at "\\* Head1")))
561 ;; 3. Inside a greater element: move to greater element beginning.
562 (org-test-with-temp-text
563 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
564 (goto-line 3)
565 (org-up-element)
566 (should (looking-at "#\\+BEGIN_CENTER")))
567 ;; 4. List tests.
568 (org-test-with-temp-text "* Top
569 - item1
571 - sub1
573 - sub2
575 Paragraph within sub2.
577 - item2"
578 ;; 4.1. Within an item: move to the item beginning.
579 (goto-line 8)
580 (org-up-element)
581 (should (looking-at " - sub2"))
582 ;; 4.2. At an item in a sub-list: move to parent item.
583 (goto-line 4)
584 (org-up-element)
585 (should (looking-at "- item1"))
586 ;; 4.3. At an item in top list: move to beginning of whole list.
587 (goto-line 10)
588 (org-up-element)
589 (should (looking-at "- item1"))
590 ;; 4.4. Special case. At very top point: should move to parent of
591 ;; list.
592 (goto-line 2)
593 (org-up-element)
594 (should (looking-at "\\* Top"))))
596 (ert-deftest test-org/down-element ()
597 "Test `org-down-element' specifications."
598 ;; Error when the element hasn't got a recursive type.
599 (org-test-with-temp-text "Paragraph."
600 (should-error (org-down-element)))
601 ;; Error when the element has no contents
602 (org-test-with-temp-text "* Headline"
603 (should-error (org-down-element)))
604 ;; When at a plain-list, move to first item.
605 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
606 (goto-line 2)
607 (org-down-element)
608 (should (looking-at " - Item 1.1")))
609 (org-test-with-temp-text "#+NAME: list\n- Item 1"
610 (org-down-element)
611 (should (looking-at " Item 1")))
612 ;; When at a table, move to first row
613 (org-test-with-temp-text "#+NAME: table\n| a | b |"
614 (org-down-element)
615 (should (looking-at " a | b |")))
616 ;; Otherwise, move inside the greater element.
617 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
618 (org-down-element)
619 (should (looking-at "Paragraph"))))
621 (ert-deftest test-org/drag-element-backward ()
622 "Test `org-drag-element-backward' specifications."
623 ;; 1. Error when trying to move first element of buffer.
624 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
625 (should-error (org-drag-element-backward)))
626 ;; 2. Error when trying to swap nested elements.
627 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
628 (forward-line)
629 (should-error (org-drag-element-backward)))
630 ;; 3. Error when trying to swap an headline element and
631 ;; a non-headline element.
632 (org-test-with-temp-text "Test.\n* Head 1"
633 (forward-line)
634 (should-error (org-drag-element-backward)))
635 ;; 4. Otherwise, swap elements, preserving column and blank lines
636 ;; between elements.
637 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
638 (search-forward "graph")
639 (org-drag-element-backward)
640 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
641 (should (looking-at " 2")))
642 ;; 5. Preserve visibility of elements and their contents.
643 (org-test-with-temp-text "
644 #+BEGIN_CENTER
645 Text.
646 #+END_CENTER
647 - item 1
648 #+BEGIN_QUOTE
649 Text.
650 #+END_QUOTE"
651 (while (search-forward "BEGIN_" nil t) (org-cycle))
652 (search-backward "- item 1")
653 (org-drag-element-backward)
654 (should
655 (equal
656 '((63 . 82) (26 . 48))
657 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
658 (overlays-in (point-min) (point-max)))))))
660 (ert-deftest test-org/drag-element-forward ()
661 "Test `org-drag-element-forward' specifications."
662 ;; 1. Error when trying to move first element of buffer.
663 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
664 (goto-line 3)
665 (should-error (org-drag-element-forward)))
666 ;; 2. Error when trying to swap nested elements.
667 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
668 (forward-line)
669 (should-error (org-drag-element-forward)))
670 ;; 3. Error when trying to swap a non-headline element and an
671 ;; headline.
672 (org-test-with-temp-text "Test.\n* Head 1"
673 (should-error (org-drag-element-forward)))
674 ;; 4. Otherwise, swap elements, preserving column and blank lines
675 ;; between elements.
676 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
677 (search-forward "graph")
678 (org-drag-element-forward)
679 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
680 (should (looking-at " 1")))
681 ;; 5. Preserve visibility of elements and their contents.
682 (org-test-with-temp-text "
683 #+BEGIN_CENTER
684 Text.
685 #+END_CENTER
686 - item 1
687 #+BEGIN_QUOTE
688 Text.
689 #+END_QUOTE"
690 (while (search-forward "BEGIN_" nil t) (org-cycle))
691 (search-backward "#+BEGIN_CENTER")
692 (org-drag-element-forward)
693 (should
694 (equal
695 '((63 . 82) (26 . 48))
696 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
697 (overlays-in (point-min) (point-max)))))))
700 (provide 'test-org)
702 ;;; test-org.el ends here