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