57e3d53dcab6df1545aab3b7f82a54905de569f7
[org-mode.git] / testing / lisp / test-org.el
blob57e3d53dcab6df1545aab3b7f82a54905de569f7
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)))))
100 ;; In a source block, use appropriate syntax.
101 (should
102 (equal " ;; "
103 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
104 (forward-line)
105 (let ((org-edit-src-content-indentation 2))
106 (call-interactively 'comment-dwim))
107 (buffer-substring-no-properties (line-beginning-position) (point)))))
108 (should
109 (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
110 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
111 (forward-line)
112 (transient-mark-mode 1)
113 (push-mark (point) t t)
114 (forward-line 2)
115 (let ((org-edit-src-content-indentation 2))
116 (call-interactively 'comment-dwim))
117 (buffer-string)))))
121 ;;; Date and time analysis
123 (ert-deftest test-org/org-read-date ()
124 "Test `org-read-date' specifications."
125 ;; Parse ISO date with abbreviated year and month.
126 (should (equal "2012-03-29 16:40"
127 (let ((org-time-was-given t))
128 (org-read-date t nil "12-3-29 16:40"))))
129 ;; Parse Europeans dates.
130 (should (equal "2012-03-29 16:40"
131 (let ((org-time-was-given t))
132 (org-read-date t nil "29.03.2012 16:40"))))
133 ;; Parse Europeans dates without year.
134 (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
135 (let ((org-time-was-given t))
136 (org-read-date t nil "29.03. 16:40")))))
138 (ert-deftest test-org/org-parse-time-string ()
139 "Test `org-parse-time-string'."
140 (should (equal (org-parse-time-string "2012-03-29 16:40")
141 '(0 40 16 29 3 2012 nil nil nil)))
142 (should (equal (org-parse-time-string "[2012-03-29 16:40]")
143 '(0 40 16 29 3 2012 nil nil nil)))
144 (should (equal (org-parse-time-string "<2012-03-29 16:40>")
145 '(0 40 16 29 3 2012 nil nil nil)))
146 (should (equal (org-parse-time-string "<2012-03-29>")
147 '(0 0 0 29 3 2012 nil nil nil)))
148 (should (equal (org-parse-time-string "<2012-03-29>" t)
149 '(0 nil nil 29 3 2012 nil nil nil))))
152 ;;; Filling
154 (ert-deftest test-org/fill-paragraph ()
155 "Test `org-fill-paragraph' specifications."
156 ;; At an Org table, align it.
157 (should
158 (equal "| a |\n"
159 (org-test-with-temp-text "|a|"
160 (org-fill-paragraph)
161 (buffer-string))))
162 (should
163 (equal "#+name: table\n| a |\n"
164 (org-test-with-temp-text "#+name: table\n| a |"
165 (org-fill-paragraph)
166 (buffer-string))))
167 ;; At a paragraph, preserve line breaks.
168 (org-test-with-temp-text "some \\\\\nlong\ntext"
169 (let ((fill-column 20))
170 (org-fill-paragraph)
171 (should (equal (buffer-string) "some \\\\\nlong text"))))
172 ;; Correctly fill a paragraph when point is at its very end.
173 (should
174 (equal "A B"
175 (org-test-with-temp-text "A\nB"
176 (let ((fill-column 20))
177 (goto-char (point-max))
178 (org-fill-paragraph)
179 (buffer-string)))))
180 ;; Correctly fill the last paragraph of a greater element.
181 (should
182 (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
183 (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
184 (let ((fill-column 8))
185 (forward-line)
186 (end-of-line)
187 (org-fill-paragraph)
188 (buffer-string)))))
189 ;; Correctly fill an element in a narrowed buffer.
190 (should
191 (equal "01234\n6"
192 (org-test-with-temp-text "01234 6789"
193 (let ((fill-column 5))
194 (narrow-to-region 1 8)
195 (org-fill-paragraph)
196 (buffer-string)))))
197 ;; Handle `adaptive-fill-regexp' in paragraphs.
198 (should
199 (equal "> a b"
200 (org-test-with-temp-text "> a\n> b"
201 (let ((fill-column 5)
202 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
203 (org-fill-paragraph)
204 (buffer-string)))))
205 ;; Special case: Fill first paragraph when point is at an item or
206 ;; a plain-list or a footnote reference.
207 (should
208 (equal "- A B"
209 (org-test-with-temp-text "- A\n B"
210 (let ((fill-column 20))
211 (org-fill-paragraph)
212 (buffer-string)))))
213 (should
214 (equal "[fn:1] A B"
215 (org-test-with-temp-text "[fn:1] A\nB"
216 (let ((fill-column 20))
217 (org-fill-paragraph)
218 (buffer-string)))))
219 (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
220 (let ((fill-column 20))
221 (org-fill-paragraph)
222 (should (equal (buffer-string)
223 "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
224 ;; Fill contents of `comment-block' elements.
225 (should
226 (equal
227 (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
228 (let ((fill-column 20))
229 (forward-line)
230 (org-fill-paragraph)
231 (buffer-string)))
232 "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
233 ;; Fill `comment' elements.
234 (should
235 (equal " # A B"
236 (org-test-with-temp-text " # A\n # B"
237 (let ((fill-column 20))
238 (org-fill-paragraph)
239 (buffer-string)))))
240 ;; Do not mix consecutive comments when filling one of them.
241 (should
242 (equal "# A B\n\n# C"
243 (org-test-with-temp-text "# A\n# B\n\n# C"
244 (let ((fill-column 20))
245 (org-fill-paragraph)
246 (buffer-string)))))
247 ;; Use commented empty lines as separators when filling comments.
248 (should
249 (equal "# A B\n#\n# C"
250 (org-test-with-temp-text "# A\n# B\n#\n# C"
251 (let ((fill-column 20))
252 (org-fill-paragraph)
253 (buffer-string)))))
254 ;; Handle `adaptive-fill-regexp' in comments.
255 (should
256 (equal "# > a b"
257 (org-test-with-temp-text "# > a\n# > b"
258 (let ((fill-column 20)
259 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
260 (org-fill-paragraph)
261 (buffer-string)))))
262 ;; Do nothing at affiliated keywords.
263 (org-test-with-temp-text "#+NAME: para\nSome\ntext."
264 (let ((fill-column 20))
265 (org-fill-paragraph)
266 (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
267 ;; Do not move point after table when filling a table.
268 (should-not
269 (org-test-with-temp-text "| a | b |\n| c | d |\n"
270 (forward-char)
271 (org-fill-paragraph)
272 (eobp))))
274 (ert-deftest test-org/auto-fill-function ()
275 "Test auto-filling features."
276 ;; Auto fill paragraph.
277 (should
278 (equal "12345\n7890"
279 (org-test-with-temp-text "12345 7890"
280 (let ((fill-column 5))
281 (end-of-line)
282 (org-auto-fill-function)
283 (buffer-string)))))
284 ;; Auto fill first paragraph in an item.
285 (should
286 (equal "- 12345\n 7890"
287 (org-test-with-temp-text "- 12345 7890"
288 (let ((fill-column 7))
289 (end-of-line)
290 (org-auto-fill-function)
291 (buffer-string)))))
292 ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
293 (should
294 (equal "> 12345\n 7890"
295 (org-test-with-temp-text "> 12345 7890"
296 (let ((fill-column 10)
297 (adaptive-fill-regexp "[ \t]*>+[ \t]*")
298 (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
299 (end-of-line)
300 (org-auto-fill-function)
301 (buffer-string)))))
302 (should
303 (equal "> 12345\n> 12345\n> 7890"
304 (org-test-with-temp-text "> 12345\n> 12345 7890"
305 (let ((fill-column 10)
306 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
307 (goto-char (point-max))
308 (org-auto-fill-function)
309 (buffer-string)))))
310 (should-not
311 (equal " 12345\n *12345\n *12345"
312 (org-test-with-temp-text " 12345\n *12345 12345"
313 (let ((fill-column 10)
314 (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
315 (goto-char (point-max))
316 (org-auto-fill-function)
317 (buffer-string)))))
318 ;; Auto fill comments.
319 (should
320 (equal " # 12345\n # 7890"
321 (org-test-with-temp-text " # 12345 7890"
322 (let ((fill-column 10))
323 (end-of-line)
324 (org-auto-fill-function)
325 (buffer-string)))))
326 ;; A hash within a line isn't a comment.
327 (should-not
328 (equal "12345 # 7890\n# 1"
329 (org-test-with-temp-text "12345 # 7890 1"
330 (let ((fill-column 12))
331 (end-of-line)
332 (org-auto-fill-function)
333 (buffer-string)))))
334 ;; Correctly interpret empty prefix.
335 (should-not
336 (equal "# a\n# b\nRegular\n# paragraph"
337 (org-test-with-temp-text "# a\n# b\nRegular paragraph"
338 (let ((fill-column 12))
339 (end-of-line 3)
340 (org-auto-fill-function)
341 (buffer-string)))))
342 ;; Comment block: auto fill contents.
343 (should
344 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
345 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
346 (let ((fill-column 5))
347 (forward-line)
348 (end-of-line)
349 (org-auto-fill-function)
350 (buffer-string)))))
351 (should
352 (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
353 (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
354 (let ((fill-column 5))
355 (forward-line)
356 (end-of-line)
357 (org-auto-fill-function)
358 (buffer-string)))))
359 ;; Do not fill if a new item could be created.
360 (should-not
361 (equal "12345\n- 90"
362 (org-test-with-temp-text "12345 - 90"
363 (let ((fill-column 5))
364 (end-of-line)
365 (org-auto-fill-function)
366 (buffer-string)))))
367 ;; Do not fill if a line break could be introduced.
368 (should-not
369 (equal "123\\\\\n7890"
370 (org-test-with-temp-text "123\\\\ 7890"
371 (let ((fill-column 6))
372 (end-of-line)
373 (org-auto-fill-function)
374 (buffer-string)))))
375 ;; Do not fill affiliated keywords.
376 (should-not
377 (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
378 (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
379 (let ((fill-column 20))
380 (end-of-line)
381 (org-auto-fill-function)
382 (buffer-string))))))
386 ;;; Editing
388 ;;;; Insert elements
390 (ert-deftest test-org/meta-return ()
391 "Test M-RET (`org-meta-return')."
392 ;; In a table field insert a row above.
393 (should
394 (org-test-with-temp-text "| a |"
395 (forward-char)
396 (org-meta-return)
397 (forward-line -1)
398 (looking-at "| |$")))
399 ;; In a paragraph change current line into a header.
400 (should
401 (org-test-with-temp-text "a"
402 (org-meta-return)
403 (beginning-of-line)
404 (looking-at "\* a$")))
405 ;; In an item insert an item, in this case above.
406 (should
407 (org-test-with-temp-text "- a"
408 (org-meta-return)
409 (beginning-of-line)
410 (looking-at "- $")))
411 ;; In a drawer and paragraph insert an empty line, in this case above.
412 (should
413 (org-test-with-temp-text ":MYDRAWER:\na\n:END:"
414 (forward-line)
415 (org-meta-return)
416 (forward-line -1)
417 (looking-at "$")))
418 ;; In a drawer and item insert an item, in this case above.
419 (should
420 (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
421 (forward-line)
422 (org-meta-return)
423 (beginning-of-line)
424 (looking-at "- $"))))
426 (ert-deftest test-org/insert-todo-heading-respect-content ()
427 "Test `org-insert-todo-heading-respect-content' specifications."
428 ;; Create a TODO heading.
429 (should
430 (org-test-with-temp-text "* H1\n Body"
431 (org-insert-todo-heading-respect-content)
432 (nth 2 (org-heading-components))))
433 ;; Add headline after body of current subtree.
434 (should
435 (org-test-with-temp-text "* H1\nBody"
436 (org-insert-todo-heading-respect-content)
437 (eobp)))
438 (should
439 (org-test-with-temp-text "* H1\n** H2\nBody"
440 (org-insert-todo-heading-respect-content)
441 (eobp)))
442 ;; In a list, do not create a new item.
443 (should
444 (org-test-with-temp-text "* H\n- an item\n- another one"
445 (search-forward "an ")
446 (org-insert-todo-heading-respect-content)
447 (and (eobp) (org-at-heading-p)))))
451 ;;; Fixed-Width Areas
453 (ert-deftest test-org/toggle-fixed-with ()
454 "Test `org-toggle-fixed-width' specifications."
455 ;; No region: Toggle on fixed-width marker in paragraphs.
456 (should
457 (equal ": A"
458 (org-test-with-temp-text "A"
459 (org-toggle-fixed-width)
460 (buffer-string))))
461 ;; No region: Toggle off fixed-width markers in fixed-width areas.
462 (should
463 (equal "A"
464 (org-test-with-temp-text ": A"
465 (org-toggle-fixed-width)
466 (buffer-string))))
467 ;; No region: Toggle on marker in blank lines after elements or just
468 ;; after a headline.
469 (should
470 (equal "* H\n: "
471 (org-test-with-temp-text "* H\n"
472 (forward-line)
473 (org-toggle-fixed-width)
474 (buffer-string))))
475 (should
476 (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
477 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
478 (goto-char (point-max))
479 (org-toggle-fixed-width)
480 (buffer-string))))
481 ;; No region: Toggle on marker in front of one line elements (e.g.,
482 ;; headlines, clocks)
483 (should
484 (equal ": * Headline"
485 (org-test-with-temp-text "* Headline"
486 (org-toggle-fixed-width)
487 (buffer-string))))
488 (should
489 (equal ": #+KEYWORD: value"
490 (org-test-with-temp-text "#+KEYWORD: value"
491 (org-toggle-fixed-width)
492 (buffer-string))))
493 ;; No region: error in other situations.
494 (should-error
495 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
496 (forward-line)
497 (org-toggle-fixed-width)
498 (buffer-string)))
499 ;; No region: Indentation is preserved.
500 (should
501 (equal "- A\n : B"
502 (org-test-with-temp-text "- A\n B"
503 (forward-line)
504 (org-toggle-fixed-width)
505 (buffer-string))))
506 ;; Region: If it contains only fixed-width elements and blank lines,
507 ;; toggle off fixed-width markup.
508 (should
509 (equal "A\n\nB"
510 (org-test-with-temp-text ": A\n\n: B"
511 (transient-mark-mode 1)
512 (push-mark (point) t t)
513 (goto-char (point-max))
514 (org-toggle-fixed-width)
515 (buffer-string))))
516 ;; Region: If it contains anything else, toggle on fixed-width but
517 ;; not on fixed-width areas.
518 (should
519 (equal ": A\n: \n: B\n: \n: C"
520 (org-test-with-temp-text "A\n\n: B\n\nC"
521 (transient-mark-mode 1)
522 (push-mark (point) t t)
523 (goto-char (point-max))
524 (org-toggle-fixed-width)
525 (buffer-string))))
526 ;; Region: Ignore blank lines at its end, unless it contains only
527 ;; such lines.
528 (should
529 (equal ": A\n\n"
530 (org-test-with-temp-text "A\n\n"
531 (transient-mark-mode 1)
532 (push-mark (point) t t)
533 (goto-char (point-max))
534 (org-toggle-fixed-width)
535 (buffer-string))))
536 (should
537 (equal ": \n: \n"
538 (org-test-with-temp-text "\n\n"
539 (transient-mark-mode 1)
540 (push-mark (point) t t)
541 (goto-char (point-max))
542 (org-toggle-fixed-width)
543 (buffer-string)))))
547 ;;; Links
549 ;;;; Custom ID
551 (ert-deftest test-org/custom-id ()
552 "Test custom ID links specifications."
553 (should
554 (org-test-with-temp-text
555 "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]"
556 (goto-char (point-max))
557 (org-open-at-point)
558 (org-looking-at-p "\\* H1"))))
560 ;;;; Fuzzy Links
562 ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
563 ;; a named element (#+name: text) and to headlines (* Text).
565 (ert-deftest test-org/fuzzy-links ()
566 "Test fuzzy links specifications."
567 ;; 1. Fuzzy link goes in priority to a matching target.
568 (should
569 (org-test-with-temp-text "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
570 (goto-line 5)
571 (org-open-at-point)
572 (looking-at "<<Test>>")))
573 ;; 2. Then fuzzy link points to an element with a given name.
574 (should
575 (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
576 (goto-line 5)
577 (org-open-at-point)
578 (looking-at "#\\+NAME: Test")))
579 ;; 3. A target still lead to a matching headline otherwise.
580 (should
581 (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
582 (goto-line 4)
583 (org-open-at-point)
584 (looking-at "\\* Head2")))
585 ;; 4. With a leading star in link, enforce heading match.
586 (should
587 (org-test-with-temp-text "* Test\n<<Test>>\n[[*Test]]"
588 (goto-line 3)
589 (org-open-at-point)
590 (looking-at "\\* Test"))))
593 ;;;; Link Escaping
595 (ert-deftest test-org/org-link-escape-ascii-character ()
596 "Escape an ascii character."
597 (should
598 (string=
599 "%5B"
600 (org-link-escape "["))))
602 (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
603 "Escape an ascii control character."
604 (should
605 (string=
606 "%09"
607 (org-link-escape "\t"))))
609 (ert-deftest test-org/org-link-escape-multibyte-character ()
610 "Escape an unicode multibyte character."
611 (should
612 (string=
613 "%E2%82%AC"
614 (org-link-escape "€"))))
616 (ert-deftest test-org/org-link-escape-custom-table ()
617 "Escape string with custom character table."
618 (should
619 (string=
620 "Foo%3A%42ar%0A"
621 (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
623 (ert-deftest test-org/org-link-escape-custom-table-merge ()
624 "Escape string with custom table merged with default table."
625 (should
626 (string=
627 "%5BF%6F%6F%3A%42ar%0A%5D"
628 (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
630 (ert-deftest test-org/org-link-unescape-ascii-character ()
631 "Unescape an ascii character."
632 (should
633 (string=
635 (org-link-unescape "%5B"))))
637 (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
638 "Unescpae an ascii control character."
639 (should
640 (string=
641 "\n"
642 (org-link-unescape "%0A"))))
644 (ert-deftest test-org/org-link-unescape-multibyte-character ()
645 "Unescape unicode multibyte character."
646 (should
647 (string=
648 "€"
649 (org-link-unescape "%E2%82%AC"))))
651 (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
652 "Unescape old style percent escaped character."
653 (should
654 (string=
655 "àâçèéêîôùû"
656 (decode-coding-string
657 (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
659 (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
660 "Escape and unescape a URL that includes an escaped char.
661 http://article.gmane.org/gmane.emacs.orgmode/21459/"
662 (should
663 (string=
664 "http://some.host.com/form?&id=blah%2Bblah25"
665 (org-link-unescape
666 (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
668 (ert-deftest test-org/org-link-escape-chars-browser ()
669 "Test of the constant `org-link-escape-chars-browser'.
670 See there why this test is a candidate to be removed once Org
671 drops support for Emacs 24.1 and 24.2."
672 (should
673 (string=
674 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
675 "%22Release%208.2%22&idxname=emacs-orgmode")
676 (org-link-escape-browser ; Do not replace with `url-encode-url',
677 ; see docstring above.
678 (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
679 "\"Release 8.2\"&idxname=emacs-orgmode")))))
683 ;;; Node Properties
685 (ert-deftest test-org/accumulated-properties-in-drawers ()
686 "Ensure properties accumulate in subtree drawers."
687 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
688 (org-babel-next-src-block)
689 (should (equal '(2 1) (org-babel-execute-src-block)))))
693 ;;; Mark Region
695 (ert-deftest test-org/mark-subtree ()
696 "Test `org-mark-subtree' specifications."
697 ;; Error when point is before first headline.
698 (should-error
699 (org-test-with-temp-text "Paragraph\n* Headline\nBody"
700 (progn (transient-mark-mode 1)
701 (org-mark-subtree))))
702 ;; Without argument, mark current subtree.
703 (should
704 (equal
705 '(12 32)
706 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
707 (progn (transient-mark-mode 1)
708 (forward-line 2)
709 (org-mark-subtree)
710 (list (region-beginning) (region-end))))))
711 ;; With an argument, move ARG up.
712 (should
713 (equal
714 '(1 32)
715 (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
716 (progn (transient-mark-mode 1)
717 (forward-line 2)
718 (org-mark-subtree 1)
719 (list (region-beginning) (region-end))))))
720 ;; Do not get fooled by inlinetasks.
721 (when (featurep 'org-inlinetask)
722 (should
723 (= 1
724 (org-test-with-temp-text "* Headline\n*************** Task\nContents"
725 (progn (transient-mark-mode 1)
726 (forward-line 1)
727 (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
728 (region-beginning)))))))
732 ;;; Navigation
734 (ert-deftest test-org/beginning-of-line ()
735 "Test `org-beginning-of-line' specifications."
736 ;; Standard test.
737 (should
738 (org-test-with-temp-text "Some text\nSome other text"
739 (progn (org-beginning-of-line) (bolp))))
740 ;; Standard test with `visual-line-mode'.
741 (should-not
742 (org-test-with-temp-text "A long line of text\nSome other text"
743 (progn (visual-line-mode)
744 (forward-char 2)
745 (dotimes (i 1000) (insert "very "))
746 (org-beginning-of-line)
747 (bolp))))
748 ;; At an headline with special movement.
749 (should
750 (org-test-with-temp-text "* TODO Headline"
751 (let ((org-special-ctrl-a/e t))
752 (org-end-of-line)
753 (and (progn (org-beginning-of-line) (looking-at "Headline"))
754 (progn (org-beginning-of-line) (bolp))
755 (progn (org-beginning-of-line) (looking-at "Headline")))))))
757 (ert-deftest test-org/end-of-line ()
758 "Test `org-end-of-line' specifications."
759 ;; Standard test.
760 (should
761 (org-test-with-temp-text "Some text\nSome other text"
762 (progn (org-end-of-line) (eolp))))
763 ;; Standard test with `visual-line-mode'.
764 (should-not
765 (org-test-with-temp-text "A long line of text\nSome other text"
766 (progn (visual-line-mode)
767 (forward-char 2)
768 (dotimes (i 1000) (insert "very "))
769 (goto-char (point-min))
770 (org-end-of-line)
771 (eolp))))
772 ;; At an headline with special movement.
773 (should
774 (org-test-with-temp-text "* Headline1 :tag:\n"
775 (let ((org-special-ctrl-a/e t))
776 (and (progn (org-end-of-line) (looking-at " :tag:"))
777 (progn (org-end-of-line) (eolp))
778 (progn (org-end-of-line) (looking-at " :tag:"))))))
779 ;; At an headline without special movement.
780 (should
781 (org-test-with-temp-text "* Headline2 :tag:\n"
782 (let ((org-special-ctrl-a/e nil))
783 (and (progn (org-end-of-line) (eolp))
784 (progn (org-end-of-line) (eolp))))))
785 ;; At an headline, with reversed movement.
786 (should
787 (org-test-with-temp-text "* Headline3 :tag:\n"
788 (let ((org-special-ctrl-a/e 'reversed)
789 (this-command last-command))
790 (and (progn (org-end-of-line) (eolp))
791 (progn (org-end-of-line) (looking-at " :tag:"))))))
792 ;; At a block without hidden contents.
793 (should
794 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
795 (progn (org-end-of-line) (eolp))))
796 ;; At a block with hidden contents.
797 (should-not
798 (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
799 (let ((org-special-ctrl-a/e t))
800 (org-hide-block-toggle)
801 (org-end-of-line)
802 (eobp)))))
804 (ert-deftest test-org/forward-paragraph ()
805 "Test `org-forward-paragraph' specifications."
806 ;; At end of buffer, return an error.
807 (should-error
808 (org-test-with-temp-text "Paragraph"
809 (goto-char (point-max))
810 (org-forward-paragraph)))
811 ;; Standard test.
812 (should
813 (org-test-with-temp-text "P1\n\nP2\n\nP3"
814 (org-forward-paragraph)
815 (looking-at "P2")))
816 ;; Ignore depth.
817 (should
818 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
819 (org-forward-paragraph)
820 (looking-at "P1")))
821 ;; Do not enter elements with invisible contents.
822 (should
823 (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
824 (org-hide-block-toggle)
825 (org-forward-paragraph)
826 (looking-at "P3")))
827 ;; On an affiliated keyword, jump to the beginning of the element.
828 (should
829 (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
830 (org-forward-paragraph)
831 (looking-at "Para")))
832 ;; On an item or a footnote definition, move to the second element
833 ;; inside, if any.
834 (should
835 (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
836 (org-forward-paragraph)
837 (looking-at " Paragraph")))
838 (should
839 (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
840 (org-forward-paragraph)
841 (looking-at "Paragraph")))
842 ;; On an item, or a footnote definition, when the first line is
843 ;; empty, move to the first item.
844 (should
845 (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
846 (org-forward-paragraph)
847 (looking-at " Paragraph")))
848 (should
849 (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
850 (org-forward-paragraph)
851 (looking-at "Paragraph")))
852 ;; On a table (resp. a property drawer) do not move through table
853 ;; rows (resp. node properties).
854 (should
855 (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
856 (org-forward-paragraph)
857 (looking-at "Paragraph")))
858 (should
859 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nParagraph"
860 (org-forward-paragraph)
861 (looking-at "Paragraph")))
862 ;; On a verse or source block, stop after blank lines.
863 (should
864 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
865 (org-forward-paragraph)
866 (looking-at "L2")))
867 (should
868 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
869 (org-forward-paragraph)
870 (looking-at "L2"))))
872 (ert-deftest test-org/backward-paragraph ()
873 "Test `org-backward-paragraph' specifications."
874 ;; Error at beginning of buffer.
875 (should-error
876 (org-test-with-temp-text "Paragraph"
877 (org-backward-paragraph)))
878 ;; Regular test.
879 (should
880 (org-test-with-temp-text "P1\n\nP2\n\nP3"
881 (goto-char (point-max))
882 (org-backward-paragraph)
883 (looking-at "P3")))
884 (should
885 (org-test-with-temp-text "P1\n\nP2\n\nP3"
886 (goto-char (point-max))
887 (beginning-of-line)
888 (org-backward-paragraph)
889 (looking-at "P2")))
890 ;; Ignore depth.
891 (should
892 (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
893 (goto-char (point-max))
894 (beginning-of-line)
895 (org-backward-paragraph)
896 (looking-at "P2")))
897 ;; Ignore invisible elements.
898 (should
899 (org-test-with-temp-text "* H1\n P1\n* H2"
900 (org-cycle)
901 (goto-char (point-max))
902 (beginning-of-line)
903 (org-backward-paragraph)
904 (bobp)))
905 ;; On an affiliated keyword, jump to the first one.
906 (should
907 (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
908 (search-forward "c2")
909 (org-backward-paragraph)
910 (looking-at "#\\+name")))
911 ;; On the second element in an item or a footnote definition, jump
912 ;; to item or the definition.
913 (should
914 (org-test-with-temp-text "- line1\n\n line2"
915 (goto-char (point-max))
916 (beginning-of-line)
917 (org-backward-paragraph)
918 (looking-at "- line1")))
919 (should
920 (org-test-with-temp-text "[fn:1] line1\n\n line2"
921 (goto-char (point-max))
922 (beginning-of-line)
923 (org-backward-paragraph)
924 (looking-at "\\[fn:1\\] line1")))
925 ;; On a table (resp. a property drawer), ignore table rows
926 ;; (resp. node properties).
927 (should
928 (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
929 (goto-char (point-max))
930 (beginning-of-line)
931 (org-backward-paragraph)
932 (bobp)))
933 (should
934 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:\nP1"
935 (goto-char (point-max))
936 (beginning-of-line)
937 (org-backward-paragraph)
938 (bobp)))
939 ;; On a source or verse block, stop before blank lines.
940 (should
941 (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
942 (search-forward "L3")
943 (beginning-of-line)
944 (org-backward-paragraph)
945 (looking-at "L2")))
946 (should
947 (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
948 (search-forward "L3")
949 (beginning-of-line)
950 (org-backward-paragraph)
951 (looking-at "L2"))))
953 (ert-deftest test-org/forward-element ()
954 "Test `org-forward-element' specifications."
955 ;; 1. At EOB: should error.
956 (org-test-with-temp-text "Some text\n"
957 (goto-char (point-max))
958 (should-error (org-forward-element)))
959 ;; 2. Standard move: expected to ignore blank lines.
960 (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
961 (org-forward-element)
962 (should (looking-at (regexp-quote "Second paragraph."))))
963 ;; 3. Headline tests.
964 (org-test-with-temp-text "
965 * Head 1
966 ** Head 1.1
967 *** Head 1.1.1
968 ** Head 1.2"
969 ;; 3.1. At an headline beginning: move to next headline at the
970 ;; same level.
971 (goto-line 3)
972 (org-forward-element)
973 (should (looking-at (regexp-quote "** Head 1.2")))
974 ;; 3.2. At an headline beginning: move to parent headline if no
975 ;; headline at the same level.
976 (goto-line 3)
977 (org-forward-element)
978 (should (looking-at (regexp-quote "** Head 1.2"))))
979 ;; 4. Greater element tests.
980 (org-test-with-temp-text
981 "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
982 ;; 4.1. At a greater element: expected to skip contents.
983 (org-forward-element)
984 (should (looking-at (regexp-quote "Outside.")))
985 ;; 4.2. At the end of greater element contents: expected to skip
986 ;; to the end of the greater element.
987 (goto-line 2)
988 (org-forward-element)
989 (should (looking-at (regexp-quote "Outside."))))
990 ;; 5. List tests.
991 (org-test-with-temp-text "
992 - item1
994 - sub1
996 - sub2
998 - sub3
1000 Inner paragraph.
1002 - item2
1004 Outside."
1005 ;; 5.1. At list top point: expected to move to the element after
1006 ;; the list.
1007 (goto-line 2)
1008 (org-forward-element)
1009 (should (looking-at (regexp-quote "Outside.")))
1010 ;; 5.2. Special case: at the first line of a sub-list, but not at
1011 ;; beginning of line, move to next item.
1012 (goto-line 2)
1013 (forward-char)
1014 (org-forward-element)
1015 (should (looking-at "- item2"))
1016 (goto-line 4)
1017 (forward-char)
1018 (org-forward-element)
1019 (should (looking-at " - sub2"))
1020 ;; 5.3 At sub-list beginning: expected to move after the sub-list.
1021 (goto-line 4)
1022 (org-forward-element)
1023 (should (looking-at (regexp-quote " Inner paragraph.")))
1024 ;; 5.4. At sub-list end: expected to move outside the sub-list.
1025 (goto-line 8)
1026 (org-forward-element)
1027 (should (looking-at (regexp-quote " Inner paragraph.")))
1028 ;; 5.5. At an item: expected to move to next item, if any.
1029 (goto-line 6)
1030 (org-forward-element)
1031 (should (looking-at " - sub3"))))
1033 (ert-deftest test-org/backward-element ()
1034 "Test `org-backward-element' specifications."
1035 ;; 1. Should error at BOB.
1036 (org-test-with-temp-text " \nParagraph."
1037 (should-error (org-backward-element)))
1038 ;; 2. Should move at BOB when called on the first element in buffer.
1039 (should
1040 (org-test-with-temp-text "\n#+TITLE: test"
1041 (progn (forward-line)
1042 (org-backward-element)
1043 (bobp))))
1044 ;; 3. Not at the beginning of an element: move at its beginning.
1045 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1046 (goto-line 3)
1047 (end-of-line)
1048 (org-backward-element)
1049 (should (looking-at (regexp-quote "Paragraph2."))))
1050 ;; 4. Headline tests.
1051 (org-test-with-temp-text "
1052 * Head 1
1053 ** Head 1.1
1054 *** Head 1.1.1
1055 ** Head 1.2"
1056 ;; 4.1. At an headline beginning: move to previous headline at the
1057 ;; same level.
1058 (goto-line 5)
1059 (org-backward-element)
1060 (should (looking-at (regexp-quote "** Head 1.1")))
1061 ;; 4.2. At an headline beginning: move to parent headline if no
1062 ;; headline at the same level.
1063 (goto-line 3)
1064 (org-backward-element)
1065 (should (looking-at (regexp-quote "* Head 1")))
1066 ;; 4.3. At the first top-level headline: should error.
1067 (goto-line 2)
1068 (should-error (org-backward-element)))
1069 ;; 5. At beginning of first element inside a greater element:
1070 ;; expected to move to greater element's beginning.
1071 (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
1072 (goto-line 3)
1073 (org-backward-element)
1074 (should (looking-at "#\\+BEGIN_CENTER")))
1075 ;; 6. At the beginning of the first element in a section: should
1076 ;; move back to headline, if any.
1077 (should
1078 (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
1079 (progn (goto-char (point-max))
1080 (beginning-of-line)
1081 (org-backward-element)
1082 (org-at-heading-p))))
1083 ;; 7. List tests.
1084 (org-test-with-temp-text "
1085 - item1
1087 - sub1
1089 - sub2
1091 - sub3
1093 Inner paragraph.
1095 - item2
1098 Outside."
1099 ;; 7.1. At beginning of sub-list: expected to move to the
1100 ;; paragraph before it.
1101 (goto-line 4)
1102 (org-backward-element)
1103 (should (looking-at "item1"))
1104 ;; 7.2. At an item in a list: expected to move at previous item.
1105 (goto-line 8)
1106 (org-backward-element)
1107 (should (looking-at " - sub2"))
1108 (goto-line 12)
1109 (org-backward-element)
1110 (should (looking-at "- item1"))
1111 ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
1112 ;; beginning.
1113 (goto-line 10)
1114 (org-backward-element)
1115 (should (looking-at " - sub1"))
1116 (goto-line 15)
1117 (org-backward-element)
1118 (should (looking-at "- item1"))
1119 ;; 7.4. At blank-lines before list end: expected to move to top
1120 ;; item.
1121 (goto-line 14)
1122 (org-backward-element)
1123 (should (looking-at "- item1"))))
1125 (ert-deftest test-org/up-element ()
1126 "Test `org-up-element' specifications."
1127 ;; 1. At BOB or with no surrounding element: should error.
1128 (org-test-with-temp-text "Paragraph."
1129 (should-error (org-up-element)))
1130 (org-test-with-temp-text "* Head1\n* Head2"
1131 (goto-line 2)
1132 (should-error (org-up-element)))
1133 (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
1134 (goto-line 3)
1135 (should-error (org-up-element)))
1136 ;; 2. At an headline: move to parent headline.
1137 (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
1138 (goto-line 3)
1139 (org-up-element)
1140 (should (looking-at "\\* Head1")))
1141 ;; 3. Inside a greater element: move to greater element beginning.
1142 (org-test-with-temp-text
1143 "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
1144 (goto-line 3)
1145 (org-up-element)
1146 (should (looking-at "#\\+BEGIN_CENTER")))
1147 ;; 4. List tests.
1148 (org-test-with-temp-text "* Top
1149 - item1
1151 - sub1
1153 - sub2
1155 Paragraph within sub2.
1157 - item2"
1158 ;; 4.1. Within an item: move to the item beginning.
1159 (goto-line 8)
1160 (org-up-element)
1161 (should (looking-at " - sub2"))
1162 ;; 4.2. At an item in a sub-list: move to parent item.
1163 (goto-line 4)
1164 (org-up-element)
1165 (should (looking-at "- item1"))
1166 ;; 4.3. At an item in top list: move to beginning of whole list.
1167 (goto-line 10)
1168 (org-up-element)
1169 (should (looking-at "- item1"))
1170 ;; 4.4. Special case. At very top point: should move to parent of
1171 ;; list.
1172 (goto-line 2)
1173 (org-up-element)
1174 (should (looking-at "\\* Top"))))
1176 (ert-deftest test-org/down-element ()
1177 "Test `org-down-element' specifications."
1178 ;; Error when the element hasn't got a recursive type.
1179 (org-test-with-temp-text "Paragraph."
1180 (should-error (org-down-element)))
1181 ;; Error when the element has no contents
1182 (org-test-with-temp-text "* Headline"
1183 (should-error (org-down-element)))
1184 ;; When at a plain-list, move to first item.
1185 (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
1186 (goto-line 2)
1187 (org-down-element)
1188 (should (looking-at " - Item 1.1")))
1189 (org-test-with-temp-text "#+NAME: list\n- Item 1"
1190 (org-down-element)
1191 (should (looking-at " Item 1")))
1192 ;; When at a table, move to first row
1193 (org-test-with-temp-text "#+NAME: table\n| a | b |"
1194 (org-down-element)
1195 (should (looking-at " a | b |")))
1196 ;; Otherwise, move inside the greater element.
1197 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
1198 (org-down-element)
1199 (should (looking-at "Paragraph"))))
1201 (ert-deftest test-org/drag-element-backward ()
1202 "Test `org-drag-element-backward' specifications."
1203 ;; 1. Error when trying to move first element of buffer.
1204 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1205 (should-error (org-drag-element-backward)))
1206 ;; 2. Error when trying to swap nested elements.
1207 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1208 (forward-line)
1209 (should-error (org-drag-element-backward)))
1210 ;; 3. Error when trying to swap an headline element and
1211 ;; a non-headline element.
1212 (org-test-with-temp-text "Test.\n* Head 1"
1213 (forward-line)
1214 (should-error (org-drag-element-backward)))
1215 ;; 4. Otherwise, swap elements, preserving column and blank lines
1216 ;; between elements.
1217 (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
1218 (search-forward "graph")
1219 (org-drag-element-backward)
1220 (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
1221 (should (looking-at " 2")))
1222 ;; 5. Preserve visibility of elements and their contents.
1223 (org-test-with-temp-text "
1224 #+BEGIN_CENTER
1225 Text.
1226 #+END_CENTER
1227 - item 1
1228 #+BEGIN_QUOTE
1229 Text.
1230 #+END_QUOTE"
1231 (while (search-forward "BEGIN_" nil t) (org-cycle))
1232 (search-backward "- item 1")
1233 (org-drag-element-backward)
1234 (should
1235 (equal
1236 '((63 . 82) (26 . 48))
1237 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
1238 (overlays-in (point-min) (point-max)))))))
1240 (ert-deftest test-org/drag-element-forward ()
1241 "Test `org-drag-element-forward' specifications."
1242 ;; 1. Error when trying to move first element of buffer.
1243 (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
1244 (goto-line 3)
1245 (should-error (org-drag-element-forward)))
1246 ;; 2. Error when trying to swap nested elements.
1247 (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
1248 (forward-line)
1249 (should-error (org-drag-element-forward)))
1250 ;; 3. Error when trying to swap a non-headline element and an
1251 ;; headline.
1252 (org-test-with-temp-text "Test.\n* Head 1"
1253 (should-error (org-drag-element-forward)))
1254 ;; 4. Otherwise, swap elements, preserving column and blank lines
1255 ;; between elements.
1256 (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
1257 (search-forward "graph")
1258 (org-drag-element-forward)
1259 (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
1260 (should (looking-at " 1")))
1261 ;; 5. Preserve visibility of elements and their contents.
1262 (org-test-with-temp-text "
1263 #+BEGIN_CENTER
1264 Text.
1265 #+END_CENTER
1266 - item 1
1267 #+BEGIN_QUOTE
1268 Text.
1269 #+END_QUOTE"
1270 (while (search-forward "BEGIN_" nil t) (org-cycle))
1271 (search-backward "#+BEGIN_CENTER")
1272 (org-drag-element-forward)
1273 (should
1274 (equal
1275 '((63 . 82) (26 . 48))
1276 (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
1277 (overlays-in (point-min) (point-max)))))))
1281 ;;; Planning
1283 (ert-deftest test-org/timestamp-has-time-p ()
1284 "Test `org-timestamp-has-time-p' specifications."
1285 ;; With time.
1286 (should
1287 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1288 (org-timestamp-has-time-p (org-element-context))))
1289 ;; Without time.
1290 (should-not
1291 (org-test-with-temp-text "<2012-03-29 Thu>"
1292 (org-timestamp-has-time-p (org-element-context)))))
1294 (ert-deftest test-org/timestamp-format ()
1295 "Test `org-timestamp-format' specifications."
1296 ;; Regular test.
1297 (should
1298 (equal
1299 "2012-03-29 16:40"
1300 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1301 (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
1302 ;; Range end.
1303 (should
1304 (equal
1305 "2012-03-29"
1306 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
1307 (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
1309 (ert-deftest test-org/timestamp-split-range ()
1310 "Test `org-timestamp-split-range' specifications."
1311 ;; Extract range start (active).
1312 (should
1313 (equal '(2012 3 29)
1314 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1315 (let ((ts (org-timestamp-split-range (org-element-context))))
1316 (mapcar (lambda (p) (org-element-property p ts))
1317 '(:year-end :month-end :day-end))))))
1318 ;; Extract range start (inactive)
1319 (should
1320 (equal '(2012 3 29)
1321 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1322 (let ((ts (org-timestamp-split-range (org-element-context))))
1323 (mapcar (lambda (p) (org-element-property p ts))
1324 '(:year-end :month-end :day-end))))))
1325 ;; Extract range end (active).
1326 (should
1327 (equal '(2012 3 30)
1328 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1329 (let ((ts (org-timestamp-split-range
1330 (org-element-context) t)))
1331 (mapcar (lambda (p) (org-element-property p ts))
1332 '(:year-end :month-end :day-end))))))
1333 ;; Extract range end (inactive)
1334 (should
1335 (equal '(2012 3 30)
1336 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1337 (let ((ts (org-timestamp-split-range
1338 (org-element-context) t)))
1339 (mapcar (lambda (p) (org-element-property p ts))
1340 '(:year-end :month-end :day-end))))))
1341 ;; Return the timestamp if not a range.
1342 (should
1343 (org-test-with-temp-text "[2012-03-29 Thu]"
1344 (let* ((ts-orig (org-element-context))
1345 (ts-copy (org-timestamp-split-range ts-orig)))
1346 (eq ts-orig ts-copy))))
1347 (should
1348 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1349 (let* ((ts-orig (org-element-context))
1350 (ts-copy (org-timestamp-split-range ts-orig)))
1351 (eq ts-orig ts-copy))))
1352 ;; Check that parent is the same when a range was split.
1353 (should
1354 (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
1355 (let* ((ts-orig (org-element-context))
1356 (ts-copy (org-timestamp-split-range ts-orig)))
1357 (eq (org-element-property :parent ts-orig)
1358 (org-element-property :parent ts-copy))))))
1360 (ert-deftest test-org/timestamp-translate ()
1361 "Test `org-timestamp-translate' specifications."
1362 ;; Translate whole date range.
1363 (should
1364 (equal "<29>--<30>"
1365 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1366 (let ((org-display-custom-times t)
1367 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1368 (org-timestamp-translate (org-element-context))))))
1369 ;; Translate date range start.
1370 (should
1371 (equal "<29>"
1372 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1373 (let ((org-display-custom-times t)
1374 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1375 (org-timestamp-translate (org-element-context) 'start)))))
1376 ;; Translate date range end.
1377 (should
1378 (equal "<30>"
1379 (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
1380 (let ((org-display-custom-times t)
1381 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1382 (org-timestamp-translate (org-element-context) 'end)))))
1383 ;; Translate time range.
1384 (should
1385 (equal "<08>--<16>"
1386 (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
1387 (let ((org-display-custom-times t)
1388 (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
1389 (org-timestamp-translate (org-element-context))))))
1390 ;; Translate non-range timestamp.
1391 (should
1392 (equal "<29>"
1393 (org-test-with-temp-text "<2012-03-29 Thu>"
1394 (let ((org-display-custom-times t)
1395 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1396 (org-timestamp-translate (org-element-context))))))
1397 ;; Do not change `diary' timestamps.
1398 (should
1399 (equal "<%%(org-float t 4 2)>"
1400 (org-test-with-temp-text "<%%(org-float t 4 2)>"
1401 (let ((org-display-custom-times t)
1402 (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
1403 (org-timestamp-translate (org-element-context)))))))
1407 ;;; Targets and Radio Targets
1409 (ert-deftest test-org/all-targets ()
1410 "Test `org-all-targets' specifications."
1411 ;; Without an argument.
1412 (should
1413 (equal '("radio-target" "target")
1414 (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
1415 (org-all-targets))))
1416 (should
1417 (equal '("radio-target")
1418 (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
1419 ;; With argument.
1420 (should
1421 (equal '("radio-target")
1422 (org-test-with-temp-text "<<target>> <<<radio-target>>>"
1423 (org-all-targets t)))))
1426 ;;; Visibility
1428 (ert-deftest test-org/flag-drawer ()
1429 "Test `org-flag-drawer' specifications."
1430 ;; Hide drawer.
1431 (should
1432 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1433 (org-flag-drawer t)
1434 (get-char-property (line-end-position) 'invisible)))
1435 ;; Show drawer.
1436 (should-not
1437 (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
1438 (org-flag-drawer t)
1439 (org-flag-drawer nil)
1440 (get-char-property (line-end-position) 'invisible)))
1441 ;; Test optional argument.
1442 (should
1443 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
1444 (let ((drawer (save-excursion (search-forward ":D2")
1445 (org-element-at-point))))
1446 (org-flag-drawer t drawer)
1447 (get-char-property (progn (search-forward ":D2") (line-end-position))
1448 'invisible))))
1449 (should-not
1450 (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
1451 (let ((drawer (save-excursion (search-forward ":D2")
1452 (org-element-at-point))))
1453 (org-flag-drawer t drawer)
1454 (get-char-property (line-end-position) 'invisible))))
1455 ;; Do not hide fake drawers.
1456 (should-not
1457 (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
1458 (forward-line 1)
1459 (org-flag-drawer t)
1460 (get-char-property (line-end-position) 'invisible)))
1461 ;; Do not hide incomplete drawers.
1462 (should-not
1463 (org-test-with-temp-text ":D:\nparagraph"
1464 (forward-line 1)
1465 (org-flag-drawer t)
1466 (get-char-property (line-end-position) 'invisible))))
1469 (provide 'test-org)
1471 ;;; test-org.el ends here