Merge branch 'maint'
[org-mode/org-tableheadings.git] / testing / lisp / test-org-element.el
blobeba39d31d20022ff933bbece1c0462296600833f
1 ;;; test-org-element.el --- Tests for org-element.el
3 ;; Copyright (C) 2012-2015 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Code:
22 (unless (featurep 'org-element)
23 (signal 'missing-test-dependency "org-element"))
25 (defun org-test-parse-and-interpret (text)
26 "Parse TEXT as Org syntax and interpret it.
27 Return interpreted string."
28 (with-temp-buffer
29 (org-mode)
30 (insert text)
31 (org-element-interpret-data (org-element-parse-buffer))))
35 ;;; Test `org-element-map'
37 (ert-deftest test-org-element/map ()
38 "Test `org-element-map'."
39 ;; Can map to `plain-text' objects.
40 (should
41 (= 2
42 (org-test-with-temp-text "Some text \alpha
43 #+BEGIN_CENTER
44 Some other text
45 #+END_CENTER"
46 (let ((count 0))
47 (org-element-map
48 (org-element-parse-buffer) 'plain-text
49 (lambda (s) (when (string-match "text" s) (incf count))))
50 count))))
51 ;; Applies to secondary strings
52 (should
53 (org-element-map '("some " (bold nil "bold") "text") 'bold 'identity))
54 ;; Enter secondary strings before entering contents.
55 (should
56 (equal
57 "alpha"
58 (org-element-property
59 :name
60 (org-test-with-temp-text "* Some \\alpha headline\n\\beta entity."
61 (org-element-map (org-element-parse-buffer) 'entity 'identity nil t)))))
62 ;; Apply NO-RECURSION argument.
63 (should-not
64 (org-test-with-temp-text "#+BEGIN_CENTER\n\\alpha\n#+END_CENTER"
65 (org-element-map
66 (org-element-parse-buffer) 'entity 'identity nil nil 'center-block)))
67 ;; Use WITH-AFFILIATED argument.
68 (should
69 (equal
70 '("a" "1" "b" "2")
71 (org-test-with-temp-text "#+CAPTION[a]: 1\n#+CAPTION[b]: 2\nParagraph"
72 (org-element-map
73 (org-element-at-point) 'plain-text 'identity nil nil nil t)))))
77 ;;; Test Setters
79 (ert-deftest test-org-element/put-property ()
80 "Test `org-element-put-property' specifications."
81 ;; Standard test.
82 (org-test-with-temp-text "* Headline\n *a*"
83 (let ((tree (org-element-parse-buffer)))
84 (org-element-put-property
85 (org-element-map tree 'bold 'identity nil t) :test 1)
86 (should (org-element-property
87 :test (org-element-map tree 'bold 'identity nil t)))))
88 ;; Put property on a string.
89 (should
90 (org-element-property :test (org-element-put-property "Paragraph" :test t))))
92 (ert-deftest test-org-element/set-contents ()
93 "Test `org-element-set-contents' specifications."
94 ;; Accept multiple entries.
95 (should
96 (equal '("b" (italic nil "a"))
97 (org-test-with-temp-text "* Headline\n *a*"
98 (let ((tree (org-element-parse-buffer)))
99 (org-element-set-contents
100 (org-element-map tree 'bold 'identity nil t) "b" '(italic nil "a"))
101 (org-element-contents
102 (org-element-map tree 'bold 'identity nil t))))))
103 ;; Accept atoms and elements.
104 (should
105 (equal '("b")
106 (org-test-with-temp-text "* Headline\n *a*"
107 (let ((tree (org-element-parse-buffer)))
108 (org-element-set-contents
109 (org-element-map tree 'bold 'identity nil t) "b")
110 (org-element-contents
111 (org-element-map tree 'bold 'identity nil t))))))
112 (should
113 (equal '((italic nil "b"))
114 (org-test-with-temp-text "* Headline\n *a*"
115 (let ((tree (org-element-parse-buffer)))
116 (org-element-set-contents
117 (org-element-map tree 'bold 'identity nil t) '(italic nil "b"))
118 (org-element-contents
119 (org-element-map tree 'bold 'identity nil t))))))
120 ;; Allow nil contents.
121 (should-not
122 (org-test-with-temp-text "* Headline\n *a*"
123 (let ((tree (org-element-parse-buffer)))
124 (org-element-set-contents (org-element-map tree 'bold 'identity nil t))
125 (org-element-contents (org-element-map tree 'bold 'identity nil t))))))
127 (ert-deftest test-org-element/secondary-p ()
128 "Test `org-element-secondary-p' specifications."
129 ;; In a secondary string, return property name.
130 (should
131 (eq :title
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))
135 nil t))))
136 ;; Outside a secondary string, return nil.
137 (should-not
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)))
141 nil t))))
143 ;; FIXME: `org-element-class' is a defsubst and cannot be tested
144 ;; properly (i.e., "make test" fails).
146 ;; (ert-deftest test-org-element/class ()
147 ;; "Test `org-element-class' specifications."
148 ;; ;; Regular tests.
149 ;; (should (eq 'element (org-element-class '(paragraph nil) nil)))
150 ;; (should (eq 'object (org-element-class '(target nil) nil)))
151 ;; ;; Special types.
152 ;; (should (eq 'element (org-element-class '(org-data nil) nil)))
153 ;; (should (eq 'object (org-element-class "text" nil)))
154 ;; (should (eq 'object (org-element-class '("secondary " "string") nil)))
155 ;; ;; Pseudo elements.
156 ;; (should (eq 'element (org-element-class '(foo nil) nil)))
157 ;; (should (eq 'element (org-element-class '(foo nil) '(center-block nil))))
158 ;; (should (eq 'element (org-element-class '(foo nil) '(org-data nil))))
159 ;; ;; Pseudo objects.
160 ;; (should (eq 'object (org-element-class '(foo nil) '(bold nil))))
161 ;; (should (eq 'object (org-element-class '(foo nil) '(paragraph nil))))
162 ;; (should (eq 'object (org-element-class '(foo nil) '("secondary"))))
163 ;; (should
164 ;; (eq 'object
165 ;; (let* ((datum '(foo nil))
166 ;; (headline `(headline (:title (,datum)))))
167 ;; (org-element-put-property datum :parent headline)
168 ;; (org-element-class datum)))))
170 (ert-deftest test-org-element/adopt-elements ()
171 "Test `org-element-adopt-elements' specifications."
172 ;; Adopt an element.
173 (should
174 (equal '(plain-text italic)
175 (org-test-with-temp-text "* Headline\n *a*"
176 (let ((tree (org-element-parse-buffer)))
177 (org-element-adopt-elements
178 (org-element-map tree 'bold 'identity nil t) '(italic nil "a"))
179 (mapcar (lambda (blob) (org-element-type blob))
180 (org-element-contents
181 (org-element-map tree 'bold 'identity nil t)))))))
182 ;; Adopt a string.
183 (should
184 (equal '("a" "b")
185 (org-test-with-temp-text "* Headline\n *a*"
186 (let ((tree (org-element-parse-buffer)))
187 (org-element-adopt-elements
188 (org-element-map tree 'bold 'identity nil t) "b")
189 (org-element-contents
190 (org-element-map tree 'bold 'identity nil t)))))))
192 (ert-deftest test-org-element/extract-element ()
193 "Test `org-element-extract-element' specifications."
194 ;; Extract a greater element.
195 (should
196 (equal '(org-data nil)
197 (org-test-with-temp-text "* Headline"
198 (let* ((tree (org-element-parse-buffer))
199 (element (org-element-map tree 'headline 'identity nil t)))
200 (org-element-extract-element element)
201 tree))))
202 ;; Extract an element.
203 (should-not
204 (org-element-map
205 (org-test-with-temp-text "Paragraph"
206 (let* ((tree (org-element-parse-buffer))
207 (element (org-element-map tree 'paragraph 'identity nil t)))
208 (org-element-extract-element element)
209 tree))
210 'paragraph
211 'identity))
212 ;; Extract an object, even in a secondary string.
213 (should-not
214 (org-element-map
215 (org-test-with-temp-text "*bold*"
216 (let* ((tree (org-element-parse-buffer))
217 (element (org-element-map tree 'bold 'identity nil t)))
218 (org-element-extract-element element)
219 tree))
220 'bold
221 'identity))
222 (should-not
223 (org-element-map
224 (org-test-with-temp-text "* Headline *bold*"
225 (let* ((tree (org-element-parse-buffer))
226 (element (org-element-map tree 'bold 'identity nil t)))
227 (org-element-extract-element element)
228 tree))
229 'bold
230 'identity))
231 ;; Return value doesn't have any :parent set.
232 (should-not
233 (org-element-property
234 :parent
235 (org-test-with-temp-text "* Headline\n Paragraph with *bold* text."
236 (let* ((tree (org-element-parse-buffer))
237 (element (org-element-map tree 'bold 'identity nil t)))
238 (org-element-extract-element element))))))
240 (ert-deftest test-org-element/insert-before ()
241 "Test `org-element-insert-before' specifications."
242 ;; Standard test.
243 (should
244 (equal
245 '(italic entity bold)
246 (org-test-with-temp-text "/some/ *paragraph*"
247 (let* ((tree (org-element-parse-buffer))
248 (paragraph (org-element-map tree 'paragraph 'identity nil t))
249 (bold (org-element-map tree 'bold 'identity nil t)))
250 (org-element-insert-before '(entity (:name "\\alpha")) bold)
251 (org-element-map tree '(bold entity italic) #'org-element-type nil)))))
252 ;; Insert an object in a secondary string.
253 (should
254 (equal
255 '(entity italic)
256 (org-test-with-temp-text "* /A/\n Paragraph."
257 (let* ((tree (org-element-parse-buffer))
258 (headline (org-element-map tree 'headline 'identity nil t))
259 (italic (org-element-map tree 'italic 'identity nil t)))
260 (org-element-insert-before '(entity (:name "\\alpha")) italic)
261 (org-element-map (org-element-property :title headline) '(entity italic)
262 #'org-element-type))))))
264 (ert-deftest test-org-element/set-element ()
265 "Test `org-element-set-element' specifications."
266 ;; Check if new element is inserted.
267 (should
268 (org-test-with-temp-text "* Headline\n*a*"
269 (let* ((tree (org-element-parse-buffer))
270 (bold (org-element-map tree 'bold 'identity nil t)))
271 (org-element-set-element bold '(italic nil "b"))
272 (org-element-map tree 'italic 'identity))))
273 ;; Check if old element is removed.
274 (should-not
275 (org-test-with-temp-text "* Headline\n*a*"
276 (let* ((tree (org-element-parse-buffer))
277 (bold (org-element-map tree 'bold 'identity nil t)))
278 (org-element-set-element bold '(italic nil "b"))
279 (org-element-map tree 'bold 'identity))))
280 ;; Check if :parent property is correctly set.
281 (should
282 (eq 'paragraph
283 (org-test-with-temp-text "* Headline\n*a*"
284 (let* ((tree (org-element-parse-buffer))
285 (bold (org-element-map tree 'bold 'identity nil t)))
286 (org-element-set-element bold '(italic nil "b"))
287 (org-element-type
288 (org-element-property
289 :parent (org-element-map tree 'italic 'identity nil t)))))))
290 ;; Allow to replace strings with elements.
291 (should
292 (equal '("b")
293 (org-test-with-temp-text "* Headline"
294 (let* ((tree (org-element-parse-buffer))
295 (text (org-element-map tree 'plain-text 'identity nil t)))
296 (org-element-set-element text (list 'bold nil "b"))
297 (org-element-map tree 'plain-text 'identity)))))
298 ;; Allow to replace elements with strings.
299 (should
300 (equal "a"
301 (org-test-with-temp-text "* =verbatim="
302 (let* ((tree (org-element-parse-buffer))
303 (verb (org-element-map tree 'verbatim 'identity nil t)))
304 (org-element-set-element verb "a")
305 (org-element-map tree 'plain-text 'identity nil t)))))
306 ;; Allow to replace strings with strings.
307 (should
308 (equal "b"
309 (org-test-with-temp-text "a"
310 (let* ((tree (org-element-parse-buffer))
311 (text (org-element-map tree 'plain-text 'identity nil t)))
312 (org-element-set-element text "b")
313 (org-element-map tree 'plain-text 'identity nil t))))))
315 (ert-deftest test-org-element/copy ()
316 "Test `org-element-copy' specifications."
317 ;; Preserve type.
318 (should (eq 'bold
319 (org-test-with-temp-text "*bold*"
320 (org-element-type (org-element-copy (org-element-context))))))
321 (should (eq 'plain-text
322 (org-test-with-temp-text "*bold*"
323 (org-element-type
324 (org-element-map (org-element-parse-buffer) 'plain-text
325 #'org-element-copy nil t)))))
326 ;; Preserve properties except `:parent'.
327 (should (= 7
328 (org-test-with-temp-text "*bold*"
329 (org-element-property
330 :end (org-element-copy (org-element-context))))))
331 (should-not
332 (org-test-with-temp-text "*bold*"
333 (org-element-property
334 :parent (org-element-copy (org-element-context)))))
335 (should-not
336 (org-test-with-temp-text "*bold*"
337 (org-element-property
338 :parent
339 (org-element-map (org-element-parse-buffer) 'plain-text
340 #'org-element-copy nil t))))
341 ;; Copying nil returns nil.
342 (should-not (org-element-copy nil))
343 ;; Return a copy secondary strings.
344 (should (equal '("text") (org-element-copy '("text"))))
345 (should-not (eq '("text") (org-element-copy '("text")))))
349 ;;; Test Parsers
351 ;;;; Affiliated Keywords
353 (ert-deftest test-org-element/affiliated-keywords-parser ()
354 "Test affiliated keywords parsing."
355 ;; Read simple keywords.
356 (should
357 (equal "para"
358 (org-element-property
359 :name
360 (org-test-with-temp-text "#+NAME: para\nParagraph"
361 (org-element-at-point)))))
362 (should
363 (= 1
364 (org-element-property
365 :begin
366 (org-test-with-temp-text "#+NAME: para\nParagraph"
367 (org-element-at-point)))))
368 ;; Parse multiple keywords.
369 (should
370 (equal
371 '("line2" "line1")
372 (org-element-property
373 :attr_ascii
374 (org-test-with-temp-text
375 "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph"
376 (org-element-at-point)))))
377 ;; Parse "parsed" keywords.
378 (should
379 (equal
380 '(("caption"))
381 (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
382 (car (org-element-property :caption (org-element-at-point))))))
383 ;; Parse dual keywords.
384 (should
385 (equal
386 '((("long") "short"))
387 (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
388 (org-element-property :caption (org-element-at-point)))))
389 ;; Allow multiple caption keywords.
390 (should
391 (equal
392 '((("l2") "s2") (("l1") "s1"))
393 (org-test-with-temp-text "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph"
394 (org-element-property :caption (org-element-at-point)))))
395 (should
396 (equal
397 '((("l1")) (nil "s1"))
398 (org-test-with-temp-text "#+CAPTION[s1]:\n#+CAPTION: l1\nParagraph"
399 (org-element-property :caption (org-element-at-point)))))
400 ;; Corner case: orphaned keyword at the end of an element.
401 (should
402 (eq 'keyword
403 (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
404 (progn (search-forward "name")
405 (org-element-type (org-element-at-point))))))
406 (should-not
407 (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
408 (progn (search-forward "Some")
409 (org-element-property :name (org-element-at-point))))))
412 ;;;; Babel Call
414 (ert-deftest test-org-element/babel-call-parser ()
415 "Test `babel-call' parsing."
416 ;; Standard test.
417 (should
418 (eq 'babel-call
419 (org-test-with-temp-text "#+CALL: test()"
420 (org-element-type (org-element-at-point)))))
421 ;; Ignore case.
422 (should
423 (eq 'babel-call
424 (org-test-with-temp-text "#+call: test()"
425 (org-element-type (org-element-at-point)))))
426 ;; Handle non-empty blank line at the end of buffer.
427 (should
428 (org-test-with-temp-text "#+CALL: test()\n "
429 (= (org-element-property :end (org-element-at-point)) (point-max))))
430 ;; Parse call name.
431 (should
432 (equal "test"
433 (org-test-with-temp-text "#+CALL: test()"
434 (org-element-property :call (org-element-at-point)))))
435 ;; Parse inside header. It may contain paired square brackets.
436 (should
437 (equal ":results output"
438 (org-test-with-temp-text "#+CALL: test[:results output]()"
439 (org-element-property :inside-header (org-element-at-point)))))
440 (should
441 (equal ":results output, a=table[1:2], b=2"
442 (org-test-with-temp-text
443 "#+CALL: test[:results output, a=table[1:2], b=2]()"
444 (org-element-property :inside-header (org-element-at-point)))))
445 ;; Parse arguments, which can be nested. However, stop at paired
446 ;; parenthesis, even when, e.g.,end header contains some.
447 (should
448 (equal "n=4"
449 (org-test-with-temp-text "#+CALL: test(n=4)"
450 (org-element-property :arguments (org-element-at-point)))))
451 (should
452 (equal "test()"
453 (org-test-with-temp-text "#+CALL: test(test())"
454 (org-element-property :arguments (org-element-at-point)))))
455 (should
456 (equal "a=1"
457 (org-test-with-temp-text "#+CALL: test(a=1) :post another-call()"
458 (org-element-property :arguments (org-element-at-point)))))
459 ;; Parse end header.
460 (should
461 (equal ":results html"
462 (org-test-with-temp-text "#+CALL: test() :results html"
463 (org-element-property :end-header (org-element-at-point))))))
466 ;;;; Bold
468 (ert-deftest test-org-element/bold-parser ()
469 "Test `bold' parser."
470 ;; Standard test.
471 (should
472 (org-test-with-temp-text "*bold*"
473 (org-element-map (org-element-parse-buffer) 'bold #'identity nil t)))
474 ;; Multi-line markup.
475 (should
476 (equal
477 (org-element-contents
478 (org-test-with-temp-text "*first line\nsecond line*"
479 (org-element-map (org-element-parse-buffer) 'bold #'identity nil t)))
480 '("first line\nsecond line"))))
483 ;;;; Center Block
485 (ert-deftest test-org-element/center-block-parser ()
486 "Test `center-block' parser."
487 ;; Standard test.
488 (should
489 (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
490 (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
491 ;; Ignore case.
492 (should
493 (org-test-with-temp-text "#+begin_center\nText\n#+end_center"
494 (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
495 ;; Ignore incomplete block.
496 (should-not
497 (org-test-with-temp-text "#+BEGIN_CENTER"
498 (org-element-map (org-element-parse-buffer) 'center-block
499 'identity nil t)))
500 ;; Handle non-empty blank line at the end of buffer.
501 (should
502 (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
503 (= (org-element-property :end (org-element-at-point)) (point-max)))))
506 ;;;; Clock
508 (ert-deftest test-org-element/clock-parser ()
509 "Test `clock' parser."
510 ;; Running clock.
511 (let ((clock (org-test-with-temp-text "CLOCK: [2012-01-01 sun. 00:01]"
512 (org-element-at-point))))
513 (should (eq (org-element-property :status clock) 'running))
514 (should
515 (equal (org-element-property :raw-value
516 (org-element-property :value clock))
517 "[2012-01-01 sun. 00:01]"))
518 (should-not (org-element-property :duration clock)))
519 ;; Closed clock.
520 (let ((clock
521 (org-test-with-temp-text
522 "CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
523 (org-element-at-point))))
524 (should (eq (org-element-property :status clock) 'closed))
525 (should (equal (org-element-property :raw-value
526 (org-element-property :value clock))
527 "[2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02]"))
528 (should (equal (org-element-property :duration clock) "0:01"))))
531 ;;;; Code
533 (ert-deftest test-org-element/code-parser ()
534 "Test `code' parser."
535 ;; Regular test.
536 (should
537 (org-test-with-temp-text "~code~"
538 (org-element-map (org-element-parse-buffer) 'code #'identity)))
539 ;; Multi-line markup.
540 (should
541 (equal
542 (org-element-property
543 :value
544 (org-test-with-temp-text "~first line\nsecond line~"
545 (org-element-map
546 (org-element-parse-buffer) 'code #'identity nil t)))
547 "first line\nsecond line")))
550 ;;;; Comment
552 (ert-deftest test-org-element/comment-parser ()
553 "Test `comment' parser."
554 ;; Regular comment.
555 (should
556 (org-test-with-temp-text "# Comment"
557 (org-element-map (org-element-parse-buffer) 'comment 'identity)))
558 ;; Inline comment.
559 (should
560 (org-test-with-temp-text " # Comment"
561 (org-element-map (org-element-parse-buffer) 'comment 'identity)))
562 ;; Preserve indentation.
563 (should
564 (equal
565 (org-element-property
566 :value
567 (org-test-with-temp-text "# No blank\n# One blank"
568 (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
569 "No blank\n One blank"))
570 ;; Comment with blank lines.
571 (should
572 (equal
573 (org-element-property
574 :value
575 (org-test-with-temp-text "# First part\n# \n#\n# Second part"
576 (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
577 "First part\n\n\nSecond part"))
578 ;; Do not mix comments and keywords.
579 (should
580 (eq 1
581 (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
582 (length (org-element-map (org-element-parse-buffer) 'comment
583 'identity)))))
584 (should
585 (equal "comment"
586 (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
587 (org-element-property
588 :value
589 (org-element-map (org-element-parse-buffer) 'comment
590 'identity nil t)))))
591 ;; Correctly handle non-empty blank lines at the end of buffer.
592 (should
593 (org-test-with-temp-text "# A\n "
594 (= (org-element-property :end (org-element-at-point)) (point-max)))))
597 ;;;; Comment Block
599 (ert-deftest test-org-element/comment-block-parser ()
600 "Test `comment-block' parser."
601 ;; Standard test.
602 (should
603 (org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
604 (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
605 ;; Ignore case.
606 (should
607 (org-test-with-temp-text "#+begin_comment\nText\n#+end_comment"
608 (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
609 ;; Ignore incomplete block.
610 (should-not
611 (org-test-with-temp-text "#+BEGIN_COMMENT"
612 (org-element-map (org-element-parse-buffer) 'comment-block
613 'identity nil t)))
614 ;; Handle non-empty blank line at the end of buffer.
615 (should
616 (org-test-with-temp-text "#+BEGIN_COMMENT\nC\n#+END_COMMENT\n "
617 (= (org-element-property :end (org-element-at-point)) (point-max)))))
620 ;;;; Diary Sexp
622 (ert-deftest test-org-element/diary-sexp-parser ()
623 "Test `diary-sexp' parser."
624 ;; Standard test.
625 (should
626 (eq 'diary-sexp
627 (org-test-with-temp-text
628 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old"
629 (org-element-type (org-element-at-point)))))
630 ;; Diary sexp must live at beginning of line
631 (should-not
632 (eq 'diary-sexp
633 (org-test-with-temp-text " %%(org-bbdb-anniversaries)"
634 (org-element-type (org-element-at-point)))))
635 ;; Handle non-empty blank line at the end of buffer.
636 (should
637 (org-test-with-temp-text "%%(org-bbdb-anniversaries)\n "
638 (= (org-element-property :end (org-element-at-point)) (point-max)))))
641 ;;;; Drawer
643 (ert-deftest test-org-element/drawer-parser ()
644 "Test `drawer' parser."
645 ;; Standard test.
646 (should
647 (org-test-with-temp-text ":TEST:\nText\n:END:"
648 (org-element-map (org-element-parse-buffer) 'drawer 'identity)))
649 ;; Ignore incomplete drawer.
650 (should-not
651 (org-test-with-temp-text ":TEST:"
652 (org-element-map (org-element-parse-buffer) 'drawer 'identity nil t)))
653 ;; Handle non-empty blank line at the end of buffer.
654 (should
655 (org-test-with-temp-text ":TEST:\nC\n:END:\n "
656 (= (org-element-property :end (org-element-at-point)) (point-max)))))
659 ;;;; Dynamic Block
661 (ert-deftest test-org-element/dynamic-block-parser ()
662 "Test `dynamic-block' parser."
663 ;; Standard test.
664 (should
665 (org-test-with-temp-text
666 "#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
667 (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
668 ;; Ignore case.
669 (should
670 (org-test-with-temp-text
671 "#+begin: myblock :param1 val1 :param2 val2\nText\n#+end:"
672 (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
673 ;; Ignore incomplete block.
674 (should-not
675 (org-test-with-temp-text "#+BEGIN: myblock :param1 val1 :param2 val2"
676 (org-element-map (org-element-parse-buffer) 'dynamic-block
677 'identity nil t)))
678 ;; Handle non-empty blank line at the end of buffer.
679 (should
680 (org-test-with-temp-text "#+BEGIN: myblock :param val1\nC\n#+END:\n "
681 (= (org-element-property :end (org-element-at-point)) (point-max)))))
684 ;;;; Entity
686 (ert-deftest test-org-element/entity-parser ()
687 "Test `entity' parser."
688 ;; Without brackets.
689 (should
690 (org-test-with-temp-text "\\sin"
691 (org-element-map (org-element-parse-buffer) 'entity 'identity)))
692 ;; With brackets.
693 (should
694 (org-element-property
695 :use-brackets-p
696 (org-test-with-temp-text "\\alpha{}text"
697 (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
698 ;; User-defined entity.
699 (should
700 (equal
701 (org-element-property
702 :name
703 (let ((org-entities-user
704 '(("test" "test" nil "test" "test" "test" "test"))))
705 (org-test-with-temp-text "\\test"
706 (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
707 "test"))
708 ;; Special case: entity at the end of a container.
709 (should
710 (eq 'entity
711 (org-test-with-temp-text "*\\alpha \\beta*"
712 (search-forward "be")
713 (org-element-type (org-element-context))))))
716 ;;;; Example Block
718 (ert-deftest test-org-element/example-block-parser ()
719 "Test `example-block' parser."
720 ;; Standard test.
721 (should
722 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
723 (org-element-map (org-element-parse-buffer) 'example-block 'identity)))
724 ;; Ignore incomplete block.
725 (should-not
726 (eq 'example-block
727 (org-test-with-temp-text "#+BEGIN_EXAMPLE"
728 (org-element-type (org-element-at-point)))))
729 ;; Properly un-escape code.
730 (should
731 (equal "* Headline\n #+keyword:\nText\n"
732 (org-test-with-temp-text
733 "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword:\nText\n#+END_EXAMPLE"
734 (org-element-property :value (org-element-at-point)))))
735 ;; Handle non-empty blank line at the end of buffer.
736 (should
737 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nC\n#+END_EXAMPLE\n "
738 (= (org-element-property :end (org-element-at-point)) (point-max)))))
740 (ert-deftest test-org-element/block-switches ()
741 "Test `example-block' and `src-block' switches parsing."
742 (let ((org-coderef-label-format "(ref:%s)"))
743 ;; 1. Test "-i" switch.
744 (should-not
745 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
746 (org-element-property :preserve-indent (org-element-at-point))))
747 (should
748 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
749 (org-element-property :preserve-indent (org-element-at-point))))
750 (should-not
751 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
752 (org-element-property :preserve-indent (org-element-at-point))))
753 (should
754 (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
755 (org-element-property :preserve-indent (org-element-at-point))))
756 ;; 2. "-n -r -k" combination should number lines, retain labels but
757 ;; not use them in coderefs.
758 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
759 (let ((element (org-element-at-point)))
760 (should (org-element-property :number-lines element))
761 (should (org-element-property :retain-labels element))
762 (should-not (org-element-property :use-labels element))))
763 (org-test-with-temp-text
764 "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
765 (let ((element (org-element-at-point)))
766 (should (org-element-property :number-lines element))
767 (should (org-element-property :retain-labels element))
768 (should-not (org-element-property :use-labels element))))
769 ;; 3. "-n -r" combination should number-lines remove labels and not
770 ;; use them in coderefs.
771 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
772 (let ((element (org-element-at-point)))
773 (should (org-element-property :number-lines element))
774 (should-not (org-element-property :retain-labels element))
775 (should-not (org-element-property :use-labels element))))
776 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
777 (let ((element (org-element-at-point)))
778 (should (org-element-property :number-lines element))
779 (should-not (org-element-property :retain-labels element))
780 (should-not (org-element-property :use-labels element))))
781 ;; 4. "-n" or "+n" should number lines, retain labels and use them
782 ;; in coderefs.
783 (should
784 (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
785 (let ((element (org-element-at-point)))
786 (and (org-element-property :number-lines element)
787 (org-element-property :retain-labels element)
788 (org-element-property :use-labels element)))))
789 (should
790 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
791 (let ((element (org-element-at-point)))
792 (and (org-element-property :number-lines element)
793 (org-element-property :retain-labels element)
794 (org-element-property :use-labels element)))))
795 (should
796 (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
797 (let ((element (org-element-at-point)))
798 (and (org-element-property :number-lines element)
799 (org-element-property :retain-labels element)
800 (org-element-property :use-labels element)))))
801 (should
802 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
803 (let ((element (org-element-at-point)))
804 (and (org-element-property :number-lines element)
805 (org-element-property :retain-labels element)
806 (org-element-property :use-labels element)))))
807 ;; 5. No switch should not number lines, but retain labels and use
808 ;; them in coderefs.
809 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
810 (let ((element (org-element-at-point)))
811 (should (not (org-element-property :number-lines element)))
812 (should (org-element-property :retain-labels element))
813 (should (org-element-property :use-labels element))))
814 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
815 (let ((element (org-element-at-point)))
816 (should (not (org-element-property :number-lines element)))
817 (should (org-element-property :retain-labels element))
818 (should (org-element-property :use-labels element))))
819 ;; 6. "-r" switch only: do not number lines, remove labels, and
820 ;; don't use labels in coderefs.
821 (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
822 (let ((element (org-element-at-point)))
823 (should (not (org-element-property :number-lines element)))
824 (should (not (org-element-property :retain-labels element)))
825 (should (not (org-element-property :use-labels element)))))
826 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
827 (let ((element (org-element-at-point)))
828 (should (not (org-element-property :number-lines element)))
829 (should (not (org-element-property :retain-labels element)))
830 (should (not (org-element-property :use-labels element)))))
831 ;; 7. Recognize coderefs with user-defined syntax.
832 (should
833 (equal
834 "[ref:%s]"
835 (org-test-with-temp-text
836 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
837 (org-element-property :label-fmt (org-element-at-point)))))
838 (should
839 (equal
840 "[ref:%s]"
841 (org-test-with-temp-text
842 "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
843 (org-element-property :label-fmt (org-element-at-point)))))))
846 ;;;; Export Block
848 (ert-deftest test-org-element/export-block-parser ()
849 "Test `export-block' parser."
850 ;; Standard test.
851 (should
852 (eq 'export-block
853 (org-test-with-temp-text "#+BEGIN_EXPORT LATEX\nText\n#+END_EXPORT"
854 (org-element-type (org-element-at-point)))))
855 (should
856 (equal "LATEX"
857 (org-test-with-temp-text "#+BEGIN_EXPORT LATEX\nText\n#+END_EXPORT"
858 (org-element-property :type (org-element-at-point)))))
859 ;; Ignore case.
860 (should
861 (eq 'export-block
862 (org-test-with-temp-text "#+begin_export latex\nText\n#+end_export"
863 (org-element-type (org-element-at-point)))))
864 ;; Ignore incomplete block.
865 (should-not
866 (eq 'export-block
867 (org-test-with-temp-text "#+BEGIN_EXPORT"
868 (org-element-type (org-element-at-point)))))
869 ;; Handle non-empty blank line at the end of buffer.
870 (should
871 (org-test-with-temp-text "#+BEGIN_EXPORT latex\nC\n#+END_EXPORT\n "
872 (= (org-element-property :end (org-element-at-point)) (point-max))))
873 ;; Un-escape commas in `:value'.
874 (should
875 (equal "* H\n"
876 (org-test-with-temp-text "#+BEGIN_EXPORT org\n,* H\n#+END_EXPORT\n "
877 (org-element-property :value (org-element-at-point))))))
880 ;;;; Export Snippet
882 (ert-deftest test-org-element/export-snippet-parser ()
883 "Test `export-snippet' parser."
884 (should
885 (equal
886 '("back-end" . "contents")
887 (org-test-with-temp-text "@@back-end:contents@@"
888 (org-element-map
889 (org-element-parse-buffer) 'export-snippet
890 (lambda (snippet) (cons (org-element-property :back-end snippet)
891 (org-element-property :value snippet)))
892 nil t)))))
895 ;;;; Fixed Width
897 (ert-deftest test-org-element/fixed-width-parser ()
898 "Test fixed-width area parsing."
899 ;; Preserve indentation.
900 (should
901 (org-test-with-temp-text ": no blank\n: one blank"
902 (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
903 ;; Fixed-width with empty lines.
904 (should
905 (org-test-with-temp-text ": first part\n:\n: \n: second part"
906 (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
907 ;; Parse indented fixed-width markers.
908 (should
909 (org-test-with-temp-text "Text\n : no blank\n : one blank"
910 (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
911 ;; Distinguish fixed-width areas within a list and outside of it.
912 (should
913 (= 2
914 (length
915 (org-test-with-temp-text "
916 - Item
917 : fixed-width inside
918 : fixed-width outside"
919 (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))))
920 ;; Handle non-empty blank line at the end of buffer.
921 (should
922 (org-test-with-temp-text ": A\n "
923 (= (org-element-property :end (org-element-at-point)) (point-max)))))
926 ;;;; Footnote Definition
928 (ert-deftest test-org-element/footnote-definition-parser ()
929 "Test `footnote-definition' parser."
930 (should
931 (org-test-with-temp-text "[fn:1] Definition"
932 (eq (org-element-type (org-element-at-point)) 'footnote-definition)))
933 ;; Footnote with more contents.
934 (should
935 (= 29 (org-test-with-temp-text "[fn:1] Definition\n\n| a | b |"
936 (org-element-property :end (org-element-at-point)))))
937 ;; Test difference between :contents-end and :end property
938 (should
939 (< (org-test-with-temp-text "[fn:1] Definition\n\n\n"
940 (org-element-property :contents-end (org-element-at-point)))
941 (org-test-with-temp-text "[fn:1] Definition\n\n\n"
942 (org-element-property :end (org-element-at-point)))))
943 ;; Footnote starting with special syntax.
944 (should-not
945 (org-test-with-temp-text "[fn:1] <point>- no item"
946 (eq (org-element-type (org-element-at-point)) 'item)))
947 ;; Correctly handle footnote starting with an empty line.
948 (should
949 (= 9
950 (org-test-with-temp-text "[fn:1]\n\n Body"
951 (org-element-property :contents-begin (org-element-at-point)))))
952 ;; Handle non-empty blank line at the end of buffer.
953 (should
954 (org-test-with-temp-text "[fn:1] Definition\n "
955 (= (org-element-property :end (org-element-at-point)) (point-max))))
956 ;; Footnote with attributes.
957 (should
958 (= 1
959 (org-test-with-temp-text "#+attr_latex: :offset 0in\n[fn:1] A footnote."
960 (length
961 (org-element-map (org-element-parse-buffer) 'footnote-definition
962 #'identity)))))
963 (should
964 (org-test-with-temp-text "[fn:1] 1\n\n#+attr_latex: :offset 0in\n[fn:2] 2"
965 (goto-char (org-element-property :end (org-element-at-point)))
966 (looking-at "#")))
967 ;; An empty footnote has no contents.
968 (should-not
969 (org-test-with-temp-text "[fn:1]\n\n"
970 (let ((footnote (org-element-at-point)))
971 (or (org-element-property :contents-begin footnote)
972 (org-element-property :contents-end footnote)))))
973 ;; Parse `:pre-blank'.
974 (should
975 (= 0
976 (org-test-with-temp-text "[fn:1] A"
977 (org-element-property :pre-blank (org-element-at-point)))))
978 (should
979 (= 1
980 (org-test-with-temp-text "[fn:1]\nA"
981 (org-element-property :pre-blank (org-element-at-point)))))
982 (should
983 (= 2
984 (org-test-with-temp-text "[fn:1]\n\nA"
985 (org-element-property :pre-blank (org-element-at-point))))))
988 ;;;; Footnotes Reference.
990 (ert-deftest test-org-element/footnote-reference-parser ()
991 "Test `footnote-reference' parser."
992 ;; Parse a standard reference.
993 (should
994 (org-test-with-temp-text "Text[fn:label]"
995 (org-element-map
996 (org-element-parse-buffer) 'footnote-reference 'identity)))
997 ;; Parse an inline reference.
998 (should
999 (org-test-with-temp-text "Text[fn:test:def]"
1000 (org-element-map
1001 (org-element-parse-buffer) 'footnote-reference 'identity)))
1002 ;; Parse an anonymous reference.
1003 (should
1004 (org-test-with-temp-text "Text[fn::def]"
1005 (org-element-map
1006 (org-element-parse-buffer) 'footnote-reference 'identity)))
1007 ;; Parse nested footnotes.
1008 (should
1009 (= 2
1010 (length
1011 (org-test-with-temp-text "Text[fn::def [fn:label]]"
1012 (org-element-map
1013 (org-element-parse-buffer) 'footnote-reference 'identity)))))
1014 ;; Parse adjacent footnotes.
1015 (should
1016 (org-test-with-temp-text "Text[fn:label1][fn:label2]"
1017 (= 2
1018 (length
1019 (org-element-map
1020 (org-element-parse-buffer) 'footnote-reference 'identity)))))
1021 ;; Only properly closed footnotes are recognized as such.
1022 (should-not
1023 (org-test-with-temp-text "Text[fn:label"
1024 (org-element-map
1025 (org-element-parse-buffer) 'footnote-reference 'identity))))
1028 ;;;; Headline
1030 (ert-deftest test-org-element/headline-comment-keyword ()
1031 "Test COMMENT keyword recognition."
1032 ;; Reference test.
1033 (org-test-with-temp-text "* Headline"
1034 (let ((org-comment-string "COMMENT"))
1035 (should-not (org-element-property :commentedp (org-element-at-point)))))
1036 ;; Standard position.
1037 (org-test-with-temp-text "* COMMENT Headline"
1038 (let ((org-comment-string "COMMENT")
1039 (headline (org-element-at-point)))
1040 (should (org-element-property :commentedp headline))
1041 (should (equal (org-element-property :raw-value headline) "Headline"))))
1042 ;; Case sensitivity.
1043 (org-test-with-temp-text "* COMMENT Headline"
1044 (let* ((org-comment-string "Comment")
1045 (headline (org-element-at-point)))
1046 (should-not (org-element-property :commentedp headline))
1047 (should (equal (org-element-property :raw-value headline)
1048 "COMMENT Headline"))))
1049 ;; With another keyword.
1050 (org-test-with-temp-text "* TODO COMMENT Headline"
1051 (let* ((org-comment-string "COMMENT")
1052 (org-todo-keywords '((sequence "TODO" "DONE")))
1053 (headline (org-element-at-point)))
1054 (should (org-element-property :commentedp headline))
1055 (should (equal (org-element-property :raw-value headline) "Headline"))))
1056 ;; With the keyword only.
1057 (org-test-with-temp-text "* COMMENT"
1058 (let* ((org-comment-string "COMMENT")
1059 (headline (org-element-at-point)))
1060 (should (org-element-property :commentedp headline))
1061 (should (equal (org-element-property :raw-value headline) "")))))
1063 (ert-deftest test-org-element/headline-archive-tag ()
1064 "Test ARCHIVE tag recognition."
1065 ;; Reference test.
1066 (should-not
1067 (org-test-with-temp-text "* Headline"
1068 (let ((org-archive-tag "ARCHIVE"))
1069 (org-element-property :archivedp (org-element-at-point)))))
1070 ;; Single tag.
1071 (org-test-with-temp-text "* Headline :ARCHIVE:"
1072 (let ((org-archive-tag "ARCHIVE"))
1073 (let ((headline (org-element-at-point)))
1074 (should (org-element-property :archivedp headline)))))
1075 ;; Multiple tags.
1076 (org-test-with-temp-text "* Headline :test:ARCHIVE:"
1077 (let ((org-archive-tag "ARCHIVE"))
1078 (let ((headline (org-element-at-point)))
1079 (should (org-element-property :archivedp headline)))))
1080 ;; Tag is case-sensitive.
1081 (should-not
1082 (org-test-with-temp-text "* Headline :ARCHIVE:"
1083 (let ((org-archive-tag "Archive"))
1084 (org-element-property :archivedp (org-element-at-point))))))
1086 (ert-deftest test-org-element/headline-properties ()
1087 "Test properties from property drawer."
1088 ;; All properties from property drawer have their symbol upper
1089 ;; cased.
1090 (should
1091 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
1092 (org-element-property :FOO (org-element-at-point))))
1093 (should-not
1094 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
1095 (org-element-property :foo (org-element-at-point))))
1096 ;; Also parse properties associated in inlinetasks.
1097 (when (featurep 'org-inlinetask)
1098 (should
1099 (org-test-with-temp-text "*************** Inlinetask
1100 :PROPERTIES:
1101 :foo: bar
1102 :END:
1103 *************** END"
1104 (org-element-property :FOO (org-element-at-point)))))
1105 ;; Do not find property drawer in a verbatim area.
1106 (should-not
1107 (org-test-with-temp-text
1108 "* Headline
1109 #+BEGIN_EXAMPLE
1110 :PROPERTIES:
1111 :foo: bar
1112 :END:
1113 #+END_EXAMPLE"
1114 (org-element-property :FOO (org-element-at-point))))
1115 ;; Do not use properties from a drawer associated to an inlinetask.
1116 (when (featurep 'org-inlinetask)
1117 (should-not
1118 (org-test-with-temp-text
1119 "* Headline
1120 *************** Inlinetask
1121 :PROPERTIES:
1122 :foo: bar
1123 :END:
1124 *************** END"
1125 (org-element-property
1126 :FOO (let ((org-inlinetask-min-level 15)) (org-element-at-point))))))
1127 ;; Do not find incomplete drawers.
1128 (should-not
1129 (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar"
1130 (org-element-property :FOO (org-element-at-point)))))
1133 ;;;; Horizontal Rule
1135 (ert-deftest test-org-element/horizontal-rule-parser ()
1136 "Test `horizontal-rule' parser."
1137 ;; Standard.
1138 (should
1139 (org-test-with-temp-text "-----"
1140 (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
1141 ;; Indented.
1142 (should
1143 (org-test-with-temp-text " -----"
1144 (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
1145 ;; Hyphen must be alone on the line.
1146 (should-not
1147 (org-test-with-temp-text "-----wrong"
1148 (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
1149 ;; 4 hyphens is too small.
1150 (should-not
1151 (org-test-with-temp-text "----"
1152 (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
1153 ;; Handle non-empty blank line at the end of buffer.
1154 (should
1155 (org-test-with-temp-text "-----\n "
1156 (= (org-element-property :end (org-element-at-point)) (point-max)))))
1159 ;;;; Inline Babel Call
1161 (ert-deftest test-org-element/inline-babel-call-parser ()
1162 "Test `inline-babel-call' parser."
1163 ;; Standard test.
1164 (should
1165 (eq 'inline-babel-call
1166 (org-test-with-temp-text "call_test()"
1167 (org-element-type (org-element-context)))))
1168 (should
1169 (eq 'inline-babel-call
1170 (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
1171 (org-element-type (org-element-context)))))
1172 ;; Parse call name.
1173 (should
1174 (equal
1175 "test"
1176 (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
1177 (org-element-property :call (org-element-context)))))
1178 ;; Parse inside header.
1179 (should
1180 (equal
1181 ":results output"
1182 (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
1183 (org-element-property :inside-header (org-element-context)))))
1184 ;; Parse arguments.
1185 (should
1186 (equal
1187 "x=2"
1188 (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
1189 (org-element-property :arguments (org-element-context)))))
1190 ;; Parse end header.
1191 (should
1192 (equal
1193 ":results html"
1194 (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
1195 (org-element-property :end-header (org-element-context)))))
1196 ;; Handle multi-line babel calls.
1197 (should
1198 (eq 'inline-babel-call
1199 (org-test-with-temp-text
1200 "call_test[:results\noutput](x=2)[:results html]"
1201 (org-element-type (org-element-context)))))
1202 (should
1203 (eq 'inline-babel-call
1204 (org-test-with-temp-text
1205 "call_test[:results output](x=2\ny=3)[:results html]"
1206 (org-element-type (org-element-context)))))
1207 (should
1208 (eq 'inline-babel-call
1209 (org-test-with-temp-text
1210 "call_test[:results output](x=2)[:results\nhtml]"
1211 (org-element-type (org-element-context)))))
1212 ;; Parse parameters containing round brackets.
1213 (should
1214 (eq 'inline-babel-call
1215 (org-test-with-temp-text "call_test[:var x='(1)](x=2)"
1216 (org-element-type (org-element-context))))))
1219 ;;;; Inline Src Block
1221 (ert-deftest test-org-element/inline-src-block-parser ()
1222 "Test `inline-src-block' parser."
1223 (should
1224 (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
1225 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1226 ;; With prefix.
1227 (should
1228 (org-test-with-temp-text "foosrc_emacs-lisp[]{(+ 1 1)}"
1229 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1230 ;; With switches.
1231 (should
1232 (org-test-with-temp-text "src_emacs-lisp[:foo bar]{(+ 1 1)}"
1233 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1234 (should
1235 (org-test-with-temp-text "src_emacs-lisp[ :foo bar]{(+ 1 1)}"
1236 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1237 ;; Empty switches.
1238 (should
1239 (org-test-with-temp-text "src_emacs-lisp[]{(+ 1 1)}"
1240 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1241 ;; Invalid syntax.
1242 (should-not
1243 (org-test-with-temp-text "src_emacs-lisp[]foo{(+ 1 1)}"
1244 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1245 ;; Invalid language name
1246 (should-not
1247 (org-test-with-temp-text "src_emacs-\tlisp{(+ 1 1)}"
1248 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1249 ;; Test parsing at the beginning of an item.
1250 (should
1251 (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
1252 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1253 ;; Test parsing multi-line source blocks.
1254 (should
1255 (eq 'inline-src-block
1256 (org-test-with-temp-text "src_emacs-lisp{(+ 1\n 1)}"
1257 (org-element-type (org-element-context)))))
1258 (should
1259 (eq 'inline-src-block
1260 (org-test-with-temp-text "src_emacs-lisp[\n:foo bar]{(+ 1 1)}"
1261 (org-element-type (org-element-context)))))
1262 (should
1263 (eq 'inline-src-block
1264 (org-test-with-temp-text "src_emacs-lisp[:foo\nbar]{(+ 1 1)}"
1265 (org-element-type (org-element-context)))))
1266 ;; Besides curly brackets, ignore any other bracket type.
1267 (should
1268 (equal "[foo"
1269 (org-test-with-temp-text "src_emacs-lisp{[foo}"
1270 (org-element-property :value (org-element-context)))))
1271 (should
1272 (equal "foo]"
1273 (org-test-with-temp-text "src_emacs-lisp{foo]}"
1274 (org-element-property :value (org-element-context)))))
1275 (should
1276 (equal "(foo"
1277 (org-test-with-temp-text "src_emacs-lisp{(foo}"
1278 (org-element-property :value (org-element-context)))))
1279 (should
1280 (equal "foo)"
1281 (org-test-with-temp-text "src_emacs-lisp{foo)}"
1282 (org-element-property :value (org-element-context)))))
1283 ;; Parse parameters containing square brackets.
1284 (should
1285 (eq 'inline-src-block
1286 (org-test-with-temp-text "src_emacs-lisp[:var table=t[1,1]]{(+ 1 1)}"
1287 (org-element-type (org-element-context))))))
1290 ;;;; Inlinetask
1292 (ert-deftest test-org-element/inlinetask-parser ()
1293 "Test `inlinetask' parser."
1294 (when (featurep 'org-inlinetask)
1295 (let ((org-inlinetask-min-level 15))
1296 ;; Regular inlinetask.
1297 (should
1298 (eq 'inlinetask
1299 (org-test-with-temp-text
1300 "*************** Task\nTest\n*************** END"
1301 (org-element-type (org-element-at-point)))))
1302 (should
1303 (eq 'inlinetask
1304 (org-test-with-temp-text
1305 "*************** Task\nTest\n*************** END"
1306 (org-element-type (org-element-at-point)))))
1307 ;; Degenerate inlinetask.
1308 (should
1309 (eq 'inlinetask
1310 (org-test-with-temp-text "*************** Task"
1311 (org-element-type (org-element-at-point)))))
1312 ;; Mixed inlinetasks.
1313 (should-not
1314 (org-test-with-temp-text
1316 *************** Task
1317 *************** Task2
1318 Contents
1319 *************** END"
1320 (forward-line)
1321 (goto-char (org-element-property :end (org-element-at-point)))
1322 (eobp)))
1323 ;; TODO keyword.
1324 (should
1325 (equal
1326 "TODO"
1327 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
1328 (org-test-with-temp-text "*************** TODO Task"
1329 (org-element-property :todo-keyword (org-element-at-point))))))
1330 ;; Planning info.
1331 (should
1332 (org-test-with-temp-text "
1333 *************** Task<point>
1334 DEADLINE: <2012-03-29 thu.>
1335 *************** END"
1336 (org-element-property :deadline (org-element-at-point))))
1337 (should
1338 (eq 'planning
1339 (org-test-with-temp-text "
1340 *************** Task
1341 <point>DEADLINE: <2012-03-29 thu.>
1342 *************** END"
1343 (org-element-type (org-element-at-point)))))
1344 (should-not
1345 (org-test-with-temp-text "
1346 *************** Task<point>
1347 DEADLINE: <2012-03-29 thu.>"
1348 (org-element-property :deadline (org-element-at-point))))
1349 (should-not
1350 (eq 'planning
1351 (org-test-with-temp-text "
1352 *************** Task
1353 <point>DEADLINE: <2012-03-29 thu.>"
1354 (org-element-type (org-element-at-point)))))
1355 ;; Priority.
1356 (should
1359 (org-test-with-temp-text "
1360 *************** [#A] Task"
1361 (forward-line)
1362 (org-element-property :priority (org-element-at-point)))))
1363 ;; Tags.
1364 (should
1365 (equal
1366 '("test")
1367 (org-test-with-temp-text "
1368 *************** Task :test:"
1369 (forward-line)
1370 (org-element-property :tags (org-element-at-point)))))
1371 ;; Regular properties are accessed through upper case keywords.
1372 (should
1373 (org-test-with-temp-text "
1374 *************** Task
1375 :PROPERTIES:
1376 :foo: bar
1377 :END:
1378 *************** END"
1379 (forward-line)
1380 (org-element-property :FOO (org-element-at-point))))
1381 (should-not
1382 (org-test-with-temp-text "
1383 *************** Task
1384 :PROPERTIES:
1385 :foo: bar
1386 :END:
1387 *************** END"
1388 (forward-line)
1389 (org-element-property :foo (org-element-at-point))))
1390 ;; Handle non-empty blank line at the end of buffer.
1391 (should
1392 (org-test-with-temp-text "*************** Task\n*************** END\n "
1393 (= (org-element-property :end (org-element-at-point)) (point-max)))))))
1396 ;;;; Italic
1398 (ert-deftest test-org-element/italic-parser ()
1399 "Test `italic' parser."
1400 ;; Regular test.
1401 (should
1402 (org-test-with-temp-text "/italic/"
1403 (org-element-map (org-element-parse-buffer) 'italic #'identity nil t)))
1404 ;; Multi-line markup.
1405 (should
1406 (equal
1407 (org-element-contents
1408 (org-test-with-temp-text "/first line\nsecond line/"
1409 (org-element-map (org-element-parse-buffer) 'italic #'identity nil t)))
1410 '("first line\nsecond line"))))
1413 ;;;; Item
1415 (ert-deftest test-org-element/item-parser ()
1416 "Test `item' parser."
1417 ;; Standard test.
1418 (should
1419 (org-test-with-temp-text "- item"
1420 (org-element-map (org-element-parse-buffer) 'item 'identity)))
1421 ;; Counter.
1422 (should
1423 (= 6
1424 (org-element-property
1425 :counter
1426 (org-test-with-temp-text "6. [@6] item"
1427 (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
1428 ;; Tag
1429 (should
1430 (equal
1431 '("tag")
1432 (org-element-property
1433 :tag
1434 (org-test-with-temp-text "- tag :: description"
1435 (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
1436 ;; No tags in ordered lists.
1437 (should-not
1438 (org-element-property
1439 :tag
1440 (org-test-with-temp-text "1. tag :: description"
1441 (org-element-map (org-element-parse-buffer) 'item 'identity nil t))))
1442 ;; Check-boxes
1443 (should
1444 (equal
1445 '(trans on off)
1446 (org-test-with-temp-text "
1447 - [-] item 1
1448 - [X] item 1.1
1449 - [ ] item 1.2"
1450 (org-element-map (org-element-parse-buffer) 'item
1451 (lambda (item) (org-element-property :checkbox item))))))
1452 ;; Item starting with special syntax.
1453 (should
1454 (equal '(("- item"))
1455 (org-test-with-temp-text "- - item"
1456 (org-element-map (org-element-parse-buffer) 'paragraph
1457 'org-element-contents))))
1458 ;; Block in an item: ignore indentation within the block.
1459 (should
1460 (org-test-with-temp-text
1461 "-<point> item\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1462 (= (org-element-property :end (org-element-at-point)) (point-max))))
1463 ;; Parse `:pre-blank'.
1464 (should
1465 (= 0
1466 (org-test-with-temp-text "-<point> A"
1467 (org-element-property :pre-blank (org-element-at-point)))))
1468 (should
1469 (= 1
1470 (org-test-with-temp-text "-<point>\n A"
1471 (org-element-property :pre-blank (org-element-at-point)))))
1472 (should
1473 (= 2
1474 (org-test-with-temp-text "-<point>\n\n A"
1475 (org-element-property :pre-blank (org-element-at-point)))))
1476 ;; Last item in a list or sub-list has no `:post-blank' lines, since
1477 ;; those belong to the plain-list.
1478 (should
1479 (= 0
1480 (org-test-with-temp-text "- A\n\n- <point>B\n\nEnd list"
1481 (org-element-property :post-blank (org-element-at-point)))))
1482 (should
1483 (= 0
1484 (org-test-with-temp-text "- A\n\n - B\n\n<point> - C\n\n End sub-list"
1485 (org-element-property :post-blank (org-element-at-point))))))
1488 ;;;; Keyword
1490 (ert-deftest test-org-element/keyword-parser ()
1491 "Test `keyword' parser."
1492 ;; Standard test.
1493 (should
1494 (org-test-with-temp-text "#+KEYWORD: value"
1495 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1496 ;; Keywords are case-insensitive.
1497 (should
1498 (org-test-with-temp-text "#+keyword: value"
1499 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1500 ;; Affiliated keywords are not keywords.
1501 (should-not
1502 (org-test-with-temp-text "#+NAME: value
1503 Paragraph"
1504 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1505 ;; Do not mix keywords with Babel calls and dynamic blocks.
1506 (should-not
1507 (org-test-with-temp-text "#+CALL: fun()"
1508 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1509 (should-not
1510 (org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
1511 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1512 ;; Handle non-empty blank line at the end of buffer.
1513 (should
1514 (org-test-with-temp-text "#+KEYWORD: value\n "
1515 (= (org-element-property :end (org-element-at-point)) (point-max)))))
1518 ;;;; Latex Environment
1520 (ert-deftest test-org-element/latex-environment-parser ()
1521 "Test `latex-environment' parser."
1522 (should
1523 (org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
1524 (org-element-map (org-element-parse-buffer) 'latex-environment 'identity)))
1525 ;; Allow nested environments.
1526 (should
1527 (equal
1528 "\\begin{outer}
1529 \\begin{inner}
1530 e^{i\\pi}+1=0
1531 \\end{inner}
1532 \\end{outer}"
1533 (org-test-with-temp-text "
1534 \\begin{outer}
1535 \\begin{inner}
1536 e^{i\\pi}+1=0
1537 \\end{inner}
1538 \\end{outer}"
1539 (org-element-property
1540 :value
1541 (org-element-map
1542 (org-element-parse-buffer) 'latex-environment 'identity nil t)))))
1543 ;; Allow environments with options and arguments.
1544 (should
1545 (eq 'latex-environment
1546 (org-test-with-temp-text
1547 "\\begin{theorem}[Euler]\ne^{i\\pi}+1=0\n\\end{theorem}"
1548 (org-element-type (org-element-at-point)))))
1549 (should
1550 (eq 'latex-environment
1551 (org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
1552 (org-element-type (org-element-at-point)))))
1553 ;; Allow environments without newline after \begin{.}.
1554 (should
1555 (eq 'latex-environment
1556 (org-test-with-temp-text "\\begin{env}{arg}something\nvalue\n\\end{env}"
1557 (org-element-type (org-element-at-point)))))
1558 ;; Allow one-line environments.
1559 (should
1560 (eq 'latex-environment
1561 (org-test-with-temp-text "\\begin{env}{arg}something\\end{env}"
1562 (org-element-type (org-element-at-point)))))
1563 ;; Should not allow different tags.
1564 (should-not
1565 (eq 'latex-environment
1566 (org-test-with-temp-text "\\begin{env*}{arg}something\\end{env}"
1567 (org-element-type (org-element-at-point)))))
1568 ;; LaTeX environments must be on separate lines.
1569 (should-not
1570 (eq 'latex-environment
1571 (org-test-with-temp-text "\\begin{env} x \\end{env} y"
1572 (org-element-type (org-element-at-point)))))
1573 (should-not
1574 (eq 'latex-environment
1575 (org-test-with-temp-text "y \\begin{env} x<point> \\end{env}"
1576 (org-element-type (org-element-at-point)))))
1577 ;; Handle non-empty blank line at the end of buffer.
1578 (should
1579 (org-test-with-temp-text "\\begin{env}\n\\end{env}\n "
1580 (= (org-element-property :end (org-element-at-point)) (point-max)))))
1583 ;;;; Latex Fragment
1585 (ert-deftest test-org-element/latex-fragment-parser ()
1586 "Test `latex-fragment' parser."
1587 ;; Basic $...$ test.
1588 (should
1589 (eq 'latex-fragment
1590 (org-test-with-temp-text "$a$"
1591 (org-element-type (org-element-context)))))
1592 ;; Test valid characters after $...$ construct.
1593 (should-not
1594 (eq 'latex-fragment
1595 (org-test-with-temp-text "$a$a"
1596 (org-element-type (org-element-context)))))
1597 (should
1598 (eq 'latex-fragment
1599 (org-test-with-temp-text "$a$!"
1600 (org-element-type (org-element-context)))))
1601 (should
1602 (eq 'latex-fragment
1603 (org-test-with-temp-text "$a$,"
1604 (org-element-type (org-element-context)))))
1605 (should
1606 (eq 'latex-fragment
1607 (org-test-with-temp-text "$a$\""
1608 (org-element-type (org-element-context)))))
1609 (should
1610 (eq 'latex-fragment
1611 (org-test-with-temp-text "$a$)"
1612 (org-element-type (org-element-context)))))
1613 (should
1614 (eq 'latex-fragment
1615 (org-test-with-temp-text "$a$ "
1616 (org-element-type (org-element-context)))))
1617 (should
1618 (eq 'latex-fragment
1619 (org-test-with-temp-text "$a$'"
1620 (org-element-type (org-element-context)))))
1621 ;; Test forbidden characters inside $...$.
1622 (should-not
1623 (eq 'latex-fragment
1624 (org-test-with-temp-text "$.a$"
1625 (org-element-type (org-element-context)))))
1626 (should-not
1627 (eq 'latex-fragment
1628 (org-test-with-temp-text "$,a$"
1629 (org-element-type (org-element-context)))))
1630 (should-not
1631 (eq 'latex-fragment
1632 (org-test-with-temp-text "$;a$"
1633 (org-element-type (org-element-context)))))
1634 (should-not
1635 (eq 'latex-fragment
1636 (org-test-with-temp-text "$ a$"
1637 (org-element-type (org-element-context)))))
1638 (should-not
1639 (eq 'latex-fragment
1640 (org-test-with-temp-text "$a.$"
1641 (org-element-type (org-element-context)))))
1642 (should-not
1643 (eq 'latex-fragment
1644 (org-test-with-temp-text "$a,$"
1645 (org-element-type (org-element-context)))))
1646 (should-not
1647 (eq 'latex-fragment
1648 (org-test-with-temp-text "$a $"
1649 (org-element-type (org-element-context)))))
1650 ;; Test $$...$$.
1651 (should
1652 (eq 'latex-fragment
1653 (org-test-with-temp-text "$$a$$"
1654 (org-element-type (org-element-context)))))
1655 ;; Test \(...\).
1656 (should
1657 (eq 'latex-fragment
1658 (org-test-with-temp-text "\\(a\\)"
1659 (org-element-type (org-element-context)))))
1660 ;; Test \[...\].
1661 (should
1662 (eq 'latex-fragment
1663 (org-test-with-temp-text "\\[a\\]"
1664 (org-element-type (org-element-context)))))
1665 ;; Test fragment at the beginning of an item.
1666 (should
1667 (eq 'latex-fragment
1668 (org-test-with-temp-text "- $<point>x$"
1669 (org-element-type (org-element-context))))))
1672 ;;;; Line Break
1674 (ert-deftest test-org-element/line-break-parser ()
1675 "Test `line-break' parser."
1676 ;; Regular test.
1677 (should
1678 (org-test-with-temp-text "Text \\\\"
1679 (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
1680 ;; Line break with trailing white spaces.
1681 (should
1682 (org-test-with-temp-text "Text \\\\ "
1683 (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
1684 ;; Three backslashes are too much.
1685 (should-not
1686 (org-test-with-temp-text "Text \\\\\\"
1687 (org-element-map (org-element-parse-buffer) 'line-break 'identity))))
1690 ;;;; Link
1692 (ert-deftest test-org-element/link-parser ()
1693 "Test `link' parser."
1694 ;; Radio target.
1695 (should
1696 (equal
1697 "radio"
1698 (org-test-with-temp-text "<<<radio>>>A radio link"
1699 (org-update-radio-target-regexp)
1700 (org-element-property
1701 :type
1702 (org-element-map (org-element-parse-buffer) 'link #'identity nil t)))))
1703 ;; Pathological case: radio target of length 1 at beginning of line
1704 ;; not followed by spaces.
1705 (should
1706 (org-test-with-temp-text "* <<<a>>>\n<point>a-bug"
1707 (org-update-radio-target-regexp)
1708 (org-element-parse-buffer)))
1709 ;; Pathological case: radio target in an emphasis environment.
1710 (should
1711 (eq 'bold
1712 (org-test-with-temp-text "* <<<radio>>>\n<point>*radio*"
1713 (org-update-radio-target-regexp)
1714 (org-element-type (org-element-context)))))
1715 (should
1716 (eq 'link
1717 (org-test-with-temp-text "* <<<radio>>>\n*<point>radio*"
1718 (org-update-radio-target-regexp)
1719 (org-element-type (org-element-context)))))
1720 ;; Standard link.
1722 ;; ... with description.
1723 (should
1724 (equal
1725 '("Orgmode.org")
1726 (org-test-with-temp-text "[[https://orgmode.org][Orgmode.org]]"
1727 (org-element-contents
1728 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1729 ;; ... without description.
1730 (should
1731 (equal
1732 "https"
1733 (org-test-with-temp-text "[[https://orgmode.org]]"
1734 (org-element-property
1735 :type
1736 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1737 ;; ... with expansion.
1738 (should
1739 (equal
1740 "//orgmode.org/worg"
1741 (org-test-with-temp-text "[[Org:worg]]"
1742 (let ((org-link-abbrev-alist '(("Org" . "https://orgmode.org/"))))
1743 (org-element-property
1744 :path
1745 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))))))
1746 ;; ... with translation.
1747 (should
1748 (equal
1749 "127.0.0.1"
1750 (org-test-with-temp-text "[[https://orgmode.org]]"
1751 (let ((org-link-translation-function
1752 (lambda (type _) (cons type "127.0.0.1"))))
1753 (org-element-property
1754 :path
1755 (org-element-map (org-element-parse-buffer) 'link
1756 #'identity nil t))))))
1757 ;; ... custom-id link.
1758 (should
1759 (equal
1760 "custom-id"
1761 (org-test-with-temp-text "[[#some-id]]"
1762 (org-element-property
1763 :type
1764 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1765 ;; ... coderef link.
1766 (should
1767 (equal
1768 "coderef"
1769 (org-test-with-temp-text "[[(reference)]]"
1770 (org-element-property
1771 :type
1772 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1773 ;; ... fuzzy link.
1774 (should
1775 (equal
1776 "fuzzy"
1777 (org-test-with-temp-text "[[target-or-title]]"
1778 (org-element-property
1779 :type
1780 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1781 ;; ... file-type link with search option.
1782 (should
1783 (equal
1784 '(("file" "projects.org" "*task title"))
1785 (org-test-with-temp-text "[[file:projects.org::*task title]]"
1786 (org-element-map (org-element-parse-buffer) 'link
1787 (lambda (l) (list (org-element-property :type l)
1788 (org-element-property :path l)
1789 (org-element-property :search-option l)))))))
1790 ;; ... file-type link with application...
1791 (should
1792 (equal
1793 '("file" "projects.org" "emacs")
1794 (org-test-with-temp-text "[[file+emacs:projects.org]]"
1795 (let ((l (org-element-context)))
1796 (list (org-element-property :type l)
1797 (org-element-property :path l)
1798 (org-element-property :application l))))))
1799 ;; ... `:path' in a file-type link must be compatible with "file"
1800 ;; scheme in URI syntax, even if Org syntax isn't...
1801 (should
1802 (org-test-with-temp-text-in-file ""
1803 (let ((file (expand-file-name (buffer-file-name))))
1804 (insert (format "[[file://%s]]" file))
1805 (equal (org-element-property :path (org-element-context)) file))))
1806 (should
1807 (org-test-with-temp-text-in-file ""
1808 (let ((file (expand-file-name (buffer-file-name))))
1809 (insert (format "[[file:%s]]" file))
1810 (equal (org-element-property :path (org-element-context)) file))))
1811 (should
1812 (org-test-with-temp-text-in-file ""
1813 (let ((file (expand-file-name (buffer-file-name))))
1814 (insert (format "[[file:%s]]" file))
1815 (equal (org-element-property :path (org-element-context)) file))))
1816 ;; ... multi-line link.
1817 (should
1818 (equal "ls *.org"
1819 (org-test-with-temp-text "[[shell:ls\n*.org]]"
1820 (org-element-property :path (org-element-context)))))
1821 ;; Plain link.
1822 (should
1823 (org-test-with-temp-text "A link: https://orgmode.org"
1824 (org-element-map (org-element-parse-buffer) 'link 'identity)))
1825 ;; Angular link. Follow RFC 3986.
1826 (should
1827 (eq 'link
1828 (org-test-with-temp-text "A link: <point><https://orgmode.org>"
1829 (org-element-type (org-element-context)))))
1830 (should
1831 (equal "//orgmode.org"
1832 (org-test-with-temp-text "A link: <point><https://orgmode\n.org>"
1833 (org-element-property :path (org-element-context)))))
1834 ;; Link abbreviation.
1835 (should
1836 (equal "https"
1837 (org-test-with-temp-text
1838 "#+LINK: orgmode https://www.orgmode.org/\n[[orgmode:#docs]]"
1839 (progn (org-mode-restart)
1840 (goto-char (1- (point-max)))
1841 (org-element-property :type (org-element-context))))))
1842 ;; Link abbreviation in a secondary string.
1843 (should
1844 (equal "https"
1845 (org-test-with-temp-text
1846 "#+LINK: orgmode https://www.orgmode.org/\n* H [[orgmode:#docs]]"
1847 (progn (org-mode-restart)
1848 (org-element-map (org-element-parse-buffer) 'link
1849 (lambda (link) (org-element-property :type link))
1850 nil t nil t))))))
1853 ;;;; Macro
1855 (ert-deftest test-org-element/macro-parser ()
1856 "Test `macro' parser."
1857 ;; Without arguments.
1858 (should
1859 (org-test-with-temp-text "{{{macro}}}"
1860 (org-element-map (org-element-parse-buffer) 'macro 'identity)))
1861 ;; With arguments.
1862 (should
1863 (org-test-with-temp-text "{{{macro(arg1,arg2)}}}"
1864 (org-element-map (org-element-parse-buffer) 'macro 'identity)))
1865 ;; Properly handle protected commas in arguments...
1866 (should
1867 (= 2
1868 (length
1869 (org-test-with-temp-text "{{{macro(arg1\\,arg1,arg2)}}}"
1870 (org-element-property :args (org-element-context))))))
1871 ;; ... even when last argument ends with a protected comma.
1872 (should
1873 (equal '("C-,")
1874 (org-test-with-temp-text "{{{macro(C-\\,)}}}"
1875 (org-element-property :args (org-element-context)))))
1876 ;; Allow to escape escaping character.
1877 (should
1878 (equal '("C-\\" "")
1879 (org-test-with-temp-text "{{{macro(C-\\\\,)}}}"
1880 (org-element-property :args (org-element-context)))))
1881 ;; No need to escape backslashes elsewhere.
1882 (should
1883 (equal '("\\")
1884 (org-test-with-temp-text "{{{macro(\\)}}}"
1885 (org-element-property :args (org-element-context))))))
1888 ;;;; Node Property
1890 (ert-deftest test-org-element/node-property ()
1891 "Test `node-property' parser."
1892 ;; Standard test.
1893 (should
1894 (equal '("abc" "value")
1895 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:abc: value\n:END:"
1896 (let ((element (org-element-at-point)))
1897 (list (org-element-property :key element)
1898 (org-element-property :value element))))))
1899 ;; Value should be trimmed.
1900 (should
1901 (equal "value"
1902 (org-test-with-temp-text
1903 "* H\n:PROPERTIES:\n<point>:abc: value \n:END:"
1904 (org-element-property :value (org-element-at-point)))))
1905 ;; A node property requires to be wrapped within a property drawer.
1906 (should-not
1907 (eq 'node-property
1908 (org-test-with-temp-text ":abc: value"
1909 (org-element-type (org-element-at-point)))))
1910 ;; Accept empty properties.
1911 (should
1912 (equal '(("foo" "value") ("bar" ""))
1913 (org-test-with-temp-text "* H\n:PROPERTIES:\n:foo: value\n:bar:\n:END:"
1914 (org-element-map (org-element-parse-buffer) 'node-property
1915 (lambda (p)
1916 (list (org-element-property :key p)
1917 (org-element-property :value p))))))))
1920 ;;;; Paragraph
1922 (ert-deftest test-org-element/paragraph-parser ()
1923 "Test `paragraph' parser."
1924 ;; Standard test.
1925 (should
1926 (org-test-with-temp-text "Paragraph"
1927 (org-element-map (org-element-parse-buffer) 'paragraph 'identity nil t)))
1928 ;; Property find end of a paragraph stuck to another element.
1929 (should
1930 (eq ?#
1931 (org-test-with-temp-text "Paragraph\n# Comment"
1932 (org-element-map (org-element-parse-buffer) 'paragraph
1933 (lambda (p) (char-after (org-element-property :end p)))
1934 nil t))))
1935 ;; Include ill-formed Keywords.
1936 (should
1937 (org-test-with-temp-text "#+wrong_keyword something"
1938 (org-element-map (org-element-parse-buffer) 'paragraph 'identity)))
1939 ;; Include incomplete-drawers.
1940 (should
1941 (org-test-with-temp-text ":TEST:\nParagraph"
1942 (let ((elem (org-element-at-point)))
1943 (and (eq (org-element-type elem) 'paragraph)
1944 (= (point-max) (org-element-property :end elem))))))
1945 ;; Include incomplete blocks.
1946 (should
1947 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph"
1948 (let ((elem (org-element-at-point)))
1949 (and (eq (org-element-type elem) 'paragraph)
1950 (= (point-max) (org-element-property :end elem))))))
1951 ;; Include incomplete dynamic blocks.
1952 (should
1953 (org-test-with-temp-text "#+BEGIN: \nParagraph"
1954 (let ((elem (org-element-at-point)))
1955 (and (eq (org-element-type elem) 'paragraph)
1956 (= (point-max) (org-element-property :end elem))))))
1957 ;; Include incomplete latex environments.
1958 (should
1959 (org-test-with-temp-text "\begin{equation}\nParagraph"
1960 (let ((elem (org-element-at-point)))
1961 (and (eq (org-element-type elem) 'paragraph)
1962 (= (point-max) (org-element-property :end elem))))))
1963 (should
1964 (org-test-with-temp-text "Paragraph\n\begin{equation}"
1965 (let ((elem (org-element-at-point)))
1966 (and (eq (org-element-type elem) 'paragraph)
1967 (= (point-max) (org-element-property :end elem))))))
1968 ;; Stop at affiliated keywords.
1969 (should
1970 (org-test-with-temp-text "Paragraph\n#+NAME: test\n| table |"
1971 (let ((elem (org-element-at-point)))
1972 (and (eq (org-element-type elem) 'paragraph)
1973 (not (org-element-property :name elem))
1974 (= (org-element-property :end elem) (line-beginning-position 2))))))
1975 (should
1976 (org-test-with-temp-text
1977 "Paragraph\n#+CAPTION[with short caption]: test\n| table |"
1978 (let ((elem (org-element-at-point)))
1979 (and (eq (org-element-type elem) 'paragraph)
1980 (not (org-element-property :name elem))
1981 (= (org-element-property :end elem) (line-beginning-position 2))))))
1982 ;; Do not steal affiliated keywords from container.
1983 (should
1984 (org-test-with-temp-text "#+ATTR_LATEX: test\n- item<point> 1"
1985 (let ((elem (org-element-at-point)))
1986 (and (eq (org-element-type elem) 'paragraph)
1987 (not (org-element-property :attr_latex elem))
1988 (/= (org-element-property :begin elem) 1)))))
1989 ;; Handle non-empty blank line at the end of buffer.
1990 (should
1991 (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
1992 (= (org-element-property :end (org-element-at-point)) (point-max)))))
1995 ;;;; Plain List
1997 (ert-deftest test-org-element/plain-list-parser ()
1998 "Test `plain-list' parser."
1999 (org-test-with-temp-text "- item"
2000 (should (org-element-map (org-element-parse-buffer) 'plain-list 'identity)))
2001 ;; Blank lines after a list or sub-list belongs to that list.
2002 (should
2003 (= 1
2004 (org-test-with-temp-text "- A\n\n- B\n\nEnd list"
2005 (org-element-property :post-blank (org-element-at-point)))))
2006 (should
2007 (= 1
2008 (org-test-with-temp-text "- A\n\n<point> - B\n\n - C\n\n End sub-list"
2009 (org-element-property :post-blank (org-element-at-point)))))
2010 ;; Blank lines after the list only belong to outer plain list,
2011 ;; however.
2012 (should
2013 (equal
2014 '(t t)
2015 (org-test-with-temp-text "
2016 - outer
2017 - inner
2019 Outside list"
2020 (let ((endings (org-element-map (org-element-parse-buffer) 'plain-list
2021 (lambda (pl) (org-element-property :end pl)))))
2022 (list
2023 ;; Move to ending of outer list.
2024 (progn (goto-char (car endings)) (looking-at "Outside list"))
2025 ;; Move to ending of inner list.
2026 (progn (goto-char (nth 1 endings)) (looking-at "^$")))))))
2027 ;; Correctly compute end of list if it doesn't end at a line
2028 ;; beginning.
2029 (should
2030 (org-test-with-temp-text "- list\n \n "
2031 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2034 ;;;; Planning
2036 (ert-deftest test-org-element/planning-parser ()
2037 "Test `planning' parser."
2038 ;; Test various keywords.
2039 (should
2040 (org-element-property
2041 :closed
2042 (org-test-with-temp-text "* H\n<point>CLOSED: [2012-03-29 thu.]"
2043 (org-element-at-point))))
2044 (should
2045 (org-element-property
2046 :deadline
2047 (org-test-with-temp-text "* H\n<point>DEADLINE: <2012-03-29 thu.>"
2048 (org-element-at-point))))
2049 (should
2050 (org-element-property
2051 :scheduled
2052 (org-test-with-temp-text "* H\n<point>SCHEDULED: <2012-03-29 thu.>"
2053 (org-element-at-point))))
2054 ;; Planning line only exists right after a headline.
2055 (should-not
2056 (eq 'planning
2057 (org-test-with-temp-text "DEADLINE: <2012-03-29 thu.>"
2058 (org-element-type (org-element-at-point)))))
2059 (should-not
2060 (eq 'planning
2061 (org-test-with-temp-text
2062 "* H\n# Comment\n<point>DEADLINE: <2012-03-29 thu.>"
2063 (org-element-type (org-element-at-point)))))
2064 (should-not
2065 (eq 'planning
2066 (org-test-with-temp-text
2067 "* H\n\n<point>DEADLINE: <2012-03-29 thu.>"
2068 (org-element-type (org-element-at-point))))))
2071 ;;;; Property Drawer
2073 (ert-deftest test-org-element/property-drawer-parser ()
2074 "Test `property-drawer' parser."
2075 ;; Standard test.
2076 (should
2077 (eq 'property-drawer
2078 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value\n:END:"
2079 (org-element-type (org-element-at-point)))))
2080 (should
2081 (eq 'property-drawer
2082 (org-test-with-temp-text
2083 "* H\nDEADLINE: <2014-03-04 tue.>\n<point>:PROPERTIES:\n:prop: value\n:END:"
2084 (org-element-type (org-element-at-point)))))
2085 ;; Allow properties without value and no property at all.
2086 (should
2087 (eq 'property-drawer
2088 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop:\n:END:"
2089 (org-element-type (org-element-at-point)))))
2090 (should
2091 (eq 'property-drawer
2092 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:"
2093 (org-element-type (org-element-at-point)))))
2094 ;; Ignore incomplete drawer, drawer at a wrong location or with
2095 ;; wrong contents.
2096 (should-not
2097 (eq 'property-drawer
2098 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value"
2099 (org-element-type (org-element-at-point)))))
2100 (should-not
2101 (eq 'property-drawer
2102 (org-test-with-temp-text
2103 "* H\nParagraph\n<point>:PROPERTIES:\n:prop: value\n:END:"
2104 (org-element-type (org-element-at-point)))))
2105 (should-not
2106 (eq 'property-drawer
2107 (org-test-with-temp-text
2108 "* H\nParagraph\n<point>:PROPERTIES:\nparagraph\n:END:"
2109 (org-element-type (org-element-at-point)))))
2110 (should-not
2111 (eq 'property-drawer
2112 (org-test-with-temp-text
2113 "* H\n\n<point>:PROPERTIES:\n:prop: value\n:END:"
2114 (org-element-type (org-element-at-point)))))
2115 ;; Handle non-empty blank line at the end of buffer.
2116 (should
2117 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:\n "
2118 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2121 ;;;; Quote Block
2123 (ert-deftest test-org-element/quote-block-parser ()
2124 "Test `quote-block' parser."
2125 ;; Regular test.
2126 (should
2127 (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
2128 (org-element-map (org-element-parse-buffer) 'quote-block 'identity)))
2129 ;; Ignore incomplete block.
2130 (should-not
2131 (org-test-with-temp-text "#+BEGIN_QUOTE"
2132 (org-element-map (org-element-parse-buffer) 'quote-block 'identity nil t)))
2133 ;; Handle non-empty blank line at the end of buffer.
2134 (should
2135 (org-test-with-temp-text "#+BEGIN_QUOTE\nC\n#+END_QUOTE\n "
2136 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2139 ;;;; Radio Target
2141 (ert-deftest test-org-element/radio-target-parser ()
2142 "Test `radio-target' parser."
2143 ;; Standard test.
2144 (should
2145 (eq 'radio-target
2146 (org-test-with-temp-text "<<<radio>>>"
2147 (org-element-type (org-element-context)))))
2148 ;; Radio targets with objects.
2149 (should
2150 (eq 'radio-target
2151 (org-test-with-temp-text "<<<radio \\alpha>>>"
2152 (org-element-type (org-element-context)))))
2153 ;; Radio targets starting with an object.
2154 (should
2155 (eq 'radio-target
2156 (org-test-with-temp-text "<<<\\alpha radio>>>"
2157 (org-element-type (org-element-context)))))
2158 ;; Radio targets cannot begin or end with white space.
2159 (should-not
2160 (eq 'radio-target
2161 (org-test-with-temp-text "<<< radio>>>"
2162 (org-element-type (org-element-context)))))
2163 (should-not
2164 (eq 'radio-target
2165 (org-test-with-temp-text "<<<radio >>>"
2166 (org-element-type (org-element-context))))))
2169 ;;;; Section
2171 (ert-deftest test-org-element/section-parser ()
2172 "Test `section' parser."
2173 ;; Standard test.
2174 (should
2175 (org-test-with-temp-text "* Headline\nText"
2176 (org-element-map (org-element-parse-buffer) 'section 'identity)))
2177 ;; There's a section before the first headline.
2178 (should
2179 (org-test-with-temp-text "Text"
2180 (org-element-map (org-element-parse-buffer) 'section 'identity)))
2181 ;; A section cannot be empty.
2182 (should-not
2183 (org-test-with-temp-text "* Headline 1\n* Headline 2"
2184 (org-element-map (org-element-parse-buffer) 'section 'identity)))
2185 ;; A section doesn't contain sub-trees.
2186 (should-not
2187 (org-test-with-temp-text "* Head\nText\n** Sub-Head"
2188 (org-element-map
2189 (org-element-map (org-element-parse-buffer) 'section 'identity nil t)
2190 'headline 'identity))))
2193 ;;;; Special Block
2195 (ert-deftest test-org-element/special-block-parser ()
2196 "Test `special-block' parser."
2197 ;; Standard test.
2198 (should
2199 (equal "SPECIAL"
2200 (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
2201 (org-element-property :type (org-element-at-point)))))
2202 ;; Special blocks are case sensitive.
2203 (should
2204 (equal "CaSe"
2205 (org-test-with-temp-text "#+BEGIN_CaSe\nText\n#+END_CaSe"
2206 (org-element-property :type (org-element-at-point)))))
2207 ;; Special blocks can contain paragraphs.
2208 (should
2209 (eq 'paragraph
2210 (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
2211 (forward-line)
2212 (org-element-type (org-element-at-point)))))
2213 ;; Ignore incomplete block.
2214 (should-not
2215 (eq 'special-block
2216 (org-test-with-temp-text "#+BEGIN_SPECIAL"
2217 (org-element-type (org-element-at-point)))))
2218 ;; Allow special characters in type.
2219 (should
2220 (equal '(special-block "SPECIAL*")
2221 (org-test-with-temp-text "#+BEGIN_SPECIAL*\nContents\n#+END_SPECIAL*"
2222 (let ((element (org-element-at-point)))
2223 (list (org-element-type element)
2224 (org-element-property :type element))))))
2225 ;; Handle non-empty blank line at the end of buffer.
2226 (should
2227 (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n "
2228 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2231 ;;;; Src Block
2233 (ert-deftest test-org-element/src-block-parser ()
2234 "Test `src-block' parser."
2235 ;; Regular tests.
2236 (should
2237 (org-test-with-temp-text "#+BEGIN_SRC org\nText\n#+END_SRC"
2238 (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
2239 ;; Ignore incomplete block.
2240 (should-not
2241 (org-test-with-temp-text "#+BEGIN_SRC"
2242 (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
2243 ;; Properly un-escape code.
2244 (should
2245 (equal "* Headline\n #+keyword\nText\n"
2246 (org-test-with-temp-text
2247 "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
2248 (org-element-property :value (org-element-at-point)))))
2249 ;; Handle non-empty blank line at the end of buffer.
2250 (should
2251 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\nC\n#+END_SRC\n "
2252 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2255 ;;;; Statistics Cookie
2257 (ert-deftest test-org-element/statistics-cookie ()
2258 "Test `statistics-cookie' parser."
2259 ;; With numbers.
2260 (should
2261 (org-test-with-temp-text "[1/2]"
2262 (org-element-map (org-element-parse-buffer) 'statistics-cookie 'identity)))
2263 ;; With percents.
2264 (should
2265 (org-test-with-temp-text "[33%]"
2266 (org-element-map
2267 (org-element-parse-buffer) 'statistics-cookie 'identity))))
2270 ;;;; Strike Through
2272 (ert-deftest test-org-element/strike-through-parser ()
2273 "Test `strike-through' parser."
2274 ;; Regular test.
2275 (should
2276 (org-test-with-temp-text "+strike-through+"
2277 (org-element-map (org-element-parse-buffer) 'strike-through #'identity)))
2278 ;; Multi-line markup.
2279 (should
2280 (equal
2281 (org-element-contents
2282 (org-test-with-temp-text "+first line\nsecond line+"
2283 (org-element-map
2284 (org-element-parse-buffer) 'strike-through #'identity nil t)))
2285 '("first line\nsecond line"))))
2288 ;;;; Subscript
2290 (ert-deftest test-org-element/subscript-parser ()
2291 "Test `subscript' parser."
2292 ;; Without braces.
2293 (should
2294 (org-test-with-temp-text "a_b"
2295 (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
2296 ;; With braces.
2297 (should
2298 (org-test-with-temp-text "a_{b}"
2299 (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
2300 ;; Multiple subscripts in a paragraph.
2301 (should
2302 (= 2
2303 (org-test-with-temp-text "a_b and c_d"
2304 (length
2305 (org-element-map (org-element-parse-buffer) 'subscript 'identity))))))
2308 ;;;; Superscript
2310 (ert-deftest test-org-element/superscript-parser ()
2311 "Test `superscript' parser."
2312 ;; Without braces.
2313 (should
2314 (org-test-with-temp-text "a^b"
2315 (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
2316 ;; With braces.
2317 (should
2318 (org-test-with-temp-text "a^{b}"
2319 (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
2320 ;; Multiple superscript in a paragraph.
2321 (should
2322 (= 2
2323 (org-test-with-temp-text "a^b and c^d"
2324 (length
2325 (org-element-map
2326 (org-element-parse-buffer) 'superscript 'identity))))))
2329 ;;;; Table
2331 (ert-deftest test-org-element/table-parser ()
2332 "Test `table' parser."
2333 (should
2334 (org-test-with-temp-text "| a |"
2335 (org-element-map (org-element-parse-buffer) 'table 'identity)))
2336 ;; TBLFM keyword is case insensitive.
2337 (should
2338 (org-test-with-temp-text "| a |\n#+tblfm: test"
2339 (org-element-property
2340 :tblfm
2341 (org-element-map (org-element-parse-buffer) 'table 'identity nil t))))
2342 ;; Handle multiple TBLFM lines.
2343 (should
2344 (= 2
2345 (org-test-with-temp-text "| a |\n#+TBLFM: test1\n#+TBLFM: test2"
2346 (length (org-element-property
2347 :tblfm
2348 (org-element-map
2349 (org-element-parse-buffer) 'table 'identity nil t))))))
2350 ;; Handle non-empty blank line at the end of buffer.
2351 (should
2352 (org-test-with-temp-text "| a |\n "
2353 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2356 ;;;; Table Cell
2358 (ert-deftest test-org-element/table-cell-parser ()
2359 "Test `table-cell' parser."
2360 ;; Regular table cell.
2361 (should
2362 (org-test-with-temp-text "| a |"
2363 (org-element-map (org-element-parse-buffer) 'table-cell 'identity)))
2364 ;; Last vertical bar may be omitted.
2365 (should
2366 (org-test-with-temp-text "| a "
2367 (org-element-map (org-element-parse-buffer) 'table-cell 'identity))))
2370 ;;;; Table Row
2372 (ert-deftest test-org-element/table-row-parser ()
2373 "Test `table-row' parser."
2374 (should
2375 (equal '(standard rule)
2376 (org-test-with-temp-text "| a |\n|---|"
2377 (org-element-map
2378 (org-element-parse-buffer) 'table-row
2379 (lambda (row) (org-element-property :type row)))))))
2382 ;;;; Target
2384 (ert-deftest test-org-element/target-parser ()
2385 "Test `target' parser."
2386 (should
2387 (org-test-with-temp-text "<<target>>"
2388 (org-element-map (org-element-parse-buffer) 'target 'identity))))
2391 ;;;; Timestamp
2393 (ert-deftest test-org-element/timestamp ()
2394 "Test `timestamp' parser."
2395 ;; Active timestamp.
2396 (should
2397 (org-test-with-temp-text "<2012-03-29 16:40>"
2398 (eq (org-element-property :type (org-element-context)) 'active)))
2399 (should-not
2400 (org-test-with-temp-text "<2012-03-29 Thu>"
2401 (let ((timestamp (org-element-context)))
2402 (or (org-element-property :hour-start timestamp)
2403 (org-element-property :minute-start timestamp)))))
2404 (should
2405 (equal '(2012 3 29 16 40)
2406 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
2407 (let ((object (org-element-context)))
2408 (list (org-element-property :year-start object)
2409 (org-element-property :month-start object)
2410 (org-element-property :day-start object)
2411 (org-element-property :hour-start object)
2412 (org-element-property :minute-start object))))))
2413 ;; Inactive timestamp.
2414 (should
2415 (org-test-with-temp-text "[2012-03-29 Thu 16:40]"
2416 (eq (org-element-property :type (org-element-context)) 'inactive)))
2417 ;; Time range.
2418 (should
2419 (equal '(2012 3 29 16 40 7 30)
2420 (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
2421 (let ((object (org-element-context)))
2422 (list (org-element-property :year-end object)
2423 (org-element-property :month-end object)
2424 (org-element-property :day-end object)
2425 (org-element-property :hour-end object)
2426 (org-element-property :minute-end object)
2427 (org-element-property :hour-start object)
2428 (org-element-property :minute-start object))))))
2429 (should
2430 (eq 'active-range
2431 (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
2432 (org-element-property :type (org-element-context)))))
2433 ;; Date range.
2434 (should
2435 (org-test-with-temp-text "[2012-03-29 Thu 16:40]--[2012-03-29 Thu 16:41]"
2436 (eq (org-element-property :type (org-element-context)) 'inactive-range)))
2437 (should-not
2438 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
2439 (let ((timestamp (org-element-context)))
2440 (or (org-element-property :hour-end timestamp)
2441 (org-element-property :minute-end timestamp)))))
2442 ;; With repeater, warning delay and both.
2443 (should
2444 (eq 'catch-up
2445 (org-test-with-temp-text "<2012-03-29 Thu ++1y>"
2446 (org-element-property :repeater-type (org-element-context)))))
2447 (should
2448 (eq 'first
2449 (org-test-with-temp-text "<2012-03-29 Thu --1y>"
2450 (org-element-property :warning-type (org-element-context)))))
2451 (should
2452 (equal '(cumulate all)
2453 (org-test-with-temp-text "<2012-03-29 Thu +1y -1y>"
2454 (let ((ts (org-element-context)))
2455 (list (org-element-property :repeater-type ts)
2456 (org-element-property :warning-type ts)))))))
2459 ;;;; Underline
2461 (ert-deftest test-org-element/underline-parser ()
2462 "Test `underline' parser."
2463 ;; Regular test.
2464 (should
2465 (org-test-with-temp-text "_underline_"
2466 (org-element-map (org-element-parse-buffer) 'underline #'identity)))
2467 ;; Multi-line markup.
2468 (should
2469 (equal
2470 (org-element-contents
2471 (org-test-with-temp-text "_first line\nsecond line_"
2472 (org-element-map
2473 (org-element-parse-buffer) 'underline #'identity nil t)))
2474 '("first line\nsecond line"))))
2477 ;;;; Verbatim
2479 (ert-deftest test-org-element/verbatim-parser ()
2480 "Test `verbatim' parser."
2481 ;; Regular test.
2482 (should
2483 (org-test-with-temp-text "=verbatim="
2484 (org-element-map (org-element-parse-buffer) 'verbatim #'identity)))
2485 ;; Multi-line markup.
2486 (should
2487 (equal
2488 (org-element-property
2489 :value
2490 (org-test-with-temp-text "=first line\nsecond line="
2491 (org-element-map (org-element-parse-buffer) 'verbatim #'identity nil t)))
2492 "first line\nsecond line")))
2495 ;;;; Verse Block
2497 (ert-deftest test-org-element/verse-block-parser ()
2498 "Test `verse-block' parser."
2499 ;; Standard test.
2500 (should
2501 (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
2502 (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
2503 ;; Ignore case.
2504 (should
2505 (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
2506 (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
2507 ;; Parse objects in verse blocks.
2508 (should
2509 (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
2510 (org-element-map (org-element-parse-buffer) 'entity 'identity)))
2511 ;; Ignore incomplete verse block.
2512 (should-not
2513 (org-test-with-temp-text "#+BEGIN_VERSE"
2514 (org-element-map (org-element-parse-buffer) 'verse-block 'identity nil t)))
2515 ;; Handle non-empty blank line at the end of buffer.
2516 (should
2517 (org-test-with-temp-text "#+BEGIN_VERSE\nC\n#+END_VERSE\n "
2518 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2522 ;;; Test Interpreters.
2524 (ert-deftest test-org-element/interpret-data ()
2525 "Test `org-element-interpret-data' specifications."
2526 ;; Interpret simple affiliated keywords.
2527 (should
2528 (equal
2529 (org-element-interpret-data
2530 '(org-data nil (paragraph (:name "para") "Paragraph")))
2531 "#+name: para\nParagraph\n"))
2532 ;; Interpret multiple affiliated keywords.
2533 (should
2534 (equal
2535 (org-element-interpret-data
2536 '(org-data nil (paragraph (:attr_ascii ("line2" "line1")) "Paragraph")))
2537 "#+attr_ascii: line1\n#+attr_ascii: line2\nParagraph\n"))
2538 ;; Interpret parsed affiliated keywords.
2539 (should
2540 (equal
2541 (org-element-interpret-data
2542 '(org-data nil (paragraph (:caption (("caption"))) "Paragraph")))
2543 "#+caption: caption\nParagraph\n"))
2544 ;; Interpret dual affiliated keywords.
2545 (should
2546 (equal
2547 (org-element-interpret-data
2548 '(org-data nil (paragraph (:caption ((("long") "short"))) "Paragraph")))
2549 "#+caption[short]: long\nParagraph\n"))
2550 ;; Interpret multiple parsed dual keywords.
2551 (should
2552 (equal
2553 (org-element-interpret-data
2554 '(org-data nil (paragraph
2555 (:caption ((("l2") "s2") (("l1") "s1"))) "Paragraph")))
2556 "#+caption[s1]: l1\n#+caption[s2]: l2\nParagraph\n"))
2557 ;; Pseudo objects and elements are transparent.
2558 (should
2559 (equal "A B"
2560 (org-trim
2561 (org-element-interpret-data
2562 '(paragraph nil (pseudo-object (:post-blank 1) "A") "B")))))
2563 (should
2564 (equal "A\n\nB\n"
2565 (org-element-interpret-data
2566 '(center nil
2567 (pseudo-element (:post-blank 1) (paragraph nil "A"))
2568 (paragraph nil "B"))))))
2570 (ert-deftest test-org-element/center-block-interpreter ()
2571 "Test center block interpreter."
2572 (should
2573 (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
2574 "#+begin_center\nTest\n#+end_center\n")))
2576 (ert-deftest test-org-element/drawer-interpreter ()
2577 "Test drawer interpreter."
2578 (should
2579 (equal (org-test-parse-and-interpret ":TEST:\nTest\n:END:")
2580 ":TEST:\nTest\n:END:\n")))
2582 (ert-deftest test-org-element/dynamic-block-interpreter ()
2583 "Test dynamic block interpreter."
2584 (should
2585 (equal (org-test-parse-and-interpret
2586 "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
2587 "#+begin: myblock :parameter value1\nTest\n#+end:\n")))
2589 (ert-deftest test-org-element/footnote-definition-interpreter ()
2590 "Test footnote definition interpreter."
2591 (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n"))
2592 ;; Handle `:pre-blank' in definitions.
2593 (should
2594 (equal (org-test-parse-and-interpret "[fn:1]\nparagraph")
2595 "[fn:1]\nparagraph\n"))
2596 (should
2597 (equal (org-test-parse-and-interpret "[fn:1]\n\nparagraph")
2598 "[fn:1]\n\nparagraph\n")))
2600 (ert-deftest test-org-element/headline-interpreter ()
2601 "Test headline and section interpreters."
2602 ;; 1. Standard test.
2603 (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
2604 ;; 2. With TODO keywords.
2605 (should
2606 (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
2607 (org-test-parse-and-interpret "* TODO Headline"))
2608 "* TODO Headline\n"))
2609 ;; 3. With tags...
2611 ;; 3.1. ... and a positive `org-tags-column' value.
2612 (should
2613 (equal (let ((org-tags-column 20))
2614 (org-test-parse-and-interpret "* Headline :tag:"))
2615 "* Headline :tag:\n"))
2616 ;; 3.2. ... and a negative `org-tags-column' value.
2617 (should
2618 (equal (let ((org-tags-column -20))
2619 (org-test-parse-and-interpret "* Headline :tag:"))
2620 "* Headline :tag:\n"))
2621 ;; 3.3. ... and a null `org-tags-column' value.
2622 (should
2623 (equal (let ((org-tags-column 0))
2624 (org-test-parse-and-interpret "* Headline :tag:"))
2625 "* Headline :tag:\n"))
2626 ;; 4. With priority cookie.
2627 (should
2628 (equal (org-test-parse-and-interpret "* [#B] Headline")
2629 "* [#B] Headline\n"))
2630 ;; 5. With comment keyword.
2631 (should
2632 (equal (let ((org-comment-string "COMMENT"))
2633 (org-test-parse-and-interpret "* COMMENT Headline"))
2634 "* COMMENT Headline\n"))
2635 ;; 6. Keep same number of blank lines before body.
2636 (should
2637 (equal (org-test-parse-and-interpret
2638 "* Headline\n\n\nText after two blank lines.")
2639 "* Headline\n\n\nText after two blank lines.\n"))
2640 ;; 8. Preserve `org-odd-levels-only' state.
2641 (should
2642 (equal "* H\n*** H2\n"
2643 (let ((org-odd-levels-only t))
2644 (org-test-parse-and-interpret "* H\n*** H2")))))
2646 (ert-deftest test-org-element/inlinetask-interpreter ()
2647 "Test inlinetask interpretation."
2648 (when (featurep 'org-inlinetask)
2649 (let ((org-inlinetask-min-level 15))
2650 ;; 1. Regular inlinetask.
2651 (should (equal (org-test-parse-and-interpret
2652 "*************** Task\nTest\n*************** END")
2653 "*************** Task\nTest\n*************** end\n"))
2654 ;; 2. Degenerate inlinetask.
2655 (should (equal (org-test-parse-and-interpret "*************** Task")
2656 "*************** Task\n"))
2657 ;; 3. Prefer degenerate form when there are no contents.
2658 (should (equal (org-test-parse-and-interpret
2659 "*************** Task\n*************** end")
2660 "*************** Task\n"))
2661 ;; 4. With TODO keywords.
2662 (should
2663 (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
2664 (org-test-parse-and-interpret "*************** TODO Task"))
2665 "*************** TODO Task\n"))
2666 ;; 5. With tags...
2668 ;; 5.1. ... and a positive `org-tags-column' value.
2669 (should
2670 (equal (let ((org-tags-column 30))
2671 (org-test-parse-and-interpret "*************** Task :tag:"))
2672 "*************** Task :tag:\n"))
2673 ;; 5.2. ... and a negative `org-tags-column' value.
2674 (should
2675 (equal (let ((org-tags-column -30))
2676 (org-test-parse-and-interpret "*************** Task :tag:"))
2677 "*************** Task :tag:\n"))
2678 ;; 5.3. ... and a null `org-tags-column' value.
2679 (should
2680 (equal (let ((org-tags-column 0))
2681 (org-test-parse-and-interpret "*************** Task :tag:"))
2682 "*************** Task :tag:\n"))
2683 ;; 6. With priority cookie.
2684 (should
2685 (equal (org-test-parse-and-interpret "*************** [#B] Task")
2686 "*************** [#B] Task\n")))))
2688 (ert-deftest test-org-element/plain-list-interpreter ()
2689 "Test plain-list and item interpreters."
2690 (let ((org-list-two-spaces-after-bullet-regexp nil))
2691 ;; Unordered list.
2692 (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
2693 ;; Description list.
2694 (should
2695 (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
2696 ;; Ordered list.
2697 (should
2698 (equal (let ((org-plain-list-ordered-item-terminator t))
2699 (org-test-parse-and-interpret "1. Item"))
2700 "1. Item\n"))
2701 (should
2702 (equal (let ((org-plain-list-ordered-item-terminator ?\)))
2703 (org-test-parse-and-interpret "1) Item"))
2704 "1) Item\n"))
2705 ;; Ordered list with counter.
2706 (should
2707 (equal (let ((org-plain-list-ordered-item-terminator t))
2708 (org-test-parse-and-interpret "1. [@5] Item"))
2709 "5. [@5] Item\n"))
2710 ;; List with check-boxes.
2711 (should
2712 (equal (org-test-parse-and-interpret
2713 "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3")
2714 "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3\n"))
2715 ;; Item not starting with a paragraph.
2716 (should
2717 (equal (org-test-parse-and-interpret "-\n | a | b |")
2718 "- \n | a | b |\n"))
2719 ;; Handle `:pre-blank' in items.
2720 (should
2721 (equal (org-test-parse-and-interpret "-\n paragraph")
2722 "- \n paragraph\n"))
2723 (should
2724 (equal (org-test-parse-and-interpret "-\n\n paragraph")
2725 "- \n\n paragraph\n"))
2726 ;; Special case: correctly handle "*" bullets.
2727 (should (org-test-parse-and-interpret " * item"))
2728 ;; Special case: correctly handle empty items.
2729 (should (org-test-parse-and-interpret "-"))))
2731 (ert-deftest test-org-element/quote-block-interpreter ()
2732 "Test quote block interpreter."
2733 (should (equal (org-test-parse-and-interpret
2734 "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
2735 "#+begin_quote\nTest\n#+end_quote\n")))
2737 (ert-deftest test-org-element/special-block-interpreter ()
2738 "Test special block interpreter."
2739 (should (equal (org-test-parse-and-interpret
2740 "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
2741 "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n")))
2743 (ert-deftest test-org-element/babel-call-interpreter ()
2744 "Test Babel call interpreter."
2745 ;; Without argument.
2746 (should (equal (org-test-parse-and-interpret "#+CALL: test()")
2747 "#+call: test()\n"))
2748 ;; With argument.
2749 (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
2750 "#+call: test(x=2)\n"))
2751 ;; With header arguments.
2752 (should (equal (org-test-parse-and-interpret
2753 "#+CALL: test[:results output]() :results html")
2754 "#+call: test[:results output]() :results html\n")))
2756 (ert-deftest test-org-element/clock-interpreter ()
2757 "Test clock interpreter."
2758 ;; Running clock.
2759 (should
2760 (string-match
2761 "CLOCK: \\[2012-01-01 .* 00:01\\]"
2762 (org-test-parse-and-interpret "CLOCK: [2012-01-01 sun. 00:01]")))
2763 ;; Closed clock.
2764 (should
2765 (string-match
2766 "CLOCK: \\[2012-01-01 .* 00:01\\]--\\[2012-01-01 .* 00:02\\] => 0:01"
2767 (org-test-parse-and-interpret "
2768 CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))))
2770 (ert-deftest test-org-element/comment-interpreter ()
2771 "Test comment interpreter."
2772 ;; Regular comment.
2773 (should (equal (org-test-parse-and-interpret "# Comment") "# Comment\n"))
2774 ;; Inline comment.
2775 (should (equal (org-test-parse-and-interpret " # Comment")
2776 "# Comment\n"))
2777 ;; Preserve indentation.
2778 (should (equal (org-test-parse-and-interpret " # No blank\n# One blank")
2779 "# No blank\n# One blank\n")))
2781 (ert-deftest test-org-element/comment-block-interpreter ()
2782 "Test comment block interpreter."
2783 (should (equal (org-test-parse-and-interpret
2784 "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
2785 "#+begin_comment\nTest\n#+end_comment\n"))
2786 ;; Accept missing final newline in value.
2787 (should
2788 (equal "#+begin_comment\nTest\n#+end_comment\n"
2789 (org-element-interpret-data '(comment-block (:value "Test"))))))
2791 (ert-deftest test-org-element/diary-sexp ()
2792 "Test diary-sexp interpreter."
2793 (should
2794 (equal
2795 (org-test-parse-and-interpret
2796 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old")
2797 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old\n")))
2799 (ert-deftest test-org-element/example-block-interpreter ()
2800 "Test example block interpreter."
2801 ;; Without switches.
2802 (should (equal (org-test-parse-and-interpret
2803 "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE")
2804 "#+begin_example\nTest\n#+end_example\n"))
2805 ;; With switches.
2806 (should
2807 (equal (org-test-parse-and-interpret
2808 "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE")
2809 "#+begin_example -n -k\n(+ 1 1)\n#+end_example\n"))
2810 ;; Preserve code escaping.
2811 (should
2812 (equal
2813 (org-test-parse-and-interpret
2814 "#+BEGIN_EXAMPLE\n,* Headline\n,#+KEYWORD: value\nText\n#+END_EXAMPLE")
2815 "#+begin_example\n,* Headline\n,#+KEYWORD: value\nText\n#+end_example\n"))
2816 ;; Accept missing final newline in value.
2817 (should
2818 (equal
2819 "#+begin_example\nTest\n#+end_example\n"
2820 (org-element-interpret-data '(example-block (:value "Test"))))))
2822 (ert-deftest test-org-element/export-block-interpreter ()
2823 "Test export block interpreter."
2824 (should (equal (org-test-parse-and-interpret
2825 "#+begin_export HTML\nTest\n#+end_export")
2826 "#+begin_export HTML\nTest\n#+end_export\n")))
2828 (ert-deftest test-org-element/fixed-width-interpreter ()
2829 "Test fixed width interpreter."
2830 ;; Standard test.
2831 (should (equal (org-test-parse-and-interpret ": Test") ": Test\n"))
2832 ;; Preserve indentation.
2833 (should (equal (org-test-parse-and-interpret ": 2 blanks\n: 1 blank")
2834 ": 2 blanks\n: 1 blank\n"))
2835 ;; Remove last newline character
2836 (should
2837 (equal (org-element-fixed-width-interpreter
2838 '(fixed-width (:value "Test\n")) nil)
2839 ": Test"))
2840 (should
2841 (equal (org-element-fixed-width-interpreter
2842 '(fixed-width (:value "Test")) nil)
2843 ": Test"))
2844 ;; Handle empty string.
2845 (should
2846 (equal (org-element-fixed-width-interpreter
2847 '(fixed-width (:value "")) nil)
2848 ""))
2849 ;; Handle nil value.
2850 (should-not
2851 (org-element-fixed-width-interpreter
2852 '(fixed-width (:value nil)) nil)))
2854 (ert-deftest test-org-element/horizontal-rule-interpreter ()
2855 "Test horizontal rule interpreter."
2856 (should (equal (org-test-parse-and-interpret "-------") "-----\n")))
2858 (ert-deftest test-org-element/keyword-interpreter ()
2859 "Test keyword interpreter."
2860 (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
2861 "#+keyword: value\n")))
2863 (ert-deftest test-org-element/latex-environment-interpreter ()
2864 "Test latex environment interpreter."
2865 (should (equal (org-test-parse-and-interpret
2866 "\\begin{equation}\n1+1=2\n\\end{equation}")
2867 "\\begin{equation}\n1+1=2\n\\end{equation}\n"))
2868 (should (equal (org-test-parse-and-interpret
2869 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}")
2870 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}\n")))
2872 (ert-deftest test-org-element/planning-interpreter ()
2873 "Test planning interpreter."
2874 (should
2875 (string-match
2876 "\\* Headline
2877 DEADLINE: <2012-03-29 .*?> SCHEDULED: <2012-03-29 .*?> CLOSED: \\[2012-03-29 .*?\\]"
2878 (org-test-parse-and-interpret
2879 "* Headline
2880 DEADLINE: <2012-03-29 thu.> SCHEDULED: <2012-03-29 thu.> CLOSED: [2012-03-29 thu.]"))))
2882 (ert-deftest test-org-element/property-drawer-interpreter ()
2883 "Test property drawer interpreter."
2884 (should (equal (let ((org-property-format "%-10s %s"))
2885 (org-test-parse-and-interpret
2886 "* H\n:PROPERTIES:\n:prop: value\n:END:"))
2887 "* H\n:PROPERTIES:\n:prop: value\n:END:\n")))
2889 (ert-deftest test-org-element/src-block-interpreter ()
2890 "Test src block interpreter."
2891 ;; With arguments.
2892 (should
2893 (equal (let ((org-edit-src-content-indentation 2)
2894 (org-src-preserve-indentation nil))
2895 (org-test-parse-and-interpret
2896 "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC"))
2897 "#+begin_src emacs-lisp :results silent\n (+ 1 1)\n#+end_src\n"))
2898 ;; With switches.
2899 (should
2900 (equal (let ((org-edit-src-content-indentation 2)
2901 (org-src-preserve-indentation nil))
2902 (org-test-parse-and-interpret
2903 "#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
2904 "#+begin_src emacs-lisp -n -k\n (+ 1 1)\n#+end_src\n"))
2905 ;; Preserve code escaping.
2906 (should
2907 (equal
2908 (let ((org-edit-src-content-indentation 2)
2909 (org-src-preserve-indentation nil))
2910 (org-test-parse-and-interpret
2911 "#+BEGIN_SRC org\n,* Headline\n,#+KEYWORD: value\nText\n#+END_SRC"))
2912 "#+begin_src org\n ,* Headline\n ,#+KEYWORD: value\n Text\n#+end_src\n"))
2913 ;; Do not apply `org-edit-src-content-indentation' when preserving
2914 ;; indentation.
2915 (should
2916 (equal (let ((org-edit-src-content-indentation 2)
2917 (org-src-preserve-indentation t))
2918 (org-test-parse-and-interpret
2919 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"))
2920 "#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src\n"))
2921 (should
2922 (equal (let ((org-edit-src-content-indentation 2)
2923 (org-src-preserve-indentation nil))
2924 (org-test-parse-and-interpret
2925 "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"))
2926 "#+begin_src emacs-lisp -i\n(+ 1 1)\n#+end_src\n"))
2927 ;; Accept missing final newline in value.
2928 (should
2929 (equal
2930 "#+begin_src emacs-lisp\n Test\n#+end_src\n"
2931 (let ((org-edit-src-content-indentation 2)
2932 (org-src-preserve-indentation nil))
2933 (org-element-interpret-data
2934 '(src-block (:language "emacs-lisp" :value "Test")))))))
2936 (ert-deftest test-org-element/table-interpreter ()
2937 "Test table, table-row and table-cell interpreters."
2938 ;; 1. Simple table.
2939 (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
2940 "| a | b |\n| c | d |\n"))
2941 ;; 2. With horizontal rules.
2942 (should (equal (org-test-parse-and-interpret
2943 "| a | b |\n|---+---|\n| c | d |")
2944 "| a | b |\n|---+---|\n| c | d |\n"))
2945 ;; 3. With meta-data.
2946 (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
2947 "| / | < | > |\n| * | 1 | 2 |\n"))
2948 ;; 4. With a formula.
2949 (should
2950 (equal (org-test-parse-and-interpret
2951 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)")
2952 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)\n"))
2953 ;; 5. With multiple formulas.
2954 (should
2955 (equal (org-test-parse-and-interpret
2956 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2")
2957 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2\n")))
2959 (ert-deftest test-org-element/timestamp-interpreter ()
2960 "Test timestamp interpreter."
2961 ;; Active.
2962 (should
2963 (string-match "<2012-03-29 .* 16:40>"
2964 (org-test-parse-and-interpret "<2012-03-29 thu. 16:40>")))
2965 (should
2966 (string-match "<2012-03-29 .* 16:40>"
2967 (org-element-timestamp-interpreter
2968 '(timestamp
2969 (:type active :year-start 2012 :month-start 3 :day-start 29
2970 :hour-start 16 :minute-start 40)) nil)))
2971 ;; Inactive.
2972 (should
2973 (string-match "\\[2012-03-29 .* 16:40\\]"
2974 (org-test-parse-and-interpret "[2012-03-29 thu. 16:40]")))
2975 (should
2976 (string-match
2977 "\\[2012-03-29 .* 16:40\\]"
2978 (org-element-timestamp-interpreter
2979 '(timestamp
2980 (:type inactive :year-start 2012 :month-start 3 :day-start 29
2981 :hour-start 16 :minute-start 40)) nil)))
2982 ;; Active range.
2983 (should
2984 (string-match "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
2985 (org-test-parse-and-interpret
2986 "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>")))
2987 (should
2988 (string-match
2989 "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
2990 (org-element-timestamp-interpreter
2991 '(timestamp
2992 (:type active-range :year-start 2012 :month-start 3 :day-start 29
2993 :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
2994 :day-end 29 :hour-end 16 :minute-end 41)) nil)))
2995 ;; Inactive range.
2996 (should
2997 (string-match "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
2998 (org-test-parse-and-interpret
2999 "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]")))
3000 (should
3001 (string-match
3002 "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
3003 (org-element-timestamp-interpreter
3004 '(timestamp
3005 (:type inactive-range :year-start 2012 :month-start 3 :day-start 29
3006 :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
3007 :day-end 29 :hour-end 16 :minute-end 41)) nil)))
3008 ;; Diary.
3009 (should (equal (org-test-parse-and-interpret "<%%diary-float t 4 2>")
3010 "<%%diary-float t 4 2>\n"))
3011 ;; Timestamp with repeater interval, with delay, with both.
3012 (should
3013 (string-match "<2012-03-29 .* \\+1y>"
3014 (org-test-parse-and-interpret "<2012-03-29 thu. +1y>")))
3015 (should
3016 (string-match
3017 "<2012-03-29 .* \\+1y>"
3018 (org-element-timestamp-interpreter
3019 '(timestamp
3020 (:type active :year-start 2012 :month-start 3 :day-start 29
3021 :repeater-type cumulate :repeater-value 1 :repeater-unit year))
3022 nil)))
3023 (should
3024 (string-match
3025 "<2012-03-29 .* -1y>"
3026 (org-element-timestamp-interpreter
3027 '(timestamp
3028 (:type active :year-start 2012 :month-start 3 :day-start 29
3029 :warning-type all :warning-value 1 :warning-unit year))
3030 nil)))
3031 (should
3032 (string-match
3033 "<2012-03-29 .* \\+1y -1y>"
3034 (org-element-timestamp-interpreter
3035 '(timestamp
3036 (:type active :year-start 2012 :month-start 3 :day-start 29
3037 :warning-type all :warning-value 1 :warning-unit year
3038 :repeater-type cumulate :repeater-value 1 :repeater-unit year))
3039 nil)))
3040 ;; Timestamp range with repeater interval
3041 (should
3042 (string-match "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
3043 (org-test-parse-and-interpret
3044 "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>")))
3045 (should
3046 (string-match
3047 "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
3048 (org-element-timestamp-interpreter
3049 '(timestamp
3050 (:type active-range :year-start 2012 :month-start 3 :day-start 29
3051 :year-end 2012 :month-end 3 :day-end 30 :repeater-type cumulate
3052 :repeater-value 1 :repeater-unit year))
3053 nil))))
3055 (ert-deftest test-org-element/verse-block-interpreter ()
3056 "Test verse block interpretation."
3057 (should
3058 (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
3059 "#+begin_verse\nTest\n#+end_verse\n")))
3061 (ert-deftest test-org-element/bold-interpreter ()
3062 "Test bold interpreter."
3063 (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))
3065 (ert-deftest test-org-element/code-interpreter ()
3066 "Test code interpreter."
3067 (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))
3069 (ert-deftest test-org-element/entity-interpreter ()
3070 "Test entity interpreter."
3071 ;; 1. Without brackets.
3072 (should
3073 (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
3074 ;; 2. With brackets.
3075 (should
3076 (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))
3078 (ert-deftest test-org-element/export-snippet-interpreter ()
3079 "Test export snippet interpreter."
3080 (should (equal (org-test-parse-and-interpret "@@back-end:contents@@")
3081 "@@back-end:contents@@\n")))
3083 (ert-deftest test-org-element/footnote-reference-interpreter ()
3084 "Test footnote reference interpreter."
3085 ;; Regular reference.
3086 (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
3087 ;; Named reference.
3088 (should (equal (org-test-parse-and-interpret "Text[fn:label]")
3089 "Text[fn:label]\n"))
3090 ;; Inline reference.
3091 (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
3092 "Text[fn:label:def]\n"))
3093 ;; Anonymous reference.
3094 (should (equal (org-test-parse-and-interpret "Text[fn::def]")
3095 "Text[fn::def]\n")))
3097 (ert-deftest test-org-element/inline-babel-call-interpreter ()
3098 "Test inline babel call interpreter."
3099 ;; Without arguments.
3100 (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
3101 ;; With arguments.
3102 (should (equal (org-test-parse-and-interpret "call_test(x=2)")
3103 "call_test(x=2)\n"))
3104 ;; With header arguments.
3105 (should (equal (org-test-parse-and-interpret
3106 "call_test[:results output]()[:results html]")
3107 "call_test[:results output]()[:results html]\n")))
3109 (ert-deftest test-org-element/inline-src-block-interpreter ()
3110 "Test inline src block interpreter."
3111 ;; 1. Without header argument.
3112 (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
3113 "src_emacs-lisp{(+ 1 1)}\n"))
3114 ;; 2. With header arguments.
3115 (should (equal (org-test-parse-and-interpret
3116 "src_emacs-lisp[:results silent]{(+ 1 1)}")
3117 "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))
3119 (ert-deftest test-org-element/italic-interpreter ()
3120 "Test italic interpreter."
3121 (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))
3123 (ert-deftest test-org-element/latex-fragment-interpreter ()
3124 "Test latex fragment interpreter."
3125 (should (equal (org-test-parse-and-interpret "\\command{}") "\\command{}\n"))
3126 (should (equal (org-test-parse-and-interpret "$x$") "$x$\n"))
3127 (should (equal (org-test-parse-and-interpret "$x+y$") "$x+y$\n"))
3128 (should (equal (org-test-parse-and-interpret "$$x+y$$") "$$x+y$$\n"))
3129 (should (equal (org-test-parse-and-interpret "\\(x+y\\)") "\\(x+y\\)\n"))
3130 (should (equal (org-test-parse-and-interpret "\\[x+y\\]") "\\[x+y\\]\n")))
3132 (ert-deftest test-org-element/line-break-interpreter ()
3133 "Test line break interpreter."
3134 (should (equal (org-test-parse-and-interpret "First line \\\\ \nSecond line")
3135 "First line \\\\\nSecond line\n")))
3137 (ert-deftest test-org-element/link-interpreter ()
3138 "Test link interpreter."
3139 ;; Links targeted from a radio target.
3140 (should (equal (let ((org-target-link-regexp "radio-target"))
3141 (org-test-parse-and-interpret "a radio-target"))
3142 "a radio-target\n"))
3143 ;; Links without description.
3144 (should (equal (org-test-parse-and-interpret "[[https://orgmode.org]]")
3145 "[[https://orgmode.org]]\n"))
3146 ;; Links with a description, even one containing a link.
3147 (should (equal (org-test-parse-and-interpret
3148 "[[https://orgmode.org][Org mode]]")
3149 "[[https://orgmode.org][Org mode]]\n"))
3150 (should (equal (org-test-parse-and-interpret
3151 "[[https://orgmode.org][https://orgmode.org]]")
3152 "[[https://orgmode.org][https://orgmode.org]]\n"))
3153 ;; File links.
3154 (should
3155 (equal (org-test-parse-and-interpret "[[file+emacs:todo.org]]")
3156 "[[file+emacs:todo.org]]\n"))
3157 (should
3158 (equal (org-test-parse-and-interpret "[[file:todo.org::*task]]")
3159 "[[file:todo.org::*task]]\n"))
3160 ;; Id links.
3161 (should (equal (org-test-parse-and-interpret "[[id:aaaa]]") "[[id:aaaa]]\n"))
3162 ;; Custom-id links.
3163 (should (equal (org-test-parse-and-interpret "[[#id]]") "[[#id]]\n"))
3164 ;; Code-ref links.
3165 (should (equal (org-test-parse-and-interpret "[[(ref)]]") "[[(ref)]]\n"))
3166 ;; Plain links.
3167 (should (equal (org-test-parse-and-interpret "https://orgmode.org")
3168 "https://orgmode.org\n"))
3169 ;; Angular links.
3170 (should (equal (org-test-parse-and-interpret "<https://orgmode.org>")
3171 "<https://orgmode.org>\n"))
3172 ;; Pathological case: link with a %-sign in description.
3173 (should (equal (org-test-parse-and-interpret "[[file://path][%s]]")
3174 "[[file://path][%s]]\n")))
3176 (ert-deftest test-org-element/macro-interpreter ()
3177 "Test macro interpreter."
3178 ;; 1. Without argument.
3179 (should (equal (org-test-parse-and-interpret "{{{test}}}") "{{{test}}}\n"))
3180 ;; 2. With arguments.
3181 (should (equal (org-test-parse-and-interpret "{{{test(arg1,arg2)}}}")
3182 "{{{test(arg1,arg2)}}}\n")))
3184 (ert-deftest test-org-element/radio-target-interpreter ()
3185 "Test radio target interpreter."
3186 (should (equal (org-test-parse-and-interpret "<<<some text>>>")
3187 "<<<some text>>>\n")))
3189 (ert-deftest test-org-element/statistics-cookie-interpreter ()
3190 "Test statistics cookie interpreter."
3191 ;; 1. Without percent
3192 (should (equal (org-test-parse-and-interpret "[0/1]") "[0/1]\n"))
3193 ;; 2. With percent.
3194 (should (equal (org-test-parse-and-interpret "[66%]") "[66%]\n")))
3196 (ert-deftest test-org-element/strike-through-interpreter ()
3197 "Test strike through interpreter."
3198 (should (equal (org-test-parse-and-interpret "+target+") "+target+\n")))
3200 (ert-deftest test-org-element/subscript-interpreter ()
3201 "Test subscript interpreter."
3202 ;; 1. Without brackets.
3203 (should (equal (org-test-parse-and-interpret "a_b") "a_b\n"))
3204 ;; 2. With brackets.
3205 (should (equal (org-test-parse-and-interpret "a_{b}") "a_{b}\n")))
3207 (ert-deftest test-org-element/superscript-interpreter ()
3208 "Test superscript interpreter."
3209 ;; 1. Without brackets.
3210 (should (equal (org-test-parse-and-interpret "a^b") "a^b\n"))
3211 ;; 2. With brackets.
3212 (should (equal (org-test-parse-and-interpret "a^{b}") "a^{b}\n")))
3214 (ert-deftest test-org-element/target-interpreter ()
3215 "Test target interpreter."
3216 (should (equal (org-test-parse-and-interpret "<<target>>") "<<target>>\n")))
3218 (ert-deftest test-org-element/underline-interpreter ()
3219 "Test underline interpreter."
3220 (should (equal (org-test-parse-and-interpret "_text_") "_text_\n")))
3222 (ert-deftest test-org-element/verbatim-interpreter ()
3223 "Test verbatim interpreter."
3224 (should (equal (org-test-parse-and-interpret "=text=") "=text=\n")))
3228 ;;; Test Granularity
3230 (ert-deftest test-org-element/granularity ()
3231 "Test granularity impact on buffer parsing."
3232 (org-test-with-temp-text "
3233 * Head 1
3234 ** Head 2
3235 #+BEGIN_CENTER
3236 Centered paragraph.
3237 #+END_CENTER
3238 Paragraph \\alpha."
3239 ;; 1.1. Granularity set to `headline' should parse every headline
3240 ;; in buffer, and only them.
3241 (let ((tree (org-element-parse-buffer 'headline)))
3242 (should (= 2 (length (org-element-map tree 'headline 'identity))))
3243 (should-not (org-element-map tree 'paragraph 'identity)))
3244 ;; 1.2. Granularity set to `greater-element' should not enter
3245 ;; greater elements excepted headlines and sections.
3246 (let ((tree (org-element-parse-buffer 'greater-element)))
3247 (should (= 1 (length (org-element-map tree 'center-block 'identity))))
3248 (should (= 1 (length (org-element-map tree 'paragraph 'identity))))
3249 (should-not (org-element-map tree 'entity 'identity)))
3250 ;; 1.3. Granularity set to `element' should enter every
3251 ;; greater-element.
3252 (let ((tree (org-element-parse-buffer 'element)))
3253 (should (= 2 (length (org-element-map tree 'paragraph 'identity))))
3254 (should-not (org-element-map tree 'entity 'identity)))
3255 ;; 1.4. Granularity set to `object' can see everything.
3256 (let ((tree (org-element-parse-buffer 'object)))
3257 (should (= 1 (length (org-element-map tree 'entity 'identity)))))))
3259 (ert-deftest test-org-element/secondary-string-parsing ()
3260 "Test if granularity correctly toggles secondary strings parsing."
3261 ;; With a granularity bigger than `object', no secondary string
3262 ;; should be parsed.
3263 (should
3264 (stringp
3265 (org-test-with-temp-text "* Headline"
3266 (let ((headline
3267 (org-element-map (org-element-parse-buffer 'headline) 'headline
3268 #'identity nil 'first-match)))
3269 (org-element-property :title headline)))))
3270 (should
3271 (stringp
3272 (org-test-with-temp-text "* Headline\n- tag :: item"
3273 (let ((item (org-element-map (org-element-parse-buffer 'element) 'item
3274 #'identity nil 'first-match)))
3275 (org-element-property :tag item)))))
3276 (when (featurep 'org-inlinetask)
3277 (should
3278 (stringp
3279 (let ((org-inlinetask-min-level 15))
3280 (org-test-with-temp-text "*************** Inlinetask"
3281 (let ((inlinetask (org-element-map (org-element-parse-buffer 'element)
3282 'inlinetask
3283 #'identity nil 'first-match)))
3284 (org-element-property :title inlinetask)))))))
3285 ;; With a default granularity, secondary strings should be parsed.
3286 (should
3287 (listp
3288 (org-test-with-temp-text "* Headline"
3289 (let ((headline
3290 (org-element-map (org-element-parse-buffer) 'headline
3291 #'identity nil 'first-match)))
3292 (org-element-property :title headline)))))
3293 ;; `org-element-at-point' should never parse a secondary string.
3294 (should-not
3295 (listp
3296 (org-test-with-temp-text "* Headline"
3297 (org-element-property :title (org-element-at-point)))))
3298 ;; Preserve current local variables when parsing a secondary string.
3299 (should
3300 (let ((org-entities nil)
3301 (org-entities-user nil))
3302 (org-test-with-temp-text "
3303 #+CAPTION: \\foo
3304 Text
3305 # Local Variables:
3306 # org-entities-user: ((\"foo\"))
3307 # End:"
3308 (let ((safe-local-variable-values '((org-entities-user . (("foo"))))))
3309 (hack-local-variables))
3310 (org-element-map (org-element-parse-buffer) 'entity
3311 #'identity nil nil nil t)))))
3315 ;;; Test Visible Only Parsing
3317 (ert-deftest test-org-element/parse-buffer-visible ()
3318 "Test `org-element-parse-buffer' with visible only argument."
3319 (should
3320 (equal '("H1" "H3" "H5")
3321 (org-test-with-temp-text
3322 "* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:"
3323 (org-occur ":visible:")
3324 (org-element-map (org-element-parse-buffer nil t) 'headline
3325 (lambda (hl) (org-element-property :raw-value hl)))))))
3329 ;;; Test `:parent' Property
3331 (ert-deftest test-org-element/parent-property ()
3332 "Test `:parent' property."
3333 ;; Elements.
3334 (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
3335 (let* ((tree (org-element-parse-buffer))
3336 (parent (org-element-property
3337 :parent
3338 (org-element-map tree 'paragraph 'identity nil t))))
3339 (should parent)
3340 (should (eq (org-element-map tree 'center-block 'identity nil t)
3341 parent))))
3342 ;; Objects.
3343 (org-test-with-temp-text "a_{/b/}"
3344 (let* ((tree (org-element-parse-buffer))
3345 (parent (org-element-property
3346 :parent
3347 (org-element-map tree 'italic 'identity nil t))))
3348 (should parent)
3349 (should (eq parent
3350 (org-element-map tree 'subscript 'identity nil t)))))
3351 ;; Secondary strings
3352 (org-test-with-temp-text "* /italic/"
3353 (let* ((tree (org-element-parse-buffer))
3354 (parent (org-element-property
3355 :parent (org-element-map tree 'italic 'identity nil t))))
3356 (should parent)
3357 (should (eq parent
3358 (org-element-map tree 'headline 'identity nil t))))))
3362 ;;; Test Normalize Contents
3364 (ert-deftest test-org-element/normalize-contents ()
3365 "Test `org-element-normalize-contents' specifications."
3366 ;; Remove maximum common indentation from element's contents.
3367 (should
3368 (equal
3369 (org-element-normalize-contents
3370 '(paragraph nil " Two spaces\n Three spaces"))
3371 '(paragraph nil "Two spaces\n Three spaces")))
3372 (should
3373 (equal
3374 (org-element-normalize-contents
3375 '(paragraph nil " Two spaces\nNo space"))
3376 '(paragraph nil " Two spaces\nNo space")))
3377 ;; Ignore objects within contents when computing maximum common
3378 ;; indentation. However, if contents start with an object, common
3379 ;; indentation is 0.
3380 (should
3381 (equal
3382 (org-element-normalize-contents
3383 '(paragraph nil " One " (emphasis nil "space") "\n Two spaces"))
3384 '(paragraph nil "One " (emphasis nil "space") "\n Two spaces")))
3385 (should
3386 (equal
3387 (org-element-normalize-contents
3388 '(paragraph nil (verbatim nil "V") "No space\n Two\n Three"))
3389 '(paragraph nil (verbatim nil "V") "No space\n Two\n Three")))
3390 ;; Ignore blank lines.
3391 (should
3392 (equal
3393 (org-element-normalize-contents
3394 '(paragraph nil " Two spaces\n\n \n Two spaces"))
3395 '(paragraph nil "Two spaces\n\n\nTwo spaces")))
3396 (should
3397 (equal
3398 '(paragraph nil " Two spaces\n" (verbatim nil "V") "\n Two spaces")
3399 (org-element-normalize-contents
3400 '(paragraph nil " Two spaces\n " (verbatim nil "V") "\n Two spaces"))))
3401 (should
3402 (equal
3403 '(verse-block nil "line 1\n\nline 2")
3404 (org-element-normalize-contents
3405 '(verse-block nil " line 1\n\n line 2"))))
3406 ;; Recursively enter objects in order to compute common indentation.
3407 (should
3408 (equal
3409 (org-element-normalize-contents
3410 '(paragraph nil " Two spaces " (bold nil " and\n One space")))
3411 '(paragraph nil " Two spaces " (bold nil " and\nOne space"))))
3412 ;; When optional argument is provided, ignore first line
3413 ;; indentation.
3414 (should
3415 (equal
3416 (org-element-normalize-contents
3417 '(paragraph nil "No space\n Two spaces\n Three spaces") t)
3418 '(paragraph nil "No space\nTwo spaces\n Three spaces")))
3419 (should
3420 (equal
3421 (org-element-normalize-contents
3422 '(paragraph nil (verbatim nil "V") "No space\n Two\n Three") t)
3423 '(paragraph nil (verbatim nil "V") "No space\nTwo\n Three")))
3424 ;; Corner case: do not ignore indentation of string right after
3425 ;; a line break.
3426 (should
3427 (equal
3428 (org-element-normalize-contents
3429 '(paragraph nil " 1 space" (line-break) " 2 spaces"))
3430 '(paragraph nil "1 space" (line-break) " 2 spaces"))))
3434 ;;; Test Navigation Tools.
3436 (ert-deftest test-org-element/at-point ()
3437 "Test `org-element-at-point' specifications."
3438 ;; Return closest element containing point.
3439 (should
3440 (eq 'paragraph
3441 (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
3442 (progn (search-forward "A")
3443 (org-element-type (org-element-at-point))))))
3444 ;; Correctly set `:parent' property.
3445 (should
3446 (eq 'center-block
3447 (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
3448 (progn (search-forward "A")
3449 (org-element-type
3450 (org-element-property :parent (org-element-at-point)))))))
3451 ;; Special case: at a blank line just below a headline, return that
3452 ;; headline.
3453 (should
3454 (equal "H1" (org-test-with-temp-text "* H1\n \n* H2\n"
3455 (forward-line)
3456 (org-element-property :title (org-element-at-point)))))
3457 ;; Special case: at the very beginning of a table, return `table'
3458 ;; object instead of `table-row'.
3459 (should
3460 (eq 'table
3461 (org-test-with-temp-text "| a | b |"
3462 (org-element-type (org-element-at-point)))))
3463 ;; Special case: at the very beginning of a list or sub-list, return
3464 ;; `plain-list' object instead of `item'.
3465 (should
3466 (eq 'plain-list
3467 (org-test-with-temp-text "- item"
3468 (org-element-type (org-element-at-point)))))
3469 ;; Special case: at the closing line of a greater element, be sure
3470 ;; to return it instead of the last element in its contents.
3471 (should
3472 (eq 'center-block
3473 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph\n#+END_CENTER"
3474 (progn (forward-line 2)
3475 (org-element-type (org-element-at-point))))))
3476 ;; Special case: at a blank line between two items, be sure to
3477 ;; return item above instead of the last element of its contents.
3478 (should
3479 (eq 'item
3480 (org-test-with-temp-text "- Para1\n\n- Para2"
3481 (forward-line)
3482 (org-element-type (org-element-at-point)))))
3483 ;; Special case: at the last blank line in a plain list, return it
3484 ;; instead of the last item.
3485 (should
3486 (eq 'plain-list
3487 (org-test-with-temp-text "- Para1\n- Para2\n\nPara3"
3488 (progn (forward-line 2)
3489 (org-element-type (org-element-at-point))))))
3490 ;; Special case: when a list ends at the end of buffer and there's
3491 ;; no final newline, return last element in last item.
3492 (should
3493 (eq 'paragraph
3494 (org-test-with-temp-text "- a"
3495 (end-of-line)
3496 (org-element-type (org-element-at-point)))))
3497 ;; Parse a list within a block itself contained in a list.
3498 (should
3499 (eq 'plain-list
3500 (org-test-with-temp-text
3501 "- outer\n #+begin_center\n - inner\n #+end_center"
3502 (search-forward "inner")
3503 (beginning-of-line)
3504 (org-element-type (org-element-at-point)))))
3505 ;; Do not error at eob on an empty line.
3506 (should
3507 (org-test-with-temp-text "* H\n"
3508 (forward-line)
3509 (or (org-element-at-point) t))))
3511 (ert-deftest test-org-element/context ()
3512 "Test `org-element-context' specifications."
3513 ;; Return closest object containing point.
3514 (should
3515 (eq 'underline
3516 (org-test-with-temp-text "Some *text with _under<point>line_ text*"
3517 (org-element-type (org-element-context)))))
3518 ;; Find objects in secondary strings.
3519 (should
3520 (eq 'underline
3521 (org-test-with-temp-text "* Headline _<point>with_ underlining"
3522 (org-element-type (org-element-context)))))
3523 ;; Find objects in objects.
3524 (should
3525 (eq 'macro
3526 (org-test-with-temp-text "| a | {<point>{{macro}}} |"
3527 (org-element-type (org-element-context)))))
3528 (should
3529 (eq 'table-cell
3530 (org-test-with-temp-text "| a | b<point> {{{macro}}} |"
3531 (org-element-type (org-element-context)))))
3532 ;; Find objects in item tags.
3533 (should
3534 (eq 'bold
3535 (org-test-with-temp-text "- *bo<point>ld* ::"
3536 (org-element-type (org-element-context)))))
3537 (should-not
3538 (eq 'bold
3539 (org-test-with-temp-text "- *bold* ::<point>"
3540 (org-element-type (org-element-context)))))
3541 (should-not
3542 (eq 'bold
3543 (org-test-with-temp-text "- *bold* ::\n<point>"
3544 (org-element-type (org-element-context)))))
3545 ;; Do not find objects in table rules.
3546 (should
3547 (eq 'table-row
3548 (org-test-with-temp-text "| a | b |\n|-<point>--|---|\n| c | d |"
3549 (org-element-type (org-element-context)))))
3550 ;; Find objects in parsed affiliated keywords.
3551 (should
3552 (eq 'macro
3553 (org-test-with-temp-text "#+CAPTION: {<point>{{macro}}}\n| a | b |"
3554 (org-element-type (org-element-context)))))
3555 (should
3556 (eq 'bold
3557 (org-test-with-temp-text "#+caption: *<point>bold*\nParagraph"
3558 (org-element-type (org-element-context)))))
3559 ;; Find objects at the end of buffer.
3560 (should
3561 (eq 'bold
3562 (org-test-with-temp-text "*bold*"
3563 (goto-char (point-max))
3564 (org-element-type (org-element-context)))))
3565 ;; Correctly set `:parent' property.
3566 (should
3567 (eq 'paragraph
3568 (org-test-with-temp-text "Some *bold<point>* text"
3569 (org-element-type
3570 (org-element-property :parent (org-element-context))))))
3571 ;; Between two objects, return the second one.
3572 (should
3573 (eq 'macro
3574 (org-test-with-temp-text "<<target>><point>{{{test}}}"
3575 (org-element-type (org-element-context)))))
3576 ;; Test optional argument.
3577 (should
3578 (eq 'underline
3579 (org-test-with-temp-text "Some *text with _under<point>line_ text*"
3580 (org-element-type (org-element-context (org-element-at-point))))))
3581 ;; Special case: bold object at the beginning of a headline.
3582 (should
3583 (eq 'bold
3584 (org-test-with-temp-text "* *bo<point>ld*"
3585 (org-element-type (org-element-context)))))
3586 ;; Special case: incomplete cell at the end of a table row.
3587 (should
3588 (eq 'table-cell
3589 (org-test-with-temp-text "|a|b|c<point>"
3590 (org-element-type (org-element-context)))))
3591 ;; Special case: objects in inline footnotes.
3592 (should
3593 (eq 'link
3594 (org-test-with-temp-text "[fn::[[<point>https://orgmode.org]]]"
3595 (org-element-type (org-element-context)))))
3596 ;; Special case: tags looking like a link.
3597 (should-not
3598 (eq 'link
3599 (org-test-with-temp-text "* Headline :file<point>:tags:"
3600 (org-element-type (org-element-context)))))
3601 (should
3602 (eq 'link
3603 (org-test-with-temp-text "* Headline :file<point>:tags: :real:tag:"
3604 (org-element-type (org-element-context))))))
3608 ;;; Test Tools
3610 (ert-deftest test-org-element/lineage ()
3611 "Test `org-element-lineage' specifications."
3612 ;; Regular tests. When applied to an element or object returned by
3613 ;; `org-element-at-point' or `org-element-context', the list is
3614 ;; limited to the current section.
3615 (should
3616 (equal '(paragraph center-block)
3617 (org-test-with-temp-text
3618 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3619 (mapcar #'car (org-element-lineage (org-element-context))))))
3620 (should
3621 (equal '(paragraph center-block section headline headline org-data)
3622 (org-test-with-temp-text
3623 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3624 (mapcar #'car
3625 (org-element-lineage
3626 (org-element-map (org-element-parse-buffer) 'bold
3627 #'identity nil t))))))
3628 ;; Test TYPES optional argument.
3629 (should
3630 (eq 'center-block
3631 (org-test-with-temp-text
3632 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3633 (org-element-type
3634 (org-element-lineage (org-element-context) '(center-block))))))
3635 (should-not
3636 (org-test-with-temp-text
3637 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3638 (org-element-lineage (org-element-context) '(example-block))))
3639 ;; Test WITH-SELF optional argument.
3640 (should
3641 (equal '(bold paragraph center-block)
3642 (org-test-with-temp-text
3643 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3644 (mapcar #'car (org-element-lineage (org-element-context) nil t)))))
3645 ;; When TYPES and WITH-SELF are provided, the latter is also checked
3646 ;; against the former.
3647 (should
3648 (org-test-with-temp-text
3649 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3650 (org-element-lineage (org-element-context) '(bold) t))))
3654 ;;; Test Cache.
3656 (ert-deftest test-org-element/cache ()
3657 "Test basic expectations and common pitfalls for cache."
3658 ;; Shift positions.
3659 (should
3660 (equal '(18 . 23)
3661 (org-test-with-temp-text "para1\n\npara2\n\npara3"
3662 (let ((org-element-use-cache t))
3663 (save-excursion (goto-char (point-max)) (org-element-at-point))
3664 (insert "add")
3665 (forward-line 4)
3666 (let ((element (org-element-at-point)))
3667 (cons (org-element-property :begin element)
3668 (org-element-property :end element)))))))
3669 ;; Partial shifting: when the contents of a greater element are
3670 ;; modified, only shift ending positions.
3671 (should
3672 (org-test-with-temp-text
3673 "#+BEGIN_CENTER\nPara1\n\nPara2\n\nPara3\n#+END_CENTER"
3674 (let ((org-element-use-cache t))
3675 (save-excursion (search-forward "3") (org-element-at-point))
3676 (search-forward "Para2")
3677 (insert " ")
3678 (let ((element (org-element-property :parent (org-element-at-point))))
3679 (equal (cons (org-element-property :begin element)
3680 (org-element-property :end element))
3681 (cons (point-min) (point-max)))))))
3682 ;; Re-parent shifted elements.
3683 (should
3684 (eq 'item
3685 (org-test-with-temp-text "- item\n\n\n para1\n para2"
3686 (let ((org-element-use-cache t))
3687 (end-of-line)
3688 (org-element-at-point)
3689 (save-excursion (goto-char (point-max)) (org-element-at-point))
3690 (forward-line)
3691 (delete-char 1)
3692 (goto-char (point-max))
3693 (org-element-type
3694 (org-element-property :parent (org-element-at-point)))))))
3695 ;; Preserve local structures when re-parenting.
3696 (should
3697 (eq 'table
3698 (let ((org-element-use-cache t))
3699 (org-test-with-temp-text
3700 "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n| c | d |\n#+end_center"
3701 (save-excursion (search-forward "| c |") (org-element-at-point))
3702 (insert "- item")
3703 (search-forward "| c |")
3704 (beginning-of-line)
3705 (org-element-type
3706 (org-element-property :parent (org-element-at-point)))))))
3707 (should-not
3708 (eq 'center-block
3709 (org-test-with-temp-text
3710 "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n#+end_center"
3711 (let ((org-element-use-cache t))
3712 (save-excursion (search-forward "| a |") (org-element-at-point))
3713 (insert "- item")
3714 (search-forward "| a |")
3715 (beginning-of-line)
3716 (org-element-type
3717 (org-element-property :parent (org-element-at-point)))))))
3718 ;; When re-parenting, also propagate changes to list structures.
3719 (should
3720 (= 2
3721 (org-test-with-temp-text "\n Para\n - item<point>"
3722 (let ((org-element-use-cache t))
3723 (org-element-at-point)
3724 (goto-char (point-min))
3725 (insert "- Top\n")
3726 (search-forward "- item")
3727 (beginning-of-line)
3728 (length (org-element-property :structure (org-element-at-point)))))))
3729 ;; Modifying the last line of an element alters the element below.
3730 (should
3731 (org-test-with-temp-text "para1\n\npara2"
3732 (let ((org-element-use-cache t))
3733 (goto-char (point-max))
3734 (org-element-at-point)
3735 (forward-line -1)
3736 (insert "merge")
3737 (let ((element (org-element-at-point)))
3738 (equal (cons (org-element-property :begin element)
3739 (org-element-property :end element))
3740 (cons (point-min) (point-max)))))))
3741 ;; Modifying the first line of an element alters the element above.
3742 (should
3743 (org-test-with-temp-text ": fixed-width\n:not-fixed-width"
3744 (let ((org-element-use-cache t))
3745 (goto-char (point-max))
3746 (org-element-at-point)
3747 (search-backward ":")
3748 (forward-char)
3749 (insert " ")
3750 (let ((element (org-element-at-point)))
3751 (equal (cons (org-element-property :begin element)
3752 (org-element-property :end element))
3753 (cons (point-min) (point-max)))))))
3754 ;; Sensitive change: adding a line alters document structure both
3755 ;; above and below.
3756 (should
3757 (eq 'example-block
3758 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n"
3759 (let ((org-element-use-cache t))
3760 (goto-char (point-max))
3761 (org-element-at-point)
3762 (insert "#+END_EXAMPLE")
3763 (search-backward "Para1")
3764 (org-element-type (org-element-at-point))))))
3765 (should
3766 (eq 'example-block
3767 (org-test-with-temp-text "Para1\n\nPara2\n#+END_EXAMPLE"
3768 (let ((org-element-use-cache t))
3769 (save-excursion (goto-char (point-max)) (org-element-at-point))
3770 (insert "#+BEGIN_EXAMPLE\n")
3771 (search-forward "Para2")
3772 (org-element-type (org-element-at-point))))))
3773 ;; Sensitive change: removing a line alters document structure both
3774 ;; above and below.
3775 (should
3776 (eq 'example-block
3777 (org-test-with-temp-text
3778 "# +BEGIN_EXAMPLE\nPara1\n\nPara2\n#+END_EXAMPLE"
3779 (let ((org-element-use-cache t))
3780 (save-excursion (goto-char (point-max)) (org-element-at-point))
3781 (forward-char)
3782 (delete-char 1)
3783 (search-forward "Para2")
3784 (org-element-type (org-element-at-point))))))
3785 (should
3786 (eq 'example-block
3787 (org-test-with-temp-text
3788 "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n# +END_EXAMPLE"
3789 (let ((org-element-use-cache t))
3790 (save-excursion (goto-char (point-max)) (org-element-at-point))
3791 (search-forward "# ")
3792 (delete-char -1)
3793 (search-backward "Para1")
3794 (org-element-type (org-element-at-point))))))
3795 ;; Corner case: watch out drawers named "PROPERTIES" as they are
3796 ;; fragile, unlike to other drawers.
3797 (should
3798 (eq 'node-property
3799 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A<point>\n:END:"
3800 (let ((org-element-use-cache t))
3801 (org-element-at-point)
3802 (insert "+:")
3803 (org-element-type (org-element-at-point))))))
3804 ;; Properly handle elements not altered by modifications but whose
3805 ;; parents were removed from cache.
3806 (should
3807 (org-test-with-temp-text
3808 "Paragraph\n\n\n\n#+begin_center\n<point>contents\n#+end_center"
3809 (let ((org-element-use-cache t)
3810 (parent-end (point-max)))
3811 (org-element-at-point)
3812 (save-excursion (search-backward "Paragraph")
3813 (forward-line 2)
3814 (insert "\n "))
3815 (eq (org-element-property
3816 :end (org-element-property :parent (org-element-at-point)))
3817 (+ parent-end 3))))))
3820 (provide 'test-org-element)
3822 ;;; test-org-element.el ends here