1 ;;; test-org-element.el --- Tests for org-element.el
3 ;; Copyright (C) 2012-2015 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/>.
22 (eval-and-compile (require 'cl-lib
))
24 (unless (featurep 'org-element
)
25 (signal 'missing-test-dependency
"org-element"))
27 (defun org-test-parse-and-interpret (text)
28 "Parse TEXT as Org syntax and interpret it.
29 Return interpreted string."
33 (org-element-interpret-data (org-element-parse-buffer))))
37 ;;; Test `org-element-map'
39 (ert-deftest test-org-element
/map
()
40 "Test `org-element-map'."
41 ;; Can map to `plain-text' objects.
44 (org-test-with-temp-text "Some text \alpha
50 (org-element-parse-buffer) 'plain-text
51 (lambda (s) (when (string-match "text" s
) (cl-incf count
))))
53 ;; Applies to secondary strings
55 (org-element-map '("some " (bold nil
"bold") "text") 'bold
'identity
))
56 ;; Enter secondary strings before entering contents.
62 (org-test-with-temp-text "* Some \\alpha headline\n\\beta entity."
63 (org-element-map (org-element-parse-buffer) 'entity
'identity nil t
)))))
64 ;; Apply NO-RECURSION argument.
66 (org-test-with-temp-text "#+BEGIN_CENTER\n\\alpha\n#+END_CENTER"
68 (org-element-parse-buffer) 'entity
'identity nil nil
'center-block
)))
69 ;; Use WITH-AFFILIATED argument.
73 (org-test-with-temp-text "#+CAPTION[a]: 1\n#+CAPTION[b]: 2\nParagraph"
75 (org-element-at-point) 'plain-text
'identity nil nil nil t
)))))
81 (ert-deftest test-org-element
/put-property
()
82 "Test `org-element-put-property' specifications."
84 (org-test-with-temp-text "* Headline\n *a*"
85 (let ((tree (org-element-parse-buffer)))
86 (org-element-put-property
87 (org-element-map tree
'bold
'identity nil t
) :test
1)
88 (should (org-element-property
89 :test
(org-element-map tree
'bold
'identity nil t
)))))
90 ;; Put property on a string.
92 (org-element-property :test
(org-element-put-property "Paragraph" :test t
))))
94 (ert-deftest test-org-element
/set-contents
()
95 "Test `org-element-set-contents' specifications."
96 ;; Accept multiple entries.
98 (equal '("b" (italic nil
"a"))
99 (org-test-with-temp-text "* Headline\n *a*"
100 (let ((tree (org-element-parse-buffer)))
101 (org-element-set-contents
102 (org-element-map tree
'bold
'identity nil t
) "b" '(italic nil
"a"))
103 (org-element-contents
104 (org-element-map tree
'bold
'identity nil t
))))))
105 ;; Accept atoms and elements.
108 (org-test-with-temp-text "* Headline\n *a*"
109 (let ((tree (org-element-parse-buffer)))
110 (org-element-set-contents
111 (org-element-map tree
'bold
'identity nil t
) "b")
112 (org-element-contents
113 (org-element-map tree
'bold
'identity nil t
))))))
115 (equal '((italic nil
"b"))
116 (org-test-with-temp-text "* Headline\n *a*"
117 (let ((tree (org-element-parse-buffer)))
118 (org-element-set-contents
119 (org-element-map tree
'bold
'identity nil t
) '(italic nil
"b"))
120 (org-element-contents
121 (org-element-map tree
'bold
'identity nil t
))))))
122 ;; Allow nil contents.
124 (org-test-with-temp-text "* Headline\n *a*"
125 (let ((tree (org-element-parse-buffer)))
126 (org-element-set-contents (org-element-map tree
'bold
'identity nil t
))
127 (org-element-contents (org-element-map tree
'bold
'identity nil t
))))))
129 (ert-deftest test-org-element
/secondary-p
()
130 "Test `org-element-secondary-p' specifications."
131 ;; In a secondary string, return property name.
134 (org-test-with-temp-text "* Headline *object*"
135 (org-element-map (org-element-parse-buffer) 'bold
136 (lambda (object) (org-element-secondary-p object
))
138 ;; Outside a secondary string, return nil.
140 (org-test-with-temp-text "Paragraph *object*"
141 (org-element-map (org-element-parse-buffer) 'bold
142 (lambda (object) (org-element-type (org-element-secondary-p object
)))
145 ;; FIXME: `org-element-class' is a defsubst and cannot be tested
146 ;; properly (i.e., "make test" fails).
148 ;; (ert-deftest test-org-element/class ()
149 ;; "Test `org-element-class' specifications."
151 ;; (should (eq 'element (org-element-class '(paragraph nil) nil)))
152 ;; (should (eq 'object (org-element-class '(target nil) nil)))
154 ;; (should (eq 'element (org-element-class '(org-data nil) nil)))
155 ;; (should (eq 'object (org-element-class "text" nil)))
156 ;; (should (eq 'object (org-element-class '("secondary " "string") nil)))
157 ;; ;; Pseudo elements.
158 ;; (should (eq 'element (org-element-class '(foo nil) nil)))
159 ;; (should (eq 'element (org-element-class '(foo nil) '(center-block nil))))
160 ;; (should (eq 'element (org-element-class '(foo nil) '(org-data nil))))
161 ;; ;; Pseudo objects.
162 ;; (should (eq 'object (org-element-class '(foo nil) '(bold nil))))
163 ;; (should (eq 'object (org-element-class '(foo nil) '(paragraph nil))))
164 ;; (should (eq 'object (org-element-class '(foo nil) '("secondary"))))
167 ;; (let* ((datum '(foo nil))
168 ;; (headline `(headline (:title (,datum)))))
169 ;; (org-element-put-property datum :parent headline)
170 ;; (org-element-class datum)))))
172 (ert-deftest test-org-element
/adopt-elements
()
173 "Test `org-element-adopt-elements' specifications."
176 (equal '(plain-text italic
)
177 (org-test-with-temp-text "* Headline\n *a*"
178 (let ((tree (org-element-parse-buffer)))
179 (org-element-adopt-elements
180 (org-element-map tree
'bold
'identity nil t
) '(italic nil
"a"))
181 (mapcar (lambda (blob) (org-element-type blob
))
182 (org-element-contents
183 (org-element-map tree
'bold
'identity nil t
)))))))
187 (org-test-with-temp-text "* Headline\n *a*"
188 (let ((tree (org-element-parse-buffer)))
189 (org-element-adopt-elements
190 (org-element-map tree
'bold
'identity nil t
) "b")
191 (org-element-contents
192 (org-element-map tree
'bold
'identity nil t
)))))))
194 (ert-deftest test-org-element
/extract-element
()
195 "Test `org-element-extract-element' specifications."
196 ;; Extract a greater element.
198 (equal '(org-data nil
)
199 (org-test-with-temp-text "* Headline"
200 (let* ((tree (org-element-parse-buffer))
201 (element (org-element-map tree
'headline
'identity nil t
)))
202 (org-element-extract-element element
)
204 ;; Extract an element.
207 (org-test-with-temp-text "Paragraph"
208 (let* ((tree (org-element-parse-buffer))
209 (element (org-element-map tree
'paragraph
'identity nil t
)))
210 (org-element-extract-element element
)
214 ;; Extract an object, even in a secondary string.
217 (org-test-with-temp-text "*bold*"
218 (let* ((tree (org-element-parse-buffer))
219 (element (org-element-map tree
'bold
'identity nil t
)))
220 (org-element-extract-element element
)
226 (org-test-with-temp-text "* Headline *bold*"
227 (let* ((tree (org-element-parse-buffer))
228 (element (org-element-map tree
'bold
'identity nil t
)))
229 (org-element-extract-element element
)
233 ;; Return value doesn't have any :parent set.
235 (org-element-property
237 (org-test-with-temp-text "* Headline\n Paragraph with *bold* text."
238 (let* ((tree (org-element-parse-buffer))
239 (element (org-element-map tree
'bold
'identity nil t
)))
240 (org-element-extract-element element
))))))
242 (ert-deftest test-org-element
/insert-before
()
243 "Test `org-element-insert-before' specifications."
247 '(italic entity bold
)
248 (org-test-with-temp-text "/some/ *paragraph*"
249 (let* ((tree (org-element-parse-buffer))
250 (paragraph (org-element-map tree
'paragraph
'identity nil t
))
251 (bold (org-element-map tree
'bold
'identity nil t
)))
252 (org-element-insert-before '(entity (:name
"\\alpha")) bold
)
253 (org-element-map tree
'(bold entity italic
) #'org-element-type nil
)))))
254 ;; Insert an object in a secondary string.
258 (org-test-with-temp-text "* /A/\n Paragraph."
259 (let* ((tree (org-element-parse-buffer))
260 (headline (org-element-map tree
'headline
'identity nil t
))
261 (italic (org-element-map tree
'italic
'identity nil t
)))
262 (org-element-insert-before '(entity (:name
"\\alpha")) italic
)
263 (org-element-map (org-element-property :title headline
) '(entity italic
)
264 #'org-element-type
))))))
266 (ert-deftest test-org-element
/set-element
()
267 "Test `org-element-set-element' specifications."
268 ;; Check if new element is inserted.
270 (org-test-with-temp-text "* Headline\n*a*"
271 (let* ((tree (org-element-parse-buffer))
272 (bold (org-element-map tree
'bold
'identity nil t
)))
273 (org-element-set-element bold
'(italic nil
"b"))
274 (org-element-map tree
'italic
'identity
))))
275 ;; Check if old element is removed.
277 (org-test-with-temp-text "* Headline\n*a*"
278 (let* ((tree (org-element-parse-buffer))
279 (bold (org-element-map tree
'bold
'identity nil t
)))
280 (org-element-set-element bold
'(italic nil
"b"))
281 (org-element-map tree
'bold
'identity
))))
282 ;; Check if :parent property is correctly set.
285 (org-test-with-temp-text "* Headline\n*a*"
286 (let* ((tree (org-element-parse-buffer))
287 (bold (org-element-map tree
'bold
'identity nil t
)))
288 (org-element-set-element bold
'(italic nil
"b"))
290 (org-element-property
291 :parent
(org-element-map tree
'italic
'identity nil t
)))))))
292 ;; Allow to replace strings with elements.
295 (org-test-with-temp-text "* Headline"
296 (let* ((tree (org-element-parse-buffer))
297 (text (org-element-map tree
'plain-text
'identity nil t
)))
298 (org-element-set-element text
(list 'bold nil
"b"))
299 (org-element-map tree
'plain-text
'identity
)))))
300 ;; Allow to replace elements with strings.
303 (org-test-with-temp-text "* =verbatim="
304 (let* ((tree (org-element-parse-buffer))
305 (verb (org-element-map tree
'verbatim
'identity nil t
)))
306 (org-element-set-element verb
"a")
307 (org-element-map tree
'plain-text
'identity nil t
)))))
308 ;; Allow to replace strings with strings.
311 (org-test-with-temp-text "a"
312 (let* ((tree (org-element-parse-buffer))
313 (text (org-element-map tree
'plain-text
'identity nil t
)))
314 (org-element-set-element text
"b")
315 (org-element-map tree
'plain-text
'identity nil t
))))))
317 (ert-deftest test-org-element
/copy
()
318 "Test `org-element-copy' specifications."
321 (org-test-with-temp-text "*bold*"
322 (org-element-type (org-element-copy (org-element-context))))))
323 (should (eq 'plain-text
324 (org-test-with-temp-text "*bold*"
326 (org-element-map (org-element-parse-buffer) 'plain-text
327 #'org-element-copy nil t
)))))
328 ;; Preserve properties except `:parent'.
330 (org-test-with-temp-text "*bold*"
331 (org-element-property
332 :end
(org-element-copy (org-element-context))))))
334 (org-test-with-temp-text "*bold*"
335 (org-element-property
336 :parent
(org-element-copy (org-element-context)))))
338 (org-test-with-temp-text "*bold*"
339 (org-element-property
341 (org-element-map (org-element-parse-buffer) 'plain-text
342 #'org-element-copy nil t
))))
343 ;; Copying nil returns nil.
344 (should-not (org-element-copy nil
))
345 ;; Return a copy secondary strings.
346 (should (equal '("text") (org-element-copy '("text"))))
347 (should-not (eq '("text") (org-element-copy '("text")))))
353 ;;;; Affiliated Keywords
355 (ert-deftest test-org-element
/affiliated-keywords-parser
()
356 "Test affiliated keywords parsing."
357 ;; Read simple keywords.
360 (org-element-property
362 (org-test-with-temp-text "#+NAME: para\nParagraph"
363 (org-element-at-point)))))
366 (org-element-property
368 (org-test-with-temp-text "#+NAME: para\nParagraph"
369 (org-element-at-point)))))
370 ;; Parse multiple keywords.
374 (org-element-property
376 (org-test-with-temp-text
377 "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph"
378 (org-element-at-point)))))
379 ;; Parse "parsed" keywords.
383 (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
384 (car (org-element-property :caption
(org-element-at-point))))))
385 ;; Parse dual keywords.
388 '((("long") "short"))
389 (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
390 (org-element-property :caption
(org-element-at-point)))))
391 ;; Allow multiple caption keywords.
394 '((("l2") "s2") (("l1") "s1"))
395 (org-test-with-temp-text "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph"
396 (org-element-property :caption
(org-element-at-point)))))
399 '((("l1")) (nil "s1"))
400 (org-test-with-temp-text "#+CAPTION[s1]:\n#+CAPTION: l1\nParagraph"
401 (org-element-property :caption
(org-element-at-point)))))
402 ;; Corner case: orphaned keyword at the end of an element.
405 (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
406 (progn (search-forward "name")
407 (org-element-type (org-element-at-point))))))
409 (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
410 (progn (search-forward "Some")
411 (org-element-property :name
(org-element-at-point))))))
416 (ert-deftest test-org-element
/babel-call-parser
()
417 "Test `babel-call' parsing."
421 (org-test-with-temp-text "#+CALL: test()"
422 (org-element-type (org-element-at-point)))))
426 (org-test-with-temp-text "#+call: test()"
427 (org-element-type (org-element-at-point)))))
428 ;; Handle non-empty blank line at the end of buffer.
430 (org-test-with-temp-text "#+CALL: test()\n "
431 (= (org-element-property :end
(org-element-at-point)) (point-max))))
435 (org-test-with-temp-text "#+CALL: test()"
436 (org-element-property :call
(org-element-at-point)))))
437 ;; Parse inside header. It may contain paired square brackets.
439 (equal ":results output"
440 (org-test-with-temp-text "#+CALL: test[:results output]()"
441 (org-element-property :inside-header
(org-element-at-point)))))
443 (equal ":results output, a=table[1:2], b=2"
444 (org-test-with-temp-text
445 "#+CALL: test[:results output, a=table[1:2], b=2]()"
446 (org-element-property :inside-header
(org-element-at-point)))))
447 ;; Parse arguments, which can be nested. However, stop at paired
448 ;; parenthesis, even when, e.g.,end header contains some.
451 (org-test-with-temp-text "#+CALL: test(n=4)"
452 (org-element-property :arguments
(org-element-at-point)))))
455 (org-test-with-temp-text "#+CALL: test(test())"
456 (org-element-property :arguments
(org-element-at-point)))))
459 (org-test-with-temp-text "#+CALL: test(a=1) :post another-call()"
460 (org-element-property :arguments
(org-element-at-point)))))
463 (equal ":results html"
464 (org-test-with-temp-text "#+CALL: test() :results html"
465 (org-element-property :end-header
(org-element-at-point))))))
470 (ert-deftest test-org-element
/bold-parser
()
471 "Test `bold' parser."
474 (org-test-with-temp-text "*bold*"
475 (org-element-map (org-element-parse-buffer) 'bold
#'identity nil t
)))
476 ;; Multi-line markup.
479 (org-element-contents
480 (org-test-with-temp-text "*first line\nsecond line*"
481 (org-element-map (org-element-parse-buffer) 'bold
#'identity nil t
)))
482 '("first line\nsecond line"))))
487 (ert-deftest test-org-element
/center-block-parser
()
488 "Test `center-block' parser."
491 (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
492 (org-element-map (org-element-parse-buffer) 'center-block
'identity
)))
495 (org-test-with-temp-text "#+begin_center\nText\n#+end_center"
496 (org-element-map (org-element-parse-buffer) 'center-block
'identity
)))
497 ;; Ignore incomplete block.
499 (org-test-with-temp-text "#+BEGIN_CENTER"
500 (org-element-map (org-element-parse-buffer) 'center-block
502 ;; Handle non-empty blank line at the end of buffer.
504 (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
505 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
510 (ert-deftest test-org-element
/clock-parser
()
511 "Test `clock' parser."
513 (let ((clock (org-test-with-temp-text "CLOCK: [2012-01-01 sun. 00:01]"
514 (org-element-at-point))))
515 (should (eq (org-element-property :status clock
) 'running
))
517 (equal (org-element-property :raw-value
518 (org-element-property :value clock
))
519 "[2012-01-01 sun. 00:01]"))
520 (should-not (org-element-property :duration clock
)))
523 (org-test-with-temp-text
524 "CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
525 (org-element-at-point))))
526 (should (eq (org-element-property :status clock
) 'closed
))
527 (should (equal (org-element-property :raw-value
528 (org-element-property :value clock
))
529 "[2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02]"))
530 (should (equal (org-element-property :duration clock
) "0:01"))))
535 (ert-deftest test-org-element
/code-parser
()
536 "Test `code' parser."
539 (org-test-with-temp-text "~code~"
540 (org-element-map (org-element-parse-buffer) 'code
#'identity
)))
541 ;; Multi-line markup.
544 (org-element-property
546 (org-test-with-temp-text "~first line\nsecond line~"
548 (org-element-parse-buffer) 'code
#'identity nil t
)))
549 "first line\nsecond line")))
554 (ert-deftest test-org-element
/comment-parser
()
555 "Test `comment' parser."
558 (org-test-with-temp-text "# Comment"
559 (org-element-map (org-element-parse-buffer) 'comment
'identity
)))
562 (org-test-with-temp-text " # Comment"
563 (org-element-map (org-element-parse-buffer) 'comment
'identity
)))
564 ;; Preserve indentation.
567 (org-element-property
569 (org-test-with-temp-text "# No blank\n# One blank"
570 (org-element-map (org-element-parse-buffer) 'comment
'identity nil t
)))
571 "No blank\n One blank"))
572 ;; Comment with blank lines.
575 (org-element-property
577 (org-test-with-temp-text "# First part\n# \n#\n# Second part"
578 (org-element-map (org-element-parse-buffer) 'comment
'identity nil t
)))
579 "First part\n\n\nSecond part"))
580 ;; Do not mix comments and keywords.
583 (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
584 (length (org-element-map (org-element-parse-buffer) 'comment
588 (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
589 (org-element-property
591 (org-element-map (org-element-parse-buffer) 'comment
593 ;; Correctly handle non-empty blank lines at the end of buffer.
595 (org-test-with-temp-text "# A\n "
596 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
601 (ert-deftest test-org-element
/comment-block-parser
()
602 "Test `comment-block' parser."
605 (org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
606 (org-element-map (org-element-parse-buffer) 'comment-block
'identity
)))
609 (org-test-with-temp-text "#+begin_comment\nText\n#+end_comment"
610 (org-element-map (org-element-parse-buffer) 'comment-block
'identity
)))
611 ;; Ignore incomplete block.
613 (org-test-with-temp-text "#+BEGIN_COMMENT"
614 (org-element-map (org-element-parse-buffer) 'comment-block
616 ;; Handle non-empty blank line at the end of buffer.
618 (org-test-with-temp-text "#+BEGIN_COMMENT\nC\n#+END_COMMENT\n "
619 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
624 (ert-deftest test-org-element
/diary-sexp-parser
()
625 "Test `diary-sexp' parser."
629 (org-test-with-temp-text
630 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old"
631 (org-element-type (org-element-at-point)))))
632 ;; Diary sexp must live at beginning of line
635 (org-test-with-temp-text " %%(org-bbdb-anniversaries)"
636 (org-element-type (org-element-at-point)))))
637 ;; Handle non-empty blank line at the end of buffer.
639 (org-test-with-temp-text "%%(org-bbdb-anniversaries)\n "
640 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
645 (ert-deftest test-org-element
/drawer-parser
()
646 "Test `drawer' parser."
649 (org-test-with-temp-text ":TEST:\nText\n:END:"
650 (org-element-map (org-element-parse-buffer) 'drawer
'identity
)))
651 ;; Ignore incomplete drawer.
653 (org-test-with-temp-text ":TEST:"
654 (org-element-map (org-element-parse-buffer) 'drawer
'identity nil t
)))
655 ;; Handle non-empty blank line at the end of buffer.
657 (org-test-with-temp-text ":TEST:\nC\n:END:\n "
658 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
663 (ert-deftest test-org-element
/dynamic-block-parser
()
664 "Test `dynamic-block' parser."
667 (org-test-with-temp-text
668 "#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
669 (org-element-map (org-element-parse-buffer) 'dynamic-block
'identity
)))
672 (org-test-with-temp-text
673 "#+begin: myblock :param1 val1 :param2 val2\nText\n#+end:"
674 (org-element-map (org-element-parse-buffer) 'dynamic-block
'identity
)))
675 ;; Ignore incomplete block.
677 (org-test-with-temp-text "#+BEGIN: myblock :param1 val1 :param2 val2"
678 (org-element-map (org-element-parse-buffer) 'dynamic-block
680 ;; Handle non-empty blank line at the end of buffer.
682 (org-test-with-temp-text "#+BEGIN: myblock :param val1\nC\n#+END:\n "
683 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
688 (ert-deftest test-org-element
/entity-parser
()
689 "Test `entity' parser."
692 (org-test-with-temp-text "\\sin"
693 (org-element-map (org-element-parse-buffer) 'entity
'identity
)))
696 (org-element-property
698 (org-test-with-temp-text "\\alpha{}text"
699 (org-element-map (org-element-parse-buffer) 'entity
'identity nil t
))))
700 ;; User-defined entity.
703 (org-element-property
705 (let ((org-entities-user
706 '(("test" "test" nil
"test" "test" "test" "test"))))
707 (org-test-with-temp-text "\\test"
708 (org-element-map (org-element-parse-buffer) 'entity
'identity nil t
))))
710 ;; Special case: entity at the end of a container.
713 (org-test-with-temp-text "*\\alpha \\beta*"
714 (search-forward "be")
715 (org-element-type (org-element-context))))))
720 (ert-deftest test-org-element
/example-block-parser
()
721 "Test `example-block' parser."
724 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
725 (org-element-map (org-element-parse-buffer) 'example-block
'identity
)))
726 ;; Ignore incomplete block.
729 (org-test-with-temp-text "#+BEGIN_EXAMPLE"
730 (org-element-type (org-element-at-point)))))
731 ;; Properly un-escape code.
733 (equal "* Headline\n #+keyword:\nText\n"
734 (org-test-with-temp-text
735 "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword:\nText\n#+END_EXAMPLE"
736 (org-element-property :value
(org-element-at-point)))))
737 ;; Handle non-empty blank line at the end of buffer.
739 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nC\n#+END_EXAMPLE\n "
740 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
742 (ert-deftest test-org-element
/block-switches
()
743 "Test `example-block' and `src-block' switches parsing."
744 (let ((org-coderef-label-format "(ref:%s)"))
745 ;; 1. Test "-i" switch.
747 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
748 (org-element-property :preserve-indent
(org-element-at-point))))
750 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
751 (org-element-property :preserve-indent
(org-element-at-point))))
753 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
754 (org-element-property :preserve-indent
(org-element-at-point))))
756 (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
757 (org-element-property :preserve-indent
(org-element-at-point))))
758 ;; 2. "-n -r -k" combination should number lines, retain labels but
759 ;; not use them in coderefs.
760 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
761 (let ((element (org-element-at-point)))
762 (should (org-element-property :number-lines element
))
763 (should (org-element-property :retain-labels element
))
764 (should-not (org-element-property :use-labels element
))))
765 (org-test-with-temp-text
766 "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
767 (let ((element (org-element-at-point)))
768 (should (org-element-property :number-lines element
))
769 (should (org-element-property :retain-labels element
))
770 (should-not (org-element-property :use-labels element
))))
771 ;; 3. "-n -r" combination should number-lines remove labels and not
772 ;; use them in coderefs.
773 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
774 (let ((element (org-element-at-point)))
775 (should (org-element-property :number-lines element
))
776 (should-not (org-element-property :retain-labels element
))
777 (should-not (org-element-property :use-labels element
))))
778 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
779 (let ((element (org-element-at-point)))
780 (should (org-element-property :number-lines element
))
781 (should-not (org-element-property :retain-labels element
))
782 (should-not (org-element-property :use-labels element
))))
783 ;; 4. "-n" or "+n" should number lines, retain labels and use them
786 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
787 (let ((element (org-element-at-point)))
788 (and (org-element-property :number-lines element
)
789 (org-element-property :retain-labels element
)
790 (org-element-property :use-labels element
)))))
792 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
793 (let ((element (org-element-at-point)))
794 (and (org-element-property :number-lines element
)
795 (org-element-property :retain-labels element
)
796 (org-element-property :use-labels element
)))))
798 (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
799 (let ((element (org-element-at-point)))
800 (and (org-element-property :number-lines element
)
801 (org-element-property :retain-labels element
)
802 (org-element-property :use-labels element
)))))
804 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
805 (let ((element (org-element-at-point)))
806 (and (org-element-property :number-lines element
)
807 (org-element-property :retain-labels element
)
808 (org-element-property :use-labels element
)))))
809 ;; 5. No switch should not number lines, but retain labels and use
811 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
812 (let ((element (org-element-at-point)))
813 (should (not (org-element-property :number-lines element
)))
814 (should (org-element-property :retain-labels element
))
815 (should (org-element-property :use-labels element
))))
816 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
817 (let ((element (org-element-at-point)))
818 (should (not (org-element-property :number-lines element
)))
819 (should (org-element-property :retain-labels element
))
820 (should (org-element-property :use-labels element
))))
821 ;; 6. "-r" switch only: do not number lines, remove labels, and
822 ;; don't use labels in coderefs.
823 (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
824 (let ((element (org-element-at-point)))
825 (should (not (org-element-property :number-lines element
)))
826 (should (not (org-element-property :retain-labels element
)))
827 (should (not (org-element-property :use-labels element
)))))
828 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
829 (let ((element (org-element-at-point)))
830 (should (not (org-element-property :number-lines element
)))
831 (should (not (org-element-property :retain-labels element
)))
832 (should (not (org-element-property :use-labels element
)))))
833 ;; 7. Recognize coderefs with user-defined syntax.
837 (org-test-with-temp-text
838 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
839 (org-element-property :label-fmt
(org-element-at-point)))))
843 (org-test-with-temp-text
844 "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
845 (org-element-property :label-fmt
(org-element-at-point)))))))
850 (ert-deftest test-org-element
/export-block-parser
()
851 "Test `export-block' parser."
855 (org-test-with-temp-text "#+BEGIN_EXPORT LATEX\nText\n#+END_EXPORT"
856 (org-element-type (org-element-at-point)))))
859 (org-test-with-temp-text "#+BEGIN_EXPORT LATEX\nText\n#+END_EXPORT"
860 (org-element-property :type
(org-element-at-point)))))
864 (org-test-with-temp-text "#+begin_export latex\nText\n#+end_export"
865 (org-element-type (org-element-at-point)))))
866 ;; Ignore incomplete block.
869 (org-test-with-temp-text "#+BEGIN_EXPORT"
870 (org-element-type (org-element-at-point)))))
871 ;; Handle non-empty blank line at the end of buffer.
873 (org-test-with-temp-text "#+BEGIN_EXPORT latex\nC\n#+END_EXPORT\n "
874 (= (org-element-property :end
(org-element-at-point)) (point-max))))
875 ;; Un-escape commas in `:value'.
878 (org-test-with-temp-text "#+BEGIN_EXPORT org\n,* H\n#+END_EXPORT\n "
879 (org-element-property :value
(org-element-at-point))))))
884 (ert-deftest test-org-element
/export-snippet-parser
()
885 "Test `export-snippet' parser."
888 '("back-end" .
"contents")
889 (org-test-with-temp-text "@@back-end:contents@@"
891 (org-element-parse-buffer) 'export-snippet
892 (lambda (snippet) (cons (org-element-property :back-end snippet
)
893 (org-element-property :value snippet
)))
899 (ert-deftest test-org-element
/fixed-width-parser
()
900 "Test fixed-width area parsing."
901 ;; Preserve indentation.
903 (equal "no blank\n one blank"
904 (org-test-with-temp-text ": no blank\n: one blank"
905 (org-element-property :value
(org-element-at-point)))))
906 ;; Fixed-width with empty lines.
908 (equal "first part\n\n\nsecond part"
909 (org-test-with-temp-text ": first part\n:\n: \n: second part"
910 (org-element-property :value
(org-element-at-point)))))
911 ;; Parse indented fixed-width markers.
914 (org-test-with-temp-text "Text\n<point> : no blank\n : one blank"
915 (org-element-type (org-element-at-point)))))
916 ;; Distinguish fixed-width areas within a list and outside of it.
918 (org-test-with-temp-text "
920 : fixed-width inside<point>
921 : fixed-width outside"
922 (= (org-element-property :end
(org-element-at-point))
923 (line-beginning-position 2))))
924 ;; Handle non-empty blank line at the end of buffer.
926 (org-test-with-temp-text ": A\n "
927 (= (org-element-property :end
(org-element-at-point))
931 ;;;; Footnote Definition
933 (ert-deftest test-org-element
/footnote-definition-parser
()
934 "Test `footnote-definition' parser."
936 (org-test-with-temp-text "[fn:1] Definition"
937 (eq (org-element-type (org-element-at-point)) 'footnote-definition
)))
938 ;; Footnote with more contents.
940 (= 29 (org-test-with-temp-text "[fn:1] Definition\n\n| a | b |"
941 (org-element-property :end
(org-element-at-point)))))
942 ;; Test difference between :contents-end and :end property
944 (< (org-test-with-temp-text "[fn:1] Definition\n\n\n"
945 (org-element-property :contents-end
(org-element-at-point)))
946 (org-test-with-temp-text "[fn:1] Definition\n\n\n"
947 (org-element-property :end
(org-element-at-point)))))
948 ;; Footnote starting with special syntax.
950 (org-test-with-temp-text "[fn:1] <point>- no item"
951 (eq (org-element-type (org-element-at-point)) 'item
)))
952 ;; Correctly handle footnote starting with an empty line.
955 (org-test-with-temp-text "[fn:1]\n\n Body"
956 (org-element-property :contents-begin
(org-element-at-point)))))
957 ;; Handle non-empty blank line at the end of buffer.
959 (org-test-with-temp-text "[fn:1] Definition\n "
960 (= (org-element-property :end
(org-element-at-point)) (point-max))))
961 ;; Footnote with attributes.
964 (org-test-with-temp-text "#+attr_latex: :offset 0in\n[fn:1] A footnote."
966 (org-element-map (org-element-parse-buffer) 'footnote-definition
969 (org-test-with-temp-text "[fn:1] 1\n\n#+attr_latex: :offset 0in\n[fn:2] 2"
970 (goto-char (org-element-property :end
(org-element-at-point)))
972 ;; An empty footnote has no contents.
974 (org-test-with-temp-text "[fn:1]\n\n"
975 (let ((footnote (org-element-at-point)))
976 (or (org-element-property :contents-begin footnote
)
977 (org-element-property :contents-end footnote
)))))
978 ;; Parse `:pre-blank'.
981 (org-test-with-temp-text "[fn:1] A"
982 (org-element-property :pre-blank
(org-element-at-point)))))
985 (org-test-with-temp-text "[fn:1]\nA"
986 (org-element-property :pre-blank
(org-element-at-point)))))
989 (org-test-with-temp-text "[fn:1]\n\nA"
990 (org-element-property :pre-blank
(org-element-at-point))))))
993 ;;;; Footnotes Reference.
995 (ert-deftest test-org-element
/footnote-reference-parser
()
996 "Test `footnote-reference' parser."
997 ;; Parse a standard reference.
999 (org-test-with-temp-text "Text[fn:label]"
1001 (org-element-parse-buffer) 'footnote-reference
'identity
)))
1002 ;; Parse an inline reference.
1004 (org-test-with-temp-text "Text[fn:test:def]"
1006 (org-element-parse-buffer) 'footnote-reference
'identity
)))
1007 ;; Parse an anonymous reference.
1009 (org-test-with-temp-text "Text[fn::def]"
1011 (org-element-parse-buffer) 'footnote-reference
'identity
)))
1012 ;; Parse nested footnotes.
1016 (org-test-with-temp-text "Text[fn::def [fn:label]]"
1018 (org-element-parse-buffer) 'footnote-reference
'identity
)))))
1019 ;; Parse adjacent footnotes.
1021 (org-test-with-temp-text "Text[fn:label1][fn:label2]"
1025 (org-element-parse-buffer) 'footnote-reference
'identity
)))))
1026 ;; Only properly closed footnotes are recognized as such.
1028 (org-test-with-temp-text "Text[fn:label"
1030 (org-element-parse-buffer) 'footnote-reference
'identity
))))
1035 (ert-deftest test-org-element
/headline-todo-keyword
()
1036 "Test todo keyword recognition."
1038 (org-test-with-temp-text "* TODO Headline"
1039 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
1040 (should (org-element-property :todo-keyword
(org-element-at-point)))))
1041 ;; Todo keyword is prefix of headlines first word.
1042 (org-test-with-temp-text "* TODOHeadline"
1043 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
1044 (should-not (org-element-property :todo-keyword
(org-element-at-point))))))
1046 (ert-deftest test-org-element
/headline-comment-keyword
()
1047 "Test COMMENT keyword recognition."
1049 (org-test-with-temp-text "* Headline"
1050 (let ((org-comment-string "COMMENT"))
1051 (should-not (org-element-property :commentedp
(org-element-at-point)))))
1052 ;; Standard position.
1053 (org-test-with-temp-text "* COMMENT Headline"
1054 (let ((org-comment-string "COMMENT")
1055 (headline (org-element-at-point)))
1056 (should (org-element-property :commentedp headline
))
1057 (should (equal (org-element-property :raw-value headline
) "Headline"))))
1058 ;; Case sensitivity.
1059 (org-test-with-temp-text "* COMMENT Headline"
1060 (let* ((org-comment-string "Comment")
1061 (headline (org-element-at-point)))
1062 (should-not (org-element-property :commentedp headline
))
1063 (should (equal (org-element-property :raw-value headline
)
1064 "COMMENT Headline"))))
1065 ;; With another keyword.
1066 (org-test-with-temp-text "* TODO COMMENT Headline"
1067 (let* ((org-comment-string "COMMENT")
1068 (org-todo-keywords '((sequence "TODO" "DONE")))
1069 (headline (org-element-at-point)))
1070 (should (org-element-property :commentedp headline
))
1071 (should (equal (org-element-property :raw-value headline
) "Headline"))))
1072 ;; With the keyword only.
1073 (org-test-with-temp-text "* COMMENT"
1074 (let* ((org-comment-string "COMMENT")
1075 (headline (org-element-at-point)))
1076 (should (org-element-property :commentedp headline
))
1077 (should (equal (org-element-property :raw-value headline
) "")))))
1079 (ert-deftest test-org-element
/headline-archive-tag
()
1080 "Test ARCHIVE tag recognition."
1083 (org-test-with-temp-text "* Headline"
1084 (let ((org-archive-tag "ARCHIVE"))
1085 (org-element-property :archivedp
(org-element-at-point)))))
1087 (org-test-with-temp-text "* Headline :ARCHIVE:"
1088 (let ((org-archive-tag "ARCHIVE"))
1089 (let ((headline (org-element-at-point)))
1090 (should (org-element-property :archivedp headline
)))))
1092 (org-test-with-temp-text "* Headline :test:ARCHIVE:"
1093 (let ((org-archive-tag "ARCHIVE"))
1094 (let ((headline (org-element-at-point)))
1095 (should (org-element-property :archivedp headline
)))))
1096 ;; Tag is case-sensitive.
1098 (org-test-with-temp-text "* Headline :ARCHIVE:"
1099 (let ((org-archive-tag "Archive"))
1100 (org-element-property :archivedp
(org-element-at-point))))))
1102 (ert-deftest test-org-element
/headline-properties
()
1103 "Test properties from property drawer."
1104 ;; All properties from property drawer have their symbol upper
1107 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
1108 (org-element-property :FOO
(org-element-at-point))))
1110 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
1111 (org-element-property :foo
(org-element-at-point))))
1112 ;; Also parse properties associated in inlinetasks.
1113 (when (featurep 'org-inlinetask
)
1115 (org-test-with-temp-text "*************** Inlinetask
1119 *************** END"
1120 (org-element-property :FOO
(org-element-at-point)))))
1121 ;; Do not find property drawer in a verbatim area.
1123 (org-test-with-temp-text
1130 (org-element-property :FOO
(org-element-at-point))))
1131 ;; Do not use properties from a drawer associated to an inlinetask.
1132 (when (featurep 'org-inlinetask
)
1134 (org-test-with-temp-text
1136 *************** Inlinetask
1140 *************** END"
1141 (org-element-property
1142 :FOO
(let ((org-inlinetask-min-level 15)) (org-element-at-point))))))
1143 ;; Do not find incomplete drawers.
1145 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar"
1146 (org-element-property :FOO
(org-element-at-point)))))
1149 ;;;; Horizontal Rule
1151 (ert-deftest test-org-element
/horizontal-rule-parser
()
1152 "Test `horizontal-rule' parser."
1155 (org-test-with-temp-text "-----"
1156 (org-element-map (org-element-parse-buffer) 'horizontal-rule
'identity
)))
1159 (org-test-with-temp-text " -----"
1160 (org-element-map (org-element-parse-buffer) 'horizontal-rule
'identity
)))
1161 ;; Hyphen must be alone on the line.
1163 (org-test-with-temp-text "-----wrong"
1164 (org-element-map (org-element-parse-buffer) 'horizontal-rule
'identity
)))
1165 ;; 4 hyphens is too small.
1167 (org-test-with-temp-text "----"
1168 (org-element-map (org-element-parse-buffer) 'horizontal-rule
'identity
)))
1169 ;; Handle non-empty blank line at the end of buffer.
1171 (org-test-with-temp-text "-----\n "
1172 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1175 ;;;; Inline Babel Call
1177 (ert-deftest test-org-element
/inline-babel-call-parser
()
1178 "Test `inline-babel-call' parser."
1181 (eq 'inline-babel-call
1182 (org-test-with-temp-text "call_test()"
1183 (org-element-type (org-element-context)))))
1185 (eq 'inline-babel-call
1186 (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
1187 (org-element-type (org-element-context)))))
1192 (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
1193 (org-element-property :call
(org-element-context)))))
1194 ;; Parse inside header.
1198 (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
1199 (org-element-property :inside-header
(org-element-context)))))
1204 (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
1205 (org-element-property :arguments
(org-element-context)))))
1206 ;; Parse end header.
1210 (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
1211 (org-element-property :end-header
(org-element-context)))))
1212 ;; Handle multi-line babel calls.
1214 (eq 'inline-babel-call
1215 (org-test-with-temp-text
1216 "call_test[:results\noutput](x=2)[:results html]"
1217 (org-element-type (org-element-context)))))
1219 (eq 'inline-babel-call
1220 (org-test-with-temp-text
1221 "call_test[:results output](x=2\ny=3)[:results html]"
1222 (org-element-type (org-element-context)))))
1224 (eq 'inline-babel-call
1225 (org-test-with-temp-text
1226 "call_test[:results output](x=2)[:results\nhtml]"
1227 (org-element-type (org-element-context)))))
1228 ;; Parse parameters containing round brackets.
1230 (eq 'inline-babel-call
1231 (org-test-with-temp-text "call_test[:var x='(1)](x=2)"
1232 (org-element-type (org-element-context))))))
1235 ;;;; Inline Src Block
1237 (ert-deftest test-org-element
/inline-src-block-parser
()
1238 "Test `inline-src-block' parser."
1240 (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
1241 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1244 (org-test-with-temp-text "src_emacs-lisp[:foo bar]{(+ 1 1)}"
1245 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1247 (org-test-with-temp-text "src_emacs-lisp[ :foo bar]{(+ 1 1)}"
1248 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1251 (org-test-with-temp-text "src_emacs-lisp[]{(+ 1 1)}"
1252 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1255 (org-test-with-temp-text "src_emacs-lisp[]foo{(+ 1 1)}"
1256 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1258 (org-test-with-temp-text "foosrc_emacs-lisp[]{(+ 1 1)}"
1259 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1260 ;; Invalid language name
1262 (org-test-with-temp-text "src_emacs-\tlisp{(+ 1 1)}"
1263 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1264 ;; Test parsing at the beginning of an item.
1266 (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
1267 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1268 ;; Test parsing multi-line source blocks.
1270 (eq 'inline-src-block
1271 (org-test-with-temp-text "src_emacs-lisp{(+ 1\n 1)}"
1272 (org-element-type (org-element-context)))))
1274 (eq 'inline-src-block
1275 (org-test-with-temp-text "src_emacs-lisp[\n:foo bar]{(+ 1 1)}"
1276 (org-element-type (org-element-context)))))
1278 (eq 'inline-src-block
1279 (org-test-with-temp-text "src_emacs-lisp[:foo\nbar]{(+ 1 1)}"
1280 (org-element-type (org-element-context)))))
1281 ;; Besides curly brackets, ignore any other bracket type.
1284 (org-test-with-temp-text "src_emacs-lisp{[foo}"
1285 (org-element-property :value
(org-element-context)))))
1288 (org-test-with-temp-text "src_emacs-lisp{foo]}"
1289 (org-element-property :value
(org-element-context)))))
1292 (org-test-with-temp-text "src_emacs-lisp{(foo}"
1293 (org-element-property :value
(org-element-context)))))
1296 (org-test-with-temp-text "src_emacs-lisp{foo)}"
1297 (org-element-property :value
(org-element-context)))))
1298 ;; Parse parameters containing square brackets.
1300 (eq 'inline-src-block
1301 (org-test-with-temp-text "src_emacs-lisp[:var table=t[1,1]]{(+ 1 1)}"
1302 (org-element-type (org-element-context))))))
1307 (ert-deftest test-org-element
/inlinetask-parser
()
1308 "Test `inlinetask' parser."
1309 (when (featurep 'org-inlinetask
)
1310 (let ((org-inlinetask-min-level 15))
1311 ;; Regular inlinetask.
1314 (org-test-with-temp-text
1315 "*************** Task\nTest\n*************** END"
1316 (org-element-type (org-element-at-point)))))
1319 (org-test-with-temp-text
1320 "*************** Task\nTest\n*************** END"
1321 (org-element-type (org-element-at-point)))))
1322 ;; Degenerate inlinetask.
1325 (org-test-with-temp-text "*************** Task"
1326 (org-element-type (org-element-at-point)))))
1327 ;; Mixed inlinetasks.
1329 (org-test-with-temp-text
1331 *************** Task
1332 *************** Task2
1334 *************** END"
1336 (goto-char (org-element-property :end
(org-element-at-point)))
1342 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
1343 (org-test-with-temp-text "*************** TODO Task"
1344 (org-element-property :todo-keyword
(org-element-at-point))))))
1347 (org-test-with-temp-text "
1348 *************** Task<point>
1349 DEADLINE: <2012-03-29 thu.>
1350 *************** END"
1351 (org-element-property :deadline
(org-element-at-point))))
1354 (org-test-with-temp-text "
1355 *************** Task
1356 <point>DEADLINE: <2012-03-29 thu.>
1357 *************** END"
1358 (org-element-type (org-element-at-point)))))
1360 (org-test-with-temp-text "
1361 *************** Task<point>
1362 DEADLINE: <2012-03-29 thu.>"
1363 (org-element-property :deadline
(org-element-at-point))))
1366 (org-test-with-temp-text "
1367 *************** Task
1368 <point>DEADLINE: <2012-03-29 thu.>"
1369 (org-element-type (org-element-at-point)))))
1374 (org-test-with-temp-text "
1375 *************** [#A] Task"
1377 (org-element-property :priority
(org-element-at-point)))))
1382 (org-test-with-temp-text "
1383 *************** Task :test:"
1385 (org-element-property :tags
(org-element-at-point)))))
1386 ;; Regular properties are accessed through upper case keywords.
1388 (org-test-with-temp-text "
1389 *************** Task
1393 *************** END"
1395 (org-element-property :FOO
(org-element-at-point))))
1397 (org-test-with-temp-text "
1398 *************** Task
1402 *************** END"
1404 (org-element-property :foo
(org-element-at-point))))
1405 ;; Handle non-empty blank line at the end of buffer.
1407 (org-test-with-temp-text "*************** Task\n*************** END\n "
1408 (= (org-element-property :end
(org-element-at-point)) (point-max)))))))
1413 (ert-deftest test-org-element
/italic-parser
()
1414 "Test `italic' parser."
1417 (org-test-with-temp-text "/italic/"
1418 (org-element-map (org-element-parse-buffer) 'italic
#'identity nil t
)))
1419 ;; Multi-line markup.
1422 (org-element-contents
1423 (org-test-with-temp-text "/first line\nsecond line/"
1424 (org-element-map (org-element-parse-buffer) 'italic
#'identity nil t
)))
1425 '("first line\nsecond line"))))
1430 (ert-deftest test-org-element
/item-parser
()
1431 "Test `item' parser."
1434 (org-test-with-temp-text "- item"
1435 (org-element-map (org-element-parse-buffer) 'item
'identity
)))
1439 (org-element-property
1441 (org-test-with-temp-text "6. [@6] item"
1442 (org-element-map (org-element-parse-buffer) 'item
'identity nil t
)))))
1447 (org-element-property
1449 (org-test-with-temp-text "- tag :: description"
1450 (org-element-map (org-element-parse-buffer) 'item
'identity nil t
)))))
1451 ;; No tags in ordered lists.
1453 (org-element-property
1455 (org-test-with-temp-text "1. tag :: description"
1456 (org-element-map (org-element-parse-buffer) 'item
'identity nil t
))))
1461 (org-test-with-temp-text "
1465 (org-element-map (org-element-parse-buffer) 'item
1466 (lambda (item) (org-element-property :checkbox item
))))))
1467 ;; Item starting with special syntax.
1469 (equal '(("- item"))
1470 (org-test-with-temp-text "- - item"
1471 (org-element-map (org-element-parse-buffer) 'paragraph
1472 'org-element-contents
))))
1473 ;; Block in an item: ignore indentation within the block.
1475 (org-test-with-temp-text
1476 "-<point> item\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1477 (= (org-element-property :end
(org-element-at-point)) (point-max))))
1478 ;; Parse `:pre-blank'.
1481 (org-test-with-temp-text "-<point> A"
1482 (org-element-property :pre-blank
(org-element-at-point)))))
1485 (org-test-with-temp-text "-<point>\n A"
1486 (org-element-property :pre-blank
(org-element-at-point)))))
1489 (org-test-with-temp-text "-<point>\n\n A"
1490 (org-element-property :pre-blank
(org-element-at-point)))))
1491 ;; Last item in a list or sub-list has no `:post-blank' lines, since
1492 ;; those belong to the plain-list.
1495 (org-test-with-temp-text "- A\n\n- <point>B\n\nEnd list"
1496 (org-element-property :post-blank
(org-element-at-point)))))
1499 (org-test-with-temp-text "- A\n\n - B\n\n<point> - C\n\n End sub-list"
1500 (org-element-property :post-blank
(org-element-at-point))))))
1505 (ert-deftest test-org-element
/keyword-parser
()
1506 "Test `keyword' parser."
1509 (org-test-with-temp-text "#+KEYWORD: value"
1510 (org-element-map (org-element-parse-buffer) 'keyword
'identity
)))
1511 ;; Keywords are case-insensitive.
1513 (org-test-with-temp-text "#+keyword: value"
1514 (org-element-map (org-element-parse-buffer) 'keyword
'identity
)))
1515 ;; Affiliated keywords are not keywords.
1517 (org-test-with-temp-text "#+NAME: value
1519 (org-element-map (org-element-parse-buffer) 'keyword
'identity
)))
1520 ;; Do not mix keywords with Babel calls and dynamic blocks.
1522 (org-test-with-temp-text "#+CALL: fun()"
1523 (org-element-map (org-element-parse-buffer) 'keyword
'identity
)))
1525 (org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
1526 (org-element-map (org-element-parse-buffer) 'keyword
'identity
)))
1527 ;; Handle non-empty blank line at the end of buffer.
1529 (org-test-with-temp-text "#+KEYWORD: value\n "
1530 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1533 ;;;; Latex Environment
1535 (ert-deftest test-org-element
/latex-environment-parser
()
1536 "Test `latex-environment' parser."
1538 (org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
1539 (org-element-map (org-element-parse-buffer) 'latex-environment
'identity
)))
1540 ;; Allow nested environments.
1548 (org-test-with-temp-text "
1554 (org-element-property
1557 (org-element-parse-buffer) 'latex-environment
'identity nil t
)))))
1558 ;; Allow environments with options and arguments.
1560 (eq 'latex-environment
1561 (org-test-with-temp-text
1562 "\\begin{theorem}[Euler]\ne^{i\\pi}+1=0\n\\end{theorem}"
1563 (org-element-type (org-element-at-point)))))
1565 (eq 'latex-environment
1566 (org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
1567 (org-element-type (org-element-at-point)))))
1568 ;; Allow environments without newline after \begin{.}.
1570 (eq 'latex-environment
1571 (org-test-with-temp-text "\\begin{env}{arg}something\nvalue\n\\end{env}"
1572 (org-element-type (org-element-at-point)))))
1573 ;; Allow one-line environments.
1575 (eq 'latex-environment
1576 (org-test-with-temp-text "\\begin{env}{arg}something\\end{env}"
1577 (org-element-type (org-element-at-point)))))
1578 ;; Should not allow different tags.
1580 (eq 'latex-environment
1581 (org-test-with-temp-text "\\begin{env*}{arg}something\\end{env}"
1582 (org-element-type (org-element-at-point)))))
1583 ;; LaTeX environments must be on separate lines.
1585 (eq 'latex-environment
1586 (org-test-with-temp-text "\\begin{env} x \\end{env} y"
1587 (org-element-type (org-element-at-point)))))
1589 (eq 'latex-environment
1590 (org-test-with-temp-text "y \\begin{env} x<point> \\end{env}"
1591 (org-element-type (org-element-at-point)))))
1592 ;; Handle non-empty blank line at the end of buffer.
1594 (org-test-with-temp-text "\\begin{env}\n\\end{env}\n "
1595 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1600 (ert-deftest test-org-element
/latex-fragment-parser
()
1601 "Test `latex-fragment' parser."
1602 ;; Basic $...$ test.
1605 (org-test-with-temp-text "$a$"
1606 (org-element-type (org-element-context)))))
1607 ;; Test valid characters after $...$ construct.
1610 (org-test-with-temp-text "$a$a"
1611 (org-element-type (org-element-context)))))
1614 (org-test-with-temp-text "$a$!"
1615 (org-element-type (org-element-context)))))
1618 (org-test-with-temp-text "$a$,"
1619 (org-element-type (org-element-context)))))
1622 (org-test-with-temp-text "$a$\""
1623 (org-element-type (org-element-context)))))
1626 (org-test-with-temp-text "$a$)"
1627 (org-element-type (org-element-context)))))
1630 (org-test-with-temp-text "$a$ "
1631 (org-element-type (org-element-context)))))
1634 (org-test-with-temp-text "$a$'"
1635 (org-element-type (org-element-context)))))
1636 ;; Test forbidden characters inside $...$.
1639 (org-test-with-temp-text "$.a$"
1640 (org-element-type (org-element-context)))))
1643 (org-test-with-temp-text "$,a$"
1644 (org-element-type (org-element-context)))))
1647 (org-test-with-temp-text "$;a$"
1648 (org-element-type (org-element-context)))))
1651 (org-test-with-temp-text "$ a$"
1652 (org-element-type (org-element-context)))))
1655 (org-test-with-temp-text "$a.$"
1656 (org-element-type (org-element-context)))))
1659 (org-test-with-temp-text "$a,$"
1660 (org-element-type (org-element-context)))))
1663 (org-test-with-temp-text "$a $"
1664 (org-element-type (org-element-context)))))
1668 (org-test-with-temp-text "$$a$$"
1669 (org-element-type (org-element-context)))))
1673 (org-test-with-temp-text "\\(a\\)"
1674 (org-element-type (org-element-context)))))
1678 (org-test-with-temp-text "\\[a\\]"
1679 (org-element-type (org-element-context)))))
1680 ;; Test fragment at the beginning of an item.
1683 (org-test-with-temp-text "- $<point>x$"
1684 (org-element-type (org-element-context))))))
1689 (ert-deftest test-org-element
/line-break-parser
()
1690 "Test `line-break' parser."
1693 (org-test-with-temp-text "Text \\\\"
1694 (org-element-map (org-element-parse-buffer) 'line-break
'identity
)))
1695 ;; Line break with trailing white spaces.
1697 (org-test-with-temp-text "Text \\\\ "
1698 (org-element-map (org-element-parse-buffer) 'line-break
'identity
)))
1699 ;; Three backslashes are too much.
1701 (org-test-with-temp-text "Text \\\\\\"
1702 (org-element-map (org-element-parse-buffer) 'line-break
'identity
))))
1707 (ert-deftest test-org-element
/link-parser
()
1708 "Test `link' parser."
1713 (org-test-with-temp-text "<<<radio>>>A radio link"
1714 (org-update-radio-target-regexp)
1715 (org-element-property
1717 (org-element-map (org-element-parse-buffer) 'link
#'identity nil t
)))))
1718 ;; Pathological case: radio target of length 1 at beginning of line
1719 ;; not followed by spaces.
1721 (org-test-with-temp-text "* <<<a>>>\n<point>a-bug"
1722 (org-update-radio-target-regexp)
1723 (org-element-parse-buffer)))
1724 ;; Pathological case: radio target in an emphasis environment.
1727 (org-test-with-temp-text "* <<<radio>>>\n<point>*radio*"
1728 (org-update-radio-target-regexp)
1729 (org-element-type (org-element-context)))))
1732 (org-test-with-temp-text "* <<<radio>>>\n*<point>radio*"
1733 (org-update-radio-target-regexp)
1734 (org-element-type (org-element-context)))))
1737 ;; ... with description.
1741 (org-test-with-temp-text "[[https://orgmode.org][Orgmode.org]]"
1742 (org-element-contents
1743 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1744 ;; ... without description.
1748 (org-test-with-temp-text "[[https://orgmode.org]]"
1749 (org-element-property
1751 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1752 ;; ... with expansion.
1755 "//orgmode.org/worg"
1756 (org-test-with-temp-text "[[Org:worg]]"
1757 (let ((org-link-abbrev-alist '(("Org" .
"https://orgmode.org/"))))
1758 (org-element-property
1760 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
))))))
1761 ;; ... with translation.
1765 (org-test-with-temp-text "[[https://orgmode.org]]"
1766 (let ((org-link-translation-function
1767 (lambda (type _
) (cons type
"127.0.0.1"))))
1768 (org-element-property
1770 (org-element-map (org-element-parse-buffer) 'link
1771 #'identity nil t
))))))
1772 ;; ... custom-id link.
1776 (org-test-with-temp-text "[[#some-id]]"
1777 (org-element-property
1779 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1780 ;; ... coderef link.
1784 (org-test-with-temp-text "[[(reference)]]"
1785 (org-element-property
1787 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1792 (org-test-with-temp-text "[[target-or-title]]"
1793 (org-element-property
1795 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1796 ;; ... file-type link with search option.
1799 '(("file" "projects.org" "*task title"))
1800 (org-test-with-temp-text "[[file:projects.org::*task title]]"
1801 (org-element-map (org-element-parse-buffer) 'link
1802 (lambda (l) (list (org-element-property :type l
)
1803 (org-element-property :path l
)
1804 (org-element-property :search-option l
)))))))
1805 ;; ... file-type link with application...
1808 '("file" "projects.org" "emacs")
1809 (org-test-with-temp-text "[[file+emacs:projects.org]]"
1810 (let ((l (org-element-context)))
1811 (list (org-element-property :type l
)
1812 (org-element-property :path l
)
1813 (org-element-property :application l
))))))
1814 ;; ... `:path' in a file-type link must be compatible with "file"
1815 ;; scheme in URI syntax, even if Org syntax isn't...
1817 (org-test-with-temp-text-in-file ""
1818 (let ((file (expand-file-name (buffer-file-name))))
1819 (insert (format "[[file://%s]]" file
))
1820 (equal (org-element-property :path
(org-element-context)) file
))))
1822 (org-test-with-temp-text-in-file ""
1823 (let ((file (expand-file-name (buffer-file-name))))
1824 (insert (format "[[file:%s]]" file
))
1825 (equal (org-element-property :path
(org-element-context)) file
))))
1827 (org-test-with-temp-text-in-file ""
1828 (let ((file (expand-file-name (buffer-file-name))))
1829 (insert (format "[[file:%s]]" file
))
1830 (equal (org-element-property :path
(org-element-context)) file
))))
1831 ;; ... multi-line link.
1834 (org-test-with-temp-text "[[shell:ls\n*.org]]"
1835 (org-element-property :path
(org-element-context)))))
1838 (org-test-with-temp-text "A link: https://orgmode.org"
1839 (org-element-map (org-element-parse-buffer) 'link
'identity
)))
1840 ;; Angular link. Follow RFC 3986.
1843 (org-test-with-temp-text "A link: <point><https://orgmode.org>"
1844 (org-element-type (org-element-context)))))
1846 (equal "//orgmode.org"
1847 (org-test-with-temp-text "A link: <point><https://orgmode\n.org>"
1848 (org-element-property :path
(org-element-context)))))
1849 ;; Link abbreviation.
1852 (org-test-with-temp-text
1853 "#+LINK: orgmode https://www.orgmode.org/\n[[orgmode:#docs]]"
1854 (progn (org-mode-restart)
1855 (goto-char (1- (point-max)))
1856 (org-element-property :type
(org-element-context))))))
1857 ;; Link abbreviation in a secondary string.
1860 (org-test-with-temp-text
1861 "#+LINK: orgmode https://www.orgmode.org/\n* H [[orgmode:#docs]]"
1862 (progn (org-mode-restart)
1863 (org-element-map (org-element-parse-buffer) 'link
1864 (lambda (link) (org-element-property :type link
))
1870 (ert-deftest test-org-element
/macro-parser
()
1871 "Test `macro' parser."
1872 ;; Without arguments.
1874 (org-test-with-temp-text "{{{macro}}}"
1875 (org-element-map (org-element-parse-buffer) 'macro
'identity
)))
1878 (org-test-with-temp-text "{{{macro(arg1,arg2)}}}"
1879 (org-element-map (org-element-parse-buffer) 'macro
'identity
)))
1880 ;; Properly handle protected commas in arguments...
1884 (org-test-with-temp-text "{{{macro(arg1\\,arg1,arg2)}}}"
1885 (org-element-property :args
(org-element-context))))))
1886 ;; ... even when last argument ends with a protected comma.
1889 (org-test-with-temp-text "{{{macro(C-\\,)}}}"
1890 (org-element-property :args
(org-element-context)))))
1891 ;; Allow to escape escaping character.
1894 (org-test-with-temp-text "{{{macro(C-\\\\,)}}}"
1895 (org-element-property :args
(org-element-context)))))
1896 ;; No need to escape backslashes elsewhere.
1899 (org-test-with-temp-text "{{{macro(\\)}}}"
1900 (org-element-property :args
(org-element-context))))))
1905 (ert-deftest test-org-element
/node-property
()
1906 "Test `node-property' parser."
1909 (equal '("abc" "value")
1910 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:abc: value\n:END:"
1911 (let ((element (org-element-at-point)))
1912 (list (org-element-property :key element
)
1913 (org-element-property :value element
))))))
1914 ;; Value should be trimmed.
1917 (org-test-with-temp-text
1918 "* H\n:PROPERTIES:\n<point>:abc: value \n:END:"
1919 (org-element-property :value
(org-element-at-point)))))
1920 ;; A node property requires to be wrapped within a property drawer.
1923 (org-test-with-temp-text ":abc: value"
1924 (org-element-type (org-element-at-point)))))
1925 ;; Accept empty properties.
1927 (equal '(("foo" "value") ("bar" ""))
1928 (org-test-with-temp-text "* H\n:PROPERTIES:\n:foo: value\n:bar:\n:END:"
1929 (org-element-map (org-element-parse-buffer) 'node-property
1931 (list (org-element-property :key p
)
1932 (org-element-property :value p
))))))))
1937 (ert-deftest test-org-element
/paragraph-parser
()
1938 "Test `paragraph' parser."
1941 (org-test-with-temp-text "Paragraph"
1942 (org-element-map (org-element-parse-buffer) 'paragraph
'identity nil t
)))
1943 ;; Property find end of a paragraph stuck to another element.
1946 (org-test-with-temp-text "Paragraph\n# Comment"
1947 (org-element-map (org-element-parse-buffer) 'paragraph
1948 (lambda (p) (char-after (org-element-property :end p
)))
1950 ;; Include ill-formed Keywords.
1952 (org-test-with-temp-text "#+wrong_keyword something"
1953 (org-element-map (org-element-parse-buffer) 'paragraph
'identity
)))
1954 ;; Include incomplete-drawers.
1956 (org-test-with-temp-text ":TEST:\nParagraph"
1957 (let ((elem (org-element-at-point)))
1958 (and (eq (org-element-type elem
) 'paragraph
)
1959 (= (point-max) (org-element-property :end elem
))))))
1960 ;; Include incomplete blocks.
1962 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph"
1963 (let ((elem (org-element-at-point)))
1964 (and (eq (org-element-type elem
) 'paragraph
)
1965 (= (point-max) (org-element-property :end elem
))))))
1966 ;; Include incomplete dynamic blocks.
1968 (org-test-with-temp-text "#+BEGIN: \nParagraph"
1969 (let ((elem (org-element-at-point)))
1970 (and (eq (org-element-type elem
) 'paragraph
)
1971 (= (point-max) (org-element-property :end elem
))))))
1972 ;; Include incomplete latex environments.
1974 (org-test-with-temp-text "\begin{equation}\nParagraph"
1975 (let ((elem (org-element-at-point)))
1976 (and (eq (org-element-type elem
) 'paragraph
)
1977 (= (point-max) (org-element-property :end elem
))))))
1979 (org-test-with-temp-text "Paragraph\n\begin{equation}"
1980 (let ((elem (org-element-at-point)))
1981 (and (eq (org-element-type elem
) 'paragraph
)
1982 (= (point-max) (org-element-property :end elem
))))))
1983 ;; Stop at affiliated keywords.
1985 (org-test-with-temp-text "Paragraph\n#+NAME: test\n| table |"
1986 (let ((elem (org-element-at-point)))
1987 (and (eq (org-element-type elem
) 'paragraph
)
1988 (not (org-element-property :name elem
))
1989 (= (org-element-property :end elem
) (line-beginning-position 2))))))
1991 (org-test-with-temp-text
1992 "Paragraph\n#+CAPTION[with short caption]: test\n| table |"
1993 (let ((elem (org-element-at-point)))
1994 (and (eq (org-element-type elem
) 'paragraph
)
1995 (not (org-element-property :name elem
))
1996 (= (org-element-property :end elem
) (line-beginning-position 2))))))
1997 ;; Do not steal affiliated keywords from container.
1999 (org-test-with-temp-text "#+ATTR_LATEX: test\n- item<point> 1"
2000 (let ((elem (org-element-at-point)))
2001 (and (eq (org-element-type elem
) 'paragraph
)
2002 (not (org-element-property :attr_latex elem
))
2003 (/= (org-element-property :begin elem
) 1)))))
2004 ;; Handle non-empty blank line at the end of buffer.
2006 (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
2007 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
2012 (ert-deftest test-org-element
/plain-list-parser
()
2013 "Test `plain-list' parser."
2014 (org-test-with-temp-text "- item"
2015 (should (org-element-map (org-element-parse-buffer) 'plain-list
'identity
)))
2016 ;; Blank lines after a list or sub-list belongs to that list.
2019 (org-test-with-temp-text "- A\n\n- B\n\nEnd list"
2020 (org-element-property :post-blank
(org-element-at-point)))))
2023 (org-test-with-temp-text "- A\n\n<point> - B\n\n - C\n\n End sub-list"
2024 (org-element-property :post-blank
(org-element-at-point)))))
2025 ;; Blank lines after the list only belong to outer plain list,
2030 (org-test-with-temp-text "
2035 (let ((endings (org-element-map (org-element-parse-buffer) 'plain-list
2036 (lambda (pl) (org-element-property :end pl
)))))
2038 ;; Move to ending of outer list.
2039 (progn (goto-char (car endings
)) (looking-at "Outside list"))
2040 ;; Move to ending of inner list.
2041 (progn (goto-char (nth 1 endings
)) (looking-at "^$")))))))
2042 ;; Correctly compute end of list if it doesn't end at a line
2045 (org-test-with-temp-text "- list\n \n "
2046 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
2051 (ert-deftest test-org-element
/planning-parser
()
2052 "Test `planning' parser."
2053 ;; Test various keywords.
2055 (org-element-property
2057 (org-test-with-temp-text "* H\n<point>CLOSED: [2012-03-29 thu.]"
2058 (org-element-at-point))))
2060 (org-element-property
2062 (org-test-with-temp-text "* H\n<point>DEADLINE: <2012-03-29 thu.>"
2063 (org-element-at-point))))
2065 (org-element-property
2067 (org-test-with-temp-text "* H\n<point>SCHEDULED: <2012-03-29 thu.>"
2068 (org-element-at-point))))
2069 ;; Planning line only exists right after a headline.
2072 (org-test-with-temp-text "DEADLINE: <2012-03-29 thu.>"
2073 (org-element-type (org-element-at-point)))))
2076 (org-test-with-temp-text
2077 "* H\n# Comment\n<point>DEADLINE: <2012-03-29 thu.>"
2078 (org-element-type (org-element-at-point)))))
2081 (org-test-with-temp-text
2082 "* H\n\n<point>DEADLINE: <2012-03-29 thu.>"
2083 (org-element-type (org-element-at-point))))))
2086 ;;;; Property Drawer
2088 (ert-deftest test-org-element
/property-drawer-parser
()
2089 "Test `property-drawer' parser."
2092 (eq 'property-drawer
2093 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value\n:END:"
2094 (org-element-type (org-element-at-point)))))
2096 (eq 'property-drawer
2097 (org-test-with-temp-text
2098 "* H\nDEADLINE: <2014-03-04 tue.>\n<point>:PROPERTIES:\n:prop: value\n:END:"
2099 (org-element-type (org-element-at-point)))))
2100 ;; Allow properties without value and no property at all.
2102 (eq 'property-drawer
2103 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop:\n:END:"
2104 (org-element-type (org-element-at-point)))))
2106 (eq 'property-drawer
2107 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:"
2108 (org-element-type (org-element-at-point)))))
2109 ;; Ignore incomplete drawer, drawer at a wrong location or with
2112 (eq 'property-drawer
2113 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value"
2114 (org-element-type (org-element-at-point)))))
2116 (eq 'property-drawer
2117 (org-test-with-temp-text
2118 "* H\nParagraph\n<point>:PROPERTIES:\n:prop: value\n:END:"
2119 (org-element-type (org-element-at-point)))))
2121 (eq 'property-drawer
2122 (org-test-with-temp-text
2123 "* H\nParagraph\n<point>:PROPERTIES:\nparagraph\n:END:"
2124 (org-element-type (org-element-at-point)))))
2126 (eq 'property-drawer
2127 (org-test-with-temp-text
2128 "* H\n\n<point>:PROPERTIES:\n:prop: value\n:END:"
2129 (org-element-type (org-element-at-point)))))
2130 ;; Handle non-empty blank line at the end of buffer.
2132 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:\n "
2133 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
2138 (ert-deftest test-org-element
/quote-block-parser
()
2139 "Test `quote-block' parser."
2142 (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
2143 (org-element-map (org-element-parse-buffer) 'quote-block
'identity
)))
2144 ;; Ignore incomplete block.
2146 (org-test-with-temp-text "#+BEGIN_QUOTE"
2147 (org-element-map (org-element-parse-buffer) 'quote-block
'identity nil t
)))
2148 ;; Handle non-empty blank line at the end of buffer.
2150 (org-test-with-temp-text "#+BEGIN_QUOTE\nC\n#+END_QUOTE\n "
2151 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
2156 (ert-deftest test-org-element
/radio-target-parser
()
2157 "Test `radio-target' parser."
2161 (org-test-with-temp-text "<<<radio>>>"
2162 (org-element-type (org-element-context)))))
2163 ;; Radio targets with objects.
2166 (org-test-with-temp-text "<<<radio \\alpha>>>"
2167 (org-element-type (org-element-context)))))
2168 ;; Radio targets starting with an object.
2171 (org-test-with-temp-text "<<<\\alpha radio>>>"
2172 (org-element-type (org-element-context)))))
2173 ;; Radio targets cannot begin or end with white space.
2176 (org-test-with-temp-text "<<< radio>>>"
2177 (org-element-type (org-element-context)))))
2180 (org-test-with-temp-text "<<<radio >>>"
2181 (org-element-type (org-element-context))))))
2186 (ert-deftest test-org-element
/section-parser
()
2187 "Test `section' parser."
2190 (org-test-with-temp-text "* Headline\nText"
2191 (org-element-map (org-element-parse-buffer) 'section
'identity
)))
2192 ;; There's a section before the first headline.
2194 (org-test-with-temp-text "Text"
2195 (org-element-map (org-element-parse-buffer) 'section
'identity
)))
2196 ;; A section cannot be empty.
2198 (org-test-with-temp-text "* Headline 1\n* Headline 2"
2199 (org-element-map (org-element-parse-buffer) 'section
'identity
)))
2200 ;; A section doesn't contain sub-trees.
2202 (org-test-with-temp-text "* Head\nText\n** Sub-Head"
2204 (org-element-map (org-element-parse-buffer) 'section
'identity nil t
)
2205 'headline
'identity
))))
2210 (ert-deftest test-org-element
/special-block-parser
()
2211 "Test `special-block' parser."
2215 (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
2216 (org-element-property :type
(org-element-at-point)))))
2217 ;; Special blocks are case sensitive.
2220 (org-test-with-temp-text "#+BEGIN_CaSe\nText\n#+END_CaSe"
2221 (org-element-property :type
(org-element-at-point)))))
2222 ;; Special blocks can contain paragraphs.
2225 (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
2227 (org-element-type (org-element-at-point)))))
2228 ;; Ignore incomplete block.
2231 (org-test-with-temp-text "#+BEGIN_SPECIAL"
2232 (org-element-type (org-element-at-point)))))
2233 ;; Allow special characters in type.
2235 (equal '(special-block "SPECIAL*")
2236 (org-test-with-temp-text "#+BEGIN_SPECIAL*\nContents\n#+END_SPECIAL*"
2237 (let ((element (org-element-at-point)))
2238 (list (org-element-type element
)
2239 (org-element-property :type element
))))))
2240 ;; Handle non-empty blank line at the end of buffer.
2242 (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n "
2243 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
2248 (ert-deftest test-org-element
/src-block-parser
()
2249 "Test `src-block' parser."
2252 (org-test-with-temp-text "#+BEGIN_SRC org\nText\n#+END_SRC"
2253 (org-element-map (org-element-parse-buffer) 'src-block
'identity
)))
2254 ;; Ignore incomplete block.
2256 (org-test-with-temp-text "#+BEGIN_SRC"
2257 (org-element-map (org-element-parse-buffer) 'src-block
'identity
)))
2258 ;; Properly un-escape code.
2260 (equal "* Headline\n #+keyword\nText\n"
2261 (org-test-with-temp-text
2262 "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
2263 (org-element-property :value
(org-element-at-point)))))
2264 ;; Handle non-empty blank line at the end of buffer.
2266 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\nC\n#+END_SRC\n "
2267 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
2270 ;;;; Statistics Cookie
2272 (ert-deftest test-org-element
/statistics-cookie
()
2273 "Test `statistics-cookie' parser."
2276 (org-test-with-temp-text "[1/2]"
2277 (org-element-map (org-element-parse-buffer) 'statistics-cookie
'identity
)))
2280 (org-test-with-temp-text "[33%]"
2282 (org-element-parse-buffer) 'statistics-cookie
'identity
))))
2287 (ert-deftest test-org-element
/strike-through-parser
()
2288 "Test `strike-through' parser."
2291 (org-test-with-temp-text "+strike-through+"
2292 (org-element-map (org-element-parse-buffer) 'strike-through
#'identity
)))
2293 ;; Multi-line markup.
2296 (org-element-contents
2297 (org-test-with-temp-text "+first line\nsecond line+"
2299 (org-element-parse-buffer) 'strike-through
#'identity nil t
)))
2300 '("first line\nsecond line"))))
2305 (ert-deftest test-org-element
/subscript-parser
()
2306 "Test `subscript' parser."
2309 (org-test-with-temp-text "a_b"
2310 (org-element-map (org-element-parse-buffer) 'subscript
'identity
)))
2313 (org-test-with-temp-text "a_{b}"
2314 (org-element-map (org-element-parse-buffer) 'subscript
'identity
)))
2315 ;; Multiple subscripts in a paragraph.
2318 (org-test-with-temp-text "a_b and c_d"
2320 (org-element-map (org-element-parse-buffer) 'subscript
'identity
))))))
2325 (ert-deftest test-org-element
/superscript-parser
()
2326 "Test `superscript' parser."
2329 (org-test-with-temp-text "a^b"
2330 (org-element-map (org-element-parse-buffer) 'superscript
'identity
)))
2333 (org-test-with-temp-text "a^{b}"
2334 (org-element-map (org-element-parse-buffer) 'superscript
'identity
)))
2335 ;; Multiple superscript in a paragraph.
2338 (org-test-with-temp-text "a^b and c^d"
2341 (org-element-parse-buffer) 'superscript
'identity
))))))
2346 (ert-deftest test-org-element
/table-parser
()
2347 "Test `table' parser."
2349 (org-test-with-temp-text "| a |"
2350 (org-element-map (org-element-parse-buffer) 'table
'identity
)))
2351 ;; TBLFM keyword is case insensitive.
2353 (org-test-with-temp-text "| a |\n#+tblfm: test"
2354 (org-element-property
2356 (org-element-map (org-element-parse-buffer) 'table
'identity nil t
))))
2357 ;; Handle multiple TBLFM lines.
2360 (org-test-with-temp-text "| a |\n#+TBLFM: test1\n#+TBLFM: test2"
2361 (length (org-element-property
2364 (org-element-parse-buffer) 'table
'identity nil t
))))))
2365 ;; Handle non-empty blank line at the end of buffer.
2367 (org-test-with-temp-text "| a |\n "
2368 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
2373 (ert-deftest test-org-element
/table-cell-parser
()
2374 "Test `table-cell' parser."
2375 ;; Regular table cell.
2377 (org-test-with-temp-text "| a |"
2378 (org-element-map (org-element-parse-buffer) 'table-cell
'identity
)))
2379 ;; Last vertical bar may be omitted.
2381 (org-test-with-temp-text "| a "
2382 (org-element-map (org-element-parse-buffer) 'table-cell
'identity
))))
2387 (ert-deftest test-org-element
/table-row-parser
()
2388 "Test `table-row' parser."
2390 (equal '(standard rule
)
2391 (org-test-with-temp-text "| a |\n|---|"
2393 (org-element-parse-buffer) 'table-row
2394 (lambda (row) (org-element-property :type row
)))))))
2399 (ert-deftest test-org-element
/target-parser
()
2400 "Test `target' parser."
2402 (org-test-with-temp-text "<<target>>"
2403 (org-element-map (org-element-parse-buffer) 'target
'identity
))))
2408 (ert-deftest test-org-element
/timestamp-parser
()
2409 "Test `timestamp' parser."
2410 ;; Active timestamp.
2412 (org-test-with-temp-text "<2012-03-29 16:40>"
2413 (eq (org-element-property :type
(org-element-context)) 'active
)))
2415 (org-test-with-temp-text "<2012-03-29 Thu>"
2416 (let ((timestamp (org-element-context)))
2417 (or (org-element-property :hour-start timestamp
)
2418 (org-element-property :minute-start timestamp
)))))
2420 (equal '(2012 3 29 16 40)
2421 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
2422 (let ((object (org-element-context)))
2423 (list (org-element-property :year-start object
)
2424 (org-element-property :month-start object
)
2425 (org-element-property :day-start object
)
2426 (org-element-property :hour-start object
)
2427 (org-element-property :minute-start object
))))))
2428 ;; Inactive timestamp.
2430 (org-test-with-temp-text "[2012-03-29 Thu 16:40]"
2431 (eq (org-element-property :type
(org-element-context)) 'inactive
)))
2434 (equal '(2012 3 29 16 40 7 30)
2435 (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
2436 (let ((object (org-element-context)))
2437 (list (org-element-property :year-end object
)
2438 (org-element-property :month-end object
)
2439 (org-element-property :day-end object
)
2440 (org-element-property :hour-end object
)
2441 (org-element-property :minute-end object
)
2442 (org-element-property :hour-start object
)
2443 (org-element-property :minute-start object
))))))
2446 (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
2447 (org-element-property :type
(org-element-context)))))
2450 (org-test-with-temp-text "[2012-03-29 Thu 16:40]--[2012-03-29 Thu 16:41]"
2451 (eq (org-element-property :type
(org-element-context)) 'inactive-range
)))
2453 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
2454 (let ((timestamp (org-element-context)))
2455 (or (org-element-property :hour-end timestamp
)
2456 (org-element-property :minute-end timestamp
)))))
2457 ;; With repeater, warning delay and both.
2460 (org-test-with-temp-text "<2012-03-29 Thu ++1y>"
2461 (org-element-property :repeater-type
(org-element-context)))))
2464 (org-test-with-temp-text "<2012-03-29 Thu --1y>"
2465 (org-element-property :warning-type
(org-element-context)))))
2467 (equal '(cumulate all
)
2468 (org-test-with-temp-text "<2012-03-29 Thu +1y -1y>"
2469 (let ((ts (org-element-context)))
2470 (list (org-element-property :repeater-type ts
)
2471 (org-element-property :warning-type ts
)))))))
2476 (ert-deftest test-org-element
/underline-parser
()
2477 "Test `underline' parser."
2480 (org-test-with-temp-text "_underline_"
2481 (org-element-map (org-element-parse-buffer) 'underline
#'identity
)))
2482 ;; Multi-line markup.
2485 (org-element-contents
2486 (org-test-with-temp-text "_first line\nsecond line_"
2488 (org-element-parse-buffer) 'underline
#'identity nil t
)))
2489 '("first line\nsecond line"))))
2494 (ert-deftest test-org-element
/verbatim-parser
()
2495 "Test `verbatim' parser."
2498 (org-test-with-temp-text "=verbatim="
2499 (org-element-map (org-element-parse-buffer) 'verbatim
#'identity
)))
2500 ;; Multi-line markup.
2503 (org-element-property
2505 (org-test-with-temp-text "=first line\nsecond line="
2506 (org-element-map (org-element-parse-buffer) 'verbatim
#'identity nil t
)))
2507 "first line\nsecond line")))
2512 (ert-deftest test-org-element
/verse-block-parser
()
2513 "Test `verse-block' parser."
2516 (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
2517 (org-element-map (org-element-parse-buffer) 'verse-block
'identity
)))
2520 (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
2521 (org-element-map (org-element-parse-buffer) 'verse-block
'identity
)))
2522 ;; Parse objects in verse blocks.
2524 (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
2525 (org-element-map (org-element-parse-buffer) 'entity
'identity
)))
2526 ;; Ignore incomplete verse block.
2528 (org-test-with-temp-text "#+BEGIN_VERSE"
2529 (org-element-map (org-element-parse-buffer) 'verse-block
'identity nil t
)))
2530 ;; Handle non-empty blank line at the end of buffer.
2532 (org-test-with-temp-text "#+BEGIN_VERSE\nC\n#+END_VERSE\n "
2533 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
2537 ;;; Test Interpreters.
2539 (ert-deftest test-org-element
/interpret-data
()
2540 "Test `org-element-interpret-data' specifications."
2541 ;; Interpret simple affiliated keywords.
2544 (org-element-interpret-data
2545 '(org-data nil
(paragraph (:name
"para") "Paragraph")))
2546 "#+name: para\nParagraph\n"))
2547 ;; Interpret multiple affiliated keywords.
2550 (org-element-interpret-data
2551 '(org-data nil
(paragraph (:attr_ascii
("line2" "line1")) "Paragraph")))
2552 "#+attr_ascii: line1\n#+attr_ascii: line2\nParagraph\n"))
2553 ;; Interpret parsed affiliated keywords.
2556 (org-element-interpret-data
2557 '(org-data nil
(paragraph (:caption
(("caption"))) "Paragraph")))
2558 "#+caption: caption\nParagraph\n"))
2559 ;; Interpret dual affiliated keywords.
2562 (org-element-interpret-data
2563 '(org-data nil
(paragraph (:caption
((("long") "short"))) "Paragraph")))
2564 "#+caption[short]: long\nParagraph\n"))
2565 ;; Interpret multiple parsed dual keywords.
2568 (org-element-interpret-data
2569 '(org-data nil
(paragraph
2570 (:caption
((("l2") "s2") (("l1") "s1"))) "Paragraph")))
2571 "#+caption[s1]: l1\n#+caption[s2]: l2\nParagraph\n"))
2572 ;; Pseudo objects and elements are transparent.
2576 (org-element-interpret-data
2577 '(paragraph nil
(pseudo-object (:post-blank
1) "A") "B")))))
2580 (org-element-interpret-data
2582 (pseudo-element (:post-blank
1) (paragraph nil
"A"))
2583 (paragraph nil
"B"))))))
2585 (ert-deftest test-org-element
/center-block-interpreter
()
2586 "Test center block interpreter."
2588 (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
2589 "#+begin_center\nTest\n#+end_center\n")))
2591 (ert-deftest test-org-element
/drawer-interpreter
()
2592 "Test drawer interpreter."
2594 (equal (org-test-parse-and-interpret ":TEST:\nTest\n:END:")
2595 ":TEST:\nTest\n:END:\n")))
2597 (ert-deftest test-org-element
/dynamic-block-interpreter
()
2598 "Test dynamic block interpreter."
2600 (equal (org-test-parse-and-interpret
2601 "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
2602 "#+begin: myblock :parameter value1\nTest\n#+end:\n")))
2604 (ert-deftest test-org-element
/footnote-definition-interpreter
()
2605 "Test footnote definition interpreter."
2606 (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n"))
2607 ;; Handle `:pre-blank' in definitions.
2609 (equal (org-test-parse-and-interpret "[fn:1]\nparagraph")
2610 "[fn:1]\nparagraph\n"))
2612 (equal (org-test-parse-and-interpret "[fn:1]\n\nparagraph")
2613 "[fn:1]\n\nparagraph\n")))
2615 (ert-deftest test-org-element
/headline-interpreter
()
2616 "Test headline and section interpreters."
2617 ;; 1. Standard test.
2618 (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
2619 ;; 2. With TODO keywords.
2621 (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
2622 (org-test-parse-and-interpret "* TODO Headline"))
2623 "* TODO Headline\n"))
2626 ;; 3.1. ... and a positive `org-tags-column' value.
2628 (equal (let ((org-tags-column 20))
2629 (org-test-parse-and-interpret "* Headline :tag:"))
2630 "* Headline :tag:\n"))
2631 ;; 3.2. ... and a negative `org-tags-column' value.
2633 (equal (let ((org-tags-column -
20))
2634 (org-test-parse-and-interpret "* Headline :tag:"))
2635 "* Headline :tag:\n"))
2636 ;; 3.3. ... and a null `org-tags-column' value.
2638 (equal (let ((org-tags-column 0))
2639 (org-test-parse-and-interpret "* Headline :tag:"))
2640 "* Headline :tag:\n"))
2641 ;; 4. With priority cookie.
2643 (equal (org-test-parse-and-interpret "* [#B] Headline")
2644 "* [#B] Headline\n"))
2645 ;; 5. With comment keyword.
2647 (equal (let ((org-comment-string "COMMENT"))
2648 (org-test-parse-and-interpret "* COMMENT Headline"))
2649 "* COMMENT Headline\n"))
2650 ;; 6. Keep same number of blank lines before body.
2652 (equal (org-test-parse-and-interpret
2653 "* Headline\n\n\nText after two blank lines.")
2654 "* Headline\n\n\nText after two blank lines.\n"))
2655 ;; 8. Preserve `org-odd-levels-only' state.
2657 (equal "* H\n*** H2\n"
2658 (let ((org-odd-levels-only t
))
2659 (org-test-parse-and-interpret "* H\n*** H2")))))
2661 (ert-deftest test-org-element
/inlinetask-interpreter
()
2662 "Test inlinetask interpretation."
2663 (when (featurep 'org-inlinetask
)
2664 (let ((org-inlinetask-min-level 15))
2665 ;; 1. Regular inlinetask.
2666 (should (equal (org-test-parse-and-interpret
2667 "*************** Task\nTest\n*************** END")
2668 "*************** Task\nTest\n*************** end\n"))
2669 ;; 2. Degenerate inlinetask.
2670 (should (equal (org-test-parse-and-interpret "*************** Task")
2671 "*************** Task\n"))
2672 ;; 3. Prefer degenerate form when there are no contents.
2673 (should (equal (org-test-parse-and-interpret
2674 "*************** Task\n*************** end")
2675 "*************** Task\n"))
2676 ;; 4. With TODO keywords.
2678 (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
2679 (org-test-parse-and-interpret "*************** TODO Task"))
2680 "*************** TODO Task\n"))
2683 ;; 5.1. ... and a positive `org-tags-column' value.
2685 (equal (let ((org-tags-column 30))
2686 (org-test-parse-and-interpret "*************** Task :tag:"))
2687 "*************** Task :tag:\n"))
2688 ;; 5.2. ... and a negative `org-tags-column' value.
2690 (equal (let ((org-tags-column -
30))
2691 (org-test-parse-and-interpret "*************** Task :tag:"))
2692 "*************** Task :tag:\n"))
2693 ;; 5.3. ... and a null `org-tags-column' value.
2695 (equal (let ((org-tags-column 0))
2696 (org-test-parse-and-interpret "*************** Task :tag:"))
2697 "*************** Task :tag:\n"))
2698 ;; 6. With priority cookie.
2700 (equal (org-test-parse-and-interpret "*************** [#B] Task")
2701 "*************** [#B] Task\n")))))
2703 (ert-deftest test-org-element
/plain-list-interpreter
()
2704 "Test plain-list and item interpreters."
2705 (let ((org-list-two-spaces-after-bullet-regexp nil
))
2707 (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
2708 ;; Description list.
2710 (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
2713 (equal (let ((org-plain-list-ordered-item-terminator t
))
2714 (org-test-parse-and-interpret "1. Item"))
2717 (equal (let ((org-plain-list-ordered-item-terminator ?\
)))
2718 (org-test-parse-and-interpret "1) Item"))
2720 ;; Ordered list with counter.
2722 (equal (let ((org-plain-list-ordered-item-terminator t
))
2723 (org-test-parse-and-interpret "1. [@5] Item"))
2725 ;; List with check-boxes.
2727 (equal (org-test-parse-and-interpret
2728 "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3")
2729 "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3\n"))
2730 ;; Item not starting with a paragraph.
2732 (equal (org-test-parse-and-interpret "-\n | a | b |")
2733 "- \n | a | b |\n"))
2734 ;; Handle `:pre-blank' in items.
2736 (equal (org-test-parse-and-interpret "-\n paragraph")
2737 "- \n paragraph\n"))
2739 (equal (org-test-parse-and-interpret "-\n\n paragraph")
2740 "- \n\n paragraph\n"))
2741 ;; Special case: correctly handle "*" bullets.
2742 (should (org-test-parse-and-interpret " * item"))
2743 ;; Special case: correctly handle empty items.
2744 (should (org-test-parse-and-interpret "-"))))
2746 (ert-deftest test-org-element
/quote-block-interpreter
()
2747 "Test quote block interpreter."
2748 (should (equal (org-test-parse-and-interpret
2749 "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
2750 "#+begin_quote\nTest\n#+end_quote\n")))
2752 (ert-deftest test-org-element
/special-block-interpreter
()
2753 "Test special block interpreter."
2754 (should (equal (org-test-parse-and-interpret
2755 "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
2756 "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n")))
2758 (ert-deftest test-org-element
/babel-call-interpreter
()
2759 "Test Babel call interpreter."
2760 ;; Without argument.
2761 (should (equal (org-test-parse-and-interpret "#+CALL: test()")
2762 "#+call: test()\n"))
2764 (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
2765 "#+call: test(x=2)\n"))
2766 ;; With header arguments.
2767 (should (equal (org-test-parse-and-interpret
2768 "#+CALL: test[:results output]() :results html")
2769 "#+call: test[:results output]() :results html\n")))
2771 (ert-deftest test-org-element
/clock-interpreter
()
2772 "Test clock interpreter."
2776 "CLOCK: \\[2012-01-01 .* 00:01\\]"
2777 (org-test-parse-and-interpret "CLOCK: [2012-01-01 sun. 00:01]")))
2781 "CLOCK: \\[2012-01-01 .* 00:01\\]--\\[2012-01-01 .* 00:02\\] => 0:01"
2782 (org-test-parse-and-interpret "
2783 CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))))
2785 (ert-deftest test-org-element
/comment-interpreter
()
2786 "Test comment interpreter."
2788 (should (equal (org-test-parse-and-interpret "# Comment") "# Comment\n"))
2790 (should (equal (org-test-parse-and-interpret " # Comment")
2792 ;; Preserve indentation.
2793 (should (equal (org-test-parse-and-interpret " # No blank\n# One blank")
2794 "# No blank\n# One blank\n")))
2796 (ert-deftest test-org-element
/comment-block-interpreter
()
2797 "Test comment block interpreter."
2798 (should (equal (org-test-parse-and-interpret
2799 "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
2800 "#+begin_comment\nTest\n#+end_comment\n"))
2801 ;; Accept missing final newline in value.
2803 (equal "#+begin_comment\nTest\n#+end_comment\n"
2804 (org-element-interpret-data '(comment-block (:value
"Test"))))))
2806 (ert-deftest test-org-element
/diary-sexp
()
2807 "Test diary-sexp interpreter."
2810 (org-test-parse-and-interpret
2811 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old")
2812 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old\n")))
2814 (ert-deftest test-org-element
/example-block-interpreter
()
2815 "Test example block interpreter."
2816 ;; Without switches.
2817 (should (equal "#+begin_example\nTest\n#+end_example\n"
2818 (let ((org-src-preserve-indentation t
))
2819 (org-test-parse-and-interpret
2820 "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"))))
2823 (equal "#+begin_example -n -k\n(+ 1 1)\n#+end_example\n"
2824 (let ((org-src-preserve-indentation t
))
2825 (org-test-parse-and-interpret
2826 "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE"))))
2827 ;; Preserve code escaping.
2830 (let ((org-src-preserve-indentation t
))
2831 (org-test-parse-and-interpret
2832 "#+BEGIN_EXAMPLE\n,* Headline\n,#+KEYWORD: value\nText\n#+END_EXAMPLE"))
2833 "#+begin_example\n,* Headline\n,#+KEYWORD: value\nText\n#+end_example\n"))
2834 ;; Accept missing final newline in value.
2837 "#+begin_example\nTest\n#+end_example\n"
2838 (let ((org-src-preserve-indentation t
))
2839 (org-element-interpret-data '(example-block (:value
"Test"))))))
2840 ;; Handle indentation.
2841 (should (equal "#+begin_example\n Test\n#+end_example\n"
2842 (let ((org-src-preserve-indentation nil
)
2843 (org-edit-src-content-indentation 2))
2844 (org-test-parse-and-interpret
2845 "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"))))
2846 (should (equal "#+begin_example\n Test\n#+end_example\n"
2847 (let ((org-src-preserve-indentation t
)
2848 (org-edit-src-content-indentation 2))
2849 (org-test-parse-and-interpret
2850 "#+BEGIN_EXAMPLE\n Test\n#+END_EXAMPLE")))))
2852 (ert-deftest test-org-element
/export-block-interpreter
()
2853 "Test export block interpreter."
2854 (should (equal (org-test-parse-and-interpret
2855 "#+begin_export HTML\nTest\n#+end_export")
2856 "#+begin_export HTML\nTest\n#+end_export\n")))
2858 (ert-deftest test-org-element
/fixed-width-interpreter
()
2859 "Test fixed width interpreter."
2861 (should (equal (org-test-parse-and-interpret ": Test") ": Test\n"))
2862 ;; Preserve indentation.
2863 (should (equal (org-test-parse-and-interpret ": 2 blanks\n: 1 blank")
2864 ": 2 blanks\n: 1 blank\n"))
2865 ;; Handle empty string.
2867 (equal (org-element-fixed-width-interpreter
2868 '(fixed-width (:value
"")) nil
)
2870 ;; Handle nil value.
2872 (org-element-fixed-width-interpreter
2873 '(fixed-width (:value nil
)) nil
)))
2875 (ert-deftest test-org-element
/horizontal-rule-interpreter
()
2876 "Test horizontal rule interpreter."
2877 (should (equal (org-test-parse-and-interpret "-------") "-----\n")))
2879 (ert-deftest test-org-element
/keyword-interpreter
()
2880 "Test keyword interpreter."
2881 (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
2882 "#+keyword: value\n")))
2884 (ert-deftest test-org-element
/latex-environment-interpreter
()
2885 "Test latex environment interpreter."
2886 (should (equal (org-test-parse-and-interpret
2887 "\\begin{equation}\n1+1=2\n\\end{equation}")
2888 "\\begin{equation}\n1+1=2\n\\end{equation}\n"))
2889 (should (equal (org-test-parse-and-interpret
2890 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}")
2891 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}\n")))
2893 (ert-deftest test-org-element
/planning-interpreter
()
2894 "Test planning interpreter."
2898 DEADLINE: <2012-03-29 .*?> SCHEDULED: <2012-03-29 .*?> CLOSED: \\[2012-03-29 .*?\\]"
2899 (org-test-parse-and-interpret
2901 DEADLINE: <2012-03-29 thu.> SCHEDULED: <2012-03-29 thu.> CLOSED: [2012-03-29 thu.]"))))
2903 (ert-deftest test-org-element
/property-drawer-interpreter
()
2904 "Test property drawer interpreter."
2905 (should (equal (let ((org-property-format "%-10s %s"))
2906 (org-test-parse-and-interpret
2907 "* H\n:PROPERTIES:\n:prop: value\n:END:"))
2908 "* H\n:PROPERTIES:\n:prop: value\n:END:\n")))
2910 (ert-deftest test-org-element
/src-block-interpreter
()
2911 "Test src block interpreter."
2914 (equal (let ((org-edit-src-content-indentation 2)
2915 (org-src-preserve-indentation nil
))
2916 (org-test-parse-and-interpret
2917 "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC"))
2918 "#+begin_src emacs-lisp :results silent\n (+ 1 1)\n#+end_src\n"))
2921 (equal (let ((org-edit-src-content-indentation 2)
2922 (org-src-preserve-indentation nil
))
2923 (org-test-parse-and-interpret
2924 "#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
2925 "#+begin_src emacs-lisp -n -k\n (+ 1 1)\n#+end_src\n"))
2926 ;; Preserve code escaping.
2929 (let ((org-edit-src-content-indentation 2)
2930 (org-src-preserve-indentation nil
))
2931 (org-test-parse-and-interpret
2932 "#+BEGIN_SRC org\n,* Headline\n,#+KEYWORD: value\nText\n#+END_SRC"))
2933 "#+begin_src org\n ,* Headline\n ,#+KEYWORD: value\n Text\n#+end_src\n"))
2934 ;; Do not apply `org-edit-src-content-indentation' when preserving
2937 (equal (let ((org-edit-src-content-indentation 2)
2938 (org-src-preserve-indentation t
))
2939 (org-test-parse-and-interpret
2940 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"))
2941 "#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src\n"))
2943 (equal (let ((org-edit-src-content-indentation 2)
2944 (org-src-preserve-indentation nil
))
2945 (org-test-parse-and-interpret
2946 "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"))
2947 "#+begin_src emacs-lisp -i\n(+ 1 1)\n#+end_src\n"))
2948 ;; Accept missing final newline in value.
2951 "#+begin_src emacs-lisp\n Test\n#+end_src\n"
2952 (let ((org-edit-src-content-indentation 2)
2953 (org-src-preserve-indentation nil
))
2954 (org-element-interpret-data
2955 '(src-block (:language
"emacs-lisp" :value
"Test")))))))
2957 (ert-deftest test-org-element
/table-interpreter
()
2958 "Test table, table-row and table-cell interpreters."
2960 (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
2961 "| a | b |\n| c | d |\n"))
2962 ;; 2. With horizontal rules.
2963 (should (equal (org-test-parse-and-interpret
2964 "| a | b |\n|---+---|\n| c | d |")
2965 "| a | b |\n|---+---|\n| c | d |\n"))
2966 ;; 3. With meta-data.
2967 (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
2968 "| / | < | > |\n| * | 1 | 2 |\n"))
2969 ;; 4. With a formula.
2971 (equal (org-test-parse-and-interpret
2972 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)")
2973 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)\n"))
2974 ;; 5. With multiple formulas.
2976 (equal (org-test-parse-and-interpret
2977 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2")
2978 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2\n")))
2980 (ert-deftest test-org-element
/timestamp-interpreter
()
2981 "Test timestamp interpreter."
2984 (string-match "<2012-03-29 .* 16:40>"
2985 (org-test-parse-and-interpret "<2012-03-29 thu. 16:40>")))
2987 (string-match "<2012-03-29 .* 16:40>"
2988 (org-element-timestamp-interpreter
2990 (:type active
:year-start
2012 :month-start
3 :day-start
29
2991 :hour-start
16 :minute-start
40)) nil
)))
2994 (string-match "\\[2012-03-29 .* 16:40\\]"
2995 (org-test-parse-and-interpret "[2012-03-29 thu. 16:40]")))
2998 "\\[2012-03-29 .* 16:40\\]"
2999 (org-element-timestamp-interpreter
3001 (:type inactive
:year-start
2012 :month-start
3 :day-start
29
3002 :hour-start
16 :minute-start
40)) nil
)))
3005 (string-match "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
3006 (org-test-parse-and-interpret
3007 "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>")))
3010 "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
3011 (org-element-timestamp-interpreter
3013 (:type active-range
:year-start
2012 :month-start
3 :day-start
29
3014 :hour-start
16 :minute-start
40 :year-end
2012 :month-end
3
3015 :day-end
29 :hour-end
16 :minute-end
41)) nil
)))
3018 (string-match "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
3019 (org-test-parse-and-interpret
3020 "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]")))
3023 "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
3024 (org-element-timestamp-interpreter
3026 (:type inactive-range
:year-start
2012 :month-start
3 :day-start
29
3027 :hour-start
16 :minute-start
40 :year-end
2012 :month-end
3
3028 :day-end
29 :hour-end
16 :minute-end
41)) nil
)))
3030 (should (equal (org-test-parse-and-interpret "<%%diary-float t 4 2>")
3031 "<%%diary-float t 4 2>\n"))
3032 ;; Timestamp with repeater interval, with delay, with both.
3034 (string-match "<2012-03-29 .* \\+1y>"
3035 (org-test-parse-and-interpret "<2012-03-29 thu. +1y>")))
3038 "<2012-03-29 .* \\+1y>"
3039 (org-element-timestamp-interpreter
3041 (:type active
:year-start
2012 :month-start
3 :day-start
29
3042 :repeater-type cumulate
:repeater-value
1 :repeater-unit year
))
3046 "<2012-03-29 .* -1y>"
3047 (org-element-timestamp-interpreter
3049 (:type active
:year-start
2012 :month-start
3 :day-start
29
3050 :warning-type all
:warning-value
1 :warning-unit year
))
3054 "<2012-03-29 .* \\+1y -1y>"
3055 (org-element-timestamp-interpreter
3057 (:type active
:year-start
2012 :month-start
3 :day-start
29
3058 :warning-type all
:warning-value
1 :warning-unit year
3059 :repeater-type cumulate
:repeater-value
1 :repeater-unit year
))
3061 ;; Timestamp range with repeater interval
3063 (string-match "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
3064 (org-test-parse-and-interpret
3065 "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>")))
3068 "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
3069 (org-element-timestamp-interpreter
3071 (:type active-range
:year-start
2012 :month-start
3 :day-start
29
3072 :year-end
2012 :month-end
3 :day-end
30 :repeater-type cumulate
3073 :repeater-value
1 :repeater-unit year
))
3076 (ert-deftest test-org-element
/verse-block-interpreter
()
3077 "Test verse block interpretation."
3079 (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
3080 "#+begin_verse\nTest\n#+end_verse\n")))
3082 (ert-deftest test-org-element
/bold-interpreter
()
3083 "Test bold interpreter."
3084 (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))
3086 (ert-deftest test-org-element
/code-interpreter
()
3087 "Test code interpreter."
3088 (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))
3090 (ert-deftest test-org-element
/entity-interpreter
()
3091 "Test entity interpreter."
3092 ;; 1. Without brackets.
3094 (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
3095 ;; 2. With brackets.
3097 (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))
3099 (ert-deftest test-org-element
/export-snippet-interpreter
()
3100 "Test export snippet interpreter."
3101 (should (equal (org-test-parse-and-interpret "@@back-end:contents@@")
3102 "@@back-end:contents@@\n")))
3104 (ert-deftest test-org-element
/footnote-reference-interpreter
()
3105 "Test footnote reference interpreter."
3106 ;; Regular reference.
3107 (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
3109 (should (equal (org-test-parse-and-interpret "Text[fn:label]")
3110 "Text[fn:label]\n"))
3111 ;; Inline reference.
3112 (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
3113 "Text[fn:label:def]\n"))
3114 ;; Anonymous reference.
3115 (should (equal (org-test-parse-and-interpret "Text[fn::def]")
3116 "Text[fn::def]\n")))
3118 (ert-deftest test-org-element
/inline-babel-call-interpreter
()
3119 "Test inline babel call interpreter."
3120 ;; Without arguments.
3121 (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
3123 (should (equal (org-test-parse-and-interpret "call_test(x=2)")
3124 "call_test(x=2)\n"))
3125 ;; With header arguments.
3126 (should (equal (org-test-parse-and-interpret
3127 "call_test[:results output]()[:results html]")
3128 "call_test[:results output]()[:results html]\n")))
3130 (ert-deftest test-org-element
/inline-src-block-interpreter
()
3131 "Test inline src block interpreter."
3132 ;; 1. Without header argument.
3133 (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
3134 "src_emacs-lisp{(+ 1 1)}\n"))
3135 ;; 2. With header arguments.
3136 (should (equal (org-test-parse-and-interpret
3137 "src_emacs-lisp[:results silent]{(+ 1 1)}")
3138 "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))
3140 (ert-deftest test-org-element
/italic-interpreter
()
3141 "Test italic interpreter."
3142 (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))
3144 (ert-deftest test-org-element
/latex-fragment-interpreter
()
3145 "Test latex fragment interpreter."
3146 (should (equal (org-test-parse-and-interpret "\\command{}") "\\command{}\n"))
3147 (should (equal (org-test-parse-and-interpret "$x$") "$x$\n"))
3148 (should (equal (org-test-parse-and-interpret "$x+y$") "$x+y$\n"))
3149 (should (equal (org-test-parse-and-interpret "$$x+y$$") "$$x+y$$\n"))
3150 (should (equal (org-test-parse-and-interpret "\\(x+y\\)") "\\(x+y\\)\n"))
3151 (should (equal (org-test-parse-and-interpret "\\[x+y\\]") "\\[x+y\\]\n")))
3153 (ert-deftest test-org-element
/line-break-interpreter
()
3154 "Test line break interpreter."
3155 (should (equal (org-test-parse-and-interpret "First line \\\\ \nSecond line")
3156 "First line \\\\\nSecond line\n")))
3158 (ert-deftest test-org-element
/link-interpreter
()
3159 "Test link interpreter."
3160 ;; Links targeted from a radio target.
3161 (should (equal (let ((org-target-link-regexp "radio-target"))
3162 (org-test-parse-and-interpret "a radio-target"))
3163 "a radio-target\n"))
3164 ;; Links without description.
3165 (should (equal (org-test-parse-and-interpret "[[https://orgmode.org]]")
3166 "[[https://orgmode.org]]\n"))
3167 ;; Links with a description, even one containing a link.
3168 (should (equal (org-test-parse-and-interpret
3169 "[[https://orgmode.org][Org mode]]")
3170 "[[https://orgmode.org][Org mode]]\n"))
3171 (should (equal (org-test-parse-and-interpret
3172 "[[https://orgmode.org][https://orgmode.org]]")
3173 "[[https://orgmode.org][https://orgmode.org]]\n"))
3176 (equal (org-test-parse-and-interpret "[[file+emacs:todo.org]]")
3177 "[[file+emacs:todo.org]]\n"))
3179 (equal (org-test-parse-and-interpret "[[file:todo.org::*task]]")
3180 "[[file:todo.org::*task]]\n"))
3182 (should (equal (org-test-parse-and-interpret "[[id:aaaa]]") "[[id:aaaa]]\n"))
3184 (should (equal (org-test-parse-and-interpret "[[#id]]") "[[#id]]\n"))
3186 (should (equal (org-test-parse-and-interpret "[[(ref)]]") "[[(ref)]]\n"))
3188 (should (equal (org-test-parse-and-interpret "https://orgmode.org")
3189 "https://orgmode.org\n"))
3191 (should (equal (org-test-parse-and-interpret "<https://orgmode.org>")
3192 "<https://orgmode.org>\n"))
3193 ;; Pathological case: link with a %-sign in description.
3194 (should (equal (org-test-parse-and-interpret "[[file://path][%s]]")
3195 "[[file://path][%s]]\n")))
3197 (ert-deftest test-org-element
/macro-interpreter
()
3198 "Test macro interpreter."
3199 ;; 1. Without argument.
3200 (should (equal (org-test-parse-and-interpret "{{{test}}}") "{{{test}}}\n"))
3201 ;; 2. With arguments.
3202 (should (equal (org-test-parse-and-interpret "{{{test(arg1,arg2)}}}")
3203 "{{{test(arg1,arg2)}}}\n")))
3205 (ert-deftest test-org-element
/radio-target-interpreter
()
3206 "Test radio target interpreter."
3207 (should (equal (org-test-parse-and-interpret "<<<some text>>>")
3208 "<<<some text>>>\n")))
3210 (ert-deftest test-org-element
/statistics-cookie-interpreter
()
3211 "Test statistics cookie interpreter."
3212 ;; 1. Without percent
3213 (should (equal (org-test-parse-and-interpret "[0/1]") "[0/1]\n"))
3215 (should (equal (org-test-parse-and-interpret "[66%]") "[66%]\n")))
3217 (ert-deftest test-org-element
/strike-through-interpreter
()
3218 "Test strike through interpreter."
3219 (should (equal (org-test-parse-and-interpret "+target+") "+target+\n")))
3221 (ert-deftest test-org-element
/subscript-interpreter
()
3222 "Test subscript interpreter."
3223 ;; 1. Without brackets.
3224 (should (equal (org-test-parse-and-interpret "a_b") "a_b\n"))
3225 ;; 2. With brackets.
3226 (should (equal (org-test-parse-and-interpret "a_{b}") "a_{b}\n")))
3228 (ert-deftest test-org-element
/superscript-interpreter
()
3229 "Test superscript interpreter."
3230 ;; 1. Without brackets.
3231 (should (equal (org-test-parse-and-interpret "a^b") "a^b\n"))
3232 ;; 2. With brackets.
3233 (should (equal (org-test-parse-and-interpret "a^{b}") "a^{b}\n")))
3235 (ert-deftest test-org-element
/target-interpreter
()
3236 "Test target interpreter."
3237 (should (equal (org-test-parse-and-interpret "<<target>>") "<<target>>\n")))
3239 (ert-deftest test-org-element
/underline-interpreter
()
3240 "Test underline interpreter."
3241 (should (equal (org-test-parse-and-interpret "_text_") "_text_\n")))
3243 (ert-deftest test-org-element
/verbatim-interpreter
()
3244 "Test verbatim interpreter."
3245 (should (equal (org-test-parse-and-interpret "=text=") "=text=\n")))
3249 ;;; Test Granularity
3251 (ert-deftest test-org-element
/granularity
()
3252 "Test granularity impact on buffer parsing."
3253 (org-test-with-temp-text "
3260 ;; 1.1. Granularity set to `headline' should parse every headline
3261 ;; in buffer, and only them.
3262 (let ((tree (org-element-parse-buffer 'headline
)))
3263 (should (= 2 (length (org-element-map tree
'headline
'identity
))))
3264 (should-not (org-element-map tree
'paragraph
'identity
)))
3265 ;; 1.2. Granularity set to `greater-element' should not enter
3266 ;; greater elements excepted headlines and sections.
3267 (let ((tree (org-element-parse-buffer 'greater-element
)))
3268 (should (= 1 (length (org-element-map tree
'center-block
'identity
))))
3269 (should (= 1 (length (org-element-map tree
'paragraph
'identity
))))
3270 (should-not (org-element-map tree
'entity
'identity
)))
3271 ;; 1.3. Granularity set to `element' should enter every
3273 (let ((tree (org-element-parse-buffer 'element
)))
3274 (should (= 2 (length (org-element-map tree
'paragraph
'identity
))))
3275 (should-not (org-element-map tree
'entity
'identity
)))
3276 ;; 1.4. Granularity set to `object' can see everything.
3277 (let ((tree (org-element-parse-buffer 'object
)))
3278 (should (= 1 (length (org-element-map tree
'entity
'identity
)))))))
3280 (ert-deftest test-org-element
/secondary-string-parsing
()
3281 "Test if granularity correctly toggles secondary strings parsing."
3282 ;; With a granularity bigger than `object', no secondary string
3283 ;; should be parsed.
3286 (org-test-with-temp-text "* Headline"
3288 (org-element-map (org-element-parse-buffer 'headline
) 'headline
3289 #'identity nil
'first-match
)))
3290 (org-element-property :title headline
)))))
3293 (org-test-with-temp-text "* Headline\n- tag :: item"
3294 (let ((item (org-element-map (org-element-parse-buffer 'element
) 'item
3295 #'identity nil
'first-match
)))
3296 (org-element-property :tag item
)))))
3297 (when (featurep 'org-inlinetask
)
3300 (let ((org-inlinetask-min-level 15))
3301 (org-test-with-temp-text "*************** Inlinetask"
3302 (let ((inlinetask (org-element-map (org-element-parse-buffer 'element
)
3304 #'identity nil
'first-match
)))
3305 (org-element-property :title inlinetask
)))))))
3306 ;; With a default granularity, secondary strings should be parsed.
3309 (org-test-with-temp-text "* Headline"
3311 (org-element-map (org-element-parse-buffer) 'headline
3312 #'identity nil
'first-match
)))
3313 (org-element-property :title headline
)))))
3314 ;; `org-element-at-point' should never parse a secondary string.
3317 (org-test-with-temp-text "* Headline"
3318 (org-element-property :title
(org-element-at-point)))))
3319 ;; Preserve current local variables when parsing a secondary string.
3321 (let ((org-entities nil
)
3322 (org-entities-user nil
))
3323 (org-test-with-temp-text "
3327 # org-entities-user: ((\"foo\"))
3329 (let ((safe-local-variable-values '((org-entities-user .
(("foo"))))))
3330 (hack-local-variables))
3331 (org-element-map (org-element-parse-buffer) 'entity
3332 #'identity nil nil nil t
)))))
3336 ;;; Test Visible Only Parsing
3338 (ert-deftest test-org-element
/parse-buffer-visible
()
3339 "Test `org-element-parse-buffer' with visible only argument."
3341 (equal '("H1" "H3" "H5")
3342 (org-test-with-temp-text
3343 "* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:"
3344 (org-occur ":visible:")
3345 (org-element-map (org-element-parse-buffer nil t
) 'headline
3346 (lambda (hl) (org-element-property :raw-value hl
)))))))
3350 ;;; Test `:parent' Property
3352 (ert-deftest test-org-element
/parent-property
()
3353 "Test `:parent' property."
3355 (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
3356 (let* ((tree (org-element-parse-buffer))
3357 (parent (org-element-property
3359 (org-element-map tree
'paragraph
'identity nil t
))))
3361 (should (eq (org-element-map tree
'center-block
'identity nil t
)
3364 (org-test-with-temp-text "a_{/b/}"
3365 (let* ((tree (org-element-parse-buffer))
3366 (parent (org-element-property
3368 (org-element-map tree
'italic
'identity nil t
))))
3371 (org-element-map tree
'subscript
'identity nil t
)))))
3372 ;; Secondary strings
3373 (org-test-with-temp-text "* /italic/"
3374 (let* ((tree (org-element-parse-buffer))
3375 (parent (org-element-property
3376 :parent
(org-element-map tree
'italic
'identity nil t
))))
3379 (org-element-map tree
'headline
'identity nil t
))))))
3383 ;;; Test Normalize Contents
3385 (ert-deftest test-org-element
/normalize-contents
()
3386 "Test `org-element-normalize-contents' specifications."
3387 ;; Remove maximum common indentation from element's contents.
3390 (org-element-normalize-contents
3391 '(paragraph nil
" Two spaces\n Three spaces"))
3392 '(paragraph nil
"Two spaces\n Three spaces")))
3395 (org-element-normalize-contents
3396 '(paragraph nil
" Two spaces\nNo space"))
3397 '(paragraph nil
" Two spaces\nNo space")))
3398 ;; Ignore objects within contents when computing maximum common
3399 ;; indentation. However, if contents start with an object, common
3400 ;; indentation is 0.
3403 (org-element-normalize-contents
3404 '(paragraph nil
" One " (emphasis nil
"space") "\n Two spaces"))
3405 '(paragraph nil
"One " (emphasis nil
"space") "\n Two spaces")))
3408 (org-element-normalize-contents
3409 '(paragraph nil
(verbatim nil
"V") "No space\n Two\n Three"))
3410 '(paragraph nil
(verbatim nil
"V") "No space\n Two\n Three")))
3411 ;; Ignore blank lines.
3414 (org-element-normalize-contents
3415 '(paragraph nil
" Two spaces\n\n \n Two spaces"))
3416 '(paragraph nil
"Two spaces\n\n\nTwo spaces")))
3419 '(paragraph nil
" Two spaces\n" (verbatim nil
"V") "\n Two spaces")
3420 (org-element-normalize-contents
3421 '(paragraph nil
" Two spaces\n " (verbatim nil
"V") "\n Two spaces"))))
3424 '(verse-block nil
"line 1\n\nline 2")
3425 (org-element-normalize-contents
3426 '(verse-block nil
" line 1\n\n line 2"))))
3427 ;; Recursively enter objects in order to compute common indentation.
3430 (org-element-normalize-contents
3431 '(paragraph nil
" Two spaces " (bold nil
" and\n One space")))
3432 '(paragraph nil
" Two spaces " (bold nil
" and\nOne space"))))
3433 ;; When optional argument is provided, ignore first line
3437 (org-element-normalize-contents
3438 '(paragraph nil
"No space\n Two spaces\n Three spaces") t
)
3439 '(paragraph nil
"No space\nTwo spaces\n Three spaces")))
3442 (org-element-normalize-contents
3443 '(paragraph nil
(verbatim nil
"V") "No space\n Two\n Three") t
)
3444 '(paragraph nil
(verbatim nil
"V") "No space\nTwo\n Three")))
3445 ;; Corner case: do not ignore indentation of string right after
3449 (org-element-normalize-contents
3450 '(paragraph nil
" 1 space" (line-break) " 2 spaces"))
3451 '(paragraph nil
"1 space" (line-break) " 2 spaces"))))
3455 ;;; Test Navigation Tools.
3457 (ert-deftest test-org-element
/at-point
()
3458 "Test `org-element-at-point' specifications."
3459 ;; Return closest element containing point.
3462 (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
3463 (progn (search-forward "A")
3464 (org-element-type (org-element-at-point))))))
3465 ;; Correctly set `:parent' property.
3468 (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
3469 (progn (search-forward "A")
3471 (org-element-property :parent
(org-element-at-point)))))))
3472 ;; Special case: at a blank line just below a headline, return that
3475 (equal "H1" (org-test-with-temp-text "* H1\n \n* H2\n"
3477 (org-element-property :title
(org-element-at-point)))))
3478 ;; Special case: at the very beginning of a table, return `table'
3479 ;; object instead of `table-row'.
3482 (org-test-with-temp-text "| a | b |"
3483 (org-element-type (org-element-at-point)))))
3484 ;; Special case: at the very beginning of a list or sub-list, return
3485 ;; `plain-list' object instead of `item'.
3488 (org-test-with-temp-text "- item"
3489 (org-element-type (org-element-at-point)))))
3490 ;; Special case: at the closing line of a greater element, be sure
3491 ;; to return it instead of the last element in its contents.
3494 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph\n#+END_CENTER"
3495 (progn (forward-line 2)
3496 (org-element-type (org-element-at-point))))))
3497 ;; Special case: at a blank line between two items, be sure to
3498 ;; return item above instead of the last element of its contents.
3501 (org-test-with-temp-text "- Para1\n\n- Para2"
3503 (org-element-type (org-element-at-point)))))
3504 ;; Special case: at the last blank line in a plain list, return it
3505 ;; instead of the last item.
3508 (org-test-with-temp-text "- Para1\n- Para2\n\nPara3"
3509 (progn (forward-line 2)
3510 (org-element-type (org-element-at-point))))))
3511 ;; Special case: when a list ends at the end of buffer and there's
3512 ;; no final newline, return last element in last item.
3515 (org-test-with-temp-text "- a"
3517 (org-element-type (org-element-at-point)))))
3518 ;; Parse a list within a block itself contained in a list.
3521 (org-test-with-temp-text
3522 "- outer\n #+begin_center\n - inner\n #+end_center"
3523 (search-forward "inner")
3525 (org-element-type (org-element-at-point)))))
3526 ;; Do not error at eob on an empty line.
3528 (org-test-with-temp-text "* H\n"
3530 (or (org-element-at-point) t
))))
3532 (ert-deftest test-org-element
/context
()
3533 "Test `org-element-context' specifications."
3534 ;; Return closest object containing point.
3537 (org-test-with-temp-text "Some *text with _under<point>line_ text*"
3538 (org-element-type (org-element-context)))))
3539 ;; Find objects in secondary strings.
3542 (org-test-with-temp-text "* Headline _<point>with_ underlining"
3543 (org-element-type (org-element-context)))))
3544 ;; Find objects in objects.
3547 (org-test-with-temp-text "| a | {<point>{{macro}}} |"
3548 (org-element-type (org-element-context)))))
3551 (org-test-with-temp-text "| a | b<point> {{{macro}}} |"
3552 (org-element-type (org-element-context)))))
3553 ;; Find objects in item tags.
3556 (org-test-with-temp-text "- *bo<point>ld* ::"
3557 (org-element-type (org-element-context)))))
3560 (org-test-with-temp-text "- *bold* ::<point>"
3561 (org-element-type (org-element-context)))))
3564 (org-test-with-temp-text "- *bold* ::\n<point>"
3565 (org-element-type (org-element-context)))))
3566 ;; Do not find objects in table rules.
3569 (org-test-with-temp-text "| a | b |\n|-<point>--|---|\n| c | d |"
3570 (org-element-type (org-element-context)))))
3571 ;; Find objects in parsed affiliated keywords.
3574 (org-test-with-temp-text "#+CAPTION: {<point>{{macro}}}\n| a | b |"
3575 (org-element-type (org-element-context)))))
3578 (org-test-with-temp-text "#+caption: *<point>bold*\nParagraph"
3579 (org-element-type (org-element-context)))))
3580 ;; Find objects at the end of buffer.
3583 (org-test-with-temp-text "*bold*"
3584 (goto-char (point-max))
3585 (org-element-type (org-element-context)))))
3586 ;; Correctly set `:parent' property.
3589 (org-test-with-temp-text "Some *bold<point>* text"
3591 (org-element-property :parent
(org-element-context))))))
3592 ;; Between two objects, return the second one.
3595 (org-test-with-temp-text "<<target>><point>{{{test}}}"
3596 (org-element-type (org-element-context)))))
3597 ;; Test optional argument.
3600 (org-test-with-temp-text "Some *text with _under<point>line_ text*"
3601 (org-element-type (org-element-context (org-element-at-point))))))
3602 ;; Special case: bold object at the beginning of a headline.
3605 (org-test-with-temp-text "* *bo<point>ld*"
3606 (org-element-type (org-element-context)))))
3607 ;; Special case: incomplete cell at the end of a table row.
3610 (org-test-with-temp-text "|a|b|c<point>"
3611 (org-element-type (org-element-context)))))
3612 ;; Special case: objects in inline footnotes.
3615 (org-test-with-temp-text "[fn::[[<point>https://orgmode.org]]]"
3616 (org-element-type (org-element-context)))))
3617 ;; Special case: tags looking like a link.
3620 (org-test-with-temp-text "* Headline :file<point>:tags:"
3621 (org-element-type (org-element-context)))))
3624 (org-test-with-temp-text "* Headline :file<point>:tags: :real:tag:"
3625 (org-element-type (org-element-context))))))
3631 (ert-deftest test-org-element
/lineage
()
3632 "Test `org-element-lineage' specifications."
3633 ;; Regular tests. When applied to an element or object returned by
3634 ;; `org-element-at-point' or `org-element-context', the list is
3635 ;; limited to the current section.
3637 (equal '(paragraph center-block
)
3638 (org-test-with-temp-text
3639 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3640 (mapcar #'car
(org-element-lineage (org-element-context))))))
3642 (equal '(paragraph center-block section headline headline org-data
)
3643 (org-test-with-temp-text
3644 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3646 (org-element-lineage
3647 (org-element-map (org-element-parse-buffer) 'bold
3648 #'identity nil t
))))))
3649 ;; Test TYPES optional argument.
3652 (org-test-with-temp-text
3653 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3655 (org-element-lineage (org-element-context) '(center-block))))))
3657 (org-test-with-temp-text
3658 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3659 (org-element-lineage (org-element-context) '(example-block))))
3660 ;; Test WITH-SELF optional argument.
3662 (equal '(bold paragraph center-block
)
3663 (org-test-with-temp-text
3664 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3665 (mapcar #'car
(org-element-lineage (org-element-context) nil t
)))))
3666 ;; When TYPES and WITH-SELF are provided, the latter is also checked
3667 ;; against the former.
3669 (org-test-with-temp-text
3670 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3671 (org-element-lineage (org-element-context) '(bold) t
))))
3677 (ert-deftest test-org-element
/cache
()
3678 "Test basic expectations and common pitfalls for cache."
3682 (org-test-with-temp-text "para1\n\npara2\n\npara3"
3683 (let ((org-element-use-cache t
))
3684 (save-excursion (goto-char (point-max)) (org-element-at-point))
3687 (let ((element (org-element-at-point)))
3688 (cons (org-element-property :begin element
)
3689 (org-element-property :end element
)))))))
3690 ;; Partial shifting: when the contents of a greater element are
3691 ;; modified, only shift ending positions.
3693 (org-test-with-temp-text
3694 "#+BEGIN_CENTER\nPara1\n\nPara2\n\nPara3\n#+END_CENTER"
3695 (let ((org-element-use-cache t
))
3696 (save-excursion (search-forward "3") (org-element-at-point))
3697 (search-forward "Para2")
3699 (let ((element (org-element-property :parent
(org-element-at-point))))
3700 (equal (cons (org-element-property :begin element
)
3701 (org-element-property :end element
))
3702 (cons (point-min) (point-max)))))))
3703 ;; Re-parent shifted elements.
3706 (org-test-with-temp-text "- item\n\n\n para1\n para2"
3707 (let ((org-element-use-cache t
))
3709 (org-element-at-point)
3710 (save-excursion (goto-char (point-max)) (org-element-at-point))
3713 (goto-char (point-max))
3715 (org-element-property :parent
(org-element-at-point)))))))
3716 ;; Preserve local structures when re-parenting.
3719 (let ((org-element-use-cache t
))
3720 (org-test-with-temp-text
3721 "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n| c | d |\n#+end_center"
3722 (save-excursion (search-forward "| c |") (org-element-at-point))
3724 (search-forward "| c |")
3727 (org-element-property :parent
(org-element-at-point)))))))
3730 (org-test-with-temp-text
3731 "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n#+end_center"
3732 (let ((org-element-use-cache t
))
3733 (save-excursion (search-forward "| a |") (org-element-at-point))
3735 (search-forward "| a |")
3738 (org-element-property :parent
(org-element-at-point)))))))
3739 ;; When re-parenting, also propagate changes to list structures.
3742 (org-test-with-temp-text "\n Para\n - item<point>"
3743 (let ((org-element-use-cache t
))
3744 (org-element-at-point)
3745 (goto-char (point-min))
3747 (search-forward "- item")
3749 (length (org-element-property :structure
(org-element-at-point)))))))
3750 ;; Modifying the last line of an element alters the element below.
3752 (org-test-with-temp-text "para1\n\npara2"
3753 (let ((org-element-use-cache t
))
3754 (goto-char (point-max))
3755 (org-element-at-point)
3758 (let ((element (org-element-at-point)))
3759 (equal (cons (org-element-property :begin element
)
3760 (org-element-property :end element
))
3761 (cons (point-min) (point-max)))))))
3762 ;; Modifying the first line of an element alters the element above.
3764 (org-test-with-temp-text ": fixed-width\n:not-fixed-width"
3765 (let ((org-element-use-cache t
))
3766 (goto-char (point-max))
3767 (org-element-at-point)
3768 (search-backward ":")
3771 (let ((element (org-element-at-point)))
3772 (equal (cons (org-element-property :begin element
)
3773 (org-element-property :end element
))
3774 (cons (point-min) (point-max)))))))
3775 ;; Sensitive change: adding a line alters document structure both
3779 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n"
3780 (let ((org-element-use-cache t
))
3781 (goto-char (point-max))
3782 (org-element-at-point)
3783 (insert "#+END_EXAMPLE")
3784 (search-backward "Para1")
3785 (org-element-type (org-element-at-point))))))
3788 (org-test-with-temp-text "Para1\n\nPara2\n#+END_EXAMPLE"
3789 (let ((org-element-use-cache t
))
3790 (save-excursion (goto-char (point-max)) (org-element-at-point))
3791 (insert "#+BEGIN_EXAMPLE\n")
3792 (search-forward "Para2")
3793 (org-element-type (org-element-at-point))))))
3794 ;; Sensitive change: removing a line alters document structure both
3798 (org-test-with-temp-text
3799 "# +BEGIN_EXAMPLE\nPara1\n\nPara2\n#+END_EXAMPLE"
3800 (let ((org-element-use-cache t
))
3801 (save-excursion (goto-char (point-max)) (org-element-at-point))
3804 (search-forward "Para2")
3805 (org-element-type (org-element-at-point))))))
3808 (org-test-with-temp-text
3809 "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n# +END_EXAMPLE"
3810 (let ((org-element-use-cache t
))
3811 (save-excursion (goto-char (point-max)) (org-element-at-point))
3812 (search-forward "# ")
3814 (search-backward "Para1")
3815 (org-element-type (org-element-at-point))))))
3816 ;; Corner case: watch out drawers named "PROPERTIES" as they are
3817 ;; fragile, unlike to other drawers.
3820 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A<point>\n:END:"
3821 (let ((org-element-use-cache t
))
3822 (org-element-at-point)
3824 (org-element-type (org-element-at-point))))))
3825 ;; Properly handle elements not altered by modifications but whose
3826 ;; parents were removed from cache.
3828 (org-test-with-temp-text
3829 "Paragraph\n\n\n\n#+begin_center\n<point>contents\n#+end_center"
3830 (let ((org-element-use-cache t
)
3831 (parent-end (point-max)))
3832 (org-element-at-point)
3833 (save-excursion (search-backward "Paragraph")
3836 (eq (org-element-property
3837 :end
(org-element-property :parent
(org-element-at-point)))
3838 (+ parent-end
3))))))
3841 (provide 'test-org-element
)
3843 ;;; test-org-element.el ends here