test-org: Attempt to fix failing test on 24.3 pre-release
[org-mode.git] / testing / lisp / test-org.el
blob50a000b50dde9c57fbff7089aaa3e251150fa100
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 ;;; Date Analysis
95 (ert-deftest test-org/org-read-date ()
96 "Test `org-read-date' specifications."
97 ;; Parse ISO date with abbreviated year and month.
98 (should (equal "2012-03-29 16:40"
99 (let ((org-time-was-given t))
100 (org-read-date t nil "12-3-29 16:40"))))
101 ;; Parse Europeans dates.
102 (should (equal "2012-03-29 16:40"
103 (let ((org-time-was-given t))
104 (org-read-date t nil "29.03.2012 16:40"))))
105 ;; Parse Europeans dates without year.
106 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
107 (let ((org-time-was-given t))
108 (org-read-date t nil "29.03. 16:40")))))
112 ;;; Links
114 ;;;; Fuzzy links
116 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
117 ;; a target keyword (aka an invisible target: #+TARGET: text), to
118 ;; a named element (#+name: text) and to headlines (* Text).
120 (ert-deftest test-org/fuzzy-links ()
121 "Test fuzzy links specifications."
122 ;; 1. Fuzzy link goes in priority to a matching target.
123 (org-test-with-temp-text
124 "#+TARGET: Test\n#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
125 (goto-line 6)
126 (org-open-at-point)
127 (should (looking-at "<<Test>>")))
128 ;; 2. Fuzzy link should then go to a matching target keyword.
129 (org-test-with-temp-text
130 "#+NAME: Test\n|a|b|\n#+TARGET: Test\n* Test\n[[Test]]"
131 (goto-line 5)
132 (org-open-at-point)
133 (should (looking-at "#\\+TARGET: Test")))
134 ;; 3. Then fuzzy link points to an element with a given name.
135 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
136 (goto-line 5)
137 (org-open-at-point)
138 (should (looking-at "#\\+NAME: Test")))
139 ;; 4. A target still lead to a matching headline otherwise.
140 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
141 (goto-line 4)
142 (org-open-at-point)
143 (should (looking-at "\\* Head2")))
144 ;; 5. With a leading star in link, enforce heading match.
145 (org-test-with-temp-text "#+TARGET: Test\n* Test\n<<Test>>\n[[*Test]]"
146 (goto-line 4)
147 (org-open-at-point)
148 (should (looking-at "\\* Test"))))
152 ;;; Filling
154 (ert-deftest test-org/fill-paragraph ()
155 "Test `org-fill-paragraph' specifications."
156 ;; At an Org table, align it.
157 (org-test-with-temp-text "|a|"
158 (org-fill-paragraph)
159 (should (equal (buffer-string) "| a |\n")))
160 ;; At a paragraph, preserve line breaks.
161 (org-test-with-temp-text "some \\\\\nlong\ntext"
162 (let ((fill-column 20))
163 (org-fill-paragraph)
164 (should (equal (buffer-string) "some \\\\\nlong text"))))
165 ;; Correctly fill a paragraph when point is at its very end.
166 (should
167 (equal "A B"
168 (org-test-with-temp-text "A\nB"
169 (let ((fill-column 20))
170 (goto-char (point-max))
171 (org-fill-paragraph)
172 (buffer-string)))))
173 ;; Correctly fill the last paragraph of a greater element.
174 (should
175 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
176 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
177 (let ((fill-column 8))
178 (forward-line)
179 (end-of-line)
180 (org-fill-paragraph)
181 (buffer-string)))))
182 ;; Correctly fill an element in a narrowed buffer.
183 (should
184 (equal "01234\n6"
185 (org-test-with-temp-text "01234 6789"
186 (let ((fill-column 5))
187 (narrow-to-region 1 8)
188 (org-fill-paragraph)
189 (buffer-string)))))
190 ;; Special case: Fill first paragraph when point is at an item or
191 ;; a plain-list or a footnote reference.
192 (should
193 (equal "- A B"
194 (org-test-with-temp-text "- A\n B"
195 (let ((fill-column 20))
196 (org-fill-paragraph)
197 (buffer-string)))))
198 (should
199 (equal "[fn:1] A B"
200 (org-test-with-temp-text "[fn:1] A\nB"
201 (let ((fill-column 20))
202 (org-fill-paragraph)
203 (buffer-string)))))
204 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
205 (let ((fill-column 20))
206 (org-fill-paragraph)
207 (should (equal (buffer-string)
208 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
209 ;; Fill contents of `comment-block' elements.
210 (should
211 (equal
212 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
213 (let ((fill-column 20))
214 (forward-line)
215 (org-fill-paragraph)
216 (buffer-string)))
217 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
218 ;; Fill `comment' elements.
219 (should
220 (equal " # A B"
221 (org-test-with-temp-text " # A\n # B"
222 (let ((fill-column 20))
223 (org-fill-paragraph)
224 (buffer-string)))))
225 ;; Do nothing at affiliated keywords.
226 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
227 (let ((fill-column 20))
228 (org-fill-paragraph)
229 (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
231 (ert-deftest test-org/auto-fill-function ()
232 "Test auto-filling features."
233 ;; Auto fill paragraph.
234 (should
235 (equal "12345\n7890"
236 (org-test-with-temp-text "12345 7890"
237 (let ((fill-column 5))
238 (end-of-line)
239 (org-auto-fill-function)
240 (buffer-string)))))
241 ;; Auto fill first paragraph in an item.
242 (should
243 (equal "- 12345\n 7890"
244 (org-test-with-temp-text "- 12345 7890"
245 (let ((fill-column 7))
246 (end-of-line)
247 (org-auto-fill-function)
248 (buffer-string)))))
249 ;; Auto fill comments.
250 (should
251 (equal " # 12345\n # 7890"
252 (org-test-with-temp-text " # 12345 7890"
253 (let ((fill-column 10))
254 (end-of-line)
255 (org-auto-fill-function)
256 (buffer-string)))))
257 ;; A hash within a line isn't a comment.
258 (should-not
259 (equal "12345 # 7890\n# 1"
260 (org-test-with-temp-text "12345 # 7890 1"
261 (let ((fill-column 12))
262 (end-of-line)
263 (org-auto-fill-function)
264 (buffer-string)))))
265 ;; Correctly interpret empty prefix.
266 (should-not
267 (equal "# a\n# b\nRegular\n# paragraph"
268 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
269 (let ((fill-column 12))
270 (end-of-line 3)
271 (org-auto-fill-function)
272 (buffer-string)))))
273 ;; Comment block: auto fill contents.
274 (should
275 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
276 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
277 (let ((fill-column 5))
278 (forward-line)
279 (end-of-line)
280 (org-auto-fill-function)
281 (buffer-string)))))
282 (should
283 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
284 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
285 (let ((fill-column 5))
286 (forward-line)
287 (end-of-line)
288 (org-auto-fill-function)
289 (buffer-string)))))
290 ;; Do not fill if a new item could be created.
291 (should-not
292 (equal "12345\n- 90"
293 (org-test-with-temp-text "12345 - 90"
294 (let ((fill-column 5))
295 (end-of-line)
296 (org-auto-fill-function)
297 (buffer-string)))))
298 ;; Do not fill if a line break could be introduced.
299 (should-not
300 (equal "123\\\\\n7890"
301 (org-test-with-temp-text "123\\\\ 7890"
302 (let ((fill-column 6))
303 (end-of-line)
304 (org-auto-fill-function)
305 (buffer-string)))))
306 ;; Do not fill affiliated keywords.
307 (should-not
308 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
309 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
310 (let ((fill-column 20))
311 (end-of-line)
312 (org-auto-fill-function)
313 (buffer-string))))))
317 ;;; Comments
319 (ert-deftest test-org/comment-dwim ()
320 "Test `comment-dwim' behaviour in an Org buffer."
321 ;; No region selected, no comment on current line and line not
322 ;; empty: insert comment on line above.
323 (should
324 (equal "# \nComment"
325 (org-test-with-temp-text "Comment"
326 (progn (call-interactively 'comment-dwim)
327 (buffer-string)))))
328 ;; No region selected, no comment on current line and line empty:
329 ;; insert comment on this line.
330 (should
331 (equal "# \nParagraph"
332 (org-test-with-temp-text "\nParagraph"
333 (progn (call-interactively 'comment-dwim)
334 (buffer-string)))))
335 ;; No region selected, and a comment on this line: indent it.
336 (should
337 (equal "* Headline\n # Comment"
338 (org-test-with-temp-text "* Headline\n# Comment"
339 (progn (forward-line)
340 (let ((org-adapt-indentation t))
341 (call-interactively 'comment-dwim))
342 (buffer-string)))))
343 ;; Also recognize single # at column 0 as comments.
344 (should
345 (equal "# Comment"
346 (org-test-with-temp-text "# Comment"
347 (progn (forward-line)
348 (call-interactively 'comment-dwim)
349 (buffer-string)))))
350 ;; Region selected and only comments and blank lines within it:
351 ;; un-comment all commented lines.
352 (should
353 (equal "Comment 1\n\nComment 2"
354 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
355 (progn
356 (transient-mark-mode 1)
357 (push-mark (point) t t)
358 (goto-char (point-max))
359 (call-interactively 'comment-dwim)
360 (buffer-string)))))
361 ;; Region selected without comments: comment all non-blank lines.
362 (should
363 (equal "# Comment 1\n\n# Comment 2"
364 (org-test-with-temp-text "Comment 1\n\nComment 2"
365 (progn
366 (transient-mark-mode 1)
367 (push-mark (point) t t)
368 (goto-char (point-max))
369 (call-interactively 'comment-dwim)
370 (buffer-string)))))
371 ;; In front of a keyword without region, insert a new comment.
372 (should
373 (equal "# \n#+KEYWORD: value"
374 (org-test-with-temp-text "#+KEYWORD: value"
375 (progn (call-interactively 'comment-dwim)
376 (buffer-string))))))
380 ;;; Mark region
382 (ert-deftest test-org/mark-subtree ()
383 "Test `org-mark-subtree' specifications."
384 ;; Error when point is before first headline.
385 (should-error
386 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
387 (progn (transient-mark-mode 1)
388 (org-mark-subtree))))
389 ;; Without argument, mark current subtree.
390 (should
391 (equal
392 '(12 32)
393 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
394 (progn (transient-mark-mode 1)
395 (forward-line 2)
396 (org-mark-subtree)
397 (list (region-beginning) (region-end))))))
398 ;; With an argument, move ARG up.
399 (should
400 (equal
401 '(1 32)
402 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
403 (progn (transient-mark-mode 1)
404 (forward-line 2)
405 (org-mark-subtree 1)
406 (list (region-beginning) (region-end))))))
407 ;; Do not get fooled with inlinetasks.
408 (when (featurep 'org-inlinetask)
409 (should
410 (= 1
411 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
412 (progn (transient-mark-mode 1)
413 (forward-line 1)
414 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
415 (region-beginning)))))))
419 ;; Navigation
421 (ert-deftest test-org/beginning-of-line ()
422 "Test `org-beginning-of-line' specifications."
423 ;; Standard test.
424 (should
425 (org-test-with-temp-text "Some text\nSome other text"
426 (progn (org-beginning-of-line) (bolp))))
427 ;; Standard test with `visual-line-mode'.
428 (should-not
429 (org-test-with-temp-text "A long line of text\nSome other text"
430 (progn (visual-line-mode)
431 (forward-char 2)
432 (dotimes (i 1000) (insert "very "))
433 (org-beginning-of-line)
434 (bolp))))
435 ;; At an headline with special movement.
436 (should
437 (org-test-with-temp-text "* TODO Headline"
438 (let ((org-special-ctrl-a/e t))
439 (org-end-of-line)
440 (and (progn (org-beginning-of-line) (looking-at "Headline"))
441 (progn (org-beginning-of-line) (bolp))
442 (progn (org-beginning-of-line) (looking-at "Headline")))))))
444 (ert-deftest test-org/end-of-line ()
445 "Test `org-end-of-line' specifications."
446 ;; Standard test.
447 (should
448 (org-test-with-temp-text "Some text\nSome other text"
449 (progn (org-end-of-line) (eolp))))
450 ;; Standard test with `visual-line-mode'.
451 (should-not
452 (org-test-with-temp-text "A long line of text\nSome other text"
453 (progn (visual-line-mode)
454 (forward-char 2)
455 (dotimes (i 1000) (insert "very "))
456 (goto-char (point-min))
457 (org-end-of-line)
458 (eolp))))
459 ;; At an headline with special movement.
460 (should
461 (org-test-with-temp-text "* Headline :tag:\n"
462 (let ((org-special-ctrl-a/e t))
463 (and (progn (org-end-of-line) (looking-at " :tag:"))
464 (progn (org-end-of-line) (eolp))
465 (progn (org-end-of-line) (looking-at " :tag:"))))))
466 ;; At an headline without special movement.
467 (should
468 (org-test-with-temp-text "* Headline :tag:\n"
469 (let ((org-special-ctrl-a/e nil))
470 (and (progn (org-end-of-line) (eolp))
471 (progn (org-end-of-line) (eolp))))))
472 ;; At an headline, with reversed movement.
473 (should
474 (org-test-with-temp-text "* Headline :tag:\n"
475 (let ((org-special-ctrl-a/e 'reversed)
476 (this-command last-command))
477 (and (progn (org-end-of-line) (eolp))
478 (progn (org-end-of-line) (looking-at " :tag:"))))))
479 ;; At a block without hidden contents.
480 (should
481 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
482 (progn (org-end-of-line) (eolp))))
483 ;; At a block with hidden contents.
484 (should-not
485 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
486 (let ((org-special-ctrl-a/e t))
487 (org-hide-block-toggle)
488 (org-end-of-line)
489 (eobp)))))
491 (ert-deftest test-org/forward-element ()
492 "Test `org-forward-element' specifications."
493 ;; 1. At EOB: should error.
494 (org-test-with-temp-text "Some text\n"
495 (goto-char (point-max))
496 (should-error (org-forward-element)))
497 ;; 2. Standard move: expected to ignore blank lines.
498 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
499 (org-forward-element)
500 (should (looking-at "Second paragraph.")))
501 ;; 3. Headline tests.
502 (org-test-with-temp-text "
503 * Head 1
504 ** Head 1.1
505 *** Head 1.1.1
506 ** Head 1.2"
507 ;; 3.1. At an headline beginning: move to next headline at the
508 ;; same level.
509 (goto-line 3)
510 (org-forward-element)
511 (should (looking-at "** Head 1.2"))
512 ;; 3.2. At an headline beginning: move to parent headline if no
513 ;; headline at the same level.
514 (goto-line 3)
515 (org-forward-element)
516 (should (looking-at "** Head 1.2")))
517 ;; 4. Greater element tests.
518 (org-test-with-temp-text
519 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
520 ;; 4.1. At a greater element: expected to skip contents.
521 (org-forward-element)
522 (should (looking-at "Outside."))
523 ;; 4.2. At the end of greater element contents: expected to skip
524 ;; to the end of the greater element.
525 (goto-line 2)
526 (org-forward-element)
527 (should (looking-at "Outside.")))
528 ;; 5. List tests.
529 (org-test-with-temp-text "
530 - item1
532 - sub1
534 - sub2
536 - sub3
538 Inner paragraph.
540 - item2
542 Outside."
543 ;; 5.1. At list top point: expected to move to the element after
544 ;; the list.
545 (goto-line 2)
546 (org-forward-element)
547 (should (looking-at "Outside."))
548 ;; 5.2. Special case: at the first line of a sub-list, but not at
549 ;; beginning of line, move to next item.
550 (goto-line 2)
551 (forward-char)
552 (org-forward-element)
553 (should (looking-at "- item2"))
554 (goto-line 4)
555 (forward-char)
556 (org-forward-element)
557 (should (looking-at " - sub2"))
558 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
559 (goto-line 4)
560 (org-forward-element)
561 (should (looking-at " Inner paragraph."))
562 ;; 5.4. At sub-list end: expected to move outside the sub-list.
563 (goto-line 8)
564 (org-forward-element)
565 (should (looking-at " Inner paragraph."))
566 ;; 5.5. At an item: expected to move to next item, if any.
567 (goto-line 6)
568 (org-forward-element)
569 (should (looking-at " - sub3"))))
571 (ert-deftest test-org/backward-element ()
572 "Test `org-backward-element' specifications."
573 ;; 1. Should error at BOB.
574 (org-test-with-temp-text " \nParagraph."
575 (should-error (org-backward-element)))
576 ;; 2. Should move at BOB when called on the first element in buffer.
577 (should
578 (org-test-with-temp-text "\n#+TITLE: test"
579 (progn (forward-line)
580 (org-backward-element)
581 (bobp))))
582 ;; 3. Not at the beginning of an element: move at its beginning.
583 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
584 (goto-line 3)
585 (end-of-line)
586 (org-backward-element)
587 (should (looking-at "Paragraph2.")))
588 ;; 4. Headline tests.
589 (org-test-with-temp-text "
590 * Head 1
591 ** Head 1.1
592 *** Head 1.1.1
593 ** Head 1.2"
594 ;; 4.1. At an headline beginning: move to previous headline at the
595 ;; same level.
596 (goto-line 5)
597 (org-backward-element)
598 (should (looking-at "** Head 1.1"))
599 ;; 4.2. At an headline beginning: move to parent headline if no
600 ;; headline at the same level.
601 (goto-line 3)
602 (org-backward-element)
603 (should (looking-at "* Head 1"))
604 ;; 4.3. At the first top-level headline: should error.
605 (goto-line 2)
606 (should-error (org-backward-element)))
607 ;; 5. At beginning of first element inside a greater element:
608 ;; expected to move to greater element's beginning.
609 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
610 (goto-line 3)
611 (org-backward-element)
612 (should (looking-at "#\\+BEGIN_CENTER")))
613 ;; 6. At the beginning of the first element in a section: should
614 ;; move back to headline, if any.
615 (should
616 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
617 (progn (goto-char (point-max))
618 (beginning-of-line)
619 (org-backward-element)
620 (org-at-heading-p))))
621 ;; 7. List tests.
622 (org-test-with-temp-text "
623 - item1
625 - sub1
627 - sub2
629 - sub3
631 Inner paragraph.
633 - item2
636 Outside."
637 ;; 7.1. At beginning of sub-list: expected to move to the
638 ;; paragraph before it.
639 (goto-line 4)
640 (org-backward-element)
641 (should (looking-at "item1"))
642 ;; 7.2. At an item in a list: expected to move at previous item.
643 (goto-line 8)
644 (org-backward-element)
645 (should (looking-at " - sub2"))
646 (goto-line 12)
647 (org-backward-element)
648 (should (looking-at "- item1"))
649 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
650 ;; beginning.
651 (goto-line 10)
652 (org-backward-element)
653 (should (looking-at " - sub1"))
654 (goto-line 15)
655 (org-backward-element)
656 (should (looking-at "- item1"))
657 ;; 7.4. At blank-lines before list end: expected to move to top
658 ;; item.
659 (goto-line 14)
660 (org-backward-element)
661 (should (looking-at "- item1"))))
663 (ert-deftest test-org/up-element ()
664 "Test `org-up-element' specifications."
665 ;; 1. At BOB or with no surrounding element: should error.
666 (org-test-with-temp-text "Paragraph."
667 (should-error (org-up-element)))
668 (org-test-with-temp-text "* Head1\n* Head2"
669 (goto-line 2)
670 (should-error (org-up-element)))
671 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
672 (goto-line 3)
673 (should-error (org-up-element)))
674 ;; 2. At an headline: move to parent headline.
675 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
676 (goto-line 3)
677 (org-up-element)
678 (should (looking-at "\\* Head1")))
679 ;; 3. Inside a greater element: move to greater element beginning.
680 (org-test-with-temp-text
681 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
682 (goto-line 3)
683 (org-up-element)
684 (should (looking-at "#\\+BEGIN_CENTER")))
685 ;; 4. List tests.
686 (org-test-with-temp-text "* Top
687 - item1
689 - sub1
691 - sub2
693 Paragraph within sub2.
695 - item2"
696 ;; 4.1. Within an item: move to the item beginning.
697 (goto-line 8)
698 (org-up-element)
699 (should (looking-at " - sub2"))
700 ;; 4.2. At an item in a sub-list: move to parent item.
701 (goto-line 4)
702 (org-up-element)
703 (should (looking-at "- item1"))
704 ;; 4.3. At an item in top list: move to beginning of whole list.
705 (goto-line 10)
706 (org-up-element)
707 (should (looking-at "- item1"))
708 ;; 4.4. Special case. At very top point: should move to parent of
709 ;; list.
710 (goto-line 2)
711 (org-up-element)
712 (should (looking-at "\\* Top"))))
714 (ert-deftest test-org/down-element ()
715 "Test `org-down-element' specifications."
716 ;; Error when the element hasn't got a recursive type.
717 (org-test-with-temp-text "Paragraph."
718 (should-error (org-down-element)))
719 ;; Error when the element has no contents
720 (org-test-with-temp-text "* Headline"
721 (should-error (org-down-element)))
722 ;; When at a plain-list, move to first item.
723 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
724 (goto-line 2)
725 (org-down-element)
726 (should (looking-at " - Item 1.1")))
727 (org-test-with-temp-text "#+NAME: list\n- Item 1"
728 (org-down-element)
729 (should (looking-at " Item 1")))
730 ;; When at a table, move to first row
731 (org-test-with-temp-text "#+NAME: table\n| a | b |"
732 (org-down-element)
733 (should (looking-at " a | b |")))
734 ;; Otherwise, move inside the greater element.
735 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
736 (org-down-element)
737 (should (looking-at "Paragraph"))))
739 (ert-deftest test-org/drag-element-backward ()
740 "Test `org-drag-element-backward' specifications."
741 ;; 1. Error when trying to move first element of buffer.
742 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
743 (should-error (org-drag-element-backward)))
744 ;; 2. Error when trying to swap nested elements.
745 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
746 (forward-line)
747 (should-error (org-drag-element-backward)))
748 ;; 3. Error when trying to swap an headline element and
749 ;; a non-headline element.
750 (org-test-with-temp-text "Test.\n* Head 1"
751 (forward-line)
752 (should-error (org-drag-element-backward)))
753 ;; 4. Otherwise, swap elements, preserving column and blank lines
754 ;; between elements.
755 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
756 (search-forward "graph")
757 (org-drag-element-backward)
758 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
759 (should (looking-at " 2")))
760 ;; 5. Preserve visibility of elements and their contents.
761 (org-test-with-temp-text "
762 #+BEGIN_CENTER
763 Text.
764 #+END_CENTER
765 - item 1
766 #+BEGIN_QUOTE
767 Text.
768 #+END_QUOTE"
769 (while (search-forward "BEGIN_" nil t) (org-cycle))
770 (search-backward "- item 1")
771 (org-drag-element-backward)
772 (should
773 (equal
774 '((63 . 82) (26 . 48))
775 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
776 (overlays-in (point-min) (point-max)))))))
778 (ert-deftest test-org/drag-element-forward ()
779 "Test `org-drag-element-forward' specifications."
780 ;; 1. Error when trying to move first element of buffer.
781 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
782 (goto-line 3)
783 (should-error (org-drag-element-forward)))
784 ;; 2. Error when trying to swap nested elements.
785 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
786 (forward-line)
787 (should-error (org-drag-element-forward)))
788 ;; 3. Error when trying to swap a non-headline element and an
789 ;; headline.
790 (org-test-with-temp-text "Test.\n* Head 1"
791 (should-error (org-drag-element-forward)))
792 ;; 4. Otherwise, swap elements, preserving column and blank lines
793 ;; between elements.
794 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
795 (search-forward "graph")
796 (org-drag-element-forward)
797 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
798 (should (looking-at " 1")))
799 ;; 5. Preserve visibility of elements and their contents.
800 (org-test-with-temp-text "
801 #+BEGIN_CENTER
802 Text.
803 #+END_CENTER
804 - item 1
805 #+BEGIN_QUOTE
806 Text.
807 #+END_QUOTE"
808 (while (search-forward "BEGIN_" nil t) (org-cycle))
809 (search-backward "#+BEGIN_CENTER")
810 (org-drag-element-forward)
811 (should
812 (equal
813 '((63 . 82) (26 . 48))
814 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
815 (overlays-in (point-min) (point-max)))))))
818 (provide 'test-org)
820 ;;; test-org.el ends here