Reintroduce support for `adaptive-fill-regexp' in paragraphs and comments
[org-mode.git] / testing / lisp / test-org.el
blob53300ebcf3d08ff83eb63dd897b07b1a84788791
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:
28 ;;; Comments
30 (ert-deftest test-org/comment-dwim ()
31 "Test `comment-dwim' behaviour in an Org buffer."
32 ;; No region selected, no comment on current line and line not
33 ;; empty: insert comment on line above.
34 (should
35 (equal "# \nComment"
36 (org-test-with-temp-text "Comment"
37 (progn (call-interactively 'comment-dwim)
38 (buffer-string)))))
39 ;; No region selected, no comment on current line and line empty:
40 ;; insert comment on this line.
41 (should
42 (equal "# \nParagraph"
43 (org-test-with-temp-text "\nParagraph"
44 (progn (call-interactively 'comment-dwim)
45 (buffer-string)))))
46 ;; No region selected, and a comment on this line: indent it.
47 (should
48 (equal "* Headline\n # Comment"
49 (org-test-with-temp-text "* Headline\n# Comment"
50 (progn (forward-line)
51 (let ((org-adapt-indentation t))
52 (call-interactively 'comment-dwim))
53 (buffer-string)))))
54 ;; Also recognize single # at column 0 as comments.
55 (should
56 (equal "# Comment"
57 (org-test-with-temp-text "# Comment"
58 (progn (forward-line)
59 (call-interactively 'comment-dwim)
60 (buffer-string)))))
61 ;; Region selected and only comments and blank lines within it:
62 ;; un-comment all commented lines.
63 (should
64 (equal "Comment 1\n\nComment 2"
65 (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
66 (progn
67 (transient-mark-mode 1)
68 (push-mark (point) t t)
69 (goto-char (point-max))
70 (call-interactively 'comment-dwim)
71 (buffer-string)))))
72 ;; Region selected without comments: comment all lines if
73 ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
74 (should
75 (equal "# Comment 1\n\n# Comment 2"
76 (org-test-with-temp-text "Comment 1\n\nComment 2"
77 (progn
78 (transient-mark-mode 1)
79 (push-mark (point) t t)
80 (goto-char (point-max))
81 (let ((comment-empty-lines nil))
82 (call-interactively 'comment-dwim))
83 (buffer-string)))))
84 (should
85 (equal "# Comment 1\n# \n# Comment 2"
86 (org-test-with-temp-text "Comment 1\n\nComment 2"
87 (progn
88 (transient-mark-mode 1)
89 (push-mark (point) t t)
90 (goto-char (point-max))
91 (let ((comment-empty-lines t))
92 (call-interactively 'comment-dwim))
93 (buffer-string)))))
94 ;; In front of a keyword without region, insert a new comment.
95 (should
96 (equal "# \n#+KEYWORD: value"
97 (org-test-with-temp-text "#+KEYWORD: value"
98 (progn (call-interactively 'comment-dwim)
99 (buffer-string))))))
103 ;;; Date and time analysis
105 (ert-deftest test-org/org-read-date ()
106 "Test `org-read-date' specifications."
107 ;; Parse ISO date with abbreviated year and month.
108 (should (equal "2012-03-29 16:40"
109 (let ((org-time-was-given t))
110 (org-read-date t nil "12-3-29 16:40"))))
111 ;; Parse Europeans dates.
112 (should (equal "2012-03-29 16:40"
113 (let ((org-time-was-given t))
114 (org-read-date t nil "29.03.2012 16:40"))))
115 ;; Parse Europeans dates without year.
116 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
117 (let ((org-time-was-given t))
118 (org-read-date t nil "29.03. 16:40")))))
120 (ert-deftest test-org/org-parse-time-string ()
121 "Test `org-parse-time-string'."
122 (should (equal (org-parse-time-string "2012-03-29 16:40")
123 '(0 40 16 29 3 2012 nil nil nil)))
124 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
125 '(0 40 16 29 3 2012 nil nil nil)))
126 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
127 '(0 40 16 29 3 2012 nil nil nil)))
128 (should (equal (org-parse-time-string "<2012-03-29>")
129 '(0 0 0 29 3 2012 nil nil nil)))
130 (should (equal (org-parse-time-string "<2012-03-29>" t)
131 '(0 nil nil 29 3 2012 nil nil nil))))
134 ;;; Filling
136 (ert-deftest test-org/fill-paragraph ()
137 "Test `org-fill-paragraph' specifications."
138 ;; At an Org table, align it.
139 (should
140 (equal "| a |\n"
141 (org-test-with-temp-text "|a|"
142 (org-fill-paragraph)
143 (buffer-string))))
144 (should
145 (equal "#+name: table\n| a |\n"
146 (org-test-with-temp-text "#+name: table\n| a |"
147 (org-fill-paragraph)
148 (buffer-string))))
149 ;; At a paragraph, preserve line breaks.
150 (org-test-with-temp-text "some \\\\\nlong\ntext"
151 (let ((fill-column 20))
152 (org-fill-paragraph)
153 (should (equal (buffer-string) "some \\\\\nlong text"))))
154 ;; Correctly fill a paragraph when point is at its very end.
155 (should
156 (equal "A B"
157 (org-test-with-temp-text "A\nB"
158 (let ((fill-column 20))
159 (goto-char (point-max))
160 (org-fill-paragraph)
161 (buffer-string)))))
162 ;; Correctly fill the last paragraph of a greater element.
163 (should
164 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
165 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
166 (let ((fill-column 8))
167 (forward-line)
168 (end-of-line)
169 (org-fill-paragraph)
170 (buffer-string)))))
171 ;; Correctly fill an element in a narrowed buffer.
172 (should
173 (equal "01234\n6"
174 (org-test-with-temp-text "01234 6789"
175 (let ((fill-column 5))
176 (narrow-to-region 1 8)
177 (org-fill-paragraph)
178 (buffer-string)))))
179 ;; Handle `adaptive-fill-regexp' in paragraphs.
180 (should
181 (equal "> a b"
182 (org-test-with-temp-text "> a\n> b"
183 (let ((fill-column 5)
184 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
185 (org-fill-paragraph)
186 (buffer-string)))))
187 ;; Special case: Fill first paragraph when point is at an item or
188 ;; a plain-list or a footnote reference.
189 (should
190 (equal "- A B"
191 (org-test-with-temp-text "- A\n B"
192 (let ((fill-column 20))
193 (org-fill-paragraph)
194 (buffer-string)))))
195 (should
196 (equal "[fn:1] A B"
197 (org-test-with-temp-text "[fn:1] A\nB"
198 (let ((fill-column 20))
199 (org-fill-paragraph)
200 (buffer-string)))))
201 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
202 (let ((fill-column 20))
203 (org-fill-paragraph)
204 (should (equal (buffer-string)
205 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
206 ;; Fill contents of `comment-block' elements.
207 (should
208 (equal
209 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
210 (let ((fill-column 20))
211 (forward-line)
212 (org-fill-paragraph)
213 (buffer-string)))
214 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
215 ;; Fill `comment' elements.
216 (should
217 (equal " # A B"
218 (org-test-with-temp-text " # A\n # B"
219 (let ((fill-column 20))
220 (org-fill-paragraph)
221 (buffer-string)))))
222 ;; Do not mix consecutive comments when filling one of them.
223 (should
224 (equal "# A B\n\n# C"
225 (org-test-with-temp-text "# A\n# B\n\n# C"
226 (let ((fill-column 20))
227 (org-fill-paragraph)
228 (buffer-string)))))
229 ;; Use commented empty lines as separators when filling comments.
230 (should
231 (equal "# A B\n#\n# C"
232 (org-test-with-temp-text "# A\n# B\n#\n# C"
233 (let ((fill-column 20))
234 (org-fill-paragraph)
235 (buffer-string)))))
236 ;; Handle `adaptive-fill-regexp' in comments.
237 (should
238 (equal "# > a b"
239 (org-test-with-temp-text "# > a\n# > b"
240 (let ((fill-column 20)
241 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
242 (org-fill-paragraph)
243 (buffer-string)))))
244 ;; Do nothing at affiliated keywords.
245 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
246 (let ((fill-column 20))
247 (org-fill-paragraph)
248 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
249 ;; Do not move point after table when filling a table.
250 (should-not
251 (org-test-with-temp-text "| a | b |\n| c | d |\n"
252 (forward-char)
253 (org-fill-paragraph)
254 (eobp))))
256 (ert-deftest test-org/auto-fill-function ()
257 "Test auto-filling features."
258 ;; Auto fill paragraph.
259 (should
260 (equal "12345\n7890"
261 (org-test-with-temp-text "12345 7890"
262 (let ((fill-column 5))
263 (end-of-line)
264 (org-auto-fill-function)
265 (buffer-string)))))
266 ;; Auto fill first paragraph in an item.
267 (should
268 (equal "- 12345\n 7890"
269 (org-test-with-temp-text "- 12345 7890"
270 (let ((fill-column 7))
271 (end-of-line)
272 (org-auto-fill-function)
273 (buffer-string)))))
274 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
275 (should
276 (equal "> 12345\n> 7890"
277 (org-test-with-temp-text "> 12345 7890"
278 (let ((fill-column 5)
279 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
280 (end-of-line)
281 (org-auto-fill-function)
282 (buffer-string)))))
283 ;; Auto fill comments.
284 (should
285 (equal " # 12345\n # 7890"
286 (org-test-with-temp-text " # 12345 7890"
287 (let ((fill-column 10))
288 (end-of-line)
289 (org-auto-fill-function)
290 (buffer-string)))))
291 ;; Auto fill comments when `adaptive-fill-regexp' matches.
292 (should
293 (equal " # > 12345\n # > 7890"
294 (org-test-with-temp-text " # > 12345 7890"
295 (let ((fill-column 10)
296 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
297 (end-of-line)
298 (org-auto-fill-function)
299 (buffer-string)))))
300 ;; A hash within a line isn't a comment.
301 (should-not
302 (equal "12345 # 7890\n# 1"
303 (org-test-with-temp-text "12345 # 7890 1"
304 (let ((fill-column 12))
305 (end-of-line)
306 (org-auto-fill-function)
307 (buffer-string)))))
308 ;; Correctly interpret empty prefix.
309 (should-not
310 (equal "# a\n# b\nRegular\n# paragraph"
311 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
312 (let ((fill-column 12))
313 (end-of-line 3)
314 (org-auto-fill-function)
315 (buffer-string)))))
316 ;; Comment block: auto fill contents.
317 (should
318 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
319 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
320 (let ((fill-column 5))
321 (forward-line)
322 (end-of-line)
323 (org-auto-fill-function)
324 (buffer-string)))))
325 (should
326 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
327 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
328 (let ((fill-column 5))
329 (forward-line)
330 (end-of-line)
331 (org-auto-fill-function)
332 (buffer-string)))))
333 ;; Do not fill if a new item could be created.
334 (should-not
335 (equal "12345\n- 90"
336 (org-test-with-temp-text "12345 - 90"
337 (let ((fill-column 5))
338 (end-of-line)
339 (org-auto-fill-function)
340 (buffer-string)))))
341 ;; Do not fill if a line break could be introduced.
342 (should-not
343 (equal "123\\\\\n7890"
344 (org-test-with-temp-text "123\\\\ 7890"
345 (let ((fill-column 6))
346 (end-of-line)
347 (org-auto-fill-function)
348 (buffer-string)))))
349 ;; Do not fill affiliated keywords.
350 (should-not
351 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
352 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
353 (let ((fill-column 20))
354 (end-of-line)
355 (org-auto-fill-function)
356 (buffer-string))))))
360 ;;; Links
362 ;;;; Fuzzy Links
364 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
365 ;; a named element (#+name: text) and to headlines (* Text).
367 (ert-deftest test-org/fuzzy-links ()
368 "Test fuzzy links specifications."
369 ;; 1. Fuzzy link goes in priority to a matching target.
370 (should
371 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
372 (goto-line 5)
373 (org-open-at-point)
374 (looking-at "<<Test>>")))
375 ;; 2. Then fuzzy link points to an element with a given name.
376 (should
377 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
378 (goto-line 5)
379 (org-open-at-point)
380 (looking-at "#\\+NAME: Test")))
381 ;; 3. A target still lead to a matching headline otherwise.
382 (should
383 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
384 (goto-line 4)
385 (org-open-at-point)
386 (looking-at "\\* Head2")))
387 ;; 4. With a leading star in link, enforce heading match.
388 (should
389 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
390 (goto-line 3)
391 (org-open-at-point)
392 (looking-at "\\* Test"))))
395 ;;;; Link Escaping
397 (ert-deftest test-org/org-link-escape-ascii-character ()
398 "Escape an ascii character."
399 (should
400 (string=
401 "%5B"
402 (org-link-escape "["))))
404 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
405 "Escape an ascii control character."
406 (should
407 (string=
408 "%09"
409 (org-link-escape "\t"))))
411 (ert-deftest test-org/org-link-escape-multibyte-character ()
412 "Escape an unicode multibyte character."
413 (should
414 (string=
415 "%E2%82%AC"
416 (org-link-escape "€"))))
418 (ert-deftest test-org/org-link-escape-custom-table ()
419 "Escape string with custom character table."
420 (should
421 (string=
422 "Foo%3A%42ar%0A"
423 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
425 (ert-deftest test-org/org-link-escape-custom-table-merge ()
426 "Escape string with custom table merged with default table."
427 (should
428 (string=
429 "%5BF%6F%6F%3A%42ar%0A%5D"
430 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
432 (ert-deftest test-org/org-link-unescape-ascii-character ()
433 "Unescape an ascii character."
434 (should
435 (string=
437 (org-link-unescape "%5B"))))
439 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
440 "Unescpae an ascii control character."
441 (should
442 (string=
443 "\n"
444 (org-link-unescape "%0A"))))
446 (ert-deftest test-org/org-link-unescape-multibyte-character ()
447 "Unescape unicode multibyte character."
448 (should
449 (string=
450 "€"
451 (org-link-unescape "%E2%82%AC"))))
453 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
454 "Unescape old style percent escaped character."
455 (should
456 (string=
457 "àâçèéêîôùû"
458 (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
460 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
461 "Escape and unscape a URL that includes an escaped char.
462 http://article.gmane.org/gmane.emacs.orgmode/21459/"
463 (should
464 (string=
465 "http://some.host.com/form?&id=blah%2Bblah25"
466 (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
470 ;;; Node Properties
472 (ert-deftest test-org/accumulated-properties-in-drawers ()
473 "Ensure properties accumulate in subtree drawers."
474 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
475 (org-babel-next-src-block)
476 (should (equal '(2 1) (org-babel-execute-src-block)))))
480 ;;; Mark Region
482 (ert-deftest test-org/mark-subtree ()
483 "Test `org-mark-subtree' specifications."
484 ;; Error when point is before first headline.
485 (should-error
486 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
487 (progn (transient-mark-mode 1)
488 (org-mark-subtree))))
489 ;; Without argument, mark current subtree.
490 (should
491 (equal
492 '(12 32)
493 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
494 (progn (transient-mark-mode 1)
495 (forward-line 2)
496 (org-mark-subtree)
497 (list (region-beginning) (region-end))))))
498 ;; With an argument, move ARG up.
499 (should
500 (equal
501 '(1 32)
502 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
503 (progn (transient-mark-mode 1)
504 (forward-line 2)
505 (org-mark-subtree 1)
506 (list (region-beginning) (region-end))))))
507 ;; Do not get fooled by inlinetasks.
508 (when (featurep 'org-inlinetask)
509 (should
510 (= 1
511 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
512 (progn (transient-mark-mode 1)
513 (forward-line 1)
514 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
515 (region-beginning)))))))
519 ;;; Navigation
521 (ert-deftest test-org/beginning-of-line ()
522 "Test `org-beginning-of-line' specifications."
523 ;; Standard test.
524 (should
525 (org-test-with-temp-text "Some text\nSome other text"
526 (progn (org-beginning-of-line) (bolp))))
527 ;; Standard test with `visual-line-mode'.
528 (should-not
529 (org-test-with-temp-text "A long line of text\nSome other text"
530 (progn (visual-line-mode)
531 (forward-char 2)
532 (dotimes (i 1000) (insert "very "))
533 (org-beginning-of-line)
534 (bolp))))
535 ;; At an headline with special movement.
536 (should
537 (org-test-with-temp-text "* TODO Headline"
538 (let ((org-special-ctrl-a/e t))
539 (org-end-of-line)
540 (and (progn (org-beginning-of-line) (looking-at "Headline"))
541 (progn (org-beginning-of-line) (bolp))
542 (progn (org-beginning-of-line) (looking-at "Headline")))))))
544 (ert-deftest test-org/end-of-line ()
545 "Test `org-end-of-line' specifications."
546 ;; Standard test.
547 (should
548 (org-test-with-temp-text "Some text\nSome other text"
549 (progn (org-end-of-line) (eolp))))
550 ;; Standard test with `visual-line-mode'.
551 (should-not
552 (org-test-with-temp-text "A long line of text\nSome other text"
553 (progn (visual-line-mode)
554 (forward-char 2)
555 (dotimes (i 1000) (insert "very "))
556 (goto-char (point-min))
557 (org-end-of-line)
558 (eolp))))
559 ;; At an headline with special movement.
560 (should
561 (org-test-with-temp-text "* Headline1 :tag:\n"
562 (let ((org-special-ctrl-a/e t))
563 (and (progn (org-end-of-line) (looking-at " :tag:"))
564 (progn (org-end-of-line) (eolp))
565 (progn (org-end-of-line) (looking-at " :tag:"))))))
566 ;; At an headline without special movement.
567 (should
568 (org-test-with-temp-text "* Headline2 :tag:\n"
569 (let ((org-special-ctrl-a/e nil))
570 (and (progn (org-end-of-line) (eolp))
571 (progn (org-end-of-line) (eolp))))))
572 ;; At an headline, with reversed movement.
573 (should
574 (org-test-with-temp-text "* Headline3 :tag:\n"
575 (let ((org-special-ctrl-a/e 'reversed)
576 (this-command last-command))
577 (and (progn (org-end-of-line) (eolp))
578 (progn (org-end-of-line) (looking-at " :tag:"))))))
579 ;; At a block without hidden contents.
580 (should
581 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
582 (progn (org-end-of-line) (eolp))))
583 ;; At a block with hidden contents.
584 (should-not
585 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
586 (let ((org-special-ctrl-a/e t))
587 (org-hide-block-toggle)
588 (org-end-of-line)
589 (eobp)))))
591 (ert-deftest test-org/forward-element ()
592 "Test `org-forward-element' specifications."
593 ;; 1. At EOB: should error.
594 (org-test-with-temp-text "Some text\n"
595 (goto-char (point-max))
596 (should-error (org-forward-element)))
597 ;; 2. Standard move: expected to ignore blank lines.
598 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
599 (org-forward-element)
600 (should (looking-at (regexp-quote "Second paragraph."))))
601 ;; 3. Headline tests.
602 (org-test-with-temp-text "
603 * Head 1
604 ** Head 1.1
605 *** Head 1.1.1
606 ** Head 1.2"
607 ;; 3.1. At an headline beginning: move to next headline at the
608 ;; same level.
609 (goto-line 3)
610 (org-forward-element)
611 (should (looking-at (regexp-quote "** Head 1.2")))
612 ;; 3.2. At an headline beginning: move to parent headline if no
613 ;; headline at the same level.
614 (goto-line 3)
615 (org-forward-element)
616 (should (looking-at (regexp-quote "** Head 1.2"))))
617 ;; 4. Greater element tests.
618 (org-test-with-temp-text
619 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
620 ;; 4.1. At a greater element: expected to skip contents.
621 (org-forward-element)
622 (should (looking-at (regexp-quote "Outside.")))
623 ;; 4.2. At the end of greater element contents: expected to skip
624 ;; to the end of the greater element.
625 (goto-line 2)
626 (org-forward-element)
627 (should (looking-at (regexp-quote "Outside."))))
628 ;; 5. List tests.
629 (org-test-with-temp-text "
630 - item1
632 - sub1
634 - sub2
636 - sub3
638 Inner paragraph.
640 - item2
642 Outside."
643 ;; 5.1. At list top point: expected to move to the element after
644 ;; the list.
645 (goto-line 2)
646 (org-forward-element)
647 (should (looking-at (regexp-quote "Outside.")))
648 ;; 5.2. Special case: at the first line of a sub-list, but not at
649 ;; beginning of line, move to next item.
650 (goto-line 2)
651 (forward-char)
652 (org-forward-element)
653 (should (looking-at "- item2"))
654 (goto-line 4)
655 (forward-char)
656 (org-forward-element)
657 (should (looking-at " - sub2"))
658 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
659 (goto-line 4)
660 (org-forward-element)
661 (should (looking-at (regexp-quote " Inner paragraph.")))
662 ;; 5.4. At sub-list end: expected to move outside the sub-list.
663 (goto-line 8)
664 (org-forward-element)
665 (should (looking-at (regexp-quote " Inner paragraph.")))
666 ;; 5.5. At an item: expected to move to next item, if any.
667 (goto-line 6)
668 (org-forward-element)
669 (should (looking-at " - sub3"))))
671 (ert-deftest test-org/backward-element ()
672 "Test `org-backward-element' specifications."
673 ;; 1. Should error at BOB.
674 (org-test-with-temp-text " \nParagraph."
675 (should-error (org-backward-element)))
676 ;; 2. Should move at BOB when called on the first element in buffer.
677 (should
678 (org-test-with-temp-text "\n#+TITLE: test"
679 (progn (forward-line)
680 (org-backward-element)
681 (bobp))))
682 ;; 3. Not at the beginning of an element: move at its beginning.
683 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
684 (goto-line 3)
685 (end-of-line)
686 (org-backward-element)
687 (should (looking-at (regexp-quote "Paragraph2."))))
688 ;; 4. Headline tests.
689 (org-test-with-temp-text "
690 * Head 1
691 ** Head 1.1
692 *** Head 1.1.1
693 ** Head 1.2"
694 ;; 4.1. At an headline beginning: move to previous headline at the
695 ;; same level.
696 (goto-line 5)
697 (org-backward-element)
698 (should (looking-at (regexp-quote "** Head 1.1")))
699 ;; 4.2. At an headline beginning: move to parent headline if no
700 ;; headline at the same level.
701 (goto-line 3)
702 (org-backward-element)
703 (should (looking-at (regexp-quote "* Head 1")))
704 ;; 4.3. At the first top-level headline: should error.
705 (goto-line 2)
706 (should-error (org-backward-element)))
707 ;; 5. At beginning of first element inside a greater element:
708 ;; expected to move to greater element's beginning.
709 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
710 (goto-line 3)
711 (org-backward-element)
712 (should (looking-at "#\\+BEGIN_CENTER")))
713 ;; 6. At the beginning of the first element in a section: should
714 ;; move back to headline, if any.
715 (should
716 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
717 (progn (goto-char (point-max))
718 (beginning-of-line)
719 (org-backward-element)
720 (org-at-heading-p))))
721 ;; 7. List tests.
722 (org-test-with-temp-text "
723 - item1
725 - sub1
727 - sub2
729 - sub3
731 Inner paragraph.
733 - item2
736 Outside."
737 ;; 7.1. At beginning of sub-list: expected to move to the
738 ;; paragraph before it.
739 (goto-line 4)
740 (org-backward-element)
741 (should (looking-at "item1"))
742 ;; 7.2. At an item in a list: expected to move at previous item.
743 (goto-line 8)
744 (org-backward-element)
745 (should (looking-at " - sub2"))
746 (goto-line 12)
747 (org-backward-element)
748 (should (looking-at "- item1"))
749 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
750 ;; beginning.
751 (goto-line 10)
752 (org-backward-element)
753 (should (looking-at " - sub1"))
754 (goto-line 15)
755 (org-backward-element)
756 (should (looking-at "- item1"))
757 ;; 7.4. At blank-lines before list end: expected to move to top
758 ;; item.
759 (goto-line 14)
760 (org-backward-element)
761 (should (looking-at "- item1"))))
763 (ert-deftest test-org/up-element ()
764 "Test `org-up-element' specifications."
765 ;; 1. At BOB or with no surrounding element: should error.
766 (org-test-with-temp-text "Paragraph."
767 (should-error (org-up-element)))
768 (org-test-with-temp-text "* Head1\n* Head2"
769 (goto-line 2)
770 (should-error (org-up-element)))
771 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
772 (goto-line 3)
773 (should-error (org-up-element)))
774 ;; 2. At an headline: move to parent headline.
775 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
776 (goto-line 3)
777 (org-up-element)
778 (should (looking-at "\\* Head1")))
779 ;; 3. Inside a greater element: move to greater element beginning.
780 (org-test-with-temp-text
781 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
782 (goto-line 3)
783 (org-up-element)
784 (should (looking-at "#\\+BEGIN_CENTER")))
785 ;; 4. List tests.
786 (org-test-with-temp-text "* Top
787 - item1
789 - sub1
791 - sub2
793 Paragraph within sub2.
795 - item2"
796 ;; 4.1. Within an item: move to the item beginning.
797 (goto-line 8)
798 (org-up-element)
799 (should (looking-at " - sub2"))
800 ;; 4.2. At an item in a sub-list: move to parent item.
801 (goto-line 4)
802 (org-up-element)
803 (should (looking-at "- item1"))
804 ;; 4.3. At an item in top list: move to beginning of whole list.
805 (goto-line 10)
806 (org-up-element)
807 (should (looking-at "- item1"))
808 ;; 4.4. Special case. At very top point: should move to parent of
809 ;; list.
810 (goto-line 2)
811 (org-up-element)
812 (should (looking-at "\\* Top"))))
814 (ert-deftest test-org/down-element ()
815 "Test `org-down-element' specifications."
816 ;; Error when the element hasn't got a recursive type.
817 (org-test-with-temp-text "Paragraph."
818 (should-error (org-down-element)))
819 ;; Error when the element has no contents
820 (org-test-with-temp-text "* Headline"
821 (should-error (org-down-element)))
822 ;; When at a plain-list, move to first item.
823 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
824 (goto-line 2)
825 (org-down-element)
826 (should (looking-at " - Item 1.1")))
827 (org-test-with-temp-text "#+NAME: list\n- Item 1"
828 (org-down-element)
829 (should (looking-at " Item 1")))
830 ;; When at a table, move to first row
831 (org-test-with-temp-text "#+NAME: table\n| a | b |"
832 (org-down-element)
833 (should (looking-at " a | b |")))
834 ;; Otherwise, move inside the greater element.
835 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
836 (org-down-element)
837 (should (looking-at "Paragraph"))))
839 (ert-deftest test-org/drag-element-backward ()
840 "Test `org-drag-element-backward' specifications."
841 ;; 1. Error when trying to move first element of buffer.
842 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
843 (should-error (org-drag-element-backward)))
844 ;; 2. Error when trying to swap nested elements.
845 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
846 (forward-line)
847 (should-error (org-drag-element-backward)))
848 ;; 3. Error when trying to swap an headline element and
849 ;; a non-headline element.
850 (org-test-with-temp-text "Test.\n* Head 1"
851 (forward-line)
852 (should-error (org-drag-element-backward)))
853 ;; 4. Otherwise, swap elements, preserving column and blank lines
854 ;; between elements.
855 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
856 (search-forward "graph")
857 (org-drag-element-backward)
858 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
859 (should (looking-at " 2")))
860 ;; 5. Preserve visibility of elements and their contents.
861 (org-test-with-temp-text "
862 #+BEGIN_CENTER
863 Text.
864 #+END_CENTER
865 - item 1
866 #+BEGIN_QUOTE
867 Text.
868 #+END_QUOTE"
869 (while (search-forward "BEGIN_" nil t) (org-cycle))
870 (search-backward "- item 1")
871 (org-drag-element-backward)
872 (should
873 (equal
874 '((63 . 82) (26 . 48))
875 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
876 (overlays-in (point-min) (point-max)))))))
878 (ert-deftest test-org/drag-element-forward ()
879 "Test `org-drag-element-forward' specifications."
880 ;; 1. Error when trying to move first element of buffer.
881 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
882 (goto-line 3)
883 (should-error (org-drag-element-forward)))
884 ;; 2. Error when trying to swap nested elements.
885 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
886 (forward-line)
887 (should-error (org-drag-element-forward)))
888 ;; 3. Error when trying to swap a non-headline element and an
889 ;; headline.
890 (org-test-with-temp-text "Test.\n* Head 1"
891 (should-error (org-drag-element-forward)))
892 ;; 4. Otherwise, swap elements, preserving column and blank lines
893 ;; between elements.
894 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
895 (search-forward "graph")
896 (org-drag-element-forward)
897 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
898 (should (looking-at " 1")))
899 ;; 5. Preserve visibility of elements and their contents.
900 (org-test-with-temp-text "
901 #+BEGIN_CENTER
902 Text.
903 #+END_CENTER
904 - item 1
905 #+BEGIN_QUOTE
906 Text.
907 #+END_QUOTE"
908 (while (search-forward "BEGIN_" nil t) (org-cycle))
909 (search-backward "#+BEGIN_CENTER")
910 (org-drag-element-forward)
911 (should
912 (equal
913 '((63 . 82) (26 . 48))
914 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
915 (overlays-in (point-min) (point-max)))))))
919 ;;; Planning
921 (ert-deftest test-org/timestamp-has-time-p ()
922 "Test `org-timestamp-has-time-p' specifications."
923 ;; With time.
924 (should
925 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
926 (org-timestamp-has-time-p (org-element-context))))
927 ;; Without time.
928 (should-not
929 (org-test-with-temp-text "<2012-03-29 Thu>"
930 (org-timestamp-has-time-p (org-element-context)))))
932 (ert-deftest test-org/timestamp-format ()
933 "Test `org-timestamp-format' specifications."
934 ;; Regular test.
935 (should
936 (equal
937 "2012-03-29 16:40"
938 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
939 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
940 ;; Range end.
941 (should
942 (equal
943 "2012-03-29"
944 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
945 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
947 (ert-deftest test-org/timestamp-split-range ()
948 "Test `org-timestamp-split-range' specifications."
949 ;; Extract range start (active).
950 (should
951 (equal '(2012 3 29)
952 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
953 (let ((ts (org-timestamp-split-range (org-element-context))))
954 (mapcar (lambda (p) (org-element-property p ts))
955 '(:year-end :month-end :day-end))))))
956 ;; Extract range start (inactive)
957 (should
958 (equal '(2012 3 29)
959 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
960 (let ((ts (org-timestamp-split-range (org-element-context))))
961 (mapcar (lambda (p) (org-element-property p ts))
962 '(:year-end :month-end :day-end))))))
963 ;; Extract range end (active).
964 (should
965 (equal '(2012 3 30)
966 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
967 (let ((ts (org-timestamp-split-range
968 (org-element-context) t)))
969 (mapcar (lambda (p) (org-element-property p ts))
970 '(:year-end :month-end :day-end))))))
971 ;; Extract range end (inactive)
972 (should
973 (equal '(2012 3 30)
974 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
975 (let ((ts (org-timestamp-split-range
976 (org-element-context) t)))
977 (mapcar (lambda (p) (org-element-property p ts))
978 '(:year-end :month-end :day-end))))))
979 ;; Return the timestamp if not a range.
980 (should
981 (org-test-with-temp-text "[2012-03-29 Thu]"
982 (let* ((ts-orig (org-element-context))
983 (ts-copy (org-timestamp-split-range ts-orig)))
984 (eq ts-orig ts-copy))))
985 (should
986 (org-test-with-temp-text "<%%(org-float t 4 2)>"
987 (let* ((ts-orig (org-element-context))
988 (ts-copy (org-timestamp-split-range ts-orig)))
989 (eq ts-orig ts-copy))))
990 ;; Check that parent is the same when a range was split.
991 (should
992 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
993 (let* ((ts-orig (org-element-context))
994 (ts-copy (org-timestamp-split-range ts-orig)))
995 (eq (org-element-property :parent ts-orig)
996 (org-element-property :parent ts-copy))))))
998 (ert-deftest test-org/timestamp-translate ()
999 "Test `org-timestamp-translate' specifications."
1000 ;; Translate whole date range.
1001 (should
1002 (equal "<29>--<30>"
1003 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1004 (let ((org-display-custom-times t)
1005 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1006 (org-timestamp-translate (org-element-context))))))
1007 ;; Translate date range start.
1008 (should
1009 (equal "<29>"
1010 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1011 (let ((org-display-custom-times t)
1012 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1013 (org-timestamp-translate (org-element-context) 'start)))))
1014 ;; Translate date range end.
1015 (should
1016 (equal "<30>"
1017 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1018 (let ((org-display-custom-times t)
1019 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1020 (org-timestamp-translate (org-element-context) 'end)))))
1021 ;; Translate time range.
1022 (should
1023 (equal "<08>--<16>"
1024 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
1025 (let ((org-display-custom-times t)
1026 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
1027 (org-timestamp-translate (org-element-context))))))
1028 ;; Translate non-range timestamp.
1029 (should
1030 (equal "<29>"
1031 (org-test-with-temp-text "<2012-03-29 Thu>"
1032 (let ((org-display-custom-times t)
1033 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1034 (org-timestamp-translate (org-element-context))))))
1035 ;; Do not change `diary' timestamps.
1036 (should
1037 (equal "<%%(org-float t 4 2)>"
1038 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1039 (let ((org-display-custom-times t)
1040 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1041 (org-timestamp-translate (org-element-context)))))))
1045 ;;; Targets and Radio Targets
1047 (ert-deftest test-org/all-targets ()
1048 "Test `org-all-targets' specifications."
1049 ;; Without an argument.
1050 (should
1051 (equal '("radio-target" "target")
1052 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
1053 (org-all-targets))))
1054 (should
1055 (equal '("radio-target")
1056 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
1057 ;; With argument.
1058 (should
1059 (equal '("radio-target")
1060 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
1061 (org-all-targets t)))))
1064 (provide 'test-org)
1066 ;;; test-org.el ends here