Merge branch 'maint'
[org-mode.git] / testing / lisp / test-org-element.el
blob103ba99a3d5358eb34b86f555a60e6b8a1d77ae0
1 ;;; test-org-element.el --- Tests for org-element.el
3 ;; Copyright (C) 2012, 2013 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Code:
22 (unless (featurep 'org-element)
23 (signal 'missing-test-dependency "org-element"))
25 (defun org-test-parse-and-interpret (text)
26 "Parse TEXT as Org syntax and interpret it.
27 Return interpreted string."
28 (with-temp-buffer
29 (org-mode)
30 (insert text)
31 (org-element-interpret-data (org-element-parse-buffer))))
35 ;;; Test `org-element-map'
37 (ert-deftest test-org-element/map ()
38 "Test `org-element-map'."
39 ;; Can map to `plain-text' objects.
40 (should
41 (= 2
42 (org-test-with-temp-text "Some text \alpha
43 #+BEGIN_CENTER
44 Some other text
45 #+END_CENTER"
46 (let ((count 0))
47 (org-element-map
48 (org-element-parse-buffer) 'plain-text
49 (lambda (s) (when (string-match "text" s) (incf count))))
50 count))))
51 ;; Applies to secondary strings
52 (should
53 (org-element-map '("some " (bold nil "bold") "text") 'bold 'identity))
54 ;; Enter secondary strings before entering contents.
55 (should
56 (equal
57 "alpha"
58 (org-element-property
59 :name
60 (org-test-with-temp-text "* Some \\alpha headline\n\\beta entity."
61 (org-element-map (org-element-parse-buffer) 'entity 'identity nil t)))))
62 ;; Apply NO-RECURSION argument.
63 (should-not
64 (org-test-with-temp-text "#+BEGIN_CENTER\n\\alpha\n#+END_CENTER"
65 (org-element-map
66 (org-element-parse-buffer) 'entity 'identity nil nil 'center-block)))
67 ;; Use WITH-AFFILIATED argument.
68 (should
69 (equal
70 '("a" "1" "b" "2")
71 (org-test-with-temp-text "#+CAPTION[a]: 1\n#+CAPTION[b]: 2\nParagraph"
72 (org-element-map
73 (org-element-at-point) 'plain-text 'identity nil nil nil t)))))
77 ;;; Test Setters
79 (ert-deftest test-org-element/put-property ()
80 "Test `org-element-put-property' specifications."
81 ;; Standard test.
82 (org-test-with-temp-text "* Headline\n *a*"
83 (let ((tree (org-element-parse-buffer)))
84 (org-element-put-property
85 (org-element-map tree 'bold 'identity nil t) :test 1)
86 (should (org-element-property
87 :test (org-element-map tree 'bold 'identity nil t)))))
88 ;; Put property on a string.
89 (should
90 (org-element-property :test (org-element-put-property "Paragraph" :test t))))
92 (ert-deftest test-org-element/set-contents ()
93 "Test `org-element-set-contents' specifications."
94 ;; Accept multiple entries.
95 (should
96 (equal '("b" (italic nil "a"))
97 (org-test-with-temp-text "* Headline\n *a*"
98 (let ((tree (org-element-parse-buffer)))
99 (org-element-set-contents
100 (org-element-map tree 'bold 'identity nil t) "b" '(italic nil "a"))
101 (org-element-contents
102 (org-element-map tree 'bold 'identity nil t))))))
103 ;; Accept atoms and elements.
104 (should
105 (equal '("b")
106 (org-test-with-temp-text "* Headline\n *a*"
107 (let ((tree (org-element-parse-buffer)))
108 (org-element-set-contents
109 (org-element-map tree 'bold 'identity nil t) "b")
110 (org-element-contents
111 (org-element-map tree 'bold 'identity nil t))))))
112 (should
113 (equal '((italic nil "b"))
114 (org-test-with-temp-text "* Headline\n *a*"
115 (let ((tree (org-element-parse-buffer)))
116 (org-element-set-contents
117 (org-element-map tree 'bold 'identity nil t) '(italic nil "b"))
118 (org-element-contents
119 (org-element-map tree 'bold 'identity nil t))))))
120 ;; Allow nil contents.
121 (should-not
122 (org-test-with-temp-text "* Headline\n *a*"
123 (let ((tree (org-element-parse-buffer)))
124 (org-element-set-contents (org-element-map tree 'bold 'identity nil t))
125 (org-element-contents (org-element-map tree 'bold 'identity nil t))))))
127 (ert-deftest test-org-element/set-element ()
128 "Test `org-element-set-element' specifications."
129 (org-test-with-temp-text "* Headline\n*a*"
130 (let ((tree (org-element-parse-buffer)))
131 (org-element-set-element
132 (org-element-map tree 'bold 'identity nil t)
133 '(italic nil "b"))
134 ;; Check if object is correctly replaced.
135 (should (org-element-map tree 'italic 'identity))
136 (should-not (org-element-map tree 'bold 'identity))
137 ;; Check if new object's parent is correctly set.
138 (should
140 (org-element-property :parent
141 (org-element-map tree 'italic 'identity nil t))
142 (org-element-map tree 'paragraph 'identity nil t))))))
144 (ert-deftest test-org-element/adopt-elements ()
145 "Test `org-element-adopt-elements' specifications."
146 ;; Adopt an element.
147 (should
148 (equal '(plain-text italic)
149 (org-test-with-temp-text "* Headline\n *a*"
150 (let ((tree (org-element-parse-buffer)))
151 (org-element-adopt-elements
152 (org-element-map tree 'bold 'identity nil t) '(italic nil "a"))
153 (mapcar (lambda (blob) (org-element-type blob))
154 (org-element-contents
155 (org-element-map tree 'bold 'identity nil t)))))))
156 ;; Adopt a string.
157 (should
158 (equal '("a" "b")
159 (org-test-with-temp-text "* Headline\n *a*"
160 (let ((tree (org-element-parse-buffer)))
161 (org-element-adopt-elements
162 (org-element-map tree 'bold 'identity nil t) "b")
163 (org-element-contents
164 (org-element-map tree 'bold 'identity nil t)))))))
168 ;;; Test Parsers
170 ;;;; Affiliated Keywords
172 (ert-deftest test-org-element/affiliated-keywords-parser ()
173 "Test affiliated keywords parsing."
174 ;; Read simple keywords.
175 (should
176 (equal "para"
177 (org-element-property
178 :name
179 (org-test-with-temp-text "#+NAME: para\nParagraph"
180 (org-element-at-point)))))
181 (should
182 (= 1
183 (org-element-property
184 :begin
185 (org-test-with-temp-text "#+NAME: para\nParagraph"
186 (org-element-at-point)))))
187 ;; Parse multiple keywords.
188 (should
189 (equal
190 '("line2" "line1")
191 (org-element-property
192 :attr_ascii
193 (org-test-with-temp-text
194 "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph"
195 (org-element-at-point)))))
196 ;; Parse "parsed" keywords.
197 (should
198 (equal
199 '(("caption"))
200 (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
201 (car (org-element-property :caption (org-element-at-point))))))
202 ;; Parse dual keywords.
203 (should
204 (equal
205 '((("long") "short"))
206 (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
207 (org-element-property :caption (org-element-at-point)))))
208 ;; Allow multiple caption keywords.
209 (should
210 (equal
211 '((("l2") "s2") (("l1") "s1"))
212 (org-test-with-temp-text "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph"
213 (org-element-property :caption (org-element-at-point)))))
214 (should
215 (equal
216 '((("l1")) (nil "s1"))
217 (org-test-with-temp-text "#+CAPTION[s1]:\n#+CAPTION: l1\nParagraph"
218 (org-element-property :caption (org-element-at-point)))))
219 ;; Corner case: orphaned keyword at the end of an element.
220 (should
221 (eq 'keyword
222 (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
223 (progn (search-forward "name")
224 (org-element-type (org-element-at-point))))))
225 (should-not
226 (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
227 (progn (search-forward "Some")
228 (org-element-property :name (org-element-at-point))))))
231 ;;;; Babel Call
233 (ert-deftest test-org-element/babel-call-parser ()
234 "Test `babel-call' parsing."
235 ;; Standard test.
236 (should
237 (org-test-with-temp-text "#+CALL: test()"
238 (org-element-map (org-element-parse-buffer) 'babel-call 'identity)))
239 ;; Ignore case.
240 (should
241 (org-test-with-temp-text "#+call: test()"
242 (org-element-map (org-element-parse-buffer) 'babel-call 'identity))))
245 ;;;; Bold
247 (ert-deftest test-org-element/bold-parser ()
248 "Test `bold' parser."
249 ;; Standard test.
250 (should
251 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
252 (org-test-with-temp-text "*bold*"
253 (org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
254 ;; Multi-line markup.
255 (should
256 (equal
257 (org-element-contents
258 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
259 (org-test-with-temp-text "*first line\nsecond line*"
260 (org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
261 '("first line\nsecond line"))))
264 ;;;; Center Block
266 (ert-deftest test-org-element/center-block-parser ()
267 "Test `center-block' parser."
268 ;; Standard test.
269 (should
270 (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
271 (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
272 ;; Ignore case.
273 (should
274 (org-test-with-temp-text "#+begin_center\nText\n#+end_center"
275 (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
276 ;; Ignore incomplete block.
277 (should-not
278 (org-test-with-temp-text "#+BEGIN_CENTER"
279 (org-element-map
280 (org-element-parse-buffer) 'center-block 'identity nil t))))
283 ;;;; Clock
285 (ert-deftest test-org-element/clock-parser ()
286 "Test `clock' parser."
287 ;; Running clock.
288 (let* ((org-clock-string "CLOCK:")
289 (clock (org-test-with-temp-text "CLOCK: [2012-01-01 sun. 00:01]"
290 (org-element-at-point))))
291 (should (eq (org-element-property :status clock) 'running))
292 (should
293 (equal (org-element-property :raw-value
294 (org-element-property :value clock))
295 "[2012-01-01 sun. 00:01]"))
296 (should-not (org-element-property :duration clock)))
297 ;; Closed clock.
298 (let* ((org-clock-string "CLOCK:")
299 (clock
300 (org-test-with-temp-text
301 "CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
302 (org-element-at-point))))
303 (should (eq (org-element-property :status clock) 'closed))
304 (should (equal (org-element-property :raw-value
305 (org-element-property :value clock))
306 "[2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02]"))
307 (should (equal (org-element-property :duration clock) "0:01"))))
310 ;;;; Code
312 (ert-deftest test-org-element/code-parser ()
313 "Test `code' parser."
314 ;; Regular test.
315 (should
316 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
317 (org-test-with-temp-text "~code~"
318 (org-element-map (org-element-parse-buffer) 'code 'identity))))
319 ;; Multi-line markup.
320 (should
321 (equal
322 (org-element-property
323 :value
324 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
325 (org-test-with-temp-text "~first line\nsecond line~"
326 (org-element-map
327 (org-element-parse-buffer) 'code 'identity nil t))))
328 "first line\nsecond line")))
331 ;;;; Comment
333 (ert-deftest test-org-element/comment-parser ()
334 "Test `comment' parser."
335 ;; Regular comment.
336 (should
337 (org-test-with-temp-text "# Comment"
338 (org-element-map (org-element-parse-buffer) 'comment 'identity)))
339 ;; Inline comment.
340 (should
341 (org-test-with-temp-text " # Comment"
342 (org-element-map (org-element-parse-buffer) 'comment 'identity)))
343 ;; Preserve indentation.
344 (should
345 (equal
346 (org-element-property
347 :value
348 (org-test-with-temp-text "# No blank\n# One blank"
349 (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
350 "No blank\n One blank"))
351 ;; Comment with blank lines.
352 (should
353 (equal
354 (org-element-property
355 :value
356 (org-test-with-temp-text "# First part\n# \n#\n# Second part"
357 (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
358 "First part\n\n\nSecond part"))
359 ;; Do not mix comments and keywords.
360 (should
361 (eq 1
362 (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
363 (length (org-element-map
364 (org-element-parse-buffer) 'comment 'identity)))))
365 (should
366 (equal "comment"
367 (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
368 (org-element-property
369 :value
370 (org-element-map
371 (org-element-parse-buffer) 'comment 'identity nil t))))))
374 ;;;; Comment Block
376 (ert-deftest test-org-element/comment-block-parser ()
377 "Test `comment-block' parser."
378 ;; Standard test.
379 (should
380 (org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
381 (org-element-map
382 (org-element-parse-buffer) 'comment-block 'identity)))
383 ;; Ignore case.
384 (should
385 (org-test-with-temp-text "#+begin_comment\nText\n#+end_comment"
386 (org-element-map
387 (org-element-parse-buffer) 'comment-block 'identity)))
388 ;; Ignore incomplete block.
389 (should-not
390 (org-test-with-temp-text "#+BEGIN_COMMENT"
391 (org-element-map
392 (org-element-parse-buffer) 'comment-block 'identity nil t))))
395 ;;;; Diary Sexp
397 (ert-deftest test-org-element/diary-sexp-parser ()
398 "Test `diary-sexp' parser."
399 ;; Standard test.
400 (should
401 (eq 'diary-sexp
402 (org-test-with-temp-text
403 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old"
404 (org-element-type (org-element-at-point)))))
405 ;; Diary sexp must live at beginning of line
406 (should-not
407 (eq 'diary-sexp
408 (org-test-with-temp-text " %%(org-bbdb-anniversaries)"
409 (org-element-type (org-element-at-point))))))
412 ;;;; Drawer
414 (ert-deftest test-org-element/drawer-parser ()
415 "Test `drawer' parser."
416 ;; Standard test.
417 (should
418 (let ((org-drawers '("TEST")))
419 (org-test-with-temp-text ":TEST:\nText\n:END:"
420 (org-element-map (org-element-parse-buffer) 'drawer 'identity))))
421 ;; Do not mix regular drawers and property drawers.
422 (should-not
423 (let ((org-drawers '("PROPERTIES")))
424 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:"
425 (org-element-map
426 (org-element-parse-buffer) 'drawer 'identity nil t))))
427 ;; Ignore incomplete drawer.
428 (should-not
429 (let ((org-drawers '("TEST")))
430 (org-test-with-temp-text ":TEST:"
431 (org-element-map
432 (org-element-parse-buffer) 'drawer 'identity nil t)))))
435 ;;;; Dynamic Block
437 (ert-deftest test-org-element/dynamic-block-parser ()
438 "Test `dynamic-block' parser."
439 ;; Standard test.
440 (should
441 (org-test-with-temp-text
442 "#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
443 (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
444 ;; Ignore case.
445 (should
446 (org-test-with-temp-text
447 "#+begin: myblock :param1 val1 :param2 val2\nText\n#+end:"
448 (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
449 ;; Ignore incomplete block.
450 (should-not
451 (org-test-with-temp-text "#+BEGIN: myblock :param1 val1 :param2 val2"
452 (org-element-map
453 (org-element-parse-buffer) 'dynamic-block 'identity nil t))))
456 ;;;; Entity
458 (ert-deftest test-org-element/entity-parser ()
459 "Test `entity' parser."
460 ;; Without brackets.
461 (should
462 (org-test-with-temp-text "\\sin"
463 (org-element-map (org-element-parse-buffer) 'entity 'identity)))
464 ;; With brackets.
465 (should
466 (org-element-property
467 :use-brackets-p
468 (org-test-with-temp-text "\\alpha{}text"
469 (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
470 ;; User-defined entity.
471 (should
472 (equal
473 (org-element-property
474 :name
475 (let ((org-entities-user
476 '(("test" "test" nil "test" "test" "test" "test"))))
477 (org-test-with-temp-text "\\test"
478 (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
479 "test"))
480 ;; Special case: entity at the end of a container.
481 (should
482 (eq 'entity
483 (org-test-with-temp-text "*\\alpha \\beta*"
484 (search-forward "be")
485 (org-element-type (org-element-context))))))
488 ;;;; Example Block
490 (ert-deftest test-org-element/example-block-parser ()
491 "Test `example-block' parser."
492 ;; Standard test.
493 (should
494 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
495 (org-element-map (org-element-parse-buffer) 'example-block 'identity)))
496 ;; Ignore incomplete block.
497 (should-not
498 (eq 'example-block
499 (org-test-with-temp-text "#+BEGIN_EXAMPLE"
500 (org-element-type (org-element-at-point)))))
501 ;; Properly un-escape code.
502 (should
503 (equal "* Headline\n #+keyword\nText\n"
504 (org-test-with-temp-text
505 "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText\n#+END_EXAMPLE"
506 (org-element-property :value (org-element-at-point)))))
507 ;; Nil `org-src-preserve-indentation': Remove maximum common
508 ;; indentation.
509 (should
510 (equal " L1\nL2\n"
511 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n L1\n L2\n#+END_EXAMPLE"
512 (let ((org-src-preserve-indentation nil))
513 (org-element-property :value (org-element-at-point))))))
514 ;; Non-nil `org-src-preserve-indentation': Remove block indentation
515 ;; only, unless block contents are less indented than block
516 ;; boundaries.
517 (should
518 (equal " L1\nL2\n"
519 (org-test-with-temp-text " #+BEGIN_EXAMPLE\n L1\n L2\n #+END_EXAMPLE"
520 (let ((org-src-preserve-indentation t))
521 (org-element-property :value (org-element-at-point))))))
522 (should
523 (equal
524 " L1\n L2\n"
525 (org-test-with-temp-text " #+BEGIN_EXAMPLE\n L1\n L2\n #+END_EXAMPLE"
526 (let ((org-src-preserve-indentation t))
527 (org-element-property :value (org-element-at-point)))))))
529 (ert-deftest test-org-element/block-switches ()
530 "Test `example-block' and `src-block' switches parsing."
531 (let ((org-coderef-label-format "(ref:%s)"))
532 ;; 1. Test "-i" switch.
533 (should-not
534 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
535 (org-element-property :preserve-indent (org-element-at-point))))
536 (should
537 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
538 (org-element-property :preserve-indent (org-element-at-point))))
539 (should-not
540 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
541 (org-element-property :preserve-indent (org-element-at-point))))
542 (should
543 (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
544 (org-element-property :preserve-indent (org-element-at-point))))
545 ;; 2. "-n -r -k" combination should number lines, retain labels but
546 ;; not use them in coderefs.
547 (should
548 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
549 (let ((element (org-element-at-point)))
550 (and (org-element-property :number-lines element)
551 (org-element-property :retain-labels element)
552 (not (org-element-property :use-labels element))))))
553 (should
554 (org-test-with-temp-text
555 "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
556 (let ((element (org-element-at-point)))
557 (and (org-element-property :number-lines element)
558 (org-element-property :retain-labels element)
559 (not (org-element-property :use-labels element))))))
560 ;; 3. "-n -r" combination should number-lines remove labels and not
561 ;; use them in coderefs.
562 (should
563 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
564 (let ((element (org-element-at-point)))
565 (and (org-element-property :number-lines element)
566 (not (org-element-property :retain-labels element))
567 (not (org-element-property :use-labels element))))))
568 (should
569 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
570 (let ((element (org-element-at-point)))
571 (and (org-element-property :number-lines element)
572 (not (org-element-property :retain-labels element))
573 (not (org-element-property :use-labels element))))))
574 ;; 4. "-n" or "+n" should number lines, retain labels and use them
575 ;; in coderefs.
576 (should
577 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
578 (let ((element (org-element-at-point)))
579 (and (org-element-property :number-lines element)
580 (org-element-property :retain-labels element)
581 (org-element-property :use-labels element)))))
582 (should
583 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
584 (let ((element (org-element-at-point)))
585 (and (org-element-property :number-lines element)
586 (org-element-property :retain-labels element)
587 (org-element-property :use-labels element)))))
588 (should
589 (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
590 (let ((element (org-element-at-point)))
591 (and (org-element-property :number-lines element)
592 (org-element-property :retain-labels element)
593 (org-element-property :use-labels element)))))
594 (should
595 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
596 (let ((element (org-element-at-point)))
597 (and (org-element-property :number-lines element)
598 (org-element-property :retain-labels element)
599 (org-element-property :use-labels element)))))
600 ;; 5. No switch should not number lines, but retain labels and use
601 ;; them in coderefs.
602 (should
603 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
604 (let ((element (org-element-at-point)))
605 (and (not (org-element-property :number-lines element))
606 (org-element-property :retain-labels element)
607 (org-element-property :use-labels element)))))
608 (should
609 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
610 (let ((element (org-element-at-point)))
611 (and (not (org-element-property :number-lines element))
612 (org-element-property :retain-labels element)
613 (org-element-property :use-labels element)))))
614 ;; 6. "-r" switch only: do not number lines, remove labels, and
615 ;; don't use labels in coderefs.
616 (should
617 (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
618 (let ((element (org-element-at-point)))
619 (and (not (org-element-property :number-lines element))
620 (not (org-element-property :retain-labels element))
621 (not (org-element-property :use-labels element))))))
622 (should
623 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
624 (let ((element (org-element-at-point)))
625 (and (not (org-element-property :number-lines element))
626 (not (org-element-property :retain-labels element))
627 (not (org-element-property :use-labels element))))))
628 ;; 7. Recognize coderefs with user-defined syntax.
629 (should
630 (equal
631 "[ref:%s]"
632 (org-test-with-temp-text
633 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
634 (org-element-property :label-fmt (org-element-at-point)))))
635 (should
636 (equal
637 "[ref:%s]"
638 (org-test-with-temp-text
639 "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
640 (org-element-property :label-fmt (org-element-at-point)))))))
643 ;;;; Export Block
645 (ert-deftest test-org-element/export-block-parser ()
646 "Test `export-block' parser."
647 ;; Standard test.
648 (should
649 (org-test-with-temp-text "#+BEGIN_LATEX\nText\n#+END_LATEX"
650 (org-element-map
651 (let ((org-element-block-name-alist
652 '(("LATEX" . org-element-export-block-parser))))
653 (org-element-parse-buffer))
654 'export-block 'identity)))
655 ;; Ignore case.
656 (should
657 (org-test-with-temp-text "#+begin_latex\nText\n#+end_latex"
658 (org-element-map
659 (let ((org-element-block-name-alist
660 '(("LATEX" . org-element-export-block-parser))))
661 (org-element-parse-buffer))
662 'export-block 'identity)))
663 ;; Ignore incomplete block.
664 (should-not
665 (org-test-with-temp-text "#+BEGIN_LATEX"
666 (org-element-map
667 (let ((org-element-block-name-alist
668 '(("LATEX" . org-element-export-block-parser))))
669 (org-element-parse-buffer))
670 'export-block 'identity nil t))))
673 ;;;; Export Snippet
675 (ert-deftest test-org-element/export-snippet-parser ()
676 "Test `export-snippet' parser."
677 (should
678 (equal
679 '("back-end" . "contents")
680 (org-test-with-temp-text "@@back-end:contents@@"
681 (org-element-map
682 (org-element-parse-buffer) 'export-snippet
683 (lambda (snippet) (cons (org-element-property :back-end snippet)
684 (org-element-property :value snippet)))
685 nil t)))))
688 ;;;; Fixed Width
690 (ert-deftest test-org-element/fixed-width ()
691 "Test fixed-width area parsing."
692 ;; Preserve indentation.
693 (should
694 (org-test-with-temp-text ": no blank\n: one blank"
695 (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
696 ;; Fixed-width with empty lines.
697 (should
698 (org-test-with-temp-text ": first part\n:\n: \n: second part"
699 (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
700 ;; Parse indented fixed-width markers.
701 (should
702 (org-test-with-temp-text "Text\n : no blank\n : one blank"
703 (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
704 ;; Distinguish fixed-width areas within a list and outside of it.
705 (should
706 (= 2
707 (length
708 (org-test-with-temp-text "
709 - Item
710 : fixed-width inside
711 : fixed-width outside"
712 (org-element-map
713 (org-element-parse-buffer) 'fixed-width 'identity))))))
716 ;;;; Footnote Definition
718 (ert-deftest test-org-element/footnote-definition-parser ()
719 "Test `footnote-definition' parser."
720 (should
721 (org-test-with-temp-text "[fn:1] Definition"
722 (org-element-map (org-element-parse-buffer) 'footnote-definition
723 'identity nil t)))
724 ;; Footnote with more contents
725 (should
726 (= 29
727 (org-element-property
728 :end
729 (org-test-with-temp-text "[fn:1] Definition\n\n| a | b |"
730 (org-element-map (org-element-parse-buffer) 'footnote-definition
731 'identity nil t)))))
732 ;; Footnote starting with special syntax.
733 (should-not
734 (org-test-with-temp-text "[fn:1] - no item"
735 (org-element-map (org-element-parse-buffer) 'item 'identity)))
736 ;; Correctly handle footnote starting with an empty line.
737 (should
738 (= 9
739 (org-test-with-temp-text "[fn:1]\n\n Body"
740 (org-element-property :contents-begin
741 (org-element-at-point))))))
744 ;;;; Footnotes Reference.
746 (ert-deftest test-org-element/footnote-reference-parser ()
747 "Test `footnote-reference' parser."
748 ;; 1. Parse a standard reference.
749 (org-test-with-temp-text "Text[fn:label]"
750 (should
751 (org-element-map
752 (org-element-parse-buffer) 'footnote-reference 'identity)))
753 ;; 2. Parse a normalized reference.
754 (org-test-with-temp-text "Text[1]"
755 (should
756 (org-element-map
757 (org-element-parse-buffer) 'footnote-reference 'identity)))
758 ;; 3. Parse an inline reference.
759 (org-test-with-temp-text "Text[fn:test:def]"
760 (should
761 (org-element-map
762 (org-element-parse-buffer) 'footnote-reference 'identity)))
763 ;; 4. Parse an anonymous reference.
764 (org-test-with-temp-text "Text[fn::def]"
765 (should
766 (org-element-map
767 (org-element-parse-buffer) 'footnote-reference 'identity)))
768 ;; 5. Parse nested footnotes.
769 (org-test-with-temp-text "Text[fn::def [fn:label]]"
770 (should
771 (= 2
772 (length
773 (org-element-map
774 (org-element-parse-buffer) 'footnote-reference 'identity)))))
775 ;; 6. Parse adjacent footnotes.
776 (org-test-with-temp-text "Text[fn:label1][fn:label2]"
777 (should
778 (= 2
779 (length
780 (org-element-map
781 (org-element-parse-buffer) 'footnote-reference 'identity)))))
782 ;; 7. Only properly closed footnotes are recognized as such.
783 (org-test-with-temp-text "Text[fn:label"
784 (should-not
785 (org-element-map
786 (org-element-parse-buffer) 'footnote-reference 'identity))))
789 ;;;; Headline
791 (ert-deftest test-org-element/headline-quote-keyword ()
792 "Test QUOTE keyword recognition."
793 ;; Reference test.
794 (org-test-with-temp-text "* Headline"
795 (let ((org-quote-string "QUOTE"))
796 (should-not (org-element-property :quotedp (org-element-at-point)))))
797 ;; Standard position.
798 (org-test-with-temp-text "* QUOTE Headline"
799 (let* ((org-quote-string "QUOTE")
800 (headline (org-element-at-point)))
801 (should (org-element-property :quotedp headline))
802 ;; Test removal from raw value.
803 (should (equal (org-element-property :raw-value headline) "Headline"))))
804 ;; Case sensitivity.
805 (org-test-with-temp-text "* QUOTE Headline"
806 (let* ((org-quote-string "Quote")
807 (headline (org-element-at-point)))
808 (should-not (org-element-property :quotedp headline))
809 (should (equal (org-element-property :raw-value headline)
810 "QUOTE Headline"))))
811 ;; With another keyword.
812 (org-test-with-temp-text "* TODO QUOTE Headline"
813 (let* ((org-quote-string "QUOTE")
814 (org-todo-keywords '((sequence "TODO" "DONE")))
815 (headline (org-element-at-point)))
816 (should (org-element-property :quotedp headline))
817 (should (equal (org-element-property :raw-value headline) "Headline"))))
818 ;; With the keyword only.
819 (org-test-with-temp-text "* QUOTE"
820 (let* ((org-quote-string "QUOTE")
821 (headline (org-element-at-point)))
822 (should (org-element-property :quotedp headline))
823 (should (equal (org-element-property :raw-value headline) "")))))
825 (ert-deftest test-org-element/headline-comment-keyword ()
826 "Test COMMENT keyword recognition."
827 ;; Reference test.
828 (org-test-with-temp-text "* Headline"
829 (let ((org-comment-string "COMMENT"))
830 (should-not (org-element-property :commentedp (org-element-at-point)))))
831 ;; Standard position.
832 (org-test-with-temp-text "* COMMENT Headline"
833 (let ((org-comment-string "COMMENT")
834 (headline (org-element-at-point)))
835 (should (org-element-property :commentedp headline))
836 (should (equal (org-element-property :raw-value headline) "Headline"))))
837 ;; Case sensitivity.
838 (org-test-with-temp-text "* COMMENT Headline"
839 (let* ((org-comment-string "Comment")
840 (headline (org-element-at-point)))
841 (should-not (org-element-property :commentedp headline))
842 (should (equal (org-element-property :raw-value headline)
843 "COMMENT Headline"))))
844 ;; With another keyword.
845 (org-test-with-temp-text "* TODO COMMENT Headline"
846 (let* ((org-comment-string "COMMENT")
847 (org-todo-keywords '((sequence "TODO" "DONE")))
848 (headline (org-element-at-point)))
849 (should (org-element-property :commentedp headline))
850 (should (equal (org-element-property :raw-value headline) "Headline"))))
851 ;; With the keyword only.
852 (org-test-with-temp-text "* COMMENT"
853 (let* ((org-comment-string "COMMENT")
854 (headline (org-element-at-point)))
855 (should (org-element-property :commentedp headline))
856 (should (equal (org-element-property :raw-value headline) "")))))
858 (ert-deftest test-org-element/headline-archive-tag ()
859 "Test ARCHIVE tag recognition."
860 ;; Reference test.
861 (org-test-with-temp-text "* Headline"
862 (let ((org-archive-tag "ARCHIVE"))
863 (should-not (org-element-property :archivedp (org-element-at-point)))))
864 ;; Single tag.
865 (org-test-with-temp-text "* Headline :ARCHIVE:"
866 (let ((org-archive-tag "ARCHIVE"))
867 (let ((headline (org-element-at-point)))
868 (should (org-element-property :archivedp headline))
869 ;; Test tag removal.
870 (should-not (org-element-property :tags headline))))
871 (let ((org-archive-tag "Archive"))
872 (should-not (org-element-property :archivedp (org-element-at-point)))))
873 ;; Multiple tags.
874 (org-test-with-temp-text "* Headline :test:ARCHIVE:"
875 (let ((org-archive-tag "ARCHIVE"))
876 (let ((headline (org-element-at-point)))
877 (should (org-element-property :archivedp headline))
878 ;; Test tag removal.
879 (should (equal (org-element-property :tags headline) '("test")))))))
881 (ert-deftest test-org-element/headline-properties ()
882 "Test properties from property drawer."
883 ;; All properties from property drawer have their symbol upper
884 ;; cased.
885 (should
886 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
887 (org-element-property :FOO (org-element-at-point))))
888 (should-not
889 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
890 (org-element-property :foo (org-element-at-point)))))
893 ;;;; Horizontal Rule
895 (ert-deftest test-org-element/horizontal-rule-parser ()
896 "Test `horizontal-rule' parser."
897 ;; Standard.
898 (should
899 (org-test-with-temp-text "-----"
900 (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
901 ;; Indented.
902 (should
903 (org-test-with-temp-text " -----"
904 (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
905 ;; Hyphen must be alone on the line.
906 (should-not
907 (org-test-with-temp-text "-----wrong"
908 (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
909 ;; 4 hyphens is too small.
910 (should-not
911 (org-test-with-temp-text "----"
912 (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity))))
915 ;;;; Inline Babel Call
917 (ert-deftest test-org-element/inline-babel-call-parser ()
918 "Test `inline-babel-call' parser."
919 (should
920 (org-test-with-temp-text "call_test()"
921 (org-element-map
922 (org-element-parse-buffer) 'inline-babel-call 'identity))))
925 ;;;; Inline Src Block
927 (ert-deftest test-org-element/inline-src-block-parser ()
928 "Test `inline-src-block' parser."
929 (should
930 (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
931 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
932 ;; Test parsing at the beginning of an item.
933 (should
934 (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
935 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity))))
938 ;;;; Inlinetask
940 (ert-deftest test-org-element/inlinetask-parser ()
941 "Test `inlinetask' parser."
942 (when (featurep 'org-inlinetask)
943 (let ((org-inlinetask-min-level 15))
944 ;; 1. Regular inlinetask.
945 (should
946 (org-test-with-temp-text
947 "*************** Task\nTest\n*************** END"
948 (org-element-map (org-element-parse-buffer) 'inlinetask 'identity)))
949 ;; 2. Degenerate inlinetask.
950 (should
951 (org-test-with-temp-text "*************** Task"
952 (org-element-map (org-element-parse-buffer) 'inlinetask 'identity)))
953 ;; TODO keyword.
954 (should
955 (equal
956 "TODO"
957 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
958 (org-test-with-temp-text "*************** TODO Task"
959 (org-element-property
960 :todo-keyword
961 (org-element-map (org-element-parse-buffer) 'inlinetask
962 'identity nil t))))))
963 ;; Planning info.
964 (should
965 (equal
966 "2012-03-29 thu."
967 (org-test-with-temp-text "
968 *************** Task
969 DEADLINE: <2012-03-29 thu.>"
970 (org-element-property
971 :deadline
972 (org-element-map (org-element-parse-buffer) 'inlinetask 'identity nil t)))))
973 ;; Priority.
974 (should
975 (equal
977 (org-test-with-temp-text "
978 *************** [#A] Task"
979 (org-element-property
980 :priority
981 (org-element-map
982 (org-element-parse-buffer) 'inlinetask 'identity nil t)))))
983 ;; Tags.
984 (should
985 (equal
986 '("test")
987 (org-test-with-temp-text "
988 *************** Task :test:"
989 (org-element-property
990 :tags
991 (org-element-map
992 (org-element-parse-buffer) 'inlinetask 'identity nil t)))))
993 ;; Regular properties are accessed through upper case keywords.
994 (should
995 (org-test-with-temp-text "
996 *************** Task
997 :PROPERTIES:
998 :foo: bar
999 :END:
1000 *************** END"
1001 (forward-line)
1002 (org-element-property :FOO (org-element-at-point))))
1003 (should-not
1004 (org-test-with-temp-text "
1005 *************** Task
1006 :PROPERTIES:
1007 :foo: bar
1008 :END:
1009 *************** END"
1010 (forward-line)
1011 (org-element-property :foo (org-element-at-point)))))))
1014 ;;;; Italic
1016 (ert-deftest test-org-element/italic-parser ()
1017 "Test `italic' parser."
1018 ;; Regular test.
1019 (should
1020 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1021 (org-test-with-temp-text "/italic/"
1022 (org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
1023 ;; Multi-line markup.
1024 (should
1025 (equal
1026 (org-element-contents
1027 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1028 (org-test-with-temp-text "/first line\nsecond line/"
1029 (org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
1030 '("first line\nsecond line"))))
1033 ;;;; Item
1035 (ert-deftest test-org-element/item-parser ()
1036 "Test `item' parser."
1037 ;; Standard test.
1038 (should
1039 (org-test-with-temp-text "- item"
1040 (org-element-map (org-element-parse-buffer) 'item 'identity)))
1041 ;; Counter.
1042 (should
1043 (= 6
1044 (org-element-property
1045 :counter
1046 (org-test-with-temp-text "6. [@6] item"
1047 (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
1048 ;; Tag
1049 (should
1050 (equal
1051 '("tag")
1052 (org-element-property
1053 :tag
1054 (org-test-with-temp-text "- tag :: description"
1055 (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
1056 ;; No tags in ordered lists.
1057 (should-not
1058 (org-element-property
1059 :tag
1060 (org-test-with-temp-text "1. tag :: description"
1061 (org-element-map (org-element-parse-buffer) 'item 'identity nil t))))
1062 ;; Check-boxes
1063 (should
1064 (equal
1065 '(trans on off)
1066 (org-test-with-temp-text "
1067 - [-] item 1
1068 - [X] item 1.1
1069 - [ ] item 1.2"
1070 (org-element-map
1071 (org-element-parse-buffer) 'item
1072 (lambda (item) (org-element-property :checkbox item))))))
1073 ;; Item starting with special syntax.
1074 (should
1075 (equal '(("- item"))
1076 (org-test-with-temp-text "- - item"
1077 (org-element-map
1078 (org-element-parse-buffer) 'paragraph 'org-element-contents))))
1079 ;; Block in an item: ignore indentation within the block.
1080 (should
1081 (org-test-with-temp-text "- item\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1082 (forward-char)
1083 (goto-char (org-element-property :end (org-element-at-point)))
1084 (eobp))))
1087 ;;;; Keyword
1089 (ert-deftest test-org-element/keyword-parser ()
1090 "Test `keyword' parser."
1091 ;; Standard test.
1092 (should
1093 (org-test-with-temp-text "#+KEYWORD: value"
1094 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1095 ;; Keywords are case-insensitive.
1096 (should
1097 (org-test-with-temp-text "#+keyword: value"
1098 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1099 ;; Affiliated keywords are not keywords.
1100 (should-not
1101 (org-test-with-temp-text "#+NAME: value
1102 Paragraph"
1103 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1104 ;; Do not mix keywords with Babel calls and dynamic blocks.
1105 (should-not
1106 (org-test-with-temp-text "#+CALL: fun()"
1107 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1108 (should-not
1109 (org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
1110 (org-element-map (org-element-parse-buffer) 'keyword 'identity))))
1113 ;;;; Latex Environment
1115 (ert-deftest test-org-element/latex-environment-parser ()
1116 "Test `latex-environment' parser."
1117 (should
1118 (org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
1119 (org-element-map (org-element-parse-buffer) 'latex-environment 'identity)))
1120 ;; Allow nested environments.
1121 (should
1122 (equal
1123 "\\begin{outer}
1124 \\begin{inner}
1125 e^{i\\pi}+1=0
1126 \\end{inner}
1127 \\end{outer}"
1128 (org-test-with-temp-text "
1129 \\begin{outer}
1130 \\begin{inner}
1131 e^{i\\pi}+1=0
1132 \\end{inner}
1133 \\end{outer}"
1134 (org-element-property
1135 :value
1136 (org-element-map
1137 (org-element-parse-buffer) 'latex-environment 'identity nil t)))))
1138 ;; Allow environments with options and arguments.
1139 (should
1140 (eq 'latex-environment
1141 (org-test-with-temp-text
1142 "\\begin{theorem}[Euler]\ne^{i\\pi}+1=0\n\\end{theorem}"
1143 (org-element-type (org-element-at-point)))))
1144 (should
1145 (eq 'latex-environment
1146 (org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
1147 (org-element-type (org-element-at-point)))))
1148 (should-not
1149 (eq 'latex-environment
1150 (org-test-with-temp-text "\\begin{env}{arg} something\nvalue\n\\end{env}"
1151 (org-element-type (org-element-at-point))))))
1154 ;;;; Latex Fragment
1156 (ert-deftest test-org-element/latex-fragment-parser ()
1157 "Test `latex-fragment' parser."
1158 (should
1159 (org-test-with-temp-text "$a$"
1160 (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
1161 (should
1162 (org-test-with-temp-text "$$a$$"
1163 (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
1164 (should
1165 (org-test-with-temp-text "\\(a\\)"
1166 (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
1167 (should
1168 (org-test-with-temp-text "\\[a\\]"
1169 (org-element-map
1170 (org-element-parse-buffer) 'latex-fragment 'identity)))
1171 ;; Test fragment at the beginning of an item.
1172 (should
1173 (eq 'latex-fragment
1174 (org-test-with-temp-text "- $x$"
1175 (progn (search-forward "$")
1176 (org-element-type (org-element-context)))))))
1179 ;;;; Line Break
1181 (ert-deftest test-org-element/line-break-parser ()
1182 "Test `line-break' parser."
1183 ;; Regular test.
1184 (should
1185 (org-test-with-temp-text "Text \\\\"
1186 (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
1187 ;; Line break with trailing white spaces.
1188 (should
1189 (org-test-with-temp-text "Text \\\\ "
1190 (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
1191 ;; Three backslashes are too much.
1192 (should-not
1193 (org-test-with-temp-text "Text \\\\\\"
1194 (org-element-map (org-element-parse-buffer) 'line-break 'identity))))
1197 ;;;; Link
1199 (ert-deftest test-org-element/link-parser ()
1200 "Test `link' parser."
1201 ;; Radio target.
1202 (should
1203 (equal
1204 "radio"
1205 (org-test-with-temp-text "A radio link"
1206 (org-element-property
1207 :type
1208 (org-element-map
1209 (let ((org-target-link-regexp "radio")) (org-element-parse-buffer))
1210 'link 'identity nil t)))))
1211 ;; Standard link.
1213 ;; ... with description.
1214 (should
1215 (equal
1216 '("Orgmode.org")
1217 (org-test-with-temp-text "[[http://orgmode.org][Orgmode.org]]"
1218 (org-element-contents
1219 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1220 ;; ... without description.
1221 (should
1222 (equal
1223 "http"
1224 (org-test-with-temp-text "[[http://orgmode.org]]"
1225 (org-element-property
1226 :type
1227 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1228 ;; ... with expansion.
1229 (should
1230 (equal
1231 "//orgmode.org/worg"
1232 (org-test-with-temp-text "[[Org:worg]]"
1233 (let ((org-link-abbrev-alist '(("Org" . "http://orgmode.org/"))))
1234 (org-element-property
1235 :path
1236 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))))))
1237 ;; ... with translation.
1238 (should
1239 (equal
1240 "127.0.0.1"
1241 (org-test-with-temp-text "[[http://orgmode.org]]"
1242 (flet ((link-translate (type path) (cons type "127.0.0.1")))
1243 (let ((org-link-translation-function 'link-translate))
1244 (org-element-property
1245 :path
1246 (org-element-map (org-element-parse-buffer) 'link
1247 'identity nil t)))))))
1248 ;; ... id link.
1249 (should
1250 (equal
1251 "id"
1252 (org-test-with-temp-text "[[id:aaaa]]"
1253 (org-element-property
1254 :type
1255 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1256 ;; ... custom-id link.
1257 (should
1258 (equal
1259 "custom-id"
1260 (org-test-with-temp-text "[[#some-id]]"
1261 (org-element-property
1262 :type
1263 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1264 ;; ... coderef link.
1265 (should
1266 (equal
1267 "coderef"
1268 (org-test-with-temp-text "[[(reference)]]"
1269 (org-element-property
1270 :type
1271 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1272 ;; ... fuzzy link.
1273 (should
1274 (equal
1275 "fuzzy"
1276 (org-test-with-temp-text "[[target-or-title]]"
1277 (org-element-property
1278 :type
1279 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1280 ;; ... file-type link with search option.
1281 (should
1282 (equal
1283 '(("file" "projects.org" "*task title"))
1284 (org-test-with-temp-text "[[file:projects.org::*task title]]"
1285 (org-element-map (org-element-parse-buffer) 'link
1286 (lambda (l) (list (org-element-property :type l)
1287 (org-element-property :path l)
1288 (org-element-property :search-option l)))))))
1289 ;; ... file-type link with application.
1290 (should
1291 (equal
1292 '(("file" "projects.org" "docview"))
1293 (org-test-with-temp-text "[[docview:projects.org]]"
1294 (org-element-map (org-element-parse-buffer) 'link
1295 (lambda (l) (list (org-element-property :type l)
1296 (org-element-property :path l)
1297 (org-element-property :application l)))))))
1298 ;; Plain link.
1299 (should
1300 (org-test-with-temp-text "A link: http://orgmode.org"
1301 (org-element-map (org-element-parse-buffer) 'link 'identity)))
1302 ;; Angular link.
1303 (should
1304 (org-test-with-temp-text "A link: <http://orgmode.org>"
1305 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))
1306 ;; Link abbreviation.
1307 (should
1308 (equal "http"
1309 (org-test-with-temp-text
1310 "#+LINK: orgmode http://www.orgmode.org/\n[[orgmode:#docs]]"
1311 (progn (org-mode-restart)
1312 (goto-char (1- (point-max)))
1313 (org-element-property :type (org-element-context))))))
1314 ;; Link abbreviation in a secondary string.
1315 (should
1316 (equal "http"
1317 (org-test-with-temp-text
1318 "#+LINK: orgmode http://www.orgmode.org/\n* H [[orgmode:#docs]]"
1319 (progn (org-mode-restart)
1320 (org-element-map (org-element-parse-buffer) 'link
1321 (lambda (link) (org-element-property :type link))
1322 nil t nil t)))))
1323 ;; Plain links are allowed as description of regular links.
1324 (should
1325 (equal "file"
1326 (org-test-with-temp-text "[[http://orgmode.org][file:unicorn.jpg]]"
1327 (search-forward "file:")
1328 (org-element-property :type (org-element-context))))))
1331 ;;;; Macro
1333 (ert-deftest test-org-element/macro-parser ()
1334 "Test `macro' parser."
1335 ;; Without arguments.
1336 (should
1337 (org-test-with-temp-text "{{{macro}}}"
1338 (org-element-map (org-element-parse-buffer) 'macro 'identity)))
1339 ;; With arguments.
1340 (should
1341 (org-test-with-temp-text "{{{macro(arg1,arg2)}}}"
1342 (org-element-map (org-element-parse-buffer) 'macro 'identity)))
1343 ;; Properly handle protected commas in arguments...
1344 (should
1345 (= 2
1346 (length
1347 (org-test-with-temp-text "{{{macro(arg1\\,arg1,arg2)}}}"
1348 (org-element-property :args (org-element-context))))))
1349 ;; ... even when last argument ends with a protected comma.
1350 (should
1351 (equal '("C-,")
1352 (org-test-with-temp-text "{{{macro(C-\\,)}}}"
1353 (org-element-property :args (org-element-context)))))
1354 ;; Allow to escape escaping character.
1355 (should
1356 (equal '("C-\\" "")
1357 (org-test-with-temp-text "{{{macro(C-\\\\,)}}}"
1358 (org-element-property :args (org-element-context)))))
1359 ;; No need to escape backslashes elsewhere.
1360 (should
1361 (equal '("\\")
1362 (org-test-with-temp-text "{{{macro(\\)}}}"
1363 (org-element-property :args (org-element-context))))))
1366 ;;;; Node Property
1368 (ert-deftest test-org-element/node-property ()
1369 "Test `node-property' parser."
1370 ;; Standard test.
1371 (should
1372 (equal '("abc" "value")
1373 (org-test-with-temp-text ":PROPERTIES:\n:abc: value\n:END:"
1374 (progn (forward-line)
1375 (let ((element (org-element-at-point)))
1376 (list (org-element-property :key element)
1377 (org-element-property :value element)))))))
1378 ;; Value should be trimmed.
1379 (should
1380 (equal "value"
1381 (org-test-with-temp-text ":PROPERTIES:\n:abc: value \n:END:"
1382 (progn (forward-line)
1383 (let ((element (org-element-at-point)))
1384 (org-element-property :value element))))))
1385 ;; A node property requires to be wrapped within a property drawer.
1386 (should-not
1387 (eq 'node-property
1388 (org-test-with-temp-text ":abc: value"
1389 (org-element-type (org-element-at-point))))))
1392 ;;;; Paragraph
1394 (ert-deftest test-org-element/paragraph-parser ()
1395 "Test `paragraph' parser."
1396 ;; Standard test.
1397 (should
1398 (org-test-with-temp-text "Paragraph"
1399 (org-element-map (org-element-parse-buffer) 'paragraph 'identity nil t)))
1400 ;; Property find end of a paragraph stuck to another element.
1401 (should
1402 (eq ?#
1403 (org-test-with-temp-text "Paragraph\n# Comment"
1404 (org-element-map
1405 (org-element-parse-buffer) 'paragraph
1406 (lambda (p) (char-after (org-element-property :end p)))
1407 nil t))))
1408 ;; Include ill-formed Keywords.
1409 (should
1410 (org-test-with-temp-text "#+wrong_keyword something"
1411 (org-element-map (org-element-parse-buffer) 'paragraph 'identity)))
1412 ;; Include incomplete-drawers.
1413 (should
1414 (let ((org-drawers '("TEST")))
1415 (org-test-with-temp-text ":TEST:\nParagraph"
1416 (let ((elem (org-element-at-point)))
1417 (and (eq (org-element-type elem) 'paragraph)
1418 (= (point-max) (org-element-property :end elem)))))))
1419 ;; Include non-existent drawers.
1420 (should
1421 (let ((org-drawers '("TEST")))
1422 (org-test-with-temp-text ":NONAME:"
1423 (org-element-map (org-element-parse-buffer) 'paragraph 'identity))))
1424 ;; Include incomplete blocks.
1425 (should
1426 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph"
1427 (let ((elem (org-element-at-point)))
1428 (and (eq (org-element-type elem) 'paragraph)
1429 (= (point-max) (org-element-property :end elem))))))
1430 ;; Include incomplete dynamic blocks.
1431 (should
1432 (org-test-with-temp-text "#+BEGIN: \nParagraph"
1433 (let ((elem (org-element-at-point)))
1434 (and (eq (org-element-type elem) 'paragraph)
1435 (= (point-max) (org-element-property :end elem))))))
1436 ;; Include incomplete latex environments.
1437 (should
1438 (org-test-with-temp-text "\begin{equation}\nParagraph"
1439 (let ((elem (org-element-at-point)))
1440 (and (eq (org-element-type elem) 'paragraph)
1441 (= (point-max) (org-element-property :end elem))))))
1442 ;; Do not steal affiliated keywords from container.
1443 (should
1444 (org-test-with-temp-text "#+ATTR_LATEX: test\n- item 1"
1445 (let ((elem (progn (search-forward "item") (org-element-at-point))))
1446 (and (eq (org-element-type elem) 'paragraph)
1447 (not (org-element-property :attr_latex elem))
1448 (/= (org-element-property :begin elem) 1))))))
1451 ;;;; Plain List
1453 (ert-deftest test-org-element/plain-list-parser ()
1454 "Test `plain-list' parser."
1455 (org-test-with-temp-text "- item"
1456 (should (org-element-map (org-element-parse-buffer) 'plain-list 'identity)))
1457 ;; Blank lines after the list only belong to outer plain list.
1458 (should
1459 (equal
1460 '(t t)
1461 (org-test-with-temp-text "
1462 - outer
1463 - inner
1465 Outside list"
1466 (let ((endings (org-element-map (org-element-parse-buffer) 'plain-list
1467 (lambda (pl) (org-element-property :end pl)))))
1468 (list
1469 ;; Move to ending of outer list.
1470 (progn (goto-char (car endings)) (looking-at "Outside list"))
1471 ;; Move to ending of inner list.
1472 (progn (goto-char (nth 1 endings)) (looking-at "^$")))))))
1473 ;; Correctly compute end of list if it doesn't end at a line
1474 ;; beginning.
1475 (should
1476 (org-test-with-temp-text "- list\n \n "
1477 (= (org-element-property :end (org-element-at-point)) (point-max)))))
1480 ;;;; Planning
1482 (ert-deftest test-org-element/planning-parser ()
1483 "Test `planning' parser."
1484 (should
1485 (equal "[2012-03-29 thu.]"
1486 (org-element-property
1487 :raw-value
1488 (org-element-property
1489 :closed
1490 (org-test-with-temp-text "CLOSED: [2012-03-29 thu.]"
1491 (org-element-at-point))))))
1492 (should
1493 (equal "<2012-03-29 thu.>"
1494 (org-element-property
1495 :raw-value
1496 (org-element-property
1497 :deadline
1498 (org-test-with-temp-text "DEADLINE: <2012-03-29 thu.>"
1499 (org-element-at-point))))))
1500 (should
1501 (equal "<2012-03-29 thu.>"
1502 (org-element-property
1503 :raw-value
1504 (org-element-property
1505 :scheduled
1506 (org-test-with-temp-text "SCHEDULED: <2012-03-29 thu.>"
1507 (org-element-at-point)))))))
1510 ;;;; Property Drawer
1512 (ert-deftest test-org-element/property-drawer ()
1513 "Test `property-drawer' parser."
1514 ;; Standard test.
1515 (should
1516 (let ((org-drawers '("PROPERTIES")))
1517 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:"
1518 (org-element-map
1519 (org-element-parse-buffer) 'property-drawer 'identity nil t))))
1520 ;; Do not mix property drawers and regular drawers.
1521 (should-not
1522 (let ((org-drawers '("TEST")))
1523 (org-test-with-temp-text ":TEST:\n:prop: value\n:END:"
1524 (org-element-map
1525 (org-element-parse-buffer) 'property-drawer 'identity nil t))))
1526 ;; Ignore incomplete drawer.
1527 (should-not
1528 (let ((org-drawers '("PROPERTIES")))
1529 (org-test-with-temp-text ":PROPERTIES:\n:prop: value"
1530 (org-element-map
1531 (org-element-parse-buffer) 'property-drawer 'identity nil t)))))
1534 ;;;; Quote Block
1536 (ert-deftest test-org-element/quote-block-parser ()
1537 "Test `quote-block' parser."
1538 ;; Regular test.
1539 (should
1540 (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
1541 (org-element-map (org-element-parse-buffer) 'quote-block 'identity)))
1542 ;; Ignore incomplete block.
1543 (should-not
1544 (org-test-with-temp-text "#+BEGIN_QUOTE"
1545 (org-element-map
1546 (org-element-parse-buffer) 'quote-block 'identity nil t))))
1549 ;;;; Quote Section
1551 (ert-deftest test-org-element/quote-section-parser ()
1552 "Test `quote-section' parser."
1553 (should
1554 (let ((org-quote-string "QUOTE"))
1555 (org-test-with-temp-text "* QUOTE Headline\nBody"
1556 (org-element-map (org-element-parse-buffer) 'quote-section 'identity))))
1557 (should-not
1558 (let ((org-quote-string "TEST"))
1559 (org-test-with-temp-text "* QUOTE Headline\nBody"
1560 (org-element-map (org-element-parse-buffer) 'quote-section 'identity)))))
1563 ;;;; Radio Target
1565 (ert-deftest test-org-element/radio-target-parser ()
1566 "Test `radio-target' parser."
1567 ;; Standard test.
1568 (should
1569 (org-test-with-temp-text "<<<radio>>>"
1570 (org-element-map (org-element-parse-buffer) 'radio-target 'identity)))
1571 ;; Radio targets with objects.
1572 (should
1573 (org-test-with-temp-text "<<<radio \\alpha>>>"
1574 (org-element-map (org-element-parse-buffer) 'radio-target 'identity))))
1577 ;;;; Section
1579 (ert-deftest test-org-element/section-parser ()
1580 "Test `section' parser."
1581 ;; Standard test.
1582 (should
1583 (org-test-with-temp-text "* Headline\nText"
1584 (org-element-map (org-element-parse-buffer) 'section 'identity)))
1585 ;; There's a section before the first headline.
1586 (should
1587 (org-test-with-temp-text "Text"
1588 (org-element-map (org-element-parse-buffer) 'section 'identity)))
1589 ;; A section cannot be empty.
1590 (should-not
1591 (org-test-with-temp-text "* Headline 1\n* Headline 2"
1592 (org-element-map (org-element-parse-buffer) 'section 'identity)))
1593 ;; A section doesn't contain sub-trees.
1594 (should-not
1595 (org-test-with-temp-text "* Head\nText\n** Sub-Head"
1596 (org-element-map
1597 (org-element-map (org-element-parse-buffer) 'section 'identity nil t)
1598 'headline 'identity))))
1601 ;;;; Special Block
1603 (ert-deftest test-org-element/special-block-parser ()
1604 "Test `special-block' parser."
1605 ;; Standard test.
1606 (should
1607 (equal "SPECIAL"
1608 (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
1609 (org-element-property :type (org-element-at-point)))))
1610 ;; Special blocks can contain paragraphs.
1611 (should
1612 (eq 'paragraph
1613 (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
1614 (forward-line)
1615 (org-element-type (org-element-at-point)))))
1616 ;; Ignore incomplete block.
1617 (should-not
1618 (eq 'special-block
1619 (org-test-with-temp-text "#+BEGIN_SPECIAL"
1620 (org-element-type (org-element-at-point)))))
1621 ;; Allow special characters in type.
1622 (should
1623 (equal '(special-block "SPECIAL*")
1624 (org-test-with-temp-text "#+BEGIN_SPECIAL*\nContents\n#+END_SPECIAL*"
1625 (let ((element (org-element-at-point)))
1626 (list (org-element-type element)
1627 (org-element-property :type element)))))))
1630 ;;;; Src Block
1632 (ert-deftest test-org-element/src-block-parser ()
1633 "Test `src-block' parser."
1634 ;; Regular tests.
1635 (should
1636 (org-test-with-temp-text "#+BEGIN_SRC org\nText\n#+END_SRC"
1637 (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
1638 ;; Ignore incomplete block.
1639 (should-not
1640 (org-test-with-temp-text "#+BEGIN_SRC"
1641 (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
1642 ;; Properly un-escape code.
1643 (should
1644 (equal "* Headline\n #+keyword\nText\n"
1645 (org-test-with-temp-text
1646 "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
1647 (org-element-property :value (org-element-at-point)))))
1648 ;; Nil `org-src-preserve-indentation': Remove maximum common
1649 ;; indentation.
1650 (should
1651 (equal " L1\nL2\n"
1652 (org-test-with-temp-text "#+BEGIN_SRC org\n L1\n L2\n#+END_SRC"
1653 (let ((org-src-preserve-indentation nil))
1654 (org-element-property :value (org-element-at-point))))))
1655 ;; Non-nil `org-src-preserve-indentation': Remove block indentation
1656 ;; only, unless block contents are less indented than block
1657 ;; boundaries.
1658 (should
1659 (equal " L1\nL2\n"
1660 (org-test-with-temp-text " #+BEGIN_SRC org\n L1\n L2\n #+END_SRC"
1661 (let ((org-src-preserve-indentation t))
1662 (org-element-property :value (org-element-at-point))))))
1663 (should
1664 (equal
1665 " L1\n L2\n"
1666 (org-test-with-temp-text " #+BEGIN_SRC org\n L1\n L2\n #+END_SRC"
1667 (let ((org-src-preserve-indentation t))
1668 (org-element-property :value (org-element-at-point)))))))
1671 ;;;; Statistics Cookie
1673 (ert-deftest test-org-element/statistics-cookie ()
1674 "Test `statistics-cookie' parser."
1675 ;; With numbers.
1676 (should
1677 (org-test-with-temp-text "[1/2]"
1678 (org-element-map (org-element-parse-buffer) 'statistics-cookie 'identity)))
1679 ;; With percents.
1680 (should
1681 (org-test-with-temp-text "[33%]"
1682 (org-element-map
1683 (org-element-parse-buffer) 'statistics-cookie 'identity))))
1686 ;;;; Strike Through
1688 (ert-deftest test-org-element/strike-through-parser ()
1689 "Test `strike-through' parser."
1690 ;; Regular test.
1691 (should
1692 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1693 (org-test-with-temp-text "+strike-through+"
1694 (org-element-map (org-element-parse-buffer) 'strike-through 'identity))))
1695 ;; Multi-line markup.
1696 (should
1697 (equal
1698 (org-element-contents
1699 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1700 (org-test-with-temp-text "+first line\nsecond line+"
1701 (org-element-map
1702 (org-element-parse-buffer) 'strike-through 'identity nil t))))
1703 '("first line\nsecond line"))))
1706 ;;;; Subscript
1708 (ert-deftest test-org-element/subscript-parser ()
1709 "Test `subscript' parser."
1710 ;; Without braces.
1711 (should
1712 (org-test-with-temp-text "a_b"
1713 (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
1714 ;; With braces.
1715 (should
1716 (org-test-with-temp-text "a_{b}"
1717 (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
1718 ;; Multiple subscripts in a paragraph.
1719 (should
1720 (= 2
1721 (org-test-with-temp-text "a_b and c_d"
1722 (length
1723 (org-element-map (org-element-parse-buffer) 'subscript 'identity))))))
1726 ;;;; Superscript
1728 (ert-deftest test-org-element/superscript-parser ()
1729 "Test `superscript' parser."
1730 ;; Without braces.
1731 (should
1732 (org-test-with-temp-text "a^b"
1733 (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
1734 ;; With braces.
1735 (should
1736 (org-test-with-temp-text "a^{b}"
1737 (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
1738 ;; Multiple superscript in a paragraph.
1739 (should
1740 (= 2
1741 (org-test-with-temp-text "a^b and c^d"
1742 (length
1743 (org-element-map
1744 (org-element-parse-buffer) 'superscript 'identity))))))
1747 ;;;; Table
1749 (ert-deftest test-org-element/table-parser ()
1750 "Test `table' parser."
1751 (should
1752 (org-test-with-temp-text "| a |"
1753 (org-element-map (org-element-parse-buffer) 'table 'identity)))
1754 ;; TBLFM keyword is case insensitive.
1755 (should
1756 (org-test-with-temp-text "| a |\n#+tblfm: test"
1757 (org-element-property
1758 :tblfm
1759 (org-element-map (org-element-parse-buffer) 'table 'identity nil t))))
1760 ;; Handle multiple TBLFM lines.
1761 (should
1762 (= 2
1763 (org-test-with-temp-text "| a |\n#+TBLFM: test1\n#+TBLFM: test2"
1764 (length (org-element-property
1765 :tblfm
1766 (org-element-map
1767 (org-element-parse-buffer) 'table 'identity nil t))))))
1768 ;; Do not error when parsing a table with trailing white spaces.
1769 (should (org-test-with-temp-text "| a |\n " (org-element-parse-buffer))))
1772 ;;;; Table Cell
1774 (ert-deftest test-org-element/table-cell-parser ()
1775 "Test `table-cell' parser."
1776 (should
1777 (org-test-with-temp-text "| a |"
1778 (org-element-map (org-element-parse-buffer) 'table-cell 'identity))))
1781 ;;;; Table Row
1783 (ert-deftest test-org-element/table-row-parser ()
1784 "Test `table-row' parser."
1785 (should
1786 (equal '(standard rule)
1787 (org-test-with-temp-text "| a |\n|---|"
1788 (org-element-map
1789 (org-element-parse-buffer) 'table-row
1790 (lambda (row) (org-element-property :type row)))))))
1793 ;;;; Target
1795 (ert-deftest test-org-element/target-parser ()
1796 "Test `target' parser."
1797 (should
1798 (org-test-with-temp-text "<<target>>"
1799 (org-element-map (org-element-parse-buffer) 'target 'identity))))
1802 ;;;; Timestamp
1804 (ert-deftest test-org-element/timestamp ()
1805 "Test `timestamp' parser."
1806 ;; Active timestamp.
1807 (should
1808 (org-test-with-temp-text "<2012-03-29 16:40>"
1809 (eq (org-element-property :type (org-element-context)) 'active)))
1810 (should-not
1811 (org-test-with-temp-text "<2012-03-29 Thu>"
1812 (let ((timestamp (org-element-context)))
1813 (or (org-element-property :hour-start timestamp)
1814 (org-element-property :minute-start timestamp)))))
1815 (should
1816 (equal '(2012 3 29 16 40)
1817 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
1818 (let ((object (org-element-context)))
1819 (list (org-element-property :year-start object)
1820 (org-element-property :month-start object)
1821 (org-element-property :day-start object)
1822 (org-element-property :hour-start object)
1823 (org-element-property :minute-start object))))))
1824 ;; Inactive timestamp.
1825 (should
1826 (org-test-with-temp-text "[2012-03-29 Thu 16:40]"
1827 (eq (org-element-property :type (org-element-context)) 'inactive)))
1828 ;; Time range.
1829 (should
1830 (equal '(2012 3 29 16 40 7 30)
1831 (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
1832 (let ((object (org-element-context)))
1833 (list (org-element-property :year-end object)
1834 (org-element-property :month-end object)
1835 (org-element-property :day-end object)
1836 (org-element-property :hour-end object)
1837 (org-element-property :minute-end object)
1838 (org-element-property :hour-start object)
1839 (org-element-property :minute-start object))))))
1840 (should
1841 (eq 'active-range
1842 (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
1843 (org-element-property :type (org-element-context)))))
1844 ;; Date range.
1845 (should
1846 (org-test-with-temp-text "[2012-03-29 Thu 16:40]--[2012-03-29 Thu 16:41]"
1847 (eq (org-element-property :type (org-element-context)) 'inactive-range)))
1848 (should-not
1849 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
1850 (let ((timestamp (org-element-context)))
1851 (or (org-element-property :hour-end timestamp)
1852 (org-element-property :minute-end timestamp)))))
1853 ;; With repeater, warning delay and both.
1854 (should
1855 (eq 'catch-up
1856 (org-test-with-temp-text "<2012-03-29 Thu ++1y>"
1857 (org-element-property :repeater-type (org-element-context)))))
1858 (should
1859 (eq 'first
1860 (org-test-with-temp-text "<2012-03-29 Thu --1y>"
1861 (org-element-property :warning-type (org-element-context)))))
1862 (should
1863 (equal '(cumulate all)
1864 (org-test-with-temp-text "<2012-03-29 Thu +1y -1y>"
1865 (let ((ts (org-element-context)))
1866 (list (org-element-property :repeater-type ts)
1867 (org-element-property :warning-type ts))))))
1868 ;; Timestamps are not planning elements.
1869 (should-not
1870 (org-test-with-temp-text "SCHEDULED: <2012-03-29 Thu 16:40>"
1871 (org-element-map (org-element-parse-buffer) 'timestamp 'identity))))
1874 ;;;; Underline
1876 (ert-deftest test-org-element/underline-parser ()
1877 "Test `underline' parser."
1878 ;; Regular test.
1879 (should
1880 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1881 (org-test-with-temp-text "_underline_"
1882 (org-element-map (org-element-parse-buffer) 'underline 'identity))))
1883 ;; Multi-line markup.
1884 (should
1885 (equal
1886 (org-element-contents
1887 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1888 (org-test-with-temp-text "_first line\nsecond line_"
1889 (org-element-map
1890 (org-element-parse-buffer) 'underline 'identity nil t))))
1891 '("first line\nsecond line"))))
1894 ;;;; Verbatim
1896 (ert-deftest test-org-element/verbatim-parser ()
1897 "Test `verbatim' parser."
1898 ;; Regular test.
1899 (should
1900 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1901 (org-test-with-temp-text "=verbatim="
1902 (org-element-map (org-element-parse-buffer) 'verbatim 'identity))))
1903 ;; Multi-line markup.
1904 (should
1905 (equal
1906 (org-element-property
1907 :value
1908 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1909 (org-test-with-temp-text "=first line\nsecond line="
1910 (org-element-map
1911 (org-element-parse-buffer) 'verbatim 'identity nil t))))
1912 "first line\nsecond line")))
1915 ;;;; Verse Block
1917 (ert-deftest test-org-element/verse-block-parser ()
1918 "Test `verse-block' parser."
1919 ;; Standard test.
1920 (should
1921 (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
1922 (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
1923 ;; Ignore case.
1924 (should
1925 (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
1926 (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
1927 ;; Parse objects in verse blocks.
1928 (should
1929 (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
1930 (org-element-map (org-element-parse-buffer) 'entity 'identity)))
1931 ;; Ignore incomplete verse block.
1932 (should-not
1933 (org-test-with-temp-text "#+BEGIN_VERSE"
1934 (org-element-map
1935 (org-element-parse-buffer) 'verse-block 'identity nil t))))
1939 ;;; Test Interpreters.
1941 (ert-deftest test-org-element/affiliated-keywords-interpreter ()
1942 "Test if affiliated keywords are correctly interpreted."
1943 ;; Interpret simple keywords.
1944 (should
1945 (equal
1946 (org-element-interpret-data
1947 '(org-data nil (paragraph (:name "para") "Paragraph")))
1948 "#+NAME: para\nParagraph\n"))
1949 ;; Interpret multiple keywords.
1950 (should
1951 (equal
1952 (org-element-interpret-data
1953 '(org-data nil (paragraph (:attr_ascii ("line2" "line1")) "Paragraph")))
1954 "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph\n"))
1955 ;; Interpret parsed keywords.
1956 (should
1957 (equal
1958 (org-element-interpret-data
1959 '(org-data nil (paragraph (:caption (("caption"))) "Paragraph")))
1960 "#+CAPTION: caption\nParagraph\n"))
1961 ;; Interpret dual keywords.
1962 (should
1963 (equal
1964 (org-element-interpret-data
1965 '(org-data nil (paragraph (:caption ((("long") "short"))) "Paragraph")))
1966 "#+CAPTION[short]: long\nParagraph\n"))
1967 ;; Interpret multiple parsed dual keywords.
1968 (should
1969 (equal
1970 (org-element-interpret-data
1971 '(org-data nil (paragraph
1972 (:caption ((("l2") "s2") (("l1") "s1"))) "Paragraph")))
1973 "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph\n")))
1975 (ert-deftest test-org-element/center-block-interpreter ()
1976 "Test center block interpreter."
1977 (should
1978 (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
1979 "#+BEGIN_CENTER\nTest\n#+END_CENTER\n")))
1981 (ert-deftest test-org-element/drawer-interpreter ()
1982 "Test drawer interpreter."
1983 (should
1984 (equal (let ((org-drawers '("TEST")))
1985 (org-test-parse-and-interpret ":TEST:\nTest\n:END:"))
1986 ":TEST:\nTest\n:END:\n")))
1988 (ert-deftest test-org-element/dynamic-block-interpreter ()
1989 "Test dynamic block interpreter."
1990 (should
1991 (equal (org-test-parse-and-interpret
1992 "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
1993 "#+BEGIN: myblock :parameter value1\nTest\n#+END:\n")))
1995 (ert-deftest test-org-element/footnote-definition-interpreter ()
1996 "Test footnote definition interpreter."
1997 (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n")))
1999 (ert-deftest test-org-element/headline-interpreter ()
2000 "Test headline and section interpreters."
2001 ;; 1. Standard test.
2002 (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
2003 ;; 2. With TODO keywords.
2004 (should
2005 (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
2006 (org-test-parse-and-interpret "* TODO Headline"))
2007 "* TODO Headline\n"))
2008 ;; 3. With tags...
2010 ;; 3.1. ... and a positive `org-tags-column' value.
2011 (should
2012 (equal (let ((org-tags-column 20))
2013 (org-test-parse-and-interpret "* Headline :tag:"))
2014 "* Headline :tag:\n"))
2015 ;; 3.2. ... and a negative `org-tags-column' value.
2016 (should
2017 (equal (let ((org-tags-column -20))
2018 (org-test-parse-and-interpret "* Headline :tag:"))
2019 "* Headline :tag:\n"))
2020 ;; 3.3. ... and a null `org-tags-column' value.
2021 (should
2022 (equal (let ((org-tags-column 0))
2023 (org-test-parse-and-interpret "* Headline :tag:"))
2024 "* Headline :tag:\n"))
2025 ;; 4. With priority cookie.
2026 (should
2027 (equal (org-test-parse-and-interpret "* [#B] Headline")
2028 "* [#B] Headline\n"))
2029 ;; 5. With comment keyword.
2030 (should
2031 (equal (let ((org-comment-string "COMMENT"))
2032 (org-test-parse-and-interpret "* COMMENT Headline"))
2033 "* COMMENT Headline\n"))
2034 ;; 6. With quote section.
2035 (should
2036 (equal (let ((org-quote-string "QUOTE"))
2037 (org-test-parse-and-interpret "* QUOTE Headline"))
2038 "* QUOTE Headline\n"))
2039 ;; 7. Keep same number of blank lines before body.
2040 (should
2041 (equal (org-test-parse-and-interpret
2042 "* Headline\n\n\nText after two blank lines.")
2043 "* Headline\n\n\nText after two blank lines.\n")))
2045 (ert-deftest test-org-element/inlinetask-interpreter ()
2046 "Test inlinetask interpretation."
2047 (when (featurep 'org-inlinetask)
2048 (let ((org-inlinetask-min-level 15))
2049 ;; 1. Regular inlinetask.
2050 (should (equal (org-test-parse-and-interpret
2051 "*************** Task\nTest\n*************** END")
2052 "*************** Task\nTest\n*************** END\n"))
2053 ;; 2. Degenerate inlinetask.
2054 (should (equal (org-test-parse-and-interpret "*************** Task")
2055 "*************** Task\n"))
2056 ;; 3. Prefer degenerate form when there are no contents.
2057 (should (equal (org-test-parse-and-interpret
2058 "*************** Task\n*************** END")
2059 "*************** Task\n"))
2060 ;; 4. With TODO keywords.
2061 (should
2062 (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
2063 (org-test-parse-and-interpret "*************** TODO Task"))
2064 "*************** TODO Task\n"))
2065 ;; 5. With tags...
2067 ;; 5.1. ... and a positive `org-tags-column' value.
2068 (should
2069 (equal (let ((org-tags-column 30))
2070 (org-test-parse-and-interpret "*************** Task :tag:"))
2071 "*************** Task :tag:\n"))
2072 ;; 5.2. ... and a negative `org-tags-column' value.
2073 (should
2074 (equal (let ((org-tags-column -30))
2075 (org-test-parse-and-interpret "*************** Task :tag:"))
2076 "*************** Task :tag:\n"))
2077 ;; 5.3. ... and a null `org-tags-column' value.
2078 (should
2079 (equal (let ((org-tags-column 0))
2080 (org-test-parse-and-interpret "*************** Task :tag:"))
2081 "*************** Task :tag:\n"))
2082 ;; 6. With priority cookie.
2083 (should
2084 (equal (org-test-parse-and-interpret "*************** [#B] Task")
2085 "*************** [#B] Task\n")))))
2087 (ert-deftest test-org-element/plain-list-interpreter ()
2088 "Test plain-list and item interpreters."
2089 (let ((org-list-two-spaces-after-bullet-regexp nil))
2090 ;; Unordered list.
2091 (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
2092 ;; Description list.
2093 (should
2094 (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
2095 ;; Ordered list.
2096 (should
2097 (equal (let ((org-plain-list-ordered-item-terminator t))
2098 (org-test-parse-and-interpret "1. Item"))
2099 "1. Item\n"))
2100 (should
2101 (equal (let ((org-plain-list-ordered-item-terminator ?\)))
2102 (org-test-parse-and-interpret "1) Item"))
2103 "1) Item\n"))
2104 ;; Ordered list with counter.
2105 (should
2106 (equal (let ((org-plain-list-ordered-item-terminator t))
2107 (org-test-parse-and-interpret "1. [@5] Item"))
2108 "5. [@5] Item\n"))
2109 ;; List with check-boxes.
2110 (should
2111 (equal (org-test-parse-and-interpret
2112 "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3")
2113 "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3\n"))
2114 ;; Item not starting with a paragraph.
2115 (should
2116 (equal (org-test-parse-and-interpret "-\n | a | b |")
2117 "- \n | a | b |\n"))
2118 ;; Special case: correctly handle "*" bullets.
2119 (should (org-test-parse-and-interpret " * item"))
2120 ;; Special case: correctly handle empty items.
2121 (should (org-test-parse-and-interpret "-"))))
2123 (ert-deftest test-org-element/quote-block-interpreter ()
2124 "Test quote block interpreter."
2125 (should (equal (org-test-parse-and-interpret
2126 "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
2127 "#+BEGIN_QUOTE\nTest\n#+END_QUOTE\n")))
2129 (ert-deftest test-org-element/special-block-interpreter ()
2130 "Test special block interpreter."
2131 (should (equal (org-test-parse-and-interpret
2132 "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
2133 "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL\n")))
2135 (ert-deftest test-org-element/babel-call-interpreter ()
2136 "Test babel call interpreter."
2137 ;; 1. Without argument.
2138 (should (equal (org-test-parse-and-interpret "#+CALL: test()")
2139 "#+CALL: test()\n"))
2140 ;; 2. With argument.
2141 (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
2142 "#+CALL: test(x=2)\n"))
2143 ;; 3. With header arguments.
2144 (should (equal (org-test-parse-and-interpret
2145 "#+CALL: test[:results output]()[:results html]")
2146 "#+CALL: test[:results output]()[:results html]\n")))
2148 (ert-deftest test-org-element/clock-interpreter ()
2149 "Test clock interpreter."
2150 ;; Running clock.
2151 (should
2152 (equal (let ((org-clock-string "CLOCK:"))
2153 (org-test-parse-and-interpret "CLOCK: [2012-01-01 sun. 00:01]"))
2154 "CLOCK: [2012-01-01 sun. 00:01]\n"))
2155 ;; Closed clock.
2156 (should
2157 (equal
2158 (let ((org-clock-string "CLOCK:"))
2159 (org-test-parse-and-interpret "
2160 CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))
2161 "CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01\n")))
2163 (ert-deftest test-org-element/comment-interpreter ()
2164 "Test comment interpreter."
2165 ;; Regular comment.
2166 (should (equal (org-test-parse-and-interpret "# Comment") "# Comment\n"))
2167 ;; Inline comment.
2168 (should (equal (org-test-parse-and-interpret " # Comment")
2169 "# Comment\n"))
2170 ;; Preserve indentation.
2171 (should (equal (org-test-parse-and-interpret " # No blank\n# One blank")
2172 "# No blank\n# One blank\n")))
2174 (ert-deftest test-org-element/comment-block-interpreter ()
2175 "Test comment block interpreter."
2176 (should (equal (org-test-parse-and-interpret
2177 "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
2178 "#+BEGIN_COMMENT\nTest\n#+END_COMMENT\n")))
2180 (ert-deftest test-org-element/diary-sexp ()
2181 "Test diary-sexp interpreter."
2182 (should
2183 (equal
2184 (org-test-parse-and-interpret
2185 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old")
2186 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old\n")))
2188 (ert-deftest test-org-element/example-block-interpreter ()
2189 "Test example block interpreter."
2190 ;; Without switches.
2191 (should (equal (org-test-parse-and-interpret
2192 "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE")
2193 "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE\n"))
2194 ;; With switches.
2195 (should
2196 (equal (org-test-parse-and-interpret
2197 "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE")
2198 "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE\n"))
2199 ;; Preserve code escaping.
2200 (should
2201 (equal (org-test-parse-and-interpret
2202 "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE")
2203 "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE\n")))
2205 (ert-deftest test-org-element/export-block-interpreter ()
2206 "Test export block interpreter."
2207 (should (equal (org-test-parse-and-interpret
2208 "#+BEGIN_HTML\nTest\n#+END_HTML")
2209 "#+BEGIN_HTML\nTest\n#+END_HTML\n")))
2211 (ert-deftest test-org-element/fixed-width-interpreter ()
2212 "Test fixed width interpreter."
2213 ;; Standard test.
2214 (should (equal (org-test-parse-and-interpret ": Test") ": Test\n"))
2215 ;; Preserve indentation.
2216 (should (equal (org-test-parse-and-interpret ": 2 blanks\n: 1 blank")
2217 ": 2 blanks\n: 1 blank\n"))
2218 ;; Remove last newline character
2219 (should
2220 (equal (org-element-fixed-width-interpreter
2221 '(fixed-width (:value "Test\n")) nil)
2222 ": Test"))
2223 (should
2224 (equal (org-element-fixed-width-interpreter
2225 '(fixed-width (:value "Test")) nil)
2226 ": Test"))
2227 ;; Handle empty string.
2228 (should
2229 (equal (org-element-fixed-width-interpreter
2230 '(fixed-width (:value "")) nil)
2231 ""))
2232 ;; Handle nil value.
2233 (should-not
2234 (org-element-fixed-width-interpreter
2235 '(fixed-width (:value nil)) nil)))
2237 (ert-deftest test-org-element/horizontal-rule-interpreter ()
2238 "Test horizontal rule interpreter."
2239 (should (equal (org-test-parse-and-interpret "-------") "-----\n")))
2241 (ert-deftest test-org-element/keyword-interpreter ()
2242 "Test keyword interpreter."
2243 (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
2244 "#+KEYWORD: value\n")))
2246 (ert-deftest test-org-element/latex-environment-interpreter ()
2247 "Test latex environment interpreter."
2248 (should (equal (org-test-parse-and-interpret
2249 "\\begin{equation}\n1+1=2\n\\end{equation}")
2250 "\\begin{equation}\n1+1=2\n\\end{equation}\n"))
2251 (should (equal (org-test-parse-and-interpret
2252 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}")
2253 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}\n")))
2255 (ert-deftest test-org-element/planning-interpreter ()
2256 "Test planning interpreter."
2257 (let ((org-closed-string "CLOSED:")
2258 (org-deadline-string "DEADLINE:")
2259 (org-scheduled-string "SCHEDULED:"))
2260 (should
2261 (equal
2262 (org-test-parse-and-interpret
2263 "* Headline
2264 DEADLINE: <2012-01-01> SCHEDULED: <2012-01-01> CLOSED: [2012-01-01]")
2265 "* Headline
2266 DEADLINE: <2012-01-01> SCHEDULED: <2012-01-01> CLOSED: [2012-01-01]\n"))))
2268 (ert-deftest test-org-element/property-drawer-interpreter ()
2269 "Test property drawer interpreter."
2270 (should (equal (let ((org-property-format "%-10s %s"))
2271 (org-test-parse-and-interpret
2272 ":PROPERTIES:\n:prop: value\n:END:"))
2273 ":PROPERTIES:\n:prop: value\n:END:\n")))
2275 (ert-deftest test-org-element/src-block-interpreter ()
2276 "Test src block interpreter."
2277 ;; With arguments.
2278 (should
2279 (equal (let ((org-edit-src-content-indentation 2))
2280 (org-test-parse-and-interpret
2281 "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC"))
2282 "#+BEGIN_SRC emacs-lisp :results silent\n (+ 1 1)\n#+END_SRC\n"))
2283 ;; With switches.
2284 (should
2285 (equal (let ((org-edit-src-content-indentation 2))
2286 (org-test-parse-and-interpret
2287 "#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
2288 "#+BEGIN_SRC emacs-lisp -n -k\n (+ 1 1)\n#+END_SRC\n"))
2289 ;; Preserve code escaping.
2290 (should
2291 (equal (let ((org-edit-src-content-indentation 2))
2292 (org-test-parse-and-interpret
2293 "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC"))
2294 "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC\n"))
2295 ;; Do not apply `org-edit-src-content-indentation' when preserving
2296 ;; indentation.
2297 (should
2298 (equal (let ((org-edit-src-content-indentation 2)
2299 (org-src-preserve-indentation t))
2300 (org-test-parse-and-interpret
2301 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"))
2302 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n"))
2303 (should
2304 (equal (let ((org-edit-src-content-indentation 2)
2305 (org-src-preserve-indentation nil))
2306 (org-test-parse-and-interpret
2307 "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"))
2308 "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC\n")))
2310 (ert-deftest test-org-element/table-interpreter ()
2311 "Test table, table-row and table-cell interpreters."
2312 ;; 1. Simple table.
2313 (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
2314 "| a | b |\n| c | d |\n"))
2315 ;; 2. With horizontal rules.
2316 (should (equal (org-test-parse-and-interpret
2317 "| a | b |\n|---+---|\n| c | d |")
2318 "| a | b |\n|---+---|\n| c | d |\n"))
2319 ;; 3. With meta-data.
2320 (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
2321 "| / | < | > |\n| * | 1 | 2 |\n"))
2322 ;; 4. With a formula.
2323 (should
2324 (equal (org-test-parse-and-interpret
2325 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)")
2326 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)\n"))
2327 ;; 5. With multiple formulas.
2328 (should
2329 (equal (org-test-parse-and-interpret
2330 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2")
2331 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2\n")))
2333 (ert-deftest test-org-element/timestamp-interpreter ()
2334 "Test timestamp interpreter."
2335 ;; Active.
2336 (should (equal (org-test-parse-and-interpret "<2012-03-29 thu. 16:40>")
2337 "<2012-03-29 thu. 16:40>\n"))
2338 (should
2339 (string-match "<2012-03-29 .* 16:40>"
2340 (org-element-timestamp-interpreter
2341 '(timestamp
2342 (:type active :year-start 2012 :month-start 3 :day-start 29
2343 :hour-start 16 :minute-start 40)) nil)))
2344 ;; Inactive.
2345 (should (equal (org-test-parse-and-interpret "[2012-03-29 thu. 16:40]")
2346 "[2012-03-29 thu. 16:40]\n"))
2347 (should
2348 (string-match
2349 "\\[2012-03-29 .* 16:40\\]"
2350 (org-element-timestamp-interpreter
2351 '(timestamp
2352 (:type inactive :year-start 2012 :month-start 3 :day-start 29
2353 :hour-start 16 :minute-start 40)) nil)))
2354 ;; Active range.
2355 (should (equal (org-test-parse-and-interpret
2356 "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>")
2357 "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>\n"))
2358 (should
2359 (string-match
2360 "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
2361 (org-element-timestamp-interpreter
2362 '(timestamp
2363 (:type active-range :year-start 2012 :month-start 3 :day-start 29
2364 :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
2365 :day-end 29 :hour-end 16 :minute-end 41)) nil)))
2366 ;; Inactive range.
2367 (should (equal (org-test-parse-and-interpret
2368 "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]")
2369 "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]\n"))
2370 (should
2371 (string-match
2372 "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
2373 (org-element-timestamp-interpreter
2374 '(timestamp
2375 (:type inactive-range :year-start 2012 :month-start 3 :day-start 29
2376 :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
2377 :day-end 29 :hour-end 16 :minute-end 41)) nil)))
2378 ;; Diary.
2379 (should (equal (org-test-parse-and-interpret "<%%diary-float t 4 2>")
2380 "<%%diary-float t 4 2>\n"))
2381 ;; Timestamp with repeater interval, with delay, with both.
2382 (should (equal (org-test-parse-and-interpret "<2012-03-29 thu. +1y>")
2383 "<2012-03-29 thu. +1y>\n"))
2384 (should
2385 (string-match
2386 "<2012-03-29 .* \\+1y>"
2387 (org-element-timestamp-interpreter
2388 '(timestamp
2389 (:type active :year-start 2012 :month-start 3 :day-start 29
2390 :repeater-type cumulate :repeater-value 1 :repeater-unit year))
2391 nil)))
2392 (should
2393 (string-match
2394 "<2012-03-29 .* -1y>"
2395 (org-element-timestamp-interpreter
2396 '(timestamp
2397 (:type active :year-start 2012 :month-start 3 :day-start 29
2398 :warning-type all :warning-value 1 :warning-unit year))
2399 nil)))
2400 (should
2401 (string-match
2402 "<2012-03-29 .* \\+1y -1y>"
2403 (org-element-timestamp-interpreter
2404 '(timestamp
2405 (:type active :year-start 2012 :month-start 3 :day-start 29
2406 :warning-type all :warning-value 1 :warning-unit year
2407 :repeater-type cumulate :repeater-value 1 :repeater-unit year))
2408 nil)))
2409 ;; Timestamp range with repeater interval
2410 (should (equal (org-test-parse-and-interpret
2411 "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>")
2412 "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>\n"))
2413 (should
2414 (string-match
2415 "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
2416 (org-element-timestamp-interpreter
2417 '(timestamp
2418 (:type active-range :year-start 2012 :month-start 3 :day-start 29
2419 :year-end 2012 :month-end 3 :day-end 30 :repeater-type cumulate
2420 :repeater-value 1 :repeater-unit year))
2421 nil))))
2423 (ert-deftest test-org-element/verse-block-interpreter ()
2424 "Test verse block interpretation."
2425 (should
2426 (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
2427 "#+BEGIN_VERSE\nTest\n#+END_VERSE\n")))
2429 (ert-deftest test-org-element/bold-interpreter ()
2430 "Test bold interpreter."
2431 (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))
2433 (ert-deftest test-org-element/code-interpreter ()
2434 "Test code interpreter."
2435 (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))
2437 (ert-deftest test-org-element/entity-interpreter ()
2438 "Test entity interpreter."
2439 ;; 1. Without brackets.
2440 (should
2441 (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
2442 ;; 2. With brackets.
2443 (should
2444 (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))
2446 (ert-deftest test-org-element/export-snippet-interpreter ()
2447 "Test export snippet interpreter."
2448 (should (equal (org-test-parse-and-interpret "@@back-end:contents@@")
2449 "@@back-end:contents@@\n")))
2451 (ert-deftest test-org-element/footnote-reference-interpreter ()
2452 "Test footnote reference interpreter."
2453 ;; 1. Regular reference.
2454 (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
2455 ;; 2. Normalized reference.
2456 (should (equal (org-test-parse-and-interpret "Text[1]") "Text[1]\n"))
2457 ;; 3. Named reference.
2458 (should (equal (org-test-parse-and-interpret "Text[fn:label]")
2459 "Text[fn:label]\n"))
2460 ;; 4. Inline reference.
2461 (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
2462 "Text[fn:label:def]\n"))
2463 ;; 5. Anonymous reference.
2464 (should (equal (org-test-parse-and-interpret "Text[fn::def]")
2465 "Text[fn::def]\n")))
2467 (ert-deftest test-org-element/inline-babel-call-interpreter ()
2468 "Test inline babel call interpreter."
2469 ;; 1. Without arguments.
2470 (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
2471 ;; 2. With arguments.
2472 (should (equal (org-test-parse-and-interpret "call_test(x=2)")
2473 "call_test(x=2)\n"))
2474 ;; 3. With header arguments.
2475 (should (equal (org-test-parse-and-interpret
2476 "call_test[:results output]()[:results html]")
2477 "call_test[:results output]()[:results html]\n")))
2479 (ert-deftest test-org-element/inline-src-block-interpreter ()
2480 "Test inline src block interpreter."
2481 ;; 1. Without header argument.
2482 (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
2483 "src_emacs-lisp{(+ 1 1)}\n"))
2484 ;; 2. With header arguments.
2485 (should (equal (org-test-parse-and-interpret
2486 "src_emacs-lisp[:results silent]{(+ 1 1)}")
2487 "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))
2489 (ert-deftest test-org-element/italic-interpreter ()
2490 "Test italic interpreter."
2491 (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))
2493 (ert-deftest test-org-element/latex-fragment-interpreter ()
2494 "Test latex fragment interpreter."
2495 (let ((org-latex-regexps
2496 '(("begin" "^[ ]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^