1 ;;; test-org-element.el --- Tests for org-element.el
3 ;; Copyright (C) 2012, 2013, 2014 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 (unless (featurep 'org-element
)
23 (signal 'missing-test-dependency
"org-element"))
25 (defun org-test-parse-and-interpret (text)
26 "Parse TEXT as Org syntax and interpret it.
27 Return interpreted string."
31 (org-element-interpret-data (org-element-parse-buffer))))
35 ;;; Test `org-element-map'
37 (ert-deftest test-org-element
/map
()
38 "Test `org-element-map'."
39 ;; Can map to `plain-text' objects.
42 (org-test-with-temp-text "Some text \alpha
48 (org-element-parse-buffer) 'plain-text
49 (lambda (s) (when (string-match "text" s
) (incf count
))))
51 ;; Applies to secondary strings
53 (org-element-map '("some " (bold nil
"bold") "text") 'bold
'identity
))
54 ;; Enter secondary strings before entering contents.
60 (org-test-with-temp-text "* Some \\alpha headline\n\\beta entity."
61 (org-element-map (org-element-parse-buffer) 'entity
'identity nil t
)))))
62 ;; Apply NO-RECURSION argument.
64 (org-test-with-temp-text "#+BEGIN_CENTER\n\\alpha\n#+END_CENTER"
66 (org-element-parse-buffer) 'entity
'identity nil nil
'center-block
)))
67 ;; Use WITH-AFFILIATED argument.
71 (org-test-with-temp-text "#+CAPTION[a]: 1\n#+CAPTION[b]: 2\nParagraph"
73 (org-element-at-point) 'plain-text
'identity nil nil nil t
)))))
79 (ert-deftest test-org-element
/put-property
()
80 "Test `org-element-put-property' specifications."
82 (org-test-with-temp-text "* Headline\n *a*"
83 (let ((tree (org-element-parse-buffer)))
84 (org-element-put-property
85 (org-element-map tree
'bold
'identity nil t
) :test
1)
86 (should (org-element-property
87 :test
(org-element-map tree
'bold
'identity nil t
)))))
88 ;; Put property on a string.
90 (org-element-property :test
(org-element-put-property "Paragraph" :test t
))))
92 (ert-deftest test-org-element
/set-contents
()
93 "Test `org-element-set-contents' specifications."
94 ;; Accept multiple entries.
96 (equal '("b" (italic nil
"a"))
97 (org-test-with-temp-text "* Headline\n *a*"
98 (let ((tree (org-element-parse-buffer)))
99 (org-element-set-contents
100 (org-element-map tree
'bold
'identity nil t
) "b" '(italic nil
"a"))
101 (org-element-contents
102 (org-element-map tree
'bold
'identity nil t
))))))
103 ;; Accept atoms and elements.
106 (org-test-with-temp-text "* Headline\n *a*"
107 (let ((tree (org-element-parse-buffer)))
108 (org-element-set-contents
109 (org-element-map tree
'bold
'identity nil t
) "b")
110 (org-element-contents
111 (org-element-map tree
'bold
'identity nil t
))))))
113 (equal '((italic nil
"b"))
114 (org-test-with-temp-text "* Headline\n *a*"
115 (let ((tree (org-element-parse-buffer)))
116 (org-element-set-contents
117 (org-element-map tree
'bold
'identity nil t
) '(italic nil
"b"))
118 (org-element-contents
119 (org-element-map tree
'bold
'identity nil t
))))))
120 ;; Allow nil contents.
122 (org-test-with-temp-text "* Headline\n *a*"
123 (let ((tree (org-element-parse-buffer)))
124 (org-element-set-contents (org-element-map tree
'bold
'identity nil t
))
125 (org-element-contents (org-element-map tree
'bold
'identity nil t
))))))
127 (ert-deftest test-org-element
/secondary-p
()
128 "Test `org-element-secondary-p' specifications."
129 ;; In a secondary string, return property name.
132 (org-test-with-temp-text "* Headline *object*"
133 (org-element-map (org-element-parse-buffer) 'bold
134 (lambda (object) (org-element-secondary-p object
))
136 ;; Outside a secondary string, return nil.
138 (org-test-with-temp-text "Paragraph *object*"
139 (org-element-map (org-element-parse-buffer) 'bold
140 (lambda (object) (org-element-type (org-element-secondary-p object
)))
143 (ert-deftest test-org-element
/adopt-elements
()
144 "Test `org-element-adopt-elements' specifications."
147 (equal '(plain-text italic
)
148 (org-test-with-temp-text "* Headline\n *a*"
149 (let ((tree (org-element-parse-buffer)))
150 (org-element-adopt-elements
151 (org-element-map tree
'bold
'identity nil t
) '(italic nil
"a"))
152 (mapcar (lambda (blob) (org-element-type blob
))
153 (org-element-contents
154 (org-element-map tree
'bold
'identity nil t
)))))))
158 (org-test-with-temp-text "* Headline\n *a*"
159 (let ((tree (org-element-parse-buffer)))
160 (org-element-adopt-elements
161 (org-element-map tree
'bold
'identity nil t
) "b")
162 (org-element-contents
163 (org-element-map tree
'bold
'identity nil t
)))))))
165 (ert-deftest test-org-element
/extract-element
()
166 "Test `org-element-extract-element' specifications."
167 ;; Extract a greater element.
169 (equal '(org-data nil
)
170 (org-test-with-temp-text "* Headline"
171 (let* ((tree (org-element-parse-buffer))
172 (element (org-element-map tree
'headline
'identity nil t
)))
173 (org-element-extract-element element
)
175 ;; Extract an element.
178 (org-test-with-temp-text "Paragraph"
179 (let* ((tree (org-element-parse-buffer))
180 (element (org-element-map tree
'paragraph
'identity nil t
)))
181 (org-element-extract-element element
)
185 ;; Extract an object, even in a secondary string.
188 (org-test-with-temp-text "*bold*"
189 (let* ((tree (org-element-parse-buffer))
190 (element (org-element-map tree
'bold
'identity nil t
)))
191 (org-element-extract-element element
)
197 (org-test-with-temp-text "* Headline *bold*"
198 (let* ((tree (org-element-parse-buffer))
199 (element (org-element-map tree
'bold
'identity nil t
)))
200 (org-element-extract-element element
)
204 ;; Return value doesn't have any :parent set.
206 (org-element-property
208 (org-test-with-temp-text "* Headline\n Paragraph with *bold* text."
209 (let* ((tree (org-element-parse-buffer))
210 (element (org-element-map tree
'bold
'identity nil t
)))
211 (org-element-extract-element element
))))))
213 (ert-deftest test-org-element
/insert-before
()
214 "Test `org-element-insert-before' specifications."
218 '(italic entity bold
)
219 (org-test-with-temp-text "/some/ *paragraph*"
220 (let* ((tree (org-element-parse-buffer))
221 (paragraph (org-element-map tree
'paragraph
'identity nil t
))
222 (bold (org-element-map tree
'bold
'identity nil t
)))
223 (org-element-insert-before '(entity (:name
"\\alpha")) bold
)
224 (org-element-map tree
'(bold entity italic
) #'org-element-type nil
)))))
225 ;; Insert an object in a secondary string.
229 (org-test-with-temp-text "* /A/\n Paragraph."
230 (let* ((tree (org-element-parse-buffer))
231 (headline (org-element-map tree
'headline
'identity nil t
))
232 (italic (org-element-map tree
'italic
'identity nil t
)))
233 (org-element-insert-before '(entity (:name
"\\alpha")) italic
)
234 (org-element-map (org-element-property :title headline
) '(entity italic
)
235 #'org-element-type
))))))
237 (ert-deftest test-org-element
/set-element
()
238 "Test `org-element-set-element' specifications."
239 ;; Check if new element is inserted.
241 (org-test-with-temp-text "* Headline\n*a*"
242 (let* ((tree (org-element-parse-buffer))
243 (bold (org-element-map tree
'bold
'identity nil t
)))
244 (org-element-set-element bold
'(italic nil
"b"))
245 (org-element-map tree
'italic
'identity
))))
246 ;; Check if old element is removed.
248 (org-test-with-temp-text "* Headline\n*a*"
249 (let* ((tree (org-element-parse-buffer))
250 (bold (org-element-map tree
'bold
'identity nil t
)))
251 (org-element-set-element bold
'(italic nil
"b"))
252 (org-element-map tree
'bold
'identity
))))
253 ;; Check if :parent property is correctly set.
256 (org-test-with-temp-text "* Headline\n*a*"
257 (let* ((tree (org-element-parse-buffer))
258 (bold (org-element-map tree
'bold
'identity nil t
)))
259 (org-element-set-element bold
'(italic nil
"b"))
261 (org-element-property
262 :parent
(org-element-map tree
'italic
'identity nil t
)))))))
263 ;; Allow to replace strings with elements.
266 (org-test-with-temp-text "* Headline"
267 (let* ((tree (org-element-parse-buffer))
268 (text (org-element-map tree
'plain-text
'identity nil t
)))
269 (org-element-set-element text
(list 'bold nil
"b"))
270 (org-element-map tree
'plain-text
'identity
)))))
271 ;; Allow to replace elements with strings.
274 (org-test-with-temp-text "* =verbatim="
275 (let* ((tree (org-element-parse-buffer))
276 (verb (org-element-map tree
'verbatim
'identity nil t
)))
277 (org-element-set-element verb
"a")
278 (org-element-map tree
'plain-text
'identity nil t
)))))
279 ;; Allow to replace strings with strings.
282 (org-test-with-temp-text "a"
283 (let* ((tree (org-element-parse-buffer))
284 (text (org-element-map tree
'plain-text
'identity nil t
)))
285 (org-element-set-element text
"b")
286 (org-element-map tree
'plain-text
'identity nil t
))))))
292 ;;;; Affiliated Keywords
294 (ert-deftest test-org-element
/affiliated-keywords-parser
()
295 "Test affiliated keywords parsing."
296 ;; Read simple keywords.
299 (org-element-property
301 (org-test-with-temp-text "#+NAME: para\nParagraph"
302 (org-element-at-point)))))
305 (org-element-property
307 (org-test-with-temp-text "#+NAME: para\nParagraph"
308 (org-element-at-point)))))
309 ;; Parse multiple keywords.
313 (org-element-property
315 (org-test-with-temp-text
316 "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph"
317 (org-element-at-point)))))
318 ;; Parse "parsed" keywords.
322 (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
323 (car (org-element-property :caption
(org-element-at-point))))))
324 ;; Parse dual keywords.
327 '((("long") "short"))
328 (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
329 (org-element-property :caption
(org-element-at-point)))))
330 ;; Allow multiple caption keywords.
333 '((("l2") "s2") (("l1") "s1"))
334 (org-test-with-temp-text "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph"
335 (org-element-property :caption
(org-element-at-point)))))
338 '((("l1")) (nil "s1"))
339 (org-test-with-temp-text "#+CAPTION[s1]:\n#+CAPTION: l1\nParagraph"
340 (org-element-property :caption
(org-element-at-point)))))
341 ;; Corner case: orphaned keyword at the end of an element.
344 (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
345 (progn (search-forward "name")
346 (org-element-type (org-element-at-point))))))
348 (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
349 (progn (search-forward "Some")
350 (org-element-property :name
(org-element-at-point))))))
355 (ert-deftest test-org-element
/babel-call-parser
()
356 "Test `babel-call' parsing."
359 (org-test-with-temp-text "#+CALL: test()"
360 (org-element-map (org-element-parse-buffer) 'babel-call
'identity
)))
363 (org-test-with-temp-text "#+call: test()"
364 (org-element-map (org-element-parse-buffer) 'babel-call
'identity
)))
365 ;; Handle non-empty blank line at the end of buffer.
367 (org-test-with-temp-text "#+CALL: test()\n "
368 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
373 (ert-deftest test-org-element
/bold-parser
()
374 "Test `bold' parser."
377 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
378 (org-test-with-temp-text "*bold*"
379 (org-element-map (org-element-parse-buffer) 'bold
'identity nil t
))))
380 ;; Multi-line markup.
383 (org-element-contents
384 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
385 (org-test-with-temp-text "*first line\nsecond line*"
386 (org-element-map (org-element-parse-buffer) 'bold
'identity nil t
))))
387 '("first line\nsecond line"))))
392 (ert-deftest test-org-element
/center-block-parser
()
393 "Test `center-block' parser."
396 (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
397 (org-element-map (org-element-parse-buffer) 'center-block
'identity
)))
400 (org-test-with-temp-text "#+begin_center\nText\n#+end_center"
401 (org-element-map (org-element-parse-buffer) 'center-block
'identity
)))
402 ;; Ignore incomplete block.
404 (org-test-with-temp-text "#+BEGIN_CENTER"
405 (org-element-map (org-element-parse-buffer) 'center-block
407 ;; Handle non-empty blank line at the end of buffer.
409 (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
410 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
415 (ert-deftest test-org-element
/clock-parser
()
416 "Test `clock' parser."
418 (let ((clock (org-test-with-temp-text "CLOCK: [2012-01-01 sun. 00:01]"
419 (org-element-at-point))))
420 (should (eq (org-element-property :status clock
) 'running
))
422 (equal (org-element-property :raw-value
423 (org-element-property :value clock
))
424 "[2012-01-01 sun. 00:01]"))
425 (should-not (org-element-property :duration clock
)))
428 (org-test-with-temp-text
429 "CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
430 (org-element-at-point))))
431 (should (eq (org-element-property :status clock
) 'closed
))
432 (should (equal (org-element-property :raw-value
433 (org-element-property :value clock
))
434 "[2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02]"))
435 (should (equal (org-element-property :duration clock
) "0:01"))))
440 (ert-deftest test-org-element
/code-parser
()
441 "Test `code' parser."
444 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
445 (org-test-with-temp-text "~code~"
446 (org-element-map (org-element-parse-buffer) 'code
'identity
))))
447 ;; Multi-line markup.
450 (org-element-property
452 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
453 (org-test-with-temp-text "~first line\nsecond line~"
455 (org-element-parse-buffer) 'code
'identity nil t
))))
456 "first line\nsecond line")))
461 (ert-deftest test-org-element
/comment-parser
()
462 "Test `comment' parser."
465 (org-test-with-temp-text "# Comment"
466 (org-element-map (org-element-parse-buffer) 'comment
'identity
)))
469 (org-test-with-temp-text " # Comment"
470 (org-element-map (org-element-parse-buffer) 'comment
'identity
)))
471 ;; Preserve indentation.
474 (org-element-property
476 (org-test-with-temp-text "# No blank\n# One blank"
477 (org-element-map (org-element-parse-buffer) 'comment
'identity nil t
)))
478 "No blank\n One blank"))
479 ;; Comment with blank lines.
482 (org-element-property
484 (org-test-with-temp-text "# First part\n# \n#\n# Second part"
485 (org-element-map (org-element-parse-buffer) 'comment
'identity nil t
)))
486 "First part\n\n\nSecond part"))
487 ;; Do not mix comments and keywords.
490 (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
491 (length (org-element-map (org-element-parse-buffer) 'comment
495 (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
496 (org-element-property
498 (org-element-map (org-element-parse-buffer) 'comment
500 ;; Correctly handle non-empty blank lines at the end of buffer.
502 (org-test-with-temp-text "# A\n "
503 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
508 (ert-deftest test-org-element
/comment-block-parser
()
509 "Test `comment-block' parser."
512 (org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
513 (org-element-map (org-element-parse-buffer) 'comment-block
'identity
)))
516 (org-test-with-temp-text "#+begin_comment\nText\n#+end_comment"
517 (org-element-map (org-element-parse-buffer) 'comment-block
'identity
)))
518 ;; Ignore incomplete block.
520 (org-test-with-temp-text "#+BEGIN_COMMENT"
521 (org-element-map (org-element-parse-buffer) 'comment-block
523 ;; Handle non-empty blank line at the end of buffer.
525 (org-test-with-temp-text "#+BEGIN_COMMENT\nC\n#+END_COMMENT\n "
526 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
531 (ert-deftest test-org-element
/diary-sexp-parser
()
532 "Test `diary-sexp' parser."
536 (org-test-with-temp-text
537 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old"
538 (org-element-type (org-element-at-point)))))
539 ;; Diary sexp must live at beginning of line
542 (org-test-with-temp-text " %%(org-bbdb-anniversaries)"
543 (org-element-type (org-element-at-point)))))
544 ;; Handle non-empty blank line at the end of buffer.
546 (org-test-with-temp-text "%%(org-bbdb-anniversaries)\n "
547 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
552 (ert-deftest test-org-element
/drawer-parser
()
553 "Test `drawer' parser."
556 (org-test-with-temp-text ":TEST:\nText\n:END:"
557 (org-element-map (org-element-parse-buffer) 'drawer
'identity
)))
558 ;; Do not mix regular drawers and property drawers.
560 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:"
561 (org-element-map (org-element-parse-buffer) 'drawer
'identity nil t
)))
562 ;; Ignore incomplete drawer.
564 (org-test-with-temp-text ":TEST:"
565 (org-element-map (org-element-parse-buffer) 'drawer
'identity nil t
)))
566 ;; Handle non-empty blank line at the end of buffer.
568 (org-test-with-temp-text ":TEST:\nC\n:END:\n "
569 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
574 (ert-deftest test-org-element
/dynamic-block-parser
()
575 "Test `dynamic-block' parser."
578 (org-test-with-temp-text
579 "#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
580 (org-element-map (org-element-parse-buffer) 'dynamic-block
'identity
)))
583 (org-test-with-temp-text
584 "#+begin: myblock :param1 val1 :param2 val2\nText\n#+end:"
585 (org-element-map (org-element-parse-buffer) 'dynamic-block
'identity
)))
586 ;; Ignore incomplete block.
588 (org-test-with-temp-text "#+BEGIN: myblock :param1 val1 :param2 val2"
589 (org-element-map (org-element-parse-buffer) 'dynamic-block
591 ;; Handle non-empty blank line at the end of buffer.
593 (org-test-with-temp-text "#+BEGIN: myblock :param val1\nC\n#+END:\n "
594 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
599 (ert-deftest test-org-element
/entity-parser
()
600 "Test `entity' parser."
603 (org-test-with-temp-text "\\sin"
604 (org-element-map (org-element-parse-buffer) 'entity
'identity
)))
607 (org-element-property
609 (org-test-with-temp-text "\\alpha{}text"
610 (org-element-map (org-element-parse-buffer) 'entity
'identity nil t
))))
611 ;; User-defined entity.
614 (org-element-property
616 (let ((org-entities-user
617 '(("test" "test" nil
"test" "test" "test" "test"))))
618 (org-test-with-temp-text "\\test"
619 (org-element-map (org-element-parse-buffer) 'entity
'identity nil t
))))
621 ;; Special case: entity at the end of a container.
624 (org-test-with-temp-text "*\\alpha \\beta*"
625 (search-forward "be")
626 (org-element-type (org-element-context))))))
631 (ert-deftest test-org-element
/example-block-parser
()
632 "Test `example-block' parser."
635 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
636 (org-element-map (org-element-parse-buffer) 'example-block
'identity
)))
637 ;; Ignore incomplete block.
640 (org-test-with-temp-text "#+BEGIN_EXAMPLE"
641 (org-element-type (org-element-at-point)))))
642 ;; Properly un-escape code.
644 (equal "* Headline\n #+keyword\nText\n"
645 (org-test-with-temp-text
646 "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText\n#+END_EXAMPLE"
647 (org-element-property :value
(org-element-at-point)))))
648 ;; Remove block indentation according to block boundaries, unless
649 ;; block contents are less indented than block boundaries.
652 (org-test-with-temp-text " #+BEGIN_EXAMPLE\n L1\n L2\n #+END_EXAMPLE"
653 (let ((org-src-preserve-indentation t
))
654 (org-element-property :value
(org-element-at-point))))))
658 (org-test-with-temp-text " #+BEGIN_EXAMPLE\n L1\n L2\n #+END_EXAMPLE"
659 (let ((org-src-preserve-indentation t
))
660 (org-element-property :value
(org-element-at-point))))))
661 ;; Handle non-empty blank line at the end of buffer.
663 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nC\n#+END_EXAMPLE\n "
664 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
666 (ert-deftest test-org-element
/block-switches
()
667 "Test `example-block' and `src-block' switches parsing."
668 (let ((org-coderef-label-format "(ref:%s)"))
669 ;; 1. Test "-i" switch.
671 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
672 (org-element-property :preserve-indent
(org-element-at-point))))
674 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
675 (org-element-property :preserve-indent
(org-element-at-point))))
677 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
678 (org-element-property :preserve-indent
(org-element-at-point))))
680 (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
681 (org-element-property :preserve-indent
(org-element-at-point))))
682 ;; 2. "-n -r -k" combination should number lines, retain labels but
683 ;; not use them in coderefs.
685 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
686 (let ((element (org-element-at-point)))
687 (and (org-element-property :number-lines element
)
688 (org-element-property :retain-labels element
)
689 (not (org-element-property :use-labels element
))))))
691 (org-test-with-temp-text
692 "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
693 (let ((element (org-element-at-point)))
694 (and (org-element-property :number-lines element
)
695 (org-element-property :retain-labels element
)
696 (not (org-element-property :use-labels element
))))))
697 ;; 3. "-n -r" combination should number-lines remove labels and not
698 ;; use them in coderefs.
700 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
701 (let ((element (org-element-at-point)))
702 (and (org-element-property :number-lines element
)
703 (not (org-element-property :retain-labels element
))
704 (not (org-element-property :use-labels element
))))))
706 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
707 (let ((element (org-element-at-point)))
708 (and (org-element-property :number-lines element
)
709 (not (org-element-property :retain-labels element
))
710 (not (org-element-property :use-labels element
))))))
711 ;; 4. "-n" or "+n" should number lines, retain labels and use them
714 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
715 (let ((element (org-element-at-point)))
716 (and (org-element-property :number-lines element
)
717 (org-element-property :retain-labels element
)
718 (org-element-property :use-labels element
)))))
720 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
721 (let ((element (org-element-at-point)))
722 (and (org-element-property :number-lines element
)
723 (org-element-property :retain-labels element
)
724 (org-element-property :use-labels element
)))))
726 (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
727 (let ((element (org-element-at-point)))
728 (and (org-element-property :number-lines element
)
729 (org-element-property :retain-labels element
)
730 (org-element-property :use-labels element
)))))
732 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
733 (let ((element (org-element-at-point)))
734 (and (org-element-property :number-lines element
)
735 (org-element-property :retain-labels element
)
736 (org-element-property :use-labels element
)))))
737 ;; 5. No switch should not number lines, but retain labels and use
740 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
741 (let ((element (org-element-at-point)))
742 (and (not (org-element-property :number-lines element
))
743 (org-element-property :retain-labels element
)
744 (org-element-property :use-labels element
)))))
746 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
747 (let ((element (org-element-at-point)))
748 (and (not (org-element-property :number-lines element
))
749 (org-element-property :retain-labels element
)
750 (org-element-property :use-labels element
)))))
751 ;; 6. "-r" switch only: do not number lines, remove labels, and
752 ;; don't use labels in coderefs.
754 (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
755 (let ((element (org-element-at-point)))
756 (and (not (org-element-property :number-lines element
))
757 (not (org-element-property :retain-labels element
))
758 (not (org-element-property :use-labels element
))))))
760 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
761 (let ((element (org-element-at-point)))
762 (and (not (org-element-property :number-lines element
))
763 (not (org-element-property :retain-labels element
))
764 (not (org-element-property :use-labels element
))))))
765 ;; 7. Recognize coderefs with user-defined syntax.
769 (org-test-with-temp-text
770 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
771 (org-element-property :label-fmt
(org-element-at-point)))))
775 (org-test-with-temp-text
776 "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
777 (org-element-property :label-fmt
(org-element-at-point)))))))
782 (ert-deftest test-org-element
/export-block-parser
()
783 "Test `export-block' parser."
786 (org-test-with-temp-text "#+BEGIN_LATEX\nText\n#+END_LATEX"
788 (let ((org-element-block-name-alist
789 '(("LATEX" . org-element-export-block-parser
))))
790 (org-element-parse-buffer))
791 'export-block
'identity
)))
794 (let ((org-element-block-name-alist
795 '(("LATEX" . org-element-export-block-parser
))))
796 (org-test-with-temp-text "#+begin_latex\nText\n#+end_latex"
797 (org-element-map (org-element-parse-buffer) 'export-block
'identity
))))
798 ;; Ignore incomplete block.
800 (let ((org-element-block-name-alist
801 '(("LATEX" . org-element-export-block-parser
))))
802 (org-test-with-temp-text "#+BEGIN_LATEX"
803 (org-element-map (org-element-parse-buffer) 'export-block
805 ;; Handle non-empty blank line at the end of buffer.
807 (let ((org-element-block-name-alist
808 '(("LATEX" . org-element-export-block-parser
))))
809 (org-test-with-temp-text "#+BEGIN_LATEX\nC\n#+END_LATEX\n "
810 (= (org-element-property :end
(org-element-at-point)) (point-max))))))
815 (ert-deftest test-org-element
/export-snippet-parser
()
816 "Test `export-snippet' parser."
819 '("back-end" .
"contents")
820 (org-test-with-temp-text "@@back-end:contents@@"
822 (org-element-parse-buffer) 'export-snippet
823 (lambda (snippet) (cons (org-element-property :back-end snippet
)
824 (org-element-property :value snippet
)))
830 (ert-deftest test-org-element
/fixed-width-parser
()
831 "Test fixed-width area parsing."
832 ;; Preserve indentation.
834 (org-test-with-temp-text ": no blank\n: one blank"
835 (org-element-map (org-element-parse-buffer) 'fixed-width
'identity
)))
836 ;; Fixed-width with empty lines.
838 (org-test-with-temp-text ": first part\n:\n: \n: second part"
839 (org-element-map (org-element-parse-buffer) 'fixed-width
'identity
)))
840 ;; Parse indented fixed-width markers.
842 (org-test-with-temp-text "Text\n : no blank\n : one blank"
843 (org-element-map (org-element-parse-buffer) 'fixed-width
'identity
)))
844 ;; Distinguish fixed-width areas within a list and outside of it.
848 (org-test-with-temp-text "
851 : fixed-width outside"
852 (org-element-map (org-element-parse-buffer) 'fixed-width
'identity
)))))
853 ;; Handle non-empty blank line at the end of buffer.
855 (org-test-with-temp-text ": A\n "
856 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
859 ;;;; Footnote Definition
861 (ert-deftest test-org-element
/footnote-definition-parser
()
862 "Test `footnote-definition' parser."
864 (org-test-with-temp-text "[fn:1] Definition"
865 (org-element-map (org-element-parse-buffer) 'footnote-definition
867 ;; Footnote with more contents
870 (org-element-property
872 (org-test-with-temp-text "[fn:1] Definition\n\n| a | b |"
873 (org-element-map (org-element-parse-buffer) 'footnote-definition
875 ;; Footnote starting with special syntax.
877 (org-test-with-temp-text "[fn:1] - no item"
878 (org-element-map (org-element-parse-buffer) 'item
'identity
)))
879 ;; Correctly handle footnote starting with an empty line.
882 (org-test-with-temp-text "[fn:1]\n\n Body"
883 (org-element-property :contents-begin
(org-element-at-point)))))
884 ;; Handle non-empty blank line at the end of buffer.
886 (org-test-with-temp-text "[fn:1] Definition\n "
887 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
890 ;;;; Footnotes Reference.
892 (ert-deftest test-org-element
/footnote-reference-parser
()
893 "Test `footnote-reference' parser."
894 ;; Parse a standard reference.
896 (org-test-with-temp-text "Text[fn:label]"
898 (org-element-parse-buffer) 'footnote-reference
'identity
)))
899 ;; Parse a normalized reference.
901 (org-test-with-temp-text "Text[1]"
903 (org-element-parse-buffer) 'footnote-reference
'identity
)))
904 ;; Parse an inline reference.
906 (org-test-with-temp-text "Text[fn:test:def]"
908 (org-element-parse-buffer) 'footnote-reference
'identity
)))
909 ;; Parse an anonymous reference.
911 (org-test-with-temp-text "Text[fn::def]"
913 (org-element-parse-buffer) 'footnote-reference
'identity
)))
914 ;; Parse nested footnotes.
918 (org-test-with-temp-text "Text[fn::def [fn:label]]"
920 (org-element-parse-buffer) 'footnote-reference
'identity
)))))
921 ;; Parse adjacent footnotes.
923 (org-test-with-temp-text "Text[fn:label1][fn:label2]"
927 (org-element-parse-buffer) 'footnote-reference
'identity
)))))
928 ;; Only properly closed footnotes are recognized as such.
930 (org-test-with-temp-text "Text[fn:label"
932 (org-element-parse-buffer) 'footnote-reference
'identity
))))
937 (ert-deftest test-org-element
/headline-comment-keyword
()
938 "Test COMMENT keyword recognition."
940 (org-test-with-temp-text "* Headline"
941 (let ((org-comment-string "COMMENT"))
942 (should-not (org-element-property :commentedp
(org-element-at-point)))))
943 ;; Standard position.
944 (org-test-with-temp-text "* COMMENT Headline"
945 (let ((org-comment-string "COMMENT")
946 (headline (org-element-at-point)))
947 (should (org-element-property :commentedp headline
))
948 (should (equal (org-element-property :raw-value headline
) "Headline"))))
950 (org-test-with-temp-text "* COMMENT Headline"
951 (let* ((org-comment-string "Comment")
952 (headline (org-element-at-point)))
953 (should-not (org-element-property :commentedp headline
))
954 (should (equal (org-element-property :raw-value headline
)
955 "COMMENT Headline"))))
956 ;; With another keyword.
957 (org-test-with-temp-text "* TODO COMMENT Headline"
958 (let* ((org-comment-string "COMMENT")
959 (org-todo-keywords '((sequence "TODO" "DONE")))
960 (headline (org-element-at-point)))
961 (should (org-element-property :commentedp headline
))
962 (should (equal (org-element-property :raw-value headline
) "Headline"))))
963 ;; With the keyword only.
964 (org-test-with-temp-text "* COMMENT"
965 (let* ((org-comment-string "COMMENT")
966 (headline (org-element-at-point)))
967 (should (org-element-property :commentedp headline
))
968 (should (equal (org-element-property :raw-value headline
) "")))))
970 (ert-deftest test-org-element
/headline-archive-tag
()
971 "Test ARCHIVE tag recognition."
974 (org-test-with-temp-text "* Headline"
975 (let ((org-archive-tag "ARCHIVE"))
976 (org-element-property :archivedp
(org-element-at-point)))))
978 (org-test-with-temp-text "* Headline :ARCHIVE:"
979 (let ((org-archive-tag "ARCHIVE"))
980 (let ((headline (org-element-at-point)))
981 (should (org-element-property :archivedp headline
))
983 (should-not (org-element-property :tags headline
)))))
985 (org-test-with-temp-text "* Headline :test:ARCHIVE:"
986 (let ((org-archive-tag "ARCHIVE"))
987 (let ((headline (org-element-at-point)))
988 (should (org-element-property :archivedp headline
))
990 (should (equal (org-element-property :tags headline
) '("test"))))))
991 ;; Tag is case-sensitive.
993 (org-test-with-temp-text "* Headline :ARCHIVE:"
994 (let ((org-archive-tag "Archive"))
995 (org-element-property :archivedp
(org-element-at-point))))))
997 (ert-deftest test-org-element
/headline-properties
()
998 "Test properties from property drawer."
999 ;; All properties from property drawer have their symbol upper
1002 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
1003 (org-element-property :FOO
(org-element-at-point))))
1005 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
1006 (org-element-property :foo
(org-element-at-point))))
1007 ;; Also parse properties associated in inlinetasks.
1008 (when (featurep 'org-inlinetask
)
1010 (org-test-with-temp-text "*************** Inlinetask
1014 *************** END"
1015 (org-element-property :FOO
(org-element-at-point)))))
1016 ;; Do not find property drawer in a verbatim area.
1018 (org-test-with-temp-text
1025 (org-element-property :FOO
(org-element-at-point))))
1026 ;; Do not use properties from a drawer associated to an inlinetask.
1027 (when (featurep 'org-inlinetask
)
1029 (org-test-with-temp-text
1031 *************** Inlinetask
1035 *************** END"
1036 (org-element-property
1037 :FOO
(let ((org-inlinetask-min-level 15)) (org-element-at-point))))))
1038 ;; Do not find incomplete drawers.
1040 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar"
1041 (org-element-property :FOO
(org-element-at-point)))))
1044 ;;;; Horizontal Rule
1046 (ert-deftest test-org-element
/horizontal-rule-parser
()
1047 "Test `horizontal-rule' parser."
1050 (org-test-with-temp-text "-----"
1051 (org-element-map (org-element-parse-buffer) 'horizontal-rule
'identity
)))
1054 (org-test-with-temp-text " -----"
1055 (org-element-map (org-element-parse-buffer) 'horizontal-rule
'identity
)))
1056 ;; Hyphen must be alone on the line.
1058 (org-test-with-temp-text "-----wrong"
1059 (org-element-map (org-element-parse-buffer) 'horizontal-rule
'identity
)))
1060 ;; 4 hyphens is too small.
1062 (org-test-with-temp-text "----"
1063 (org-element-map (org-element-parse-buffer) 'horizontal-rule
'identity
)))
1064 ;; Handle non-empty blank line at the end of buffer.
1066 (org-test-with-temp-text "-----\n "
1067 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1070 ;;;; Inline Babel Call
1072 (ert-deftest test-org-element
/inline-babel-call-parser
()
1073 "Test `inline-babel-call' parser."
1075 (org-test-with-temp-text "call_test()"
1077 (org-element-parse-buffer) 'inline-babel-call
'identity
))))
1080 ;;;; Inline Src Block
1082 (ert-deftest test-org-element
/inline-src-block-parser
()
1083 "Test `inline-src-block' parser."
1085 (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
1086 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1089 (org-test-with-temp-text "src_emacs-lisp[:foo bar]{(+ 1 1)}"
1090 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1092 (org-test-with-temp-text "src_emacs-lisp[ :foo bar]{(+ 1 1)}"
1093 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1096 (org-test-with-temp-text "src_emacs-lisp[]{(+ 1 1)}"
1097 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1100 (org-test-with-temp-text "foosrc_emacs-lisp[]{(+ 1 1)}"
1101 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1103 (org-test-with-temp-text "src_emacs-lisp[]foo{(+ 1 1)}"
1104 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1105 ;; Invalid language name
1107 (org-test-with-temp-text "src_emacs-\tlisp{(+ 1 1)}"
1108 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
)))
1109 ;; Test parsing at the beginning of an item.
1111 (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
1112 (org-element-map (org-element-parse-buffer) 'inline-src-block
'identity
))))
1117 (ert-deftest test-org-element
/inlinetask-parser
()
1118 "Test `inlinetask' parser."
1119 (when (featurep 'org-inlinetask
)
1120 (let ((org-inlinetask-min-level 15))
1121 ;; Regular inlinetask.
1124 (org-test-with-temp-text
1125 "*************** Task\nTest\n*************** END"
1126 (org-element-type (org-element-at-point)))))
1127 ;; Degenerate inlinetask.
1130 (org-test-with-temp-text "*************** Task"
1131 (org-element-type (org-element-at-point)))))
1132 ;; Mixed inlinetasks.
1134 (org-test-with-temp-text
1136 *************** Task
1137 *************** Task2
1139 *************** END"
1141 (goto-char (org-element-property :end
(org-element-at-point)))
1147 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
1148 (org-test-with-temp-text "*************** TODO Task"
1149 (org-element-property :todo-keyword
(org-element-at-point))))))
1152 (org-test-with-temp-text "
1153 *************** Task
1154 DEADLINE: <2012-03-29 thu.>"
1156 (org-element-property :deadline
(org-element-at-point))))
1161 (org-test-with-temp-text "
1162 *************** [#A] Task"
1164 (org-element-property :priority
(org-element-at-point)))))
1169 (org-test-with-temp-text "
1170 *************** Task :test:"
1172 (org-element-property :tags
(org-element-at-point)))))
1173 ;; Regular properties are accessed through upper case keywords.
1175 (org-test-with-temp-text "
1176 *************** Task
1180 *************** END"
1182 (org-element-property :FOO
(org-element-at-point))))
1184 (org-test-with-temp-text "
1185 *************** Task
1189 *************** END"
1191 (org-element-property :foo
(org-element-at-point))))
1192 ;; Handle non-empty blank line at the end of buffer.
1194 (org-test-with-temp-text "*************** Task\n*************** END\n "
1195 (= (org-element-property :end
(org-element-at-point)) (point-max)))))))
1200 (ert-deftest test-org-element
/italic-parser
()
1201 "Test `italic' parser."
1204 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1205 (org-test-with-temp-text "/italic/"
1206 (org-element-map (org-element-parse-buffer) 'italic
'identity nil t
))))
1207 ;; Multi-line markup.
1210 (org-element-contents
1211 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1212 (org-test-with-temp-text "/first line\nsecond line/"
1213 (org-element-map (org-element-parse-buffer) 'italic
'identity nil t
))))
1214 '("first line\nsecond line"))))
1219 (ert-deftest test-org-element
/item-parser
()
1220 "Test `item' parser."
1223 (org-test-with-temp-text "- item"
1224 (org-element-map (org-element-parse-buffer) 'item
'identity
)))
1228 (org-element-property
1230 (org-test-with-temp-text "6. [@6] item"
1231 (org-element-map (org-element-parse-buffer) 'item
'identity nil t
)))))
1236 (org-element-property
1238 (org-test-with-temp-text "- tag :: description"
1239 (org-element-map (org-element-parse-buffer) 'item
'identity nil t
)))))
1240 ;; No tags in ordered lists.
1242 (org-element-property
1244 (org-test-with-temp-text "1. tag :: description"
1245 (org-element-map (org-element-parse-buffer) 'item
'identity nil t
))))
1250 (org-test-with-temp-text "
1255 (org-element-parse-buffer) 'item
1256 (lambda (item) (org-element-property :checkbox item
))))))
1257 ;; Item starting with special syntax.
1259 (equal '(("- item"))
1260 (org-test-with-temp-text "- - item"
1261 (org-element-map (org-element-parse-buffer) 'paragraph
1262 'org-element-contents
))))
1263 ;; Block in an item: ignore indentation within the block.
1265 (org-test-with-temp-text "- item\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1267 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1272 (ert-deftest test-org-element
/keyword-parser
()
1273 "Test `keyword' parser."
1276 (org-test-with-temp-text "#+KEYWORD: value"
1277 (org-element-map (org-element-parse-buffer) 'keyword
'identity
)))
1278 ;; Keywords are case-insensitive.
1280 (org-test-with-temp-text "#+keyword: value"
1281 (org-element-map (org-element-parse-buffer) 'keyword
'identity
)))
1282 ;; Affiliated keywords are not keywords.
1284 (org-test-with-temp-text "#+NAME: value
1286 (org-element-map (org-element-parse-buffer) 'keyword
'identity
)))
1287 ;; Do not mix keywords with Babel calls and dynamic blocks.
1289 (org-test-with-temp-text "#+CALL: fun()"
1290 (org-element-map (org-element-parse-buffer) 'keyword
'identity
)))
1292 (org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
1293 (org-element-map (org-element-parse-buffer) 'keyword
'identity
)))
1294 ;; Handle non-empty blank line at the end of buffer.
1296 (org-test-with-temp-text "#+KEYWORD: value\n "
1297 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1300 ;;;; Latex Environment
1302 (ert-deftest test-org-element
/latex-environment-parser
()
1303 "Test `latex-environment' parser."
1305 (org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
1306 (org-element-map (org-element-parse-buffer) 'latex-environment
'identity
)))
1307 ;; Allow nested environments.
1315 (org-test-with-temp-text "
1321 (org-element-property
1324 (org-element-parse-buffer) 'latex-environment
'identity nil t
)))))
1325 ;; Allow environments with options and arguments.
1327 (eq 'latex-environment
1328 (org-test-with-temp-text
1329 "\\begin{theorem}[Euler]\ne^{i\\pi}+1=0\n\\end{theorem}"
1330 (org-element-type (org-element-at-point)))))
1332 (eq 'latex-environment
1333 (org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
1334 (org-element-type (org-element-at-point)))))
1335 ;; Allow environments without newline after \begin{.}.
1337 (eq 'latex-environment
1338 (org-test-with-temp-text "\\begin{env}{arg}something\nvalue\n\\end{env}"
1339 (org-element-type (org-element-at-point)))))
1340 ;; Allow one-line environments.
1342 (eq 'latex-environment
1343 (org-test-with-temp-text "\\begin{env}{arg}something\\end{env}"
1344 (org-element-type (org-element-at-point)))))
1345 ;; Should not allow different tags.
1347 (eq 'latex-environment
1348 (org-test-with-temp-text "\\begin{env*}{arg}something\\end{env}"
1349 (org-element-type (org-element-at-point)))))
1350 ;; LaTeX environments must be on separate lines.
1352 (eq 'latex-environment
1353 (org-test-with-temp-text "\\begin{env} x \\end{env} y"
1354 (org-element-type (org-element-at-point)))))
1356 (eq 'latex-environment
1357 (org-test-with-temp-text "y \\begin{env} x<point> \\end{env}"
1358 (org-element-type (org-element-at-point)))))
1359 ;; Handle non-empty blank line at the end of buffer.
1361 (org-test-with-temp-text "\\begin{env}\n\\end{env}\n "
1362 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1367 (ert-deftest test-org-element
/latex-fragment-parser
()
1368 "Test `latex-fragment' parser."
1370 (org-test-with-temp-text "$a$"
1371 (org-element-map (org-element-parse-buffer) 'latex-fragment
'identity
)))
1373 (org-test-with-temp-text "$$a$$"
1374 (org-element-map (org-element-parse-buffer) 'latex-fragment
'identity
)))
1376 (org-test-with-temp-text "\\(a\\)"
1377 (org-element-map (org-element-parse-buffer) 'latex-fragment
'identity
)))
1379 (org-test-with-temp-text "\\[a\\]"
1381 (org-element-parse-buffer) 'latex-fragment
'identity
)))
1382 ;; Test fragment at the beginning of an item.
1385 (org-test-with-temp-text "- $x$"
1386 (progn (search-forward "$")
1387 (org-element-type (org-element-context)))))))
1392 (ert-deftest test-org-element
/line-break-parser
()
1393 "Test `line-break' parser."
1396 (org-test-with-temp-text "Text \\\\"
1397 (org-element-map (org-element-parse-buffer) 'line-break
'identity
)))
1398 ;; Line break with trailing white spaces.
1400 (org-test-with-temp-text "Text \\\\ "
1401 (org-element-map (org-element-parse-buffer) 'line-break
'identity
)))
1402 ;; Three backslashes are too much.
1404 (org-test-with-temp-text "Text \\\\\\"
1405 (org-element-map (org-element-parse-buffer) 'line-break
'identity
))))
1410 (ert-deftest test-org-element
/link-parser
()
1411 "Test `link' parser."
1416 (org-test-with-temp-text "<<<radio>>>A radio link"
1417 (org-update-radio-target-regexp)
1418 (org-element-property
1420 (org-element-map (org-element-parse-buffer) 'link
#'identity nil t
)))))
1423 ;; ... with description.
1427 (org-test-with-temp-text "[[http://orgmode.org][Orgmode.org]]"
1428 (org-element-contents
1429 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1430 ;; ... without description.
1434 (org-test-with-temp-text "[[http://orgmode.org]]"
1435 (org-element-property
1437 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1438 ;; ... with expansion.
1441 "//orgmode.org/worg"
1442 (org-test-with-temp-text "[[Org:worg]]"
1443 (let ((org-link-abbrev-alist '(("Org" .
"http://orgmode.org/"))))
1444 (org-element-property
1446 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
))))))
1447 ;; ... with translation.
1451 (org-test-with-temp-text "[[http://orgmode.org]]"
1452 (flet ((link-translate (type path
) (cons type
"127.0.0.1")))
1453 (let ((org-link-translation-function 'link-translate
))
1454 (org-element-property
1456 (org-element-map (org-element-parse-buffer) 'link
1457 'identity nil t
)))))))
1462 (org-test-with-temp-text "[[id:aaaa]]"
1463 (org-element-property
1465 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1466 ;; ... custom-id link.
1470 (org-test-with-temp-text "[[#some-id]]"
1471 (org-element-property
1473 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1474 ;; ... coderef link.
1478 (org-test-with-temp-text "[[(reference)]]"
1479 (org-element-property
1481 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1486 (org-test-with-temp-text "[[target-or-title]]"
1487 (org-element-property
1489 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))))
1490 ;; ... file-type link with search option.
1493 '(("file" "projects.org" "*task title"))
1494 (org-test-with-temp-text "[[file:projects.org::*task title]]"
1495 (org-element-map (org-element-parse-buffer) 'link
1496 (lambda (l) (list (org-element-property :type l
)
1497 (org-element-property :path l
)
1498 (org-element-property :search-option l
)))))))
1499 ;; ... file-type link with application...
1502 '("file" "projects.org" "docview")
1503 (org-test-with-temp-text "[[docview:projects.org]]"
1504 (let ((l (org-element-context)))
1505 (list (org-element-property :type l
)
1506 (org-element-property :path l
)
1507 (org-element-property :application l
))))))
1508 ;; ... `:path' in a file-type link must be compatible with "file"
1509 ;; scheme in URI syntax, even if Org syntax isn't...
1511 (org-test-with-temp-text-in-file ""
1512 (let ((file (expand-file-name (buffer-file-name))))
1513 (insert (format "[[file://%s]]" file
))
1514 (equal (org-element-property :path
(org-element-context))
1515 (concat "//" file
)))))
1517 (org-test-with-temp-text-in-file ""
1518 (let ((file (expand-file-name (buffer-file-name))))
1519 (insert (format "[[file:%s]]" file
))
1520 (equal (org-element-property :path
(org-element-context))
1521 (concat "//" file
)))))
1523 (org-test-with-temp-text-in-file ""
1524 (let ((file (file-relative-name (buffer-file-name))))
1525 (insert (format "[[file:%s]]" file
))
1526 (list (org-element-property :path
(org-element-context)) file
))))
1527 ;; ... multi-line link.
1529 (equal "//orgmode.org"
1530 (org-test-with-temp-text "[[http://orgmode.\norg]]"
1531 (org-element-property :path
(org-element-context)))))
1534 (org-test-with-temp-text "A link: http://orgmode.org"
1535 (org-element-map (org-element-parse-buffer) 'link
'identity
)))
1538 (org-test-with-temp-text "A link: <http://orgmode.org>"
1539 (org-element-map (org-element-parse-buffer) 'link
'identity nil t
)))
1540 ;; Link abbreviation.
1543 (org-test-with-temp-text
1544 "#+LINK: orgmode http://www.orgmode.org/\n[[orgmode:#docs]]"
1545 (progn (org-mode-restart)
1546 (goto-char (1- (point-max)))
1547 (org-element-property :type
(org-element-context))))))
1548 ;; Link abbreviation in a secondary string.
1551 (org-test-with-temp-text
1552 "#+LINK: orgmode http://www.orgmode.org/\n* H [[orgmode:#docs]]"
1553 (progn (org-mode-restart)
1554 (org-element-map (org-element-parse-buffer) 'link
1555 (lambda (link) (org-element-property :type link
))
1557 ;; Plain links are allowed as description of regular links.
1560 (org-test-with-temp-text "[[http://orgmode.org][file:unicorn.jpg]]"
1561 (search-forward "file:")
1562 (org-element-property :type
(org-element-context))))))
1567 (ert-deftest test-org-element
/macro-parser
()
1568 "Test `macro' parser."
1569 ;; Without arguments.
1571 (org-test-with-temp-text "{{{macro}}}"
1572 (org-element-map (org-element-parse-buffer) 'macro
'identity
)))
1575 (org-test-with-temp-text "{{{macro(arg1,arg2)}}}"
1576 (org-element-map (org-element-parse-buffer) 'macro
'identity
)))
1577 ;; Properly handle protected commas in arguments...
1581 (org-test-with-temp-text "{{{macro(arg1\\,arg1,arg2)}}}"
1582 (org-element-property :args
(org-element-context))))))
1583 ;; ... even when last argument ends with a protected comma.
1586 (org-test-with-temp-text "{{{macro(C-\\,)}}}"
1587 (org-element-property :args
(org-element-context)))))
1588 ;; Allow to escape escaping character.
1591 (org-test-with-temp-text "{{{macro(C-\\\\,)}}}"
1592 (org-element-property :args
(org-element-context)))))
1593 ;; No need to escape backslashes elsewhere.
1596 (org-test-with-temp-text "{{{macro(\\)}}}"
1597 (org-element-property :args
(org-element-context))))))
1602 (ert-deftest test-org-element
/node-property
()
1603 "Test `node-property' parser."
1606 (equal '("abc" "value")
1607 (org-test-with-temp-text ":PROPERTIES:\n<point>:abc: value\n:END:"
1608 (let ((element (org-element-at-point)))
1609 (list (org-element-property :key element
)
1610 (org-element-property :value element
))))))
1611 ;; Value should be trimmed.
1614 (org-test-with-temp-text ":PROPERTIES:\n<point>:abc: value \n:END:"
1615 (org-element-property :value
(org-element-at-point)))))
1616 ;; A node property requires to be wrapped within a property drawer.
1619 (org-test-with-temp-text ":abc: value"
1620 (org-element-type (org-element-at-point)))))
1621 ;; Accept empty properties.
1623 (equal '(("foo" "value") ("bar" ""))
1624 (org-test-with-temp-text ":PROPERTIES:\n:foo: value\n:bar:\n:END:"
1625 (org-element-map (org-element-parse-buffer) 'node-property
1627 (list (org-element-property :key p
)
1628 (org-element-property :value p
)))))))
1629 ;; Ignore all non-property lines in property drawers.
1633 (org-test-with-temp-text ":PROPERTIES:\nWrong1\n:foo: value\nWrong2\n:END:"
1634 (org-element-map (org-element-parse-buffer) 'node-property
1636 (list (org-element-property :key p
)
1637 (org-element-property :value p
))))))))
1642 (ert-deftest test-org-element
/paragraph-parser
()
1643 "Test `paragraph' parser."
1646 (org-test-with-temp-text "Paragraph"
1647 (org-element-map (org-element-parse-buffer) 'paragraph
'identity nil t
)))
1648 ;; Property find end of a paragraph stuck to another element.
1651 (org-test-with-temp-text "Paragraph\n# Comment"
1652 (org-element-map (org-element-parse-buffer) 'paragraph
1653 (lambda (p) (char-after (org-element-property :end p
)))
1655 ;; Include ill-formed Keywords.
1657 (org-test-with-temp-text "#+wrong_keyword something"
1658 (org-element-map (org-element-parse-buffer) 'paragraph
'identity
)))
1659 ;; Include incomplete-drawers.
1661 (org-test-with-temp-text ":TEST:\nParagraph"
1662 (let ((elem (org-element-at-point)))
1663 (and (eq (org-element-type elem
) 'paragraph
)
1664 (= (point-max) (org-element-property :end elem
))))))
1665 ;; Include incomplete blocks.
1667 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph"
1668 (let ((elem (org-element-at-point)))
1669 (and (eq (org-element-type elem
) 'paragraph
)
1670 (= (point-max) (org-element-property :end elem
))))))
1671 ;; Include incomplete dynamic blocks.
1673 (org-test-with-temp-text "#+BEGIN: \nParagraph"
1674 (let ((elem (org-element-at-point)))
1675 (and (eq (org-element-type elem
) 'paragraph
)
1676 (= (point-max) (org-element-property :end elem
))))))
1677 ;; Include incomplete latex environments.
1679 (org-test-with-temp-text "\begin{equation}\nParagraph"
1680 (let ((elem (org-element-at-point)))
1681 (and (eq (org-element-type elem
) 'paragraph
)
1682 (= (point-max) (org-element-property :end elem
))))))
1683 ;; Do not steal affiliated keywords from container.
1685 (org-test-with-temp-text "#+ATTR_LATEX: test\n- item 1"
1686 (let ((elem (progn (search-forward "item") (org-element-at-point))))
1687 (and (eq (org-element-type elem
) 'paragraph
)
1688 (not (org-element-property :attr_latex elem
))
1689 (/= (org-element-property :begin elem
) 1)))))
1690 ;; Handle non-empty blank line at the end of buffer.
1692 (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
1693 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1698 (ert-deftest test-org-element
/plain-list-parser
()
1699 "Test `plain-list' parser."
1700 (org-test-with-temp-text "- item"
1701 (should (org-element-map (org-element-parse-buffer) 'plain-list
'identity
)))
1702 ;; Blank lines after the list only belong to outer plain list.
1706 (org-test-with-temp-text "
1711 (let ((endings (org-element-map (org-element-parse-buffer) 'plain-list
1712 (lambda (pl) (org-element-property :end pl
)))))
1714 ;; Move to ending of outer list.
1715 (progn (goto-char (car endings
)) (looking-at "Outside list"))
1716 ;; Move to ending of inner list.
1717 (progn (goto-char (nth 1 endings
)) (looking-at "^$")))))))
1718 ;; Correctly compute end of list if it doesn't end at a line
1721 (org-test-with-temp-text "- list\n \n "
1722 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1727 (ert-deftest test-org-element
/planning-parser
()
1728 "Test `planning' parser."
1729 ;; Test various keywords.
1731 (org-element-property
1733 (org-test-with-temp-text "* H\n<point>CLOSED: [2012-03-29 thu.]"
1734 (org-element-at-point))))
1736 (org-element-property
1738 (org-test-with-temp-text "* H\n<point>DEADLINE: <2012-03-29 thu.>"
1739 (org-element-at-point))))
1741 (org-element-property
1743 (org-test-with-temp-text "* H\n<point>SCHEDULED: <2012-03-29 thu.>"
1744 (org-element-at-point))))
1745 ;; Planning line only exists right after a headline.
1748 (org-test-with-temp-text "DEADLINE: <2012-03-29 thu.>"
1749 (org-element-type (org-element-at-point)))))
1752 (org-test-with-temp-text
1753 "* H\n# Comment\n<point>DEADLINE: <2012-03-29 thu.>"
1754 (org-element-type (org-element-at-point))))))
1757 ;;;; Property Drawer
1759 (ert-deftest test-org-element
/property-drawer-parser
()
1760 "Test `property-drawer' parser."
1763 (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:"
1765 (org-element-parse-buffer) 'property-drawer
'identity nil t
)))
1766 ;; Do not mix property drawers and regular drawers.
1768 (org-test-with-temp-text ":TEST:\n:prop: value\n:END:"
1770 (org-element-parse-buffer) 'property-drawer
'identity nil t
)))
1771 ;; Ignore incomplete drawer.
1773 (org-test-with-temp-text ":PROPERTIES:\n:prop: value"
1775 (org-element-parse-buffer) 'property-drawer
'identity nil t
)))
1776 ;; Handle non-empty blank line at the end of buffer.
1778 (org-test-with-temp-text ":PROPERTIES:\n:END:\n "
1779 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1784 (ert-deftest test-org-element
/quote-block-parser
()
1785 "Test `quote-block' parser."
1788 (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
1789 (org-element-map (org-element-parse-buffer) 'quote-block
'identity
)))
1790 ;; Ignore incomplete block.
1792 (org-test-with-temp-text "#+BEGIN_QUOTE"
1793 (org-element-map (org-element-parse-buffer) 'quote-block
'identity nil t
)))
1794 ;; Handle non-empty blank line at the end of buffer.
1796 (org-test-with-temp-text "#+BEGIN_QUOTE\nC\n#+END_QUOTE\n "
1797 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1802 (ert-deftest test-org-element
/radio-target-parser
()
1803 "Test `radio-target' parser."
1807 (org-test-with-temp-text "<<<radio>>>"
1808 (org-element-type (org-element-context)))))
1809 ;; Radio targets with objects.
1812 (org-test-with-temp-text "<<<radio \\alpha>>>"
1813 (org-element-type (org-element-context)))))
1814 ;; Radio targets starting with an object.
1817 (org-test-with-temp-text "<<<\\alpha radio>>>"
1818 (org-element-type (org-element-context)))))
1819 ;; Radio targets cannot begin or end with white space.
1822 (org-test-with-temp-text "<<< radio>>>"
1823 (org-element-type (org-element-context)))))
1826 (org-test-with-temp-text "<<<radio >>>"
1827 (org-element-type (org-element-context))))))
1832 (ert-deftest test-org-element
/section-parser
()
1833 "Test `section' parser."
1836 (org-test-with-temp-text "* Headline\nText"
1837 (org-element-map (org-element-parse-buffer) 'section
'identity
)))
1838 ;; There's a section before the first headline.
1840 (org-test-with-temp-text "Text"
1841 (org-element-map (org-element-parse-buffer) 'section
'identity
)))
1842 ;; A section cannot be empty.
1844 (org-test-with-temp-text "* Headline 1\n* Headline 2"
1845 (org-element-map (org-element-parse-buffer) 'section
'identity
)))
1846 ;; A section doesn't contain sub-trees.
1848 (org-test-with-temp-text "* Head\nText\n** Sub-Head"
1850 (org-element-map (org-element-parse-buffer) 'section
'identity nil t
)
1851 'headline
'identity
))))
1856 (ert-deftest test-org-element
/special-block-parser
()
1857 "Test `special-block' parser."
1861 (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
1862 (org-element-property :type
(org-element-at-point)))))
1863 ;; Special blocks are case sensitive.
1866 (org-test-with-temp-text "#+BEGIN_CaSe\nText\n#+END_CaSe"
1867 (org-element-property :type
(org-element-at-point)))))
1868 ;; Special blocks can contain paragraphs.
1871 (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
1873 (org-element-type (org-element-at-point)))))
1874 ;; Ignore incomplete block.
1877 (org-test-with-temp-text "#+BEGIN_SPECIAL"
1878 (org-element-type (org-element-at-point)))))
1879 ;; Allow special characters in type.
1881 (equal '(special-block "SPECIAL*")
1882 (org-test-with-temp-text "#+BEGIN_SPECIAL*\nContents\n#+END_SPECIAL*"
1883 (let ((element (org-element-at-point)))
1884 (list (org-element-type element
)
1885 (org-element-property :type element
))))))
1886 ;; Handle non-empty blank line at the end of buffer.
1888 (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n "
1889 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1894 (ert-deftest test-org-element
/src-block-parser
()
1895 "Test `src-block' parser."
1898 (org-test-with-temp-text "#+BEGIN_SRC org\nText\n#+END_SRC"
1899 (org-element-map (org-element-parse-buffer) 'src-block
'identity
)))
1900 ;; Ignore incomplete block.
1902 (org-test-with-temp-text "#+BEGIN_SRC"
1903 (org-element-map (org-element-parse-buffer) 'src-block
'identity
)))
1904 ;; Properly un-escape code.
1906 (equal "* Headline\n #+keyword\nText\n"
1907 (org-test-with-temp-text
1908 "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
1909 (org-element-property :value
(org-element-at-point)))))
1910 ;; Remove block indentation according to block boundaries, unless
1911 ;; block contents are less indented than block boundaries.
1914 (org-test-with-temp-text " #+BEGIN_SRC org\n L1\n L2\n #+END_SRC"
1915 (let ((org-src-preserve-indentation t
))
1916 (org-element-property :value
(org-element-at-point))))))
1920 (org-test-with-temp-text " #+BEGIN_SRC org\n L1\n L2\n #+END_SRC"
1921 (let ((org-src-preserve-indentation t
))
1922 (org-element-property :value
(org-element-at-point))))))
1923 ;; Handle non-empty blank line at the end of buffer.
1925 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\nC\n#+END_SRC\n "
1926 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
1929 ;;;; Statistics Cookie
1931 (ert-deftest test-org-element
/statistics-cookie
()
1932 "Test `statistics-cookie' parser."
1935 (org-test-with-temp-text "[1/2]"
1936 (org-element-map (org-element-parse-buffer) 'statistics-cookie
'identity
)))
1939 (org-test-with-temp-text "[33%]"
1941 (org-element-parse-buffer) 'statistics-cookie
'identity
))))
1946 (ert-deftest test-org-element
/strike-through-parser
()
1947 "Test `strike-through' parser."
1950 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1951 (org-test-with-temp-text "+strike-through+"
1952 (org-element-map (org-element-parse-buffer) 'strike-through
'identity
))))
1953 ;; Multi-line markup.
1956 (org-element-contents
1957 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
1958 (org-test-with-temp-text "+first line\nsecond line+"
1960 (org-element-parse-buffer) 'strike-through
'identity nil t
))))
1961 '("first line\nsecond line"))))
1966 (ert-deftest test-org-element
/subscript-parser
()
1967 "Test `subscript' parser."
1970 (org-test-with-temp-text "a_b"
1971 (org-element-map (org-element-parse-buffer) 'subscript
'identity
)))
1974 (org-test-with-temp-text "a_{b}"
1975 (org-element-map (org-element-parse-buffer) 'subscript
'identity
)))
1976 ;; Multiple subscripts in a paragraph.
1979 (org-test-with-temp-text "a_b and c_d"
1981 (org-element-map (org-element-parse-buffer) 'subscript
'identity
))))))
1986 (ert-deftest test-org-element
/superscript-parser
()
1987 "Test `superscript' parser."
1990 (org-test-with-temp-text "a^b"
1991 (org-element-map (org-element-parse-buffer) 'superscript
'identity
)))
1994 (org-test-with-temp-text "a^{b}"
1995 (org-element-map (org-element-parse-buffer) 'superscript
'identity
)))
1996 ;; Multiple superscript in a paragraph.
1999 (org-test-with-temp-text "a^b and c^d"
2002 (org-element-parse-buffer) 'superscript
'identity
))))))
2007 (ert-deftest test-org-element
/table-parser
()
2008 "Test `table' parser."
2010 (org-test-with-temp-text "| a |"
2011 (org-element-map (org-element-parse-buffer) 'table
'identity
)))
2012 ;; TBLFM keyword is case insensitive.
2014 (org-test-with-temp-text "| a |\n#+tblfm: test"
2015 (org-element-property
2017 (org-element-map (org-element-parse-buffer) 'table
'identity nil t
))))
2018 ;; Handle multiple TBLFM lines.
2021 (org-test-with-temp-text "| a |\n#+TBLFM: test1\n#+TBLFM: test2"
2022 (length (org-element-property
2025 (org-element-parse-buffer) 'table
'identity nil t
))))))
2026 ;; Handle non-empty blank line at the end of buffer.
2028 (org-test-with-temp-text "| a |\n "
2029 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
2034 (ert-deftest test-org-element
/table-cell-parser
()
2035 "Test `table-cell' parser."
2036 ;; Regular table cell.
2038 (org-test-with-temp-text "| a |"
2039 (org-element-map (org-element-parse-buffer) 'table-cell
'identity
)))
2040 ;; Last vertical bar may be omitted.
2042 (org-test-with-temp-text "| a "
2043 (org-element-map (org-element-parse-buffer) 'table-cell
'identity
))))
2048 (ert-deftest test-org-element
/table-row-parser
()
2049 "Test `table-row' parser."
2051 (equal '(standard rule
)
2052 (org-test-with-temp-text "| a |\n|---|"
2054 (org-element-parse-buffer) 'table-row
2055 (lambda (row) (org-element-property :type row
)))))))
2060 (ert-deftest test-org-element
/target-parser
()
2061 "Test `target' parser."
2063 (org-test-with-temp-text "<<target>>"
2064 (org-element-map (org-element-parse-buffer) 'target
'identity
))))
2069 (ert-deftest test-org-element
/timestamp
()
2070 "Test `timestamp' parser."
2071 ;; Active timestamp.
2073 (org-test-with-temp-text "<2012-03-29 16:40>"
2074 (eq (org-element-property :type
(org-element-context)) 'active
)))
2076 (org-test-with-temp-text "<2012-03-29 Thu>"
2077 (let ((timestamp (org-element-context)))
2078 (or (org-element-property :hour-start timestamp
)
2079 (org-element-property :minute-start timestamp
)))))
2081 (equal '(2012 3 29 16 40)
2082 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
2083 (let ((object (org-element-context)))
2084 (list (org-element-property :year-start object
)
2085 (org-element-property :month-start object
)
2086 (org-element-property :day-start object
)
2087 (org-element-property :hour-start object
)
2088 (org-element-property :minute-start object
))))))
2089 ;; Inactive timestamp.
2091 (org-test-with-temp-text "[2012-03-29 Thu 16:40]"
2092 (eq (org-element-property :type
(org-element-context)) 'inactive
)))
2095 (equal '(2012 3 29 16 40 7 30)
2096 (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
2097 (let ((object (org-element-context)))
2098 (list (org-element-property :year-end object
)
2099 (org-element-property :month-end object
)
2100 (org-element-property :day-end object
)
2101 (org-element-property :hour-end object
)
2102 (org-element-property :minute-end object
)
2103 (org-element-property :hour-start object
)
2104 (org-element-property :minute-start object
))))))
2107 (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
2108 (org-element-property :type
(org-element-context)))))
2111 (org-test-with-temp-text "[2012-03-29 Thu 16:40]--[2012-03-29 Thu 16:41]"
2112 (eq (org-element-property :type
(org-element-context)) 'inactive-range
)))
2114 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
2115 (let ((timestamp (org-element-context)))
2116 (or (org-element-property :hour-end timestamp
)
2117 (org-element-property :minute-end timestamp
)))))
2118 ;; With repeater, warning delay and both.
2121 (org-test-with-temp-text "<2012-03-29 Thu ++1y>"
2122 (org-element-property :repeater-type
(org-element-context)))))
2125 (org-test-with-temp-text "<2012-03-29 Thu --1y>"
2126 (org-element-property :warning-type
(org-element-context)))))
2128 (equal '(cumulate all
)
2129 (org-test-with-temp-text "<2012-03-29 Thu +1y -1y>"
2130 (let ((ts (org-element-context)))
2131 (list (org-element-property :repeater-type ts
)
2132 (org-element-property :warning-type ts
)))))))
2137 (ert-deftest test-org-element
/underline-parser
()
2138 "Test `underline' parser."
2141 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
2142 (org-test-with-temp-text "_underline_"
2143 (org-element-map (org-element-parse-buffer) 'underline
'identity
))))
2144 ;; Multi-line markup.
2147 (org-element-contents
2148 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
2149 (org-test-with-temp-text "_first line\nsecond line_"
2151 (org-element-parse-buffer) 'underline
'identity nil t
))))
2152 '("first line\nsecond line"))))
2157 (ert-deftest test-org-element
/verbatim-parser
()
2158 "Test `verbatim' parser."
2161 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
2162 (org-test-with-temp-text "=verbatim="
2163 (org-element-map (org-element-parse-buffer) 'verbatim
'identity
))))
2164 ;; Multi-line markup.
2167 (org-element-property
2169 (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
\n,\"']\\|[^
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
2170 (org-test-with-temp-text "=first line\nsecond line="
2172 (org-element-parse-buffer) 'verbatim
'identity nil t
))))
2173 "first line\nsecond line")))
2178 (ert-deftest test-org-element
/verse-block-parser
()
2179 "Test `verse-block' parser."
2182 (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
2183 (org-element-map (org-element-parse-buffer) 'verse-block
'identity
)))
2186 (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
2187 (org-element-map (org-element-parse-buffer) 'verse-block
'identity
)))
2188 ;; Parse objects in verse blocks.
2190 (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
2191 (org-element-map (org-element-parse-buffer) 'entity
'identity
)))
2192 ;; Ignore incomplete verse block.
2194 (org-test-with-temp-text "#+BEGIN_VERSE"
2195 (org-element-map (org-element-parse-buffer) 'verse-block
'identity nil t
)))
2196 ;; Handle non-empty blank line at the end of buffer.
2198 (org-test-with-temp-text "#+BEGIN_VERSE\nC\n#+END_VERSE\n "
2199 (= (org-element-property :end
(org-element-at-point)) (point-max)))))
2203 ;;; Test Interpreters.
2205 (ert-deftest test-org-element
/interpret-data
()
2206 "Test `org-element-interpret-data' specifications."
2207 ;; Interpret simple affiliated keywords.
2210 (org-element-interpret-data
2211 '(org-data nil
(paragraph (:name
"para") "Paragraph")))
2212 "#+NAME: para\nParagraph\n"))
2213 ;; Interpret multiple affiliated keywords.
2216 (org-element-interpret-data
2217 '(org-data nil
(paragraph (:attr_ascii
("line2" "line1")) "Paragraph")))
2218 "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph\n"))
2219 ;; Interpret parsed affiliated keywords.
2222 (org-element-interpret-data
2223 '(org-data nil
(paragraph (:caption
(("caption"))) "Paragraph")))
2224 "#+CAPTION: caption\nParagraph\n"))
2225 ;; Interpret dual affiliated keywords.
2228 (org-element-interpret-data
2229 '(org-data nil
(paragraph (:caption
((("long") "short"))) "Paragraph")))
2230 "#+CAPTION[short]: long\nParagraph\n"))
2231 ;; Interpret multiple parsed dual keywords.
2234 (org-element-interpret-data
2235 '(org-data nil
(paragraph
2236 (:caption
((("l2") "s2") (("l1") "s1"))) "Paragraph")))
2237 "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph\n"))
2238 ;; Pseudo objects and elements are transparent.
2241 (org-element-interpret-data
2242 '(paragraph nil
(pseudo-object (:post-blank
1) "A") "B")
2246 (org-element-interpret-data
2248 (pseudo-element (:post-blank
1) (paragraph nil
"A"))
2249 (paragraph nil
"B"))))))
2251 (ert-deftest test-org-element
/center-block-interpreter
()
2252 "Test center block interpreter."
2254 (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
2255 "#+BEGIN_CENTER\nTest\n#+END_CENTER\n")))
2257 (ert-deftest test-org-element
/drawer-interpreter
()
2258 "Test drawer interpreter."
2260 (equal (org-test-parse-and-interpret ":TEST:\nTest\n:END:")
2261 ":TEST:\nTest\n:END:\n")))
2263 (ert-deftest test-org-element
/dynamic-block-interpreter
()
2264 "Test dynamic block interpreter."
2266 (equal (org-test-parse-and-interpret
2267 "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
2268 "#+BEGIN: myblock :parameter value1\nTest\n#+END:\n")))
2270 (ert-deftest test-org-element
/footnote-definition-interpreter
()
2271 "Test footnote definition interpreter."
2272 (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n")))
2274 (ert-deftest test-org-element
/headline-interpreter
()
2275 "Test headline and section interpreters."
2276 ;; 1. Standard test.
2277 (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
2278 ;; 2. With TODO keywords.
2280 (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
2281 (org-test-parse-and-interpret "* TODO Headline"))
2282 "* TODO Headline\n"))
2285 ;; 3.1. ... and a positive `org-tags-column' value.
2287 (equal (let ((org-tags-column 20))
2288 (org-test-parse-and-interpret "* Headline :tag:"))
2289 "* Headline :tag:\n"))
2290 ;; 3.2. ... and a negative `org-tags-column' value.
2292 (equal (let ((org-tags-column -
20))
2293 (org-test-parse-and-interpret "* Headline :tag:"))
2294 "* Headline :tag:\n"))
2295 ;; 3.3. ... and a null `org-tags-column' value.
2297 (equal (let ((org-tags-column 0))
2298 (org-test-parse-and-interpret "* Headline :tag:"))
2299 "* Headline :tag:\n"))
2300 ;; 4. With priority cookie.
2302 (equal (org-test-parse-and-interpret "* [#B] Headline")
2303 "* [#B] Headline\n"))
2304 ;; 5. With comment keyword.
2306 (equal (let ((org-comment-string "COMMENT"))
2307 (org-test-parse-and-interpret "* COMMENT Headline"))
2308 "* COMMENT Headline\n"))
2309 ;; 6. Keep same number of blank lines before body.
2311 (equal (org-test-parse-and-interpret
2312 "* Headline\n\n\nText after two blank lines.")
2313 "* Headline\n\n\nText after two blank lines.\n"))
2314 ;; 8. Preserve `org-odd-levels-only' state.
2316 (equal "* H\n*** H2\n"
2317 (let ((org-odd-levels-only t
))
2318 (org-test-parse-and-interpret "* H\n*** H2")))))
2320 (ert-deftest test-org-element
/inlinetask-interpreter
()
2321 "Test inlinetask interpretation."
2322 (when (featurep 'org-inlinetask
)
2323 (let ((org-inlinetask-min-level 15))
2324 ;; 1. Regular inlinetask.
2325 (should (equal (org-test-parse-and-interpret
2326 "*************** Task\nTest\n*************** END")
2327 "*************** Task\nTest\n*************** END\n"))
2328 ;; 2. Degenerate inlinetask.
2329 (should (equal (org-test-parse-and-interpret "*************** Task")
2330 "*************** Task\n"))
2331 ;; 3. Prefer degenerate form when there are no contents.
2332 (should (equal (org-test-parse-and-interpret
2333 "*************** Task\n*************** END")
2334 "*************** Task\n"))
2335 ;; 4. With TODO keywords.
2337 (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
2338 (org-test-parse-and-interpret "*************** TODO Task"))
2339 "*************** TODO Task\n"))
2342 ;; 5.1. ... and a positive `org-tags-column' value.
2344 (equal (let ((org-tags-column 30))
2345 (org-test-parse-and-interpret "*************** Task :tag:"))
2346 "*************** Task :tag:\n"))
2347 ;; 5.2. ... and a negative `org-tags-column' value.
2349 (equal (let ((org-tags-column -
30))
2350 (org-test-parse-and-interpret "*************** Task :tag:"))
2351 "*************** Task :tag:\n"))
2352 ;; 5.3. ... and a null `org-tags-column' value.
2354 (equal (let ((org-tags-column 0))
2355 (org-test-parse-and-interpret "*************** Task :tag:"))
2356 "*************** Task :tag:\n"))
2357 ;; 6. With priority cookie.
2359 (equal (org-test-parse-and-interpret "*************** [#B] Task")
2360 "*************** [#B] Task\n")))))
2362 (ert-deftest test-org-element
/plain-list-interpreter
()
2363 "Test plain-list and item interpreters."
2364 (let ((org-list-two-spaces-after-bullet-regexp nil
))
2366 (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
2367 ;; Description list.
2369 (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
2372 (equal (let ((org-plain-list-ordered-item-terminator t
))
2373 (org-test-parse-and-interpret "1. Item"))
2376 (equal (let ((org-plain-list-ordered-item-terminator ?\
)))
2377 (org-test-parse-and-interpret "1) Item"))
2379 ;; Ordered list with counter.
2381 (equal (let ((org-plain-list-ordered-item-terminator t
))
2382 (org-test-parse-and-interpret "1. [@5] Item"))
2384 ;; List with check-boxes.
2386 (equal (org-test-parse-and-interpret
2387 "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3")
2388 "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3\n"))
2389 ;; Item not starting with a paragraph.
2391 (equal (org-test-parse-and-interpret "-\n | a | b |")
2392 "- \n | a | b |\n"))
2393 ;; Special case: correctly handle "*" bullets.
2394 (should (org-test-parse-and-interpret " * item"))
2395 ;; Special case: correctly handle empty items.
2396 (should (org-test-parse-and-interpret "-"))))
2398 (ert-deftest test-org-element
/quote-block-interpreter
()
2399 "Test quote block interpreter."
2400 (should (equal (org-test-parse-and-interpret
2401 "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
2402 "#+BEGIN_QUOTE\nTest\n#+END_QUOTE\n")))
2404 (ert-deftest test-org-element
/special-block-interpreter
()
2405 "Test special block interpreter."
2406 (should (equal (org-test-parse-and-interpret
2407 "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
2408 "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL\n")))
2410 (ert-deftest test-org-element
/babel-call-interpreter
()
2411 "Test babel call interpreter."
2412 ;; 1. Without argument.
2413 (should (equal (org-test-parse-and-interpret "#+CALL: test()")
2414 "#+CALL: test()\n"))
2415 ;; 2. With argument.
2416 (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
2417 "#+CALL: test(x=2)\n"))
2418 ;; 3. With header arguments.
2419 (should (equal (org-test-parse-and-interpret
2420 "#+CALL: test[:results output]()[:results html]")
2421 "#+CALL: test[:results output]()[:results html]\n")))
2423 (ert-deftest test-org-element
/clock-interpreter
()
2424 "Test clock interpreter."
2428 "CLOCK: \\[2012-01-01 .* 00:01\\]"
2429 (org-test-parse-and-interpret "CLOCK: [2012-01-01 sun. 00:01]")))
2433 "CLOCK: \\[2012-01-01 .* 00:01\\]--\\[2012-01-01 .* 00:02\\] => 0:01"
2434 (org-test-parse-and-interpret "
2435 CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))))
2437 (ert-deftest test-org-element
/comment-interpreter
()
2438 "Test comment interpreter."
2440 (should (equal (org-test-parse-and-interpret "# Comment") "# Comment\n"))
2442 (should (equal (org-test-parse-and-interpret " # Comment")
2444 ;; Preserve indentation.
2445 (should (equal (org-test-parse-and-interpret " # No blank\n# One blank")
2446 "# No blank\n# One blank\n")))
2448 (ert-deftest test-org-element
/comment-block-interpreter
()
2449 "Test comment block interpreter."
2450 (should (equal (org-test-parse-and-interpret
2451 "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
2452 "#+BEGIN_COMMENT\nTest\n#+END_COMMENT\n"))
2453 ;; Accept missing final newline in value.
2456 "#+BEGIN_COMMENT\nTest\n#+END_COMMENT\n"
2457 (org-element-interpret-data '(comment-block (:value
"Test"))))))
2459 (ert-deftest test-org-element
/diary-sexp
()
2460 "Test diary-sexp interpreter."
2463 (org-test-parse-and-interpret
2464 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old")
2465 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old\n")))
2467 (ert-deftest test-org-element
/example-block-interpreter
()
2468 "Test example block interpreter."
2469 ;; Without switches.
2470 (should (equal (org-test-parse-and-interpret
2471 "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE")
2472 "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE\n"))
2475 (equal (org-test-parse-and-interpret
2476 "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE")
2477 "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE\n"))
2478 ;; Preserve code escaping.
2480 (equal (org-test-parse-and-interpret
2481 "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE")
2482 "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE\n"))
2483 ;; Accept missing final newline in value.
2486 "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE\n"
2487 (org-element-interpret-data '(example-block (:value
"Test"))))))
2489 (ert-deftest test-org-element
/export-block-interpreter
()
2490 "Test export block interpreter."
2491 (should (equal (org-test-parse-and-interpret
2492 "#+BEGIN_HTML\nTest\n#+END_HTML")
2493 "#+BEGIN_HTML\nTest\n#+END_HTML\n")))
2495 (ert-deftest test-org-element
/fixed-width-interpreter
()
2496 "Test fixed width interpreter."
2498 (should (equal (org-test-parse-and-interpret ": Test") ": Test\n"))
2499 ;; Preserve indentation.
2500 (should (equal (org-test-parse-and-interpret ": 2 blanks\n: 1 blank")
2501 ": 2 blanks\n: 1 blank\n"))
2502 ;; Remove last newline character
2504 (equal (org-element-fixed-width-interpreter
2505 '(fixed-width (:value
"Test\n")) nil
)
2508 (equal (org-element-fixed-width-interpreter
2509 '(fixed-width (:value
"Test")) nil
)
2511 ;; Handle empty string.
2513 (equal (org-element-fixed-width-interpreter
2514 '(fixed-width (:value
"")) nil
)
2516 ;; Handle nil value.
2518 (org-element-fixed-width-interpreter
2519 '(fixed-width (:value nil
)) nil
)))
2521 (ert-deftest test-org-element
/horizontal-rule-interpreter
()
2522 "Test horizontal rule interpreter."
2523 (should (equal (org-test-parse-and-interpret "-------") "-----\n")))
2525 (ert-deftest test-org-element
/keyword-interpreter
()
2526 "Test keyword interpreter."
2527 (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
2528 "#+KEYWORD: value\n")))
2530 (ert-deftest test-org-element
/latex-environment-interpreter
()
2531 "Test latex environment interpreter."
2532 (should (equal (org-test-parse-and-interpret
2533 "\\begin{equation}\n1+1=2\n\\end{equation}")
2534 "\\begin{equation}\n1+1=2\n\\end{equation}\n"))
2535 (should (equal (org-test-parse-and-interpret
2536 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}")
2537 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}\n")))
2539 (ert-deftest test-org-element
/planning-interpreter
()
2540 "Test planning interpreter."
2544 DEADLINE: <2012-03-29 .*?> SCHEDULED: <2012-03-29 .*?> CLOSED: \\[2012-03-29 .*?\\]"
2545 (org-test-parse-and-interpret
2547 DEADLINE: <2012-03-29 thu.> SCHEDULED: <2012-03-29 thu.> CLOSED: [2012-03-29 thu.]"))))
2549 (ert-deftest test-org-element
/property-drawer-interpreter
()
2550 "Test property drawer interpreter."
2551 (should (equal (let ((org-property-format "%-10s %s"))
2552 (org-test-parse-and-interpret
2553 ":PROPERTIES:\n:prop: value\n:END:"))
2554 ":PROPERTIES:\n:prop: value\n:END:\n")))
2556 (ert-deftest test-org-element
/src-block-interpreter
()
2557 "Test src block interpreter."
2560 (equal (let ((org-edit-src-content-indentation 2))
2561 (org-test-parse-and-interpret
2562 "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC"))
2563 "#+BEGIN_SRC emacs-lisp :results silent\n (+ 1 1)\n#+END_SRC\n"))
2566 (equal (let ((org-edit-src-content-indentation 2))
2567 (org-test-parse-and-interpret
2568 "#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
2569 "#+BEGIN_SRC emacs-lisp -n -k\n (+ 1 1)\n#+END_SRC\n"))
2570 ;; Preserve code escaping.
2572 (equal (let ((org-edit-src-content-indentation 2))
2573 (org-test-parse-and-interpret
2574 "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC"))
2575 "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC\n"))
2576 ;; Do not apply `org-edit-src-content-indentation' when preserving
2579 (equal (let ((org-edit-src-content-indentation 2)
2580 (org-src-preserve-indentation t
))
2581 (org-test-parse-and-interpret
2582 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"))
2583 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n"))
2585 (equal (let ((org-edit-src-content-indentation 2)
2586 (org-src-preserve-indentation nil
))
2587 (org-test-parse-and-interpret
2588 "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"))
2589 "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC\n"))
2590 ;; Accept missing final newline in value.
2593 "#+BEGIN_SRC emacs-lisp\n Test\n#+END_SRC\n"
2594 (let ((org-edit-src-content-indentation 2))
2595 (org-element-interpret-data
2596 '(src-block (:language
"emacs-lisp" :value
"Test")))))))
2598 (ert-deftest test-org-element
/table-interpreter
()
2599 "Test table, table-row and table-cell interpreters."
2601 (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
2602 "| a | b |\n| c | d |\n"))
2603 ;; 2. With horizontal rules.
2604 (should (equal (org-test-parse-and-interpret
2605 "| a | b |\n|---+---|\n| c | d |")
2606 "| a | b |\n|---+---|\n| c | d |\n"))
2607 ;; 3. With meta-data.
2608 (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
2609 "| / | < | > |\n| * | 1 | 2 |\n"))
2610 ;; 4. With a formula.
2612 (equal (org-test-parse-and-interpret
2613 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)")
2614 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)\n"))
2615 ;; 5. With multiple formulas.
2617 (equal (org-test-parse-and-interpret
2618 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2")
2619 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2\n")))
2621 (ert-deftest test-org-element
/timestamp-interpreter
()
2622 "Test timestamp interpreter."
2625 (string-match "<2012-03-29 .* 16:40>"
2626 (org-test-parse-and-interpret "<2012-03-29 thu. 16:40>")))
2628 (string-match "<2012-03-29 .* 16:40>"
2629 (org-element-timestamp-interpreter
2631 (:type active
:year-start
2012 :month-start
3 :day-start
29
2632 :hour-start
16 :minute-start
40)) nil
)))
2635 (string-match "\\[2012-03-29 .* 16:40\\]"
2636 (org-test-parse-and-interpret "[2012-03-29 thu. 16:40]")))
2639 "\\[2012-03-29 .* 16:40\\]"
2640 (org-element-timestamp-interpreter
2642 (:type inactive
:year-start
2012 :month-start
3 :day-start
29
2643 :hour-start
16 :minute-start
40)) nil
)))
2646 (string-match "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
2647 (org-test-parse-and-interpret
2648 "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>")))
2651 "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
2652 (org-element-timestamp-interpreter
2654 (:type active-range
:year-start
2012 :month-start
3 :day-start
29
2655 :hour-start
16 :minute-start
40 :year-end
2012 :month-end
3
2656 :day-end
29 :hour-end
16 :minute-end
41)) nil
)))
2659 (string-match "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
2660 (org-test-parse-and-interpret
2661 "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]")))
2664 "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
2665 (org-element-timestamp-interpreter
2667 (:type inactive-range
:year-start
2012 :month-start
3 :day-start
29
2668 :hour-start
16 :minute-start
40 :year-end
2012 :month-end
3
2669 :day-end
29 :hour-end
16 :minute-end
41)) nil
)))
2671 (should (equal (org-test-parse-and-interpret "<%%diary-float t 4 2>")
2672 "<%%diary-float t 4 2>\n"))
2673 ;; Timestamp with repeater interval, with delay, with both.
2675 (string-match "<2012-03-29 .* \\+1y>"
2676 (org-test-parse-and-interpret "<2012-03-29 thu. +1y>")))
2679 "<2012-03-29 .* \\+1y>"
2680 (org-element-timestamp-interpreter
2682 (:type active
:year-start
2012 :month-start
3 :day-start
29
2683 :repeater-type cumulate
:repeater-value
1 :repeater-unit year
))
2687 "<2012-03-29 .* -1y>"
2688 (org-element-timestamp-interpreter
2690 (:type active
:year-start
2012 :month-start
3 :day-start
29
2691 :warning-type all
:warning-value
1 :warning-unit year
))
2695 "<2012-03-29 .* \\+1y -1y>"
2696 (org-element-timestamp-interpreter
2698 (:type active
:year-start
2012 :month-start
3 :day-start
29
2699 :warning-type all
:warning-value
1 :warning-unit year
2700 :repeater-type cumulate
:repeater-value
1 :repeater-unit year
))
2702 ;; Timestamp range with repeater interval
2704 (string-match "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
2705 (org-test-parse-and-interpret
2706 "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>")))
2709 "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
2710 (org-element-timestamp-interpreter
2712 (:type active-range
:year-start
2012 :month-start
3 :day-start
29
2713 :year-end
2012 :month-end
3 :day-end
30 :repeater-type cumulate
2714 :repeater-value
1 :repeater-unit year
))
2717 (ert-deftest test-org-element
/verse-block-interpreter
()
2718 "Test verse block interpretation."
2720 (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
2721 "#+BEGIN_VERSE\nTest\n#+END_VERSE\n")))
2723 (ert-deftest test-org-element
/bold-interpreter
()
2724 "Test bold interpreter."
2725 (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))
2727 (ert-deftest test-org-element
/code-interpreter
()
2728 "Test code interpreter."
2729 (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))
2731 (ert-deftest test-org-element
/entity-interpreter
()
2732 "Test entity interpreter."
2733 ;; 1. Without brackets.
2735 (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
2736 ;; 2. With brackets.
2738 (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))
2740 (ert-deftest test-org-element
/export-snippet-interpreter
()
2741 "Test export snippet interpreter."
2742 (should (equal (org-test-parse-and-interpret "@@back-end:contents@@")
2743 "@@back-end:contents@@\n")))
2745 (ert-deftest test-org-element
/footnote-reference-interpreter
()
2746 "Test footnote reference interpreter."
2747 ;; 1. Regular reference.
2748 (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
2749 ;; 2. Normalized reference.
2750 (should (equal (org-test-parse-and-interpret "Text[1]") "Text[1]\n"))
2751 ;; 3. Named reference.
2752 (should (equal (org-test-parse-and-interpret "Text[fn:label]")
2753 "Text[fn:label]\n"))
2754 ;; 4. Inline reference.
2755 (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
2756 "Text[fn:label:def]\n"))
2757 ;; 5. Anonymous reference.
2758 (should (equal (org-test-parse-and-interpret "Text[fn::def]")
2759 "Text[fn::def]\n")))
2761 (ert-deftest test-org-element
/inline-babel-call-interpreter
()
2762 "Test inline babel call interpreter."
2763 ;; 1. Without arguments.
2764 (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
2765 ;; 2. With arguments.
2766 (should (equal (org-test-parse-and-interpret "call_test(x=2)")
2767 "call_test(x=2)\n"))
2768 ;; 3. With header arguments.
2769 (should (equal (org-test-parse-and-interpret
2770 "call_test[:results output]()[:results html]")
2771 "call_test[:results output]()[:results html]\n")))
2773 (ert-deftest test-org-element
/inline-src-block-interpreter
()
2774 "Test inline src block interpreter."
2775 ;; 1. Without header argument.
2776 (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
2777 "src_emacs-lisp{(+ 1 1)}\n"))
2778 ;; 2. With header arguments.
2779 (should (equal (org-test-parse-and-interpret
2780 "src_emacs-lisp[:results silent]{(+ 1 1)}")
2781 "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))
2783 (ert-deftest test-org-element
/italic-interpreter
()
2784 "Test italic interpreter."
2785 (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))
2787 (ert-deftest test-org-element
/latex-fragment-interpreter
()
2788 "Test latex fragment interpreter."
2789 (should (equal (org-test-parse-and-interpret "\\command{}") "\\command{}\n"))
2790 (should (equal (org-test-parse-and-interpret "$x$") "$x$\n"))
2791 (should (equal (org-test-parse-and-interpret "$x+y$") "$x+y$\n"))
2792 (should (equal (org-test-parse-and-interpret "$$x+y$$") "$$x+y$$\n"))
2793 (should (equal (org-test-parse-and-interpret "\\(x+y\\)") "\\(x+y\\)\n"))
2794 (should (equal (org-test-parse-and-interpret "\\[x+y\\]") "\\[x+y\\]\n")))
2796 (ert-deftest test-org-element
/line-break-interpreter
()
2797 "Test line break interpreter."
2798 (should (equal (org-test-parse-and-interpret "First line \\\\ \nSecond line")
2799 "First line \\\\\nSecond line\n")))
2801 (ert-deftest test-org-element
/link-interpreter
()
2802 "Test link interpreter."
2803 ;; 1. Links targeted from a radio target.
2804 (should (equal (let ((org-target-link-regexp "radio-target"))
2805 (org-test-parse-and-interpret "a radio-target"))
2806 "a radio-target\n"))
2807 ;; 2. Regular links.
2809 ;; 2.1. Without description.
2810 (should (equal (org-test-parse-and-interpret "[[http://orgmode.org]]")
2811 "[[http://orgmode.org]]\n"))
2812 ;; 2.2. With a description.
2813 (should (equal (org-test-parse-and-interpret
2814 "[[http://orgmode.org][Org mode]]")
2815 "[[http://orgmode.org][Org mode]]\n"))
2817 (should (equal (org-test-parse-and-interpret "[[id:aaaa]]") "[[id:aaaa]]\n"))
2818 ;; 2.4. Custom-id links.
2819 (should (equal (org-test-parse-and-interpret "[[#id]]") "[[#id]]\n"))
2820 ;; 2.5 Code-ref links.
2821 (should (equal (org-test-parse-and-interpret "[[(ref)]]") "[[(ref)]]\n"))
2822 ;; 3. Normalize plain links.
2823 (should (equal (org-test-parse-and-interpret "http://orgmode.org")
2824 "[[http://orgmode.org]]\n"))
2825 ;; 4. Normalize angular links.
2826 (should (equal (org-test-parse-and-interpret "<http://orgmode.org>")
2827 "[[http://orgmode.org]]\n")))
2829 (ert-deftest test-org-element
/macro-interpreter
()
2830 "Test macro interpreter."
2831 ;; 1. Without argument.
2832 (should (equal (org-test-parse-and-interpret "{{{test}}}") "{{{test}}}\n"))
2833 ;; 2. With arguments.
2834 (should (equal (org-test-parse-and-interpret "{{{test(arg1,arg2)}}}")
2835 "{{{test(arg1,arg2)}}}\n")))
2837 (ert-deftest test-org-element
/radio-target-interpreter
()
2838 "Test radio target interpreter."
2839 (should (equal (org-test-parse-and-interpret "<<<some text>>>")
2840 "<<<some text>>>\n")))
2842 (ert-deftest test-org-element
/statistics-cookie-interpreter
()
2843 "Test statistics cookie interpreter."
2844 ;; 1. Without percent
2845 (should (equal (org-test-parse-and-interpret "[0/1]") "[0/1]\n"))
2847 (should (equal (org-test-parse-and-interpret "[66%]") "[66%]\n")))
2849 (ert-deftest test-org-element
/strike-through-interpreter
()
2850 "Test strike through interpreter."
2851 (should (equal (org-test-parse-and-interpret "+target+") "+target+\n")))
2853 (ert-deftest test-org-element
/subscript-interpreter
()
2854 "Test subscript interpreter."
2855 ;; 1. Without brackets.
2856 (should (equal (org-test-parse-and-interpret "a_b") "a_b\n"))
2857 ;; 2. With brackets.
2858 (should (equal (org-test-parse-and-interpret "a_{b}") "a_{b}\n")))
2860 (ert-deftest test-org-element
/superscript-interpreter
()
2861 "Test superscript interpreter."
2862 ;; 1. Without brackets.
2863 (should (equal (org-test-parse-and-interpret "a^b") "a^b\n"))
2864 ;; 2. With brackets.
2865 (should (equal (org-test-parse-and-interpret "a^{b}") "a^{b}\n")))
2867 (ert-deftest test-org-element
/target-interpreter
()
2868 "Test target interpreter."
2869 (should (equal (org-test-parse-and-interpret "<<target>>") "<<target>>\n")))
2871 (ert-deftest test-org-element
/underline-interpreter
()
2872 "Test underline interpreter."
2873 (should (equal (org-test-parse-and-interpret "_text_") "_text_\n")))
2875 (ert-deftest test-org-element
/verbatim-interpreter
()
2876 "Test verbatim interpreter."
2877 (should (equal (org-test-parse-and-interpret "=text=") "=text=\n")))
2881 ;;; Test Granularity
2883 (ert-deftest test-org-element
/granularity
()
2884 "Test granularity impact on buffer parsing."
2885 (org-test-with-temp-text "
2892 ;; 1.1. Granularity set to `headline' should parse every headline
2893 ;; in buffer, and only them.
2894 (let ((tree (org-element-parse-buffer 'headline
)))
2895 (should (= 2 (length (org-element-map tree
'headline
'identity
))))
2896 (should-not (org-element-map tree
'paragraph
'identity
)))
2897 ;; 1.2. Granularity set to `greater-element' should not enter
2898 ;; greater elements excepted headlines and sections.
2899 (let ((tree (org-element-parse-buffer 'greater-element
)))
2900 (should (= 1 (length (org-element-map tree
'center-block
'identity
))))
2901 (should (= 1 (length (org-element-map tree
'paragraph
'identity
))))
2902 (should-not (org-element-map tree
'entity
'identity
)))
2903 ;; 1.3. Granularity set to `element' should enter every
2905 (let ((tree (org-element-parse-buffer 'element
)))
2906 (should (= 2 (length (org-element-map tree
'paragraph
'identity
))))
2907 (should-not (org-element-map tree
'entity
'identity
)))
2908 ;; 1.4. Granularity set to `object' can see everything.
2909 (let ((tree (org-element-parse-buffer 'object
)))
2910 (should (= 1 (length (org-element-map tree
'entity
'identity
)))))))
2912 (ert-deftest test-org-element
/secondary-string-parsing
()
2913 "Test if granularity correctly toggles secondary strings parsing."
2914 ;; With a granularity bigger than `object', no secondary string
2915 ;; should be parsed.
2918 (org-test-with-temp-text "* Headline"
2920 (org-element-map (org-element-parse-buffer 'headline
) 'headline
2921 #'identity nil
'first-match
)))
2922 (org-element-property :title headline
)))))
2925 (org-test-with-temp-text "* Headline\n- tag :: item"
2926 (let ((item (org-element-map (org-element-parse-buffer 'element
) 'item
2927 #'identity nil
'first-match
)))
2928 (org-element-property :tag item
)))))
2929 (when (featurep 'org-inlinetask
)
2932 (let ((org-inlinetask-min-level 15))
2933 (org-test-with-temp-text "*************** Inlinetask"
2934 (let ((inlinetask (org-element-map (org-element-parse-buffer 'element
)
2936 #'identity nil
'first-match
)))
2937 (org-element-property :title inlinetask
)))))))
2938 ;; With a default granularity, secondary strings should be parsed.
2941 (org-test-with-temp-text "* Headline"
2943 (org-element-map (org-element-parse-buffer) 'headline
2944 #'identity nil
'first-match
)))
2945 (org-element-property :title headline
)))))
2946 ;; `org-element-at-point' should never parse a secondary string.
2949 (org-test-with-temp-text "* Headline"
2950 (org-element-property :title
(org-element-at-point)))))
2951 ;; Preserve current local variables when parsing a secondary string.
2953 (let ((org-entities nil
)
2954 (org-entities-user nil
))
2955 (org-test-with-temp-text "
2959 # org-entities-user: ((\"foo\"))
2961 (let ((safe-local-variable-values '((org-entities-user .
(("foo"))))))
2962 (hack-local-variables))
2963 (org-element-map (org-element-parse-buffer) 'entity
2964 #'identity nil nil nil t
)))))
2968 ;;; Test Visible Only Parsing
2970 (ert-deftest test-org-element
/parse-buffer-visible
()
2971 "Test `org-element-parse-buffer' with visible only argument."
2973 (equal '("H1" "H3" "H5")
2974 (org-test-with-temp-text
2975 "* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:"
2976 (org-occur ":visible:")
2977 (org-element-map (org-element-parse-buffer nil t
) 'headline
2978 (lambda (hl) (org-element-property :raw-value hl
)))))))
2982 ;;; Test `:parent' Property
2984 (ert-deftest test-org-element
/parent-property
()
2985 "Test `:parent' property."
2987 (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
2988 (let* ((tree (org-element-parse-buffer))
2989 (parent (org-element-property
2991 (org-element-map tree
'paragraph
'identity nil t
))))
2993 (should (eq (org-element-map tree
'center-block
'identity nil t
)
2996 (org-test-with-temp-text "a_{/b/}"
2997 (let* ((tree (org-element-parse-buffer))
2998 (parent (org-element-property
3000 (org-element-map tree
'italic
'identity nil t
))))
3003 (org-element-map tree
'subscript
'identity nil t
)))))
3004 ;; Secondary strings
3005 (org-test-with-temp-text "* /italic/"
3006 (let* ((tree (org-element-parse-buffer))
3007 (parent (org-element-property
3008 :parent
(org-element-map tree
'italic
'identity nil t
))))
3011 (org-element-map tree
'headline
'identity nil t
))))))
3015 ;;; Test Normalize Contents
3017 (ert-deftest test-org-element
/normalize-contents
()
3018 "Test `org-element-normalize-contents' specifications."
3019 ;; Remove maximum common indentation from element's contents.
3022 (org-element-normalize-contents
3023 '(paragraph nil
" Two spaces\n Three spaces"))
3024 '(paragraph nil
"Two spaces\n Three spaces")))
3025 ;; Ignore objects within contents when computing maximum common
3029 (org-element-normalize-contents
3030 '(paragraph nil
" One " (emphasis nil
"space") "\n Two spaces"))
3031 '(paragraph nil
"One " (emphasis nil
"space") "\n Two spaces")))
3032 ;; Ignore blank lines.
3035 (org-element-normalize-contents
3036 '(paragraph nil
" Two spaces\n\n \n Two spaces"))
3037 '(paragraph nil
"Two spaces\n\n \nTwo spaces")))
3040 '(paragraph nil
" Two spaces\n" (verbatim nil
"V") "\n Two spaces")
3041 (org-element-normalize-contents
3042 '(paragraph nil
" Two spaces\n " (verbatim nil
"V") "\n Two spaces"))))
3045 '(verse-block nil
"line 1\n\nline 2")
3046 (org-element-normalize-contents
3047 '(verse-block nil
" line 1\n\n line 2"))))
3048 ;; Recursively enter objects in order to compute common indentation.
3051 (org-element-normalize-contents
3052 '(paragraph nil
" Two spaces " (bold nil
" and\n One space")))
3053 '(paragraph nil
" Two spaces " (bold nil
" and\nOne space"))))
3054 ;; When optional argument is provided, ignore first line
3058 (org-element-normalize-contents
3059 '(paragraph nil
"No space\n Two spaces\n Three spaces") t
)
3060 '(paragraph nil
"No space\nTwo spaces\n Three spaces"))))
3064 ;;; Test Navigation Tools.
3066 (ert-deftest test-org-element
/at-point
()
3067 "Test `org-element-at-point' specifications."
3068 ;; Return closest element containing point.
3071 (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
3072 (progn (search-forward "A")
3073 (org-element-type (org-element-at-point))))))
3074 ;; Correctly set `:parent' property.
3077 (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
3078 (progn (search-forward "A")
3080 (org-element-property :parent
(org-element-at-point)))))))
3081 ;; Special case: at a blank line just below a headline, return that
3084 (equal "H1" (org-test-with-temp-text "* H1\n \n* H2\n"
3086 (org-element-property :title
(org-element-at-point)))))
3087 ;; Special case: at the very beginning of a table, return `table'
3088 ;; object instead of `table-row'.
3091 (org-test-with-temp-text "| a | b |"
3092 (org-element-type (org-element-at-point)))))
3093 ;; Special case: at the very beginning of a list or sub-list, return
3094 ;; `plain-list' object instead of `item'.
3097 (org-test-with-temp-text "- item"
3098 (org-element-type (org-element-at-point)))))
3099 ;; Special case: at the closing line of a greater element, be sure
3100 ;; to return it instead of the last element in its contents.
3103 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph\n#+END_CENTER"
3104 (progn (forward-line 2)
3105 (org-element-type (org-element-at-point))))))
3106 ;; Special case: at a blank line between two items, be sure to
3107 ;; return item above instead of the last element of its contents.
3110 (org-test-with-temp-text "- Para1\n\n- Para2"
3111 (progn (forward-line)
3113 (let ((org-list-empty-line-terminates-plain-lists nil
))
3114 (org-element-at-point)))))))
3115 ;; Special case: at the last blank line in a plain list, return it
3116 ;; instead of the last item.
3119 (org-test-with-temp-text "- Para1\n- Para2\n\nPara3"
3120 (progn (forward-line 2)
3121 (org-element-type (org-element-at-point))))))
3122 ;; Special case: when a list ends at the end of buffer and there's
3123 ;; no final newline, return last element in last item.
3126 (org-test-with-temp-text "- a"
3128 (org-element-type (org-element-at-point)))))
3129 ;; Parse a list within a block itself contained in a list.
3132 (org-test-with-temp-text
3133 "- outer\n #+begin_center\n - inner\n #+end_center"
3134 (search-forward "inner")
3136 (org-element-type (org-element-at-point)))))
3137 ;; Do not error at eob on an empty line.
3139 (org-test-with-temp-text "* H\n"
3141 (or (org-element-at-point) t
))))
3143 (ert-deftest test-org-element
/context
()
3144 "Test `org-element-context' specifications."
3145 ;; Return closest object containing point.
3148 (org-test-with-temp-text "Some *text with _underline_ text*"
3149 (progn (search-forward "under")
3150 (org-element-type (org-element-context))))))
3151 ;; Find objects in secondary strings.
3154 (org-test-with-temp-text "* Headline _with_ underlining"
3155 (progn (search-forward "w")
3156 (org-element-type (org-element-context))))))
3157 ;; Find objects in objects.
3160 (org-test-with-temp-text "| a | {{{macro}}} |"
3161 (progn (search-forward "{")
3162 (org-element-type (org-element-context))))))
3165 (org-test-with-temp-text "| a | b {{{macro}}} |"
3166 (progn (search-forward "b")
3167 (org-element-type (org-element-context))))))
3168 ;; Find objects in planning lines.
3171 (org-test-with-temp-text "* H\n SCHEDULED: <2012-03-29 thu.>"
3172 (search-forward "2012")
3173 (org-element-type (org-element-context)))))
3176 (org-test-with-temp-text "* H\n SCHEDULED: <2012-03-29 thu.>"
3177 (search-forward "SCHEDULED")
3178 (org-element-type (org-element-context)))))
3179 ;; Find objects in document keywords.
3182 (org-test-with-temp-text "#+DATE: {{{macro}}}"
3183 (progn (search-forward "{")
3184 (org-element-type (org-element-context))))))
3185 ;; Do not find objects in table rules.
3188 (org-test-with-temp-text "| a | b |\n+---+---+\n| c | d |"
3190 (org-element-type (org-element-context)))))
3191 ;; Find objects in parsed affiliated keywords.
3194 (org-test-with-temp-text "#+CAPTION: {{{macro}}}\n| a | b |."
3195 (progn (search-forward "{")
3196 (org-element-type (org-element-context))))))
3199 (org-test-with-temp-text "#+caption: *bold*\nParagraph"
3200 (progn (search-forward "*")
3201 (org-element-type (org-element-context))))))
3202 ;; Find objects at the end of buffer.
3205 (org-test-with-temp-text "*bold*"
3206 (goto-char (point-max))
3207 (org-element-type (org-element-context)))))
3208 ;; Correctly set `:parent' property.
3211 (org-test-with-temp-text "Some *bold* text"
3212 (progn (search-forward "bold")
3214 (org-element-property :parent
(org-element-context)))))))
3215 ;; Between two objects, return the second one.
3218 (org-test-with-temp-text "<<target>>{{{test}}}"
3219 (progn (search-forward "{")
3221 (org-element-type (org-element-context))))))
3222 ;; Test optional argument.
3225 (org-test-with-temp-text "Some *text with _underline_ text*"
3227 (search-forward "under")
3228 (org-element-type (org-element-context (org-element-at-point)))))))
3229 ;; Special case: bold object at the beginning of a headline.
3232 (org-test-with-temp-text "* *bold*"
3233 (search-forward "bo")
3234 (org-element-type (org-element-context)))))
3235 ;; Special case: incomplete cell at the end of a table row.
3238 (org-test-with-temp-text "|a|b|c"
3239 (goto-char (point-max))
3240 (org-element-type (org-element-context)))))
3241 ;; Special case: objects in inline footnotes.
3244 (org-test-with-temp-text "[fn::[[<point>http://orgmode.org]]]"
3245 (org-element-type (org-element-context))))))
3251 (ert-deftest test-org-element
/cache
()
3252 "Test basic expectations and common pitfalls for cache."
3256 (org-test-with-temp-text "para1\n\npara2\n\npara3"
3257 (let ((org-element-use-cache t
))
3258 (save-excursion (goto-char (point-max)) (org-element-at-point))
3261 (let ((element (org-element-at-point)))
3262 (cons (org-element-property :begin element
)
3263 (org-element-property :end element
)))))))
3264 ;; Partial shifting: when the contents of a greater element are
3265 ;; modified, only shift ending positions.
3267 (org-test-with-temp-text
3268 "#+BEGIN_CENTER\nPara1\n\nPara2\n\nPara3\n#+END_CENTER"
3269 (let ((org-element-use-cache t
))
3270 (save-excursion (search-forward "3") (org-element-at-point))
3271 (search-forward "Para2")
3273 (let ((element (org-element-property :parent
(org-element-at-point))))
3274 (equal (cons (org-element-property :begin element
)
3275 (org-element-property :end element
))
3276 (cons (point-min) (point-max)))))))
3277 ;; Re-parent shifted elements.
3280 (org-test-with-temp-text "- item\n\n\n para1\n para2"
3281 (let ((org-element-use-cache t
))
3283 (org-element-at-point)
3284 (save-excursion (goto-char (point-max)) (org-element-at-point))
3287 (goto-char (point-max))
3289 (org-element-property :parent
(org-element-at-point)))))))
3290 ;; Preserve local structures when re-parenting.
3293 (org-test-with-temp-text
3294 "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n| c | d |\n#+end_center"
3295 (let ((org-element-use-cache t
))
3296 (save-excursion (search-forward "| c |") (org-element-at-point))
3298 (search-forward "| c |")
3301 (org-element-property :parent
(org-element-at-point)))))))
3304 (org-test-with-temp-text
3305 "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n#+end_center"
3306 (let ((org-element-use-cache t
))
3307 (save-excursion (search-forward "| a |") (org-element-at-point))
3309 (search-forward "| a |")
3312 (org-element-property :parent
(org-element-at-point)))))))
3313 ;; When re-parenting, also propagate changes to list structures.
3316 (org-test-with-temp-text "\n Para\n - item<point>"
3317 (let ((org-element-use-cache t
))
3318 (org-element-at-point)
3319 (goto-char (point-min))
3321 (search-forward "- item")
3323 (length (org-element-property :structure
(org-element-at-point)))))))
3324 ;; Modifying the last line of an element alters the element below.
3326 (org-test-with-temp-text "para1\n\npara2"
3327 (let ((org-element-use-cache t
))
3328 (goto-char (point-max))
3329 (org-element-at-point)
3332 (let ((element (org-element-at-point)))
3333 (equal (cons (org-element-property :begin element
)
3334 (org-element-property :end element
))
3335 (cons (point-min) (point-max)))))))
3336 ;; Modifying the first line of an element alters the element above.
3338 (org-test-with-temp-text ": fixed-width\n:not-fixed-width"
3339 (let ((org-element-use-cache t
))
3340 (goto-char (point-max))
3341 (org-element-at-point)
3342 (search-backward ":")
3345 (let ((element (org-element-at-point)))
3346 (equal (cons (org-element-property :begin element
)
3347 (org-element-property :end element
))
3348 (cons (point-min) (point-max)))))))
3349 ;; Sensitive change: adding a line alters document structure both
3353 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n"
3354 (let ((org-element-use-cache t
))
3355 (goto-char (point-max))
3356 (org-element-at-point)
3357 (insert "#+END_EXAMPLE")
3358 (search-backward "Para1")
3359 (org-element-type (org-element-at-point))))))
3362 (org-test-with-temp-text "Para1\n\nPara2\n#+END_EXAMPLE"
3363 (let ((org-element-use-cache t
))
3364 (save-excursion (goto-char (point-max)) (org-element-at-point))
3365 (insert "#+BEGIN_EXAMPLE\n")
3366 (search-forward "Para2")
3367 (org-element-type (org-element-at-point))))))
3368 ;; Sensitive change: removing a line alters document structure both
3372 (org-test-with-temp-text
3373 "# +BEGIN_EXAMPLE\nPara1\n\nPara2\n#+END_EXAMPLE"
3374 (let ((org-element-use-cache t
))
3375 (save-excursion (goto-char (point-max)) (org-element-at-point))
3378 (search-forward "Para2")
3379 (org-element-type (org-element-at-point))))))
3382 (org-test-with-temp-text
3383 "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n# +END_EXAMPLE"
3384 (let ((org-element-use-cache t
))
3385 (save-excursion (goto-char (point-max)) (org-element-at-point))
3386 (search-forward "# ")
3388 (search-backward "Para1")
3389 (org-element-type (org-element-at-point)))))))
3392 (provide 'test-org-element
)
3394 ;;; test-org-element.el ends here