Do not handle `adaptive-fill-regexp' in comments
[org-mode.git] / testing / lisp / test-org.el
blob8a1e9f1cc648298bafb710fdad402b9dac637f87
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 10)
279 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
280 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
281 (end-of-line)
282 (org-auto-fill-function)
283 (buffer-string)))))
284 (should
285 (equal "> 12345\n> 12345\n> 7890"
286 (org-test-with-temp-text "> 12345\n> 12345 7890"
287 (let ((fill-column 10)
288 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
289 (goto-char (point-max))
290 (org-auto-fill-function)
291 (buffer-string)))))
292 (should-not
293 (equal " 12345\n *12345\n *12345"
294 (org-test-with-temp-text " 12345\n *12345 12345"
295 (let ((fill-column 10)
296 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
297 (goto-char (point-max))
298 (org-auto-fill-function)
299 (buffer-string)))))
300 ;; Auto fill comments.
301 (should
302 (equal " # 12345\n # 7890"
303 (org-test-with-temp-text " # 12345 7890"
304 (let ((fill-column 10))
305 (end-of-line)
306 (org-auto-fill-function)
307 (buffer-string)))))
308 ;; A hash within a line isn't a comment.
309 (should-not
310 (equal "12345 # 7890\n# 1"
311 (org-test-with-temp-text "12345 # 7890 1"
312 (let ((fill-column 12))
313 (end-of-line)
314 (org-auto-fill-function)
315 (buffer-string)))))
316 ;; Correctly interpret empty prefix.
317 (should-not
318 (equal "# a\n# b\nRegular\n# paragraph"
319 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
320 (let ((fill-column 12))
321 (end-of-line 3)
322 (org-auto-fill-function)
323 (buffer-string)))))
324 ;; Comment block: auto fill contents.
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 (should
334 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
335 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
336 (let ((fill-column 5))
337 (forward-line)
338 (end-of-line)
339 (org-auto-fill-function)
340 (buffer-string)))))
341 ;; Do not fill if a new item could be created.
342 (should-not
343 (equal "12345\n- 90"
344 (org-test-with-temp-text "12345 - 90"
345 (let ((fill-column 5))
346 (end-of-line)
347 (org-auto-fill-function)
348 (buffer-string)))))
349 ;; Do not fill if a line break could be introduced.
350 (should-not
351 (equal "123\\\\\n7890"
352 (org-test-with-temp-text "123\\\\ 7890"
353 (let ((fill-column 6))
354 (end-of-line)
355 (org-auto-fill-function)
356 (buffer-string)))))
357 ;; Do not fill affiliated keywords.
358 (should-not
359 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
360 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
361 (let ((fill-column 20))
362 (end-of-line)
363 (org-auto-fill-function)
364 (buffer-string))))))
368 ;;; Links
370 ;;;; Fuzzy Links
372 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
373 ;; a named element (#+name: text) and to headlines (* Text).
375 (ert-deftest test-org/fuzzy-links ()
376 "Test fuzzy links specifications."
377 ;; 1. Fuzzy link goes in priority to a matching target.
378 (should
379 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
380 (goto-line 5)
381 (org-open-at-point)
382 (looking-at "<<Test>>")))
383 ;; 2. Then fuzzy link points to an element with a given name.
384 (should
385 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
386 (goto-line 5)
387 (org-open-at-point)
388 (looking-at "#\\+NAME: Test")))
389 ;; 3. A target still lead to a matching headline otherwise.
390 (should
391 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
392 (goto-line 4)
393 (org-open-at-point)
394 (looking-at "\\* Head2")))
395 ;; 4. With a leading star in link, enforce heading match.
396 (should
397 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
398 (goto-line 3)
399 (org-open-at-point)
400 (looking-at "\\* Test"))))
403 ;;;; Link Escaping
405 (ert-deftest test-org/org-link-escape-ascii-character ()
406 "Escape an ascii character."
407 (should
408 (string=
409 "%5B"
410 (org-link-escape "["))))
412 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
413 "Escape an ascii control character."
414 (should
415 (string=
416 "%09"
417 (org-link-escape "\t"))))
419 (ert-deftest test-org/org-link-escape-multibyte-character ()
420 "Escape an unicode multibyte character."
421 (should
422 (string=
423 "%E2%82%AC"
424 (org-link-escape "€"))))
426 (ert-deftest test-org/org-link-escape-custom-table ()
427 "Escape string with custom character table."
428 (should
429 (string=
430 "Foo%3A%42ar%0A"
431 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
433 (ert-deftest test-org/org-link-escape-custom-table-merge ()
434 "Escape string with custom table merged with default table."
435 (should
436 (string=
437 "%5BF%6F%6F%3A%42ar%0A%5D"
438 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
440 (ert-deftest test-org/org-link-unescape-ascii-character ()
441 "Unescape an ascii character."
442 (should
443 (string=
445 (org-link-unescape "%5B"))))
447 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
448 "Unescpae an ascii control character."
449 (should
450 (string=
451 "\n"
452 (org-link-unescape "%0A"))))
454 (ert-deftest test-org/org-link-unescape-multibyte-character ()
455 "Unescape unicode multibyte character."
456 (should
457 (string=
458 "€"
459 (org-link-unescape "%E2%82%AC"))))
461 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
462 "Unescape old style percent escaped character."
463 (should
464 (string=
465 "àâçèéêîôùû"
466 (decode-coding-string
467 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
469 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
470 "Escape and unescape a URL that includes an escaped char.
471 http://article.gmane.org/gmane.emacs.orgmode/21459/"
472 (should
473 (string=
474 "http://some.host.com/form?&id=blah%2Bblah25"
475 (org-link-unescape
476 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
478 (ert-deftest test-org/org-link-escape-chars-browser ()
479 "Escape a URL to pass to `browse-url'."
480 (should
481 (string=
482 "http://some.host.com/search?q=%22Org%20mode%22"
483 (org-link-escape "http://some.host.com/search?q=\"Org mode\""
484 org-link-escape-chars-browser))))
488 ;;; Node Properties
490 (ert-deftest test-org/accumulated-properties-in-drawers ()
491 "Ensure properties accumulate in subtree drawers."
492 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
493 (org-babel-next-src-block)
494 (should (equal '(2 1) (org-babel-execute-src-block)))))
498 ;;; Mark Region
500 (ert-deftest test-org/mark-subtree ()
501 "Test `org-mark-subtree' specifications."
502 ;; Error when point is before first headline.
503 (should-error
504 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
505 (progn (transient-mark-mode 1)
506 (org-mark-subtree))))
507 ;; Without argument, mark current subtree.
508 (should
509 (equal
510 '(12 32)
511 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
512 (progn (transient-mark-mode 1)
513 (forward-line 2)
514 (org-mark-subtree)
515 (list (region-beginning) (region-end))))))
516 ;; With an argument, move ARG up.
517 (should
518 (equal
519 '(1 32)
520 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
521 (progn (transient-mark-mode 1)
522 (forward-line 2)
523 (org-mark-subtree 1)
524 (list (region-beginning) (region-end))))))
525 ;; Do not get fooled by inlinetasks.
526 (when (featurep 'org-inlinetask)
527 (should
528 (= 1
529 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
530 (progn (transient-mark-mode 1)
531 (forward-line 1)
532 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
533 (region-beginning)))))))
537 ;;; Navigation
539 (ert-deftest test-org/beginning-of-line ()
540 "Test `org-beginning-of-line' specifications."
541 ;; Standard test.
542 (should
543 (org-test-with-temp-text "Some text\nSome other text"
544 (progn (org-beginning-of-line) (bolp))))
545 ;; Standard test with `visual-line-mode'.
546 (should-not
547 (org-test-with-temp-text "A long line of text\nSome other text"
548 (progn (visual-line-mode)
549 (forward-char 2)
550 (dotimes (i 1000) (insert "very "))
551 (org-beginning-of-line)
552 (bolp))))
553 ;; At an headline with special movement.
554 (should
555 (org-test-with-temp-text "* TODO Headline"
556 (let ((org-special-ctrl-a/e t))
557 (org-end-of-line)
558 (and (progn (org-beginning-of-line) (looking-at "Headline"))
559 (progn (org-beginning-of-line) (bolp))
560 (progn (org-beginning-of-line) (looking-at "Headline")))))))
562 (ert-deftest test-org/end-of-line ()
563 "Test `org-end-of-line' specifications."
564 ;; Standard test.
565 (should
566 (org-test-with-temp-text "Some text\nSome other text"
567 (progn (org-end-of-line) (eolp))))
568 ;; Standard test with `visual-line-mode'.
569 (should-not
570 (org-test-with-temp-text "A long line of text\nSome other text"
571 (progn (visual-line-mode)
572 (forward-char 2)
573 (dotimes (i 1000) (insert "very "))
574 (goto-char (point-min))
575 (org-end-of-line)
576 (eolp))))
577 ;; At an headline with special movement.
578 (should
579 (org-test-with-temp-text "* Headline1 :tag:\n"
580 (let ((org-special-ctrl-a/e t))
581 (and (progn (org-end-of-line) (looking-at " :tag:"))
582 (progn (org-end-of-line) (eolp))
583 (progn (org-end-of-line) (looking-at " :tag:"))))))
584 ;; At an headline without special movement.
585 (should
586 (org-test-with-temp-text "* Headline2 :tag:\n"
587 (let ((org-special-ctrl-a/e nil))
588 (and (progn (org-end-of-line) (eolp))
589 (progn (org-end-of-line) (eolp))))))
590 ;; At an headline, with reversed movement.
591 (should
592 (org-test-with-temp-text "* Headline3 :tag:\n"
593 (let ((org-special-ctrl-a/e 'reversed)
594 (this-command last-command))
595 (and (progn (org-end-of-line) (eolp))
596 (progn (org-end-of-line) (looking-at " :tag:"))))))
597 ;; At a block without hidden contents.
598 (should
599 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
600 (progn (org-end-of-line) (eolp))))
601 ;; At a block with hidden contents.
602 (should-not
603 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
604 (let ((org-special-ctrl-a/e t))
605 (org-hide-block-toggle)
606 (org-end-of-line)
607 (eobp)))))
609 (ert-deftest test-org/forward-element ()
610 "Test `org-forward-element' specifications."
611 ;; 1. At EOB: should error.
612 (org-test-with-temp-text "Some text\n"
613 (goto-char (point-max))
614 (should-error (org-forward-element)))
615 ;; 2. Standard move: expected to ignore blank lines.
616 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
617 (org-forward-element)
618 (should (looking-at (regexp-quote "Second paragraph."))))
619 ;; 3. Headline tests.
620 (org-test-with-temp-text "
621 * Head 1
622 ** Head 1.1
623 *** Head 1.1.1
624 ** Head 1.2"
625 ;; 3.1. At an headline beginning: move to next headline at the
626 ;; same level.
627 (goto-line 3)
628 (org-forward-element)
629 (should (looking-at (regexp-quote "** Head 1.2")))
630 ;; 3.2. At an headline beginning: move to parent headline if no
631 ;; headline at the same level.
632 (goto-line 3)
633 (org-forward-element)
634 (should (looking-at (regexp-quote "** Head 1.2"))))
635 ;; 4. Greater element tests.
636 (org-test-with-temp-text
637 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
638 ;; 4.1. At a greater element: expected to skip contents.
639 (org-forward-element)
640 (should (looking-at (regexp-quote "Outside.")))
641 ;; 4.2. At the end of greater element contents: expected to skip
642 ;; to the end of the greater element.
643 (goto-line 2)
644 (org-forward-element)
645 (should (looking-at (regexp-quote "Outside."))))
646 ;; 5. List tests.
647 (org-test-with-temp-text "
648 - item1
650 - sub1
652 - sub2
654 - sub3
656 Inner paragraph.
658 - item2
660 Outside."
661 ;; 5.1. At list top point: expected to move to the element after
662 ;; the list.
663 (goto-line 2)
664 (org-forward-element)
665 (should (looking-at (regexp-quote "Outside.")))
666 ;; 5.2. Special case: at the first line of a sub-list, but not at
667 ;; beginning of line, move to next item.
668 (goto-line 2)
669 (forward-char)
670 (org-forward-element)
671 (should (looking-at "- item2"))
672 (goto-line 4)
673 (forward-char)
674 (org-forward-element)
675 (should (looking-at " - sub2"))
676 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
677 (goto-line 4)
678 (org-forward-element)
679 (should (looking-at (regexp-quote " Inner paragraph.")))
680 ;; 5.4. At sub-list end: expected to move outside the sub-list.
681 (goto-line 8)
682 (org-forward-element)
683 (should (looking-at (regexp-quote " Inner paragraph.")))
684 ;; 5.5. At an item: expected to move to next item, if any.
685 (goto-line 6)
686 (org-forward-element)
687 (should (looking-at " - sub3"))))
689 (ert-deftest test-org/backward-element ()
690 "Test `org-backward-element' specifications."
691 ;; 1. Should error at BOB.
692 (org-test-with-temp-text " \nParagraph."
693 (should-error (org-backward-element)))
694 ;; 2. Should move at BOB when called on the first element in buffer.
695 (should
696 (org-test-with-temp-text "\n#+TITLE: test"
697 (progn (forward-line)
698 (org-backward-element)
699 (bobp))))
700 ;; 3. Not at the beginning of an element: move at its beginning.
701 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
702 (goto-line 3)
703 (end-of-line)
704 (org-backward-element)
705 (should (looking-at (regexp-quote "Paragraph2."))))
706 ;; 4. Headline tests.
707 (org-test-with-temp-text "
708 * Head 1
709 ** Head 1.1
710 *** Head 1.1.1
711 ** Head 1.2"
712 ;; 4.1. At an headline beginning: move to previous headline at the
713 ;; same level.
714 (goto-line 5)
715 (org-backward-element)
716 (should (looking-at (regexp-quote "** Head 1.1")))
717 ;; 4.2. At an headline beginning: move to parent headline if no
718 ;; headline at the same level.
719 (goto-line 3)
720 (org-backward-element)
721 (should (looking-at (regexp-quote "* Head 1")))
722 ;; 4.3. At the first top-level headline: should error.
723 (goto-line 2)
724 (should-error (org-backward-element)))
725 ;; 5. At beginning of first element inside a greater element:
726 ;; expected to move to greater element's beginning.
727 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
728 (goto-line 3)
729 (org-backward-element)
730 (should (looking-at "#\\+BEGIN_CENTER")))
731 ;; 6. At the beginning of the first element in a section: should
732 ;; move back to headline, if any.
733 (should
734 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
735 (progn (goto-char (point-max))
736 (beginning-of-line)
737 (org-backward-element)
738 (org-at-heading-p))))
739 ;; 7. List tests.
740 (org-test-with-temp-text "
741 - item1
743 - sub1
745 - sub2
747 - sub3
749 Inner paragraph.
751 - item2
754 Outside."
755 ;; 7.1. At beginning of sub-list: expected to move to the
756 ;; paragraph before it.
757 (goto-line 4)
758 (org-backward-element)
759 (should (looking-at "item1"))
760 ;; 7.2. At an item in a list: expected to move at previous item.
761 (goto-line 8)
762 (org-backward-element)
763 (should (looking-at " - sub2"))
764 (goto-line 12)
765 (org-backward-element)
766 (should (looking-at "- item1"))
767 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
768 ;; beginning.
769 (goto-line 10)
770 (org-backward-element)
771 (should (looking-at " - sub1"))
772 (goto-line 15)
773 (org-backward-element)
774 (should (looking-at "- item1"))
775 ;; 7.4. At blank-lines before list end: expected to move to top
776 ;; item.
777 (goto-line 14)
778 (org-backward-element)
779 (should (looking-at "- item1"))))
781 (ert-deftest test-org/up-element ()
782 "Test `org-up-element' specifications."
783 ;; 1. At BOB or with no surrounding element: should error.
784 (org-test-with-temp-text "Paragraph."
785 (should-error (org-up-element)))
786 (org-test-with-temp-text "* Head1\n* Head2"
787 (goto-line 2)
788 (should-error (org-up-element)))
789 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
790 (goto-line 3)
791 (should-error (org-up-element)))
792 ;; 2. At an headline: move to parent headline.
793 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
794 (goto-line 3)
795 (org-up-element)
796 (should (looking-at "\\* Head1")))
797 ;; 3. Inside a greater element: move to greater element beginning.
798 (org-test-with-temp-text
799 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
800 (goto-line 3)
801 (org-up-element)
802 (should (looking-at "#\\+BEGIN_CENTER")))
803 ;; 4. List tests.
804 (org-test-with-temp-text "* Top
805 - item1
807 - sub1
809 - sub2
811 Paragraph within sub2.
813 - item2"
814 ;; 4.1. Within an item: move to the item beginning.
815 (goto-line 8)
816 (org-up-element)
817 (should (looking-at " - sub2"))
818 ;; 4.2. At an item in a sub-list: move to parent item.
819 (goto-line 4)
820 (org-up-element)
821 (should (looking-at "- item1"))
822 ;; 4.3. At an item in top list: move to beginning of whole list.
823 (goto-line 10)
824 (org-up-element)
825 (should (looking-at "- item1"))
826 ;; 4.4. Special case. At very top point: should move to parent of
827 ;; list.
828 (goto-line 2)
829 (org-up-element)
830 (should (looking-at "\\* Top"))))
832 (ert-deftest test-org/down-element ()
833 "Test `org-down-element' specifications."
834 ;; Error when the element hasn't got a recursive type.
835 (org-test-with-temp-text "Paragraph."
836 (should-error (org-down-element)))
837 ;; Error when the element has no contents
838 (org-test-with-temp-text "* Headline"
839 (should-error (org-down-element)))
840 ;; When at a plain-list, move to first item.
841 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
842 (goto-line 2)
843 (org-down-element)
844 (should (looking-at " - Item 1.1")))
845 (org-test-with-temp-text "#+NAME: list\n- Item 1"
846 (org-down-element)
847 (should (looking-at " Item 1")))
848 ;; When at a table, move to first row
849 (org-test-with-temp-text "#+NAME: table\n| a | b |"
850 (org-down-element)
851 (should (looking-at " a | b |")))
852 ;; Otherwise, move inside the greater element.
853 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
854 (org-down-element)
855 (should (looking-at "Paragraph"))))
857 (ert-deftest test-org/drag-element-backward ()
858 "Test `org-drag-element-backward' specifications."
859 ;; 1. Error when trying to move first element of buffer.
860 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
861 (should-error (org-drag-element-backward)))
862 ;; 2. Error when trying to swap nested elements.
863 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
864 (forward-line)
865 (should-error (org-drag-element-backward)))
866 ;; 3. Error when trying to swap an headline element and
867 ;; a non-headline element.
868 (org-test-with-temp-text "Test.\n* Head 1"
869 (forward-line)
870 (should-error (org-drag-element-backward)))
871 ;; 4. Otherwise, swap elements, preserving column and blank lines
872 ;; between elements.
873 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
874 (search-forward "graph")
875 (org-drag-element-backward)
876 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
877 (should (looking-at " 2")))
878 ;; 5. Preserve visibility of elements and their contents.
879 (org-test-with-temp-text "
880 #+BEGIN_CENTER
881 Text.
882 #+END_CENTER
883 - item 1
884 #+BEGIN_QUOTE
885 Text.
886 #+END_QUOTE"
887 (while (search-forward "BEGIN_" nil t) (org-cycle))
888 (search-backward "- item 1")
889 (org-drag-element-backward)
890 (should
891 (equal
892 '((63 . 82) (26 . 48))
893 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
894 (overlays-in (point-min) (point-max)))))))
896 (ert-deftest test-org/drag-element-forward ()
897 "Test `org-drag-element-forward' specifications."
898 ;; 1. Error when trying to move first element of buffer.
899 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
900 (goto-line 3)
901 (should-error (org-drag-element-forward)))
902 ;; 2. Error when trying to swap nested elements.
903 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
904 (forward-line)
905 (should-error (org-drag-element-forward)))
906 ;; 3. Error when trying to swap a non-headline element and an
907 ;; headline.
908 (org-test-with-temp-text "Test.\n* Head 1"
909 (should-error (org-drag-element-forward)))
910 ;; 4. Otherwise, swap elements, preserving column and blank lines
911 ;; between elements.
912 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
913 (search-forward "graph")
914 (org-drag-element-forward)
915 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
916 (should (looking-at " 1")))
917 ;; 5. Preserve visibility of elements and their contents.
918 (org-test-with-temp-text "
919 #+BEGIN_CENTER
920 Text.
921 #+END_CENTER
922 - item 1
923 #+BEGIN_QUOTE
924 Text.
925 #+END_QUOTE"
926 (while (search-forward "BEGIN_" nil t) (org-cycle))
927 (search-backward "#+BEGIN_CENTER")
928 (org-drag-element-forward)
929 (should
930 (equal
931 '((63 . 82) (26 . 48))
932 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
933 (overlays-in (point-min) (point-max)))))))
937 ;;; Planning
939 (ert-deftest test-org/timestamp-has-time-p ()
940 "Test `org-timestamp-has-time-p' specifications."
941 ;; With time.
942 (should
943 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
944 (org-timestamp-has-time-p (org-element-context))))
945 ;; Without time.
946 (should-not
947 (org-test-with-temp-text "<2012-03-29 Thu>"
948 (org-timestamp-has-time-p (org-element-context)))))
950 (ert-deftest test-org/timestamp-format ()
951 "Test `org-timestamp-format' specifications."
952 ;; Regular test.
953 (should
954 (equal
955 "2012-03-29 16:40"
956 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
957 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
958 ;; Range end.
959 (should
960 (equal
961 "2012-03-29"
962 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
963 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
965 (ert-deftest test-org/timestamp-split-range ()
966 "Test `org-timestamp-split-range' specifications."
967 ;; Extract range start (active).
968 (should
969 (equal '(2012 3 29)
970 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
971 (let ((ts (org-timestamp-split-range (org-element-context))))
972 (mapcar (lambda (p) (org-element-property p ts))
973 '(:year-end :month-end :day-end))))))
974 ;; Extract range start (inactive)
975 (should
976 (equal '(2012 3 29)
977 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
978 (let ((ts (org-timestamp-split-range (org-element-context))))
979 (mapcar (lambda (p) (org-element-property p ts))
980 '(:year-end :month-end :day-end))))))
981 ;; Extract range end (active).
982 (should
983 (equal '(2012 3 30)
984 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
985 (let ((ts (org-timestamp-split-range
986 (org-element-context) t)))
987 (mapcar (lambda (p) (org-element-property p ts))
988 '(:year-end :month-end :day-end))))))
989 ;; Extract range end (inactive)
990 (should
991 (equal '(2012 3 30)
992 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
993 (let ((ts (org-timestamp-split-range
994 (org-element-context) t)))
995 (mapcar (lambda (p) (org-element-property p ts))
996 '(:year-end :month-end :day-end))))))
997 ;; Return the timestamp if not a range.
998 (should
999 (org-test-with-temp-text "[2012-03-29 Thu]"
1000 (let* ((ts-orig (org-element-context))
1001 (ts-copy (org-timestamp-split-range ts-orig)))
1002 (eq ts-orig ts-copy))))
1003 (should
1004 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1005 (let* ((ts-orig (org-element-context))
1006 (ts-copy (org-timestamp-split-range ts-orig)))
1007 (eq ts-orig ts-copy))))
1008 ;; Check that parent is the same when a range was split.
1009 (should
1010 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1011 (let* ((ts-orig (org-element-context))
1012 (ts-copy (org-timestamp-split-range ts-orig)))
1013 (eq (org-element-property :parent ts-orig)
1014 (org-element-property :parent ts-copy))))))
1016 (ert-deftest test-org/timestamp-translate ()
1017 "Test `org-timestamp-translate' specifications."
1018 ;; Translate whole date range.
1019 (should
1020 (equal "<29>--<30>"
1021 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1022 (let ((org-display-custom-times t)
1023 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1024 (org-timestamp-translate (org-element-context))))))
1025 ;; Translate date range start.
1026 (should
1027 (equal "<29>"
1028 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1029 (let ((org-display-custom-times t)
1030 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1031 (org-timestamp-translate (org-element-context) 'start)))))
1032 ;; Translate date range end.
1033 (should
1034 (equal "<30>"
1035 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1036 (let ((org-display-custom-times t)
1037 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1038 (org-timestamp-translate (org-element-context) 'end)))))
1039 ;; Translate time range.
1040 (should
1041 (equal "<08>--<16>"
1042 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
1043 (let ((org-display-custom-times t)
1044 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
1045 (org-timestamp-translate (org-element-context))))))
1046 ;; Translate non-range timestamp.
1047 (should
1048 (equal "<29>"
1049 (org-test-with-temp-text "<2012-03-29 Thu>"
1050 (let ((org-display-custom-times t)
1051 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1052 (org-timestamp-translate (org-element-context))))))
1053 ;; Do not change `diary' timestamps.
1054 (should
1055 (equal "<%%(org-float t 4 2)>"
1056 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1057 (let ((org-display-custom-times t)
1058 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1059 (org-timestamp-translate (org-element-context)))))))
1063 ;;; Targets and Radio Targets
1065 (ert-deftest test-org/all-targets ()
1066 "Test `org-all-targets' specifications."
1067 ;; Without an argument.
1068 (should
1069 (equal '("radio-target" "target")
1070 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
1071 (org-all-targets))))
1072 (should
1073 (equal '("radio-target")
1074 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
1075 ;; With argument.
1076 (should
1077 (equal '("radio-target")
1078 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
1079 (org-all-targets t)))))
1082 (provide 'test-org)
1084 ;;; test-org.el ends here