org.texi: Overwrite with the one generated from "org-manual.org"
[org-mode/org-tableheadings.git] / testing / lisp / test-org-element.el
blob1daecb4b61983c1c1a6801b4247957cc76a16a8f
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 switches.
1227 (should
1228 (org-test-with-temp-text "src_emacs-lisp[:foo bar]{(+ 1 1)}"
1229 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1230 (should
1231 (org-test-with-temp-text "src_emacs-lisp[ :foo bar]{(+ 1 1)}"
1232 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1233 ;; Empty switches.
1234 (should
1235 (org-test-with-temp-text "src_emacs-lisp[]{(+ 1 1)}"
1236 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1237 ;; Invalid syntax.
1238 (should-not
1239 (org-test-with-temp-text "src_emacs-lisp[]foo{(+ 1 1)}"
1240 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1241 (should-not
1242 (org-test-with-temp-text "foosrc_emacs-lisp[]{(+ 1 1)}"
1243 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1244 ;; Invalid language name
1245 (should-not
1246 (org-test-with-temp-text "src_emacs-\tlisp{(+ 1 1)}"
1247 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1248 ;; Test parsing at the beginning of an item.
1249 (should
1250 (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
1251 (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
1252 ;; Test parsing multi-line source blocks.
1253 (should
1254 (eq 'inline-src-block
1255 (org-test-with-temp-text "src_emacs-lisp{(+ 1\n 1)}"
1256 (org-element-type (org-element-context)))))
1257 (should
1258 (eq 'inline-src-block
1259 (org-test-with-temp-text "src_emacs-lisp[\n:foo bar]{(+ 1 1)}"
1260 (org-element-type (org-element-context)))))
1261 (should
1262 (eq 'inline-src-block
1263 (org-test-with-temp-text "src_emacs-lisp[:foo\nbar]{(+ 1 1)}"
1264 (org-element-type (org-element-context)))))
1265 ;; Besides curly brackets, ignore any other bracket type.
1266 (should
1267 (equal "[foo"
1268 (org-test-with-temp-text "src_emacs-lisp{[foo}"
1269 (org-element-property :value (org-element-context)))))
1270 (should
1271 (equal "foo]"
1272 (org-test-with-temp-text "src_emacs-lisp{foo]}"
1273 (org-element-property :value (org-element-context)))))
1274 (should
1275 (equal "(foo"
1276 (org-test-with-temp-text "src_emacs-lisp{(foo}"
1277 (org-element-property :value (org-element-context)))))
1278 (should
1279 (equal "foo)"
1280 (org-test-with-temp-text "src_emacs-lisp{foo)}"
1281 (org-element-property :value (org-element-context)))))
1282 ;; Parse parameters containing square brackets.
1283 (should
1284 (eq 'inline-src-block
1285 (org-test-with-temp-text "src_emacs-lisp[:var table=t[1,1]]{(+ 1 1)}"
1286 (org-element-type (org-element-context))))))
1289 ;;;; Inlinetask
1291 (ert-deftest test-org-element/inlinetask-parser ()
1292 "Test `inlinetask' parser."
1293 (when (featurep 'org-inlinetask)
1294 (let ((org-inlinetask-min-level 15))
1295 ;; Regular inlinetask.
1296 (should
1297 (eq 'inlinetask
1298 (org-test-with-temp-text
1299 "*************** Task\nTest\n*************** END"
1300 (org-element-type (org-element-at-point)))))
1301 (should
1302 (eq 'inlinetask
1303 (org-test-with-temp-text
1304 "*************** Task\nTest\n*************** END"
1305 (org-element-type (org-element-at-point)))))
1306 ;; Degenerate inlinetask.
1307 (should
1308 (eq 'inlinetask
1309 (org-test-with-temp-text "*************** Task"
1310 (org-element-type (org-element-at-point)))))
1311 ;; Mixed inlinetasks.
1312 (should-not
1313 (org-test-with-temp-text
1315 *************** Task
1316 *************** Task2
1317 Contents
1318 *************** END"
1319 (forward-line)
1320 (goto-char (org-element-property :end (org-element-at-point)))
1321 (eobp)))
1322 ;; TODO keyword.
1323 (should
1324 (equal
1325 "TODO"
1326 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
1327 (org-test-with-temp-text "*************** TODO Task"
1328 (org-element-property :todo-keyword (org-element-at-point))))))
1329 ;; Planning info.
1330 (should
1331 (org-test-with-temp-text "
1332 *************** Task<point>
1333 DEADLINE: <2012-03-29 thu.>
1334 *************** END"
1335 (org-element-property :deadline (org-element-at-point))))
1336 (should
1337 (eq 'planning
1338 (org-test-with-temp-text "
1339 *************** Task
1340 <point>DEADLINE: <2012-03-29 thu.>
1341 *************** END"
1342 (org-element-type (org-element-at-point)))))
1343 (should-not
1344 (org-test-with-temp-text "
1345 *************** Task<point>
1346 DEADLINE: <2012-03-29 thu.>"
1347 (org-element-property :deadline (org-element-at-point))))
1348 (should-not
1349 (eq 'planning
1350 (org-test-with-temp-text "
1351 *************** Task
1352 <point>DEADLINE: <2012-03-29 thu.>"
1353 (org-element-type (org-element-at-point)))))
1354 ;; Priority.
1355 (should
1358 (org-test-with-temp-text "
1359 *************** [#A] Task"
1360 (forward-line)
1361 (org-element-property :priority (org-element-at-point)))))
1362 ;; Tags.
1363 (should
1364 (equal
1365 '("test")
1366 (org-test-with-temp-text "
1367 *************** Task :test:"
1368 (forward-line)
1369 (org-element-property :tags (org-element-at-point)))))
1370 ;; Regular properties are accessed through upper case keywords.
1371 (should
1372 (org-test-with-temp-text "
1373 *************** Task
1374 :PROPERTIES:
1375 :foo: bar
1376 :END:
1377 *************** END"
1378 (forward-line)
1379 (org-element-property :FOO (org-element-at-point))))
1380 (should-not
1381 (org-test-with-temp-text "
1382 *************** Task
1383 :PROPERTIES:
1384 :foo: bar
1385 :END:
1386 *************** END"
1387 (forward-line)
1388 (org-element-property :foo (org-element-at-point))))
1389 ;; Handle non-empty blank line at the end of buffer.
1390 (should
1391 (org-test-with-temp-text "*************** Task\n*************** END\n "
1392 (= (org-element-property :end (org-element-at-point)) (point-max)))))))
1395 ;;;; Italic
1397 (ert-deftest test-org-element/italic-parser ()
1398 "Test `italic' parser."
1399 ;; Regular test.
1400 (should
1401 (org-test-with-temp-text "/italic/"
1402 (org-element-map (org-element-parse-buffer) 'italic #'identity nil t)))
1403 ;; Multi-line markup.
1404 (should
1405 (equal
1406 (org-element-contents
1407 (org-test-with-temp-text "/first line\nsecond line/"
1408 (org-element-map (org-element-parse-buffer) 'italic #'identity nil t)))
1409 '("first line\nsecond line"))))
1412 ;;;; Item
1414 (ert-deftest test-org-element/item-parser ()
1415 "Test `item' parser."
1416 ;; Standard test.
1417 (should
1418 (org-test-with-temp-text "- item"
1419 (org-element-map (org-element-parse-buffer) 'item 'identity)))
1420 ;; Counter.
1421 (should
1422 (= 6
1423 (org-element-property
1424 :counter
1425 (org-test-with-temp-text "6. [@6] item"
1426 (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
1427 ;; Tag
1428 (should
1429 (equal
1430 '("tag")
1431 (org-element-property
1432 :tag
1433 (org-test-with-temp-text "- tag :: description"
1434 (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
1435 ;; No tags in ordered lists.
1436 (should-not
1437 (org-element-property
1438 :tag
1439 (org-test-with-temp-text "1. tag :: description"
1440 (org-element-map (org-element-parse-buffer) 'item 'identity nil t))))
1441 ;; Check-boxes
1442 (should
1443 (equal
1444 '(trans on off)
1445 (org-test-with-temp-text "
1446 - [-] item 1
1447 - [X] item 1.1
1448 - [ ] item 1.2"
1449 (org-element-map (org-element-parse-buffer) 'item
1450 (lambda (item) (org-element-property :checkbox item))))))
1451 ;; Item starting with special syntax.
1452 (should
1453 (equal '(("- item"))
1454 (org-test-with-temp-text "- - item"
1455 (org-element-map (org-element-parse-buffer) 'paragraph
1456 'org-element-contents))))
1457 ;; Block in an item: ignore indentation within the block.
1458 (should
1459 (org-test-with-temp-text
1460 "-<point> item\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1461 (= (org-element-property :end (org-element-at-point)) (point-max))))
1462 ;; Parse `:pre-blank'.
1463 (should
1464 (= 0
1465 (org-test-with-temp-text "-<point> A"
1466 (org-element-property :pre-blank (org-element-at-point)))))
1467 (should
1468 (= 1
1469 (org-test-with-temp-text "-<point>\n A"
1470 (org-element-property :pre-blank (org-element-at-point)))))
1471 (should
1472 (= 2
1473 (org-test-with-temp-text "-<point>\n\n A"
1474 (org-element-property :pre-blank (org-element-at-point)))))
1475 ;; Last item in a list or sub-list has no `:post-blank' lines, since
1476 ;; those belong to the plain-list.
1477 (should
1478 (= 0
1479 (org-test-with-temp-text "- A\n\n- <point>B\n\nEnd list"
1480 (org-element-property :post-blank (org-element-at-point)))))
1481 (should
1482 (= 0
1483 (org-test-with-temp-text "- A\n\n - B\n\n<point> - C\n\n End sub-list"
1484 (org-element-property :post-blank (org-element-at-point))))))
1487 ;;;; Keyword
1489 (ert-deftest test-org-element/keyword-parser ()
1490 "Test `keyword' parser."
1491 ;; Standard test.
1492 (should
1493 (org-test-with-temp-text "#+KEYWORD: value"
1494 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1495 ;; Keywords are case-insensitive.
1496 (should
1497 (org-test-with-temp-text "#+keyword: value"
1498 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1499 ;; Affiliated keywords are not keywords.
1500 (should-not
1501 (org-test-with-temp-text "#+NAME: value
1502 Paragraph"
1503 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1504 ;; Do not mix keywords with Babel calls and dynamic blocks.
1505 (should-not
1506 (org-test-with-temp-text "#+CALL: fun()"
1507 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1508 (should-not
1509 (org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
1510 (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
1511 ;; Handle non-empty blank line at the end of buffer.
1512 (should
1513 (org-test-with-temp-text "#+KEYWORD: value\n "
1514 (= (org-element-property :end (org-element-at-point)) (point-max)))))
1517 ;;;; Latex Environment
1519 (ert-deftest test-org-element/latex-environment-parser ()
1520 "Test `latex-environment' parser."
1521 (should
1522 (org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
1523 (org-element-map (org-element-parse-buffer) 'latex-environment 'identity)))
1524 ;; Allow nested environments.
1525 (should
1526 (equal
1527 "\\begin{outer}
1528 \\begin{inner}
1529 e^{i\\pi}+1=0
1530 \\end{inner}
1531 \\end{outer}"
1532 (org-test-with-temp-text "
1533 \\begin{outer}
1534 \\begin{inner}
1535 e^{i\\pi}+1=0
1536 \\end{inner}
1537 \\end{outer}"
1538 (org-element-property
1539 :value
1540 (org-element-map
1541 (org-element-parse-buffer) 'latex-environment 'identity nil t)))))
1542 ;; Allow environments with options and arguments.
1543 (should
1544 (eq 'latex-environment
1545 (org-test-with-temp-text
1546 "\\begin{theorem}[Euler]\ne^{i\\pi}+1=0\n\\end{theorem}"
1547 (org-element-type (org-element-at-point)))))
1548 (should
1549 (eq 'latex-environment
1550 (org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
1551 (org-element-type (org-element-at-point)))))
1552 ;; Allow environments without newline after \begin{.}.
1553 (should
1554 (eq 'latex-environment
1555 (org-test-with-temp-text "\\begin{env}{arg}something\nvalue\n\\end{env}"
1556 (org-element-type (org-element-at-point)))))
1557 ;; Allow one-line environments.
1558 (should
1559 (eq 'latex-environment
1560 (org-test-with-temp-text "\\begin{env}{arg}something\\end{env}"
1561 (org-element-type (org-element-at-point)))))
1562 ;; Should not allow different tags.
1563 (should-not
1564 (eq 'latex-environment
1565 (org-test-with-temp-text "\\begin{env*}{arg}something\\end{env}"
1566 (org-element-type (org-element-at-point)))))
1567 ;; LaTeX environments must be on separate lines.
1568 (should-not
1569 (eq 'latex-environment
1570 (org-test-with-temp-text "\\begin{env} x \\end{env} y"
1571 (org-element-type (org-element-at-point)))))
1572 (should-not
1573 (eq 'latex-environment
1574 (org-test-with-temp-text "y \\begin{env} x<point> \\end{env}"
1575 (org-element-type (org-element-at-point)))))
1576 ;; Handle non-empty blank line at the end of buffer.
1577 (should
1578 (org-test-with-temp-text "\\begin{env}\n\\end{env}\n "
1579 (= (org-element-property :end (org-element-at-point)) (point-max)))))
1582 ;;;; Latex Fragment
1584 (ert-deftest test-org-element/latex-fragment-parser ()
1585 "Test `latex-fragment' parser."
1586 ;; Basic $...$ test.
1587 (should
1588 (eq 'latex-fragment
1589 (org-test-with-temp-text "$a$"
1590 (org-element-type (org-element-context)))))
1591 ;; Test valid characters after $...$ construct.
1592 (should-not
1593 (eq 'latex-fragment
1594 (org-test-with-temp-text "$a$a"
1595 (org-element-type (org-element-context)))))
1596 (should
1597 (eq 'latex-fragment
1598 (org-test-with-temp-text "$a$!"
1599 (org-element-type (org-element-context)))))
1600 (should
1601 (eq 'latex-fragment
1602 (org-test-with-temp-text "$a$,"
1603 (org-element-type (org-element-context)))))
1604 (should
1605 (eq 'latex-fragment
1606 (org-test-with-temp-text "$a$\""
1607 (org-element-type (org-element-context)))))
1608 (should
1609 (eq 'latex-fragment
1610 (org-test-with-temp-text "$a$)"
1611 (org-element-type (org-element-context)))))
1612 (should
1613 (eq 'latex-fragment
1614 (org-test-with-temp-text "$a$ "
1615 (org-element-type (org-element-context)))))
1616 (should
1617 (eq 'latex-fragment
1618 (org-test-with-temp-text "$a$'"
1619 (org-element-type (org-element-context)))))
1620 ;; Test forbidden characters inside $...$.
1621 (should-not
1622 (eq 'latex-fragment
1623 (org-test-with-temp-text "$.a$"
1624 (org-element-type (org-element-context)))))
1625 (should-not
1626 (eq 'latex-fragment
1627 (org-test-with-temp-text "$,a$"
1628 (org-element-type (org-element-context)))))
1629 (should-not
1630 (eq 'latex-fragment
1631 (org-test-with-temp-text "$;a$"
1632 (org-element-type (org-element-context)))))
1633 (should-not
1634 (eq 'latex-fragment
1635 (org-test-with-temp-text "$ a$"
1636 (org-element-type (org-element-context)))))
1637 (should-not
1638 (eq 'latex-fragment
1639 (org-test-with-temp-text "$a.$"
1640 (org-element-type (org-element-context)))))
1641 (should-not
1642 (eq 'latex-fragment
1643 (org-test-with-temp-text "$a,$"
1644 (org-element-type (org-element-context)))))
1645 (should-not
1646 (eq 'latex-fragment
1647 (org-test-with-temp-text "$a $"
1648 (org-element-type (org-element-context)))))
1649 ;; Test $$...$$.
1650 (should
1651 (eq 'latex-fragment
1652 (org-test-with-temp-text "$$a$$"
1653 (org-element-type (org-element-context)))))
1654 ;; Test \(...\).
1655 (should
1656 (eq 'latex-fragment
1657 (org-test-with-temp-text "\\(a\\)"
1658 (org-element-type (org-element-context)))))
1659 ;; Test \[...\].
1660 (should
1661 (eq 'latex-fragment
1662 (org-test-with-temp-text "\\[a\\]"
1663 (org-element-type (org-element-context)))))
1664 ;; Test fragment at the beginning of an item.
1665 (should
1666 (eq 'latex-fragment
1667 (org-test-with-temp-text "- $<point>x$"
1668 (org-element-type (org-element-context))))))
1671 ;;;; Line Break
1673 (ert-deftest test-org-element/line-break-parser ()
1674 "Test `line-break' parser."
1675 ;; Regular test.
1676 (should
1677 (org-test-with-temp-text "Text \\\\"
1678 (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
1679 ;; Line break with trailing white spaces.
1680 (should
1681 (org-test-with-temp-text "Text \\\\ "
1682 (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
1683 ;; Three backslashes are too much.
1684 (should-not
1685 (org-test-with-temp-text "Text \\\\\\"
1686 (org-element-map (org-element-parse-buffer) 'line-break 'identity))))
1689 ;;;; Link
1691 (ert-deftest test-org-element/link-parser ()
1692 "Test `link' parser."
1693 ;; Radio target.
1694 (should
1695 (equal
1696 "radio"
1697 (org-test-with-temp-text "<<<radio>>>A radio link"
1698 (org-update-radio-target-regexp)
1699 (org-element-property
1700 :type
1701 (org-element-map (org-element-parse-buffer) 'link #'identity nil t)))))
1702 ;; Pathological case: radio target of length 1 at beginning of line
1703 ;; not followed by spaces.
1704 (should
1705 (org-test-with-temp-text "* <<<a>>>\n<point>a-bug"
1706 (org-update-radio-target-regexp)
1707 (org-element-parse-buffer)))
1708 ;; Pathological case: radio target in an emphasis environment.
1709 (should
1710 (eq 'bold
1711 (org-test-with-temp-text "* <<<radio>>>\n<point>*radio*"
1712 (org-update-radio-target-regexp)
1713 (org-element-type (org-element-context)))))
1714 (should
1715 (eq 'link
1716 (org-test-with-temp-text "* <<<radio>>>\n*<point>radio*"
1717 (org-update-radio-target-regexp)
1718 (org-element-type (org-element-context)))))
1719 ;; Standard link.
1721 ;; ... with description.
1722 (should
1723 (equal
1724 '("Orgmode.org")
1725 (org-test-with-temp-text "[[https://orgmode.org][Orgmode.org]]"
1726 (org-element-contents
1727 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1728 ;; ... without description.
1729 (should
1730 (equal
1731 "https"
1732 (org-test-with-temp-text "[[https://orgmode.org]]"
1733 (org-element-property
1734 :type
1735 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1736 ;; ... with expansion.
1737 (should
1738 (equal
1739 "//orgmode.org/worg"
1740 (org-test-with-temp-text "[[Org:worg]]"
1741 (let ((org-link-abbrev-alist '(("Org" . "https://orgmode.org/"))))
1742 (org-element-property
1743 :path
1744 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))))))
1745 ;; ... with translation.
1746 (should
1747 (equal
1748 "127.0.0.1"
1749 (org-test-with-temp-text "[[https://orgmode.org]]"
1750 (let ((org-link-translation-function
1751 (lambda (type _) (cons type "127.0.0.1"))))
1752 (org-element-property
1753 :path
1754 (org-element-map (org-element-parse-buffer) 'link
1755 #'identity nil t))))))
1756 ;; ... custom-id link.
1757 (should
1758 (equal
1759 "custom-id"
1760 (org-test-with-temp-text "[[#some-id]]"
1761 (org-element-property
1762 :type
1763 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1764 ;; ... coderef link.
1765 (should
1766 (equal
1767 "coderef"
1768 (org-test-with-temp-text "[[(reference)]]"
1769 (org-element-property
1770 :type
1771 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1772 ;; ... fuzzy link.
1773 (should
1774 (equal
1775 "fuzzy"
1776 (org-test-with-temp-text "[[target-or-title]]"
1777 (org-element-property
1778 :type
1779 (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
1780 ;; ... file-type link with search option.
1781 (should
1782 (equal
1783 '(("file" "projects.org" "*task title"))
1784 (org-test-with-temp-text "[[file:projects.org::*task title]]"
1785 (org-element-map (org-element-parse-buffer) 'link
1786 (lambda (l) (list (org-element-property :type l)
1787 (org-element-property :path l)
1788 (org-element-property :search-option l)))))))
1789 ;; ... file-type link with application...
1790 (should
1791 (equal
1792 '("file" "projects.org" "emacs")
1793 (org-test-with-temp-text "[[file+emacs:projects.org]]"
1794 (let ((l (org-element-context)))
1795 (list (org-element-property :type l)
1796 (org-element-property :path l)
1797 (org-element-property :application l))))))
1798 ;; ... `:path' in a file-type link must be compatible with "file"
1799 ;; scheme in URI syntax, even if Org syntax isn't...
1800 (should
1801 (org-test-with-temp-text-in-file ""
1802 (let ((file (expand-file-name (buffer-file-name))))
1803 (insert (format "[[file://%s]]" file))
1804 (equal (org-element-property :path (org-element-context)) file))))
1805 (should
1806 (org-test-with-temp-text-in-file ""
1807 (let ((file (expand-file-name (buffer-file-name))))
1808 (insert (format "[[file:%s]]" file))
1809 (equal (org-element-property :path (org-element-context)) file))))
1810 (should
1811 (org-test-with-temp-text-in-file ""
1812 (let ((file (expand-file-name (buffer-file-name))))
1813 (insert (format "[[file:%s]]" file))
1814 (equal (org-element-property :path (org-element-context)) file))))
1815 ;; ... multi-line link.
1816 (should
1817 (equal "ls *.org"
1818 (org-test-with-temp-text "[[shell:ls\n*.org]]"
1819 (org-element-property :path (org-element-context)))))
1820 ;; Plain link.
1821 (should
1822 (org-test-with-temp-text "A link: https://orgmode.org"
1823 (org-element-map (org-element-parse-buffer) 'link 'identity)))
1824 ;; Angular link. Follow RFC 3986.
1825 (should
1826 (eq 'link
1827 (org-test-with-temp-text "A link: <point><https://orgmode.org>"
1828 (org-element-type (org-element-context)))))
1829 (should
1830 (equal "//orgmode.org"
1831 (org-test-with-temp-text "A link: <point><https://orgmode\n.org>"
1832 (org-element-property :path (org-element-context)))))
1833 ;; Link abbreviation.
1834 (should
1835 (equal "https"
1836 (org-test-with-temp-text
1837 "#+LINK: orgmode https://www.orgmode.org/\n[[orgmode:#docs]]"
1838 (progn (org-mode-restart)
1839 (goto-char (1- (point-max)))
1840 (org-element-property :type (org-element-context))))))
1841 ;; Link abbreviation in a secondary string.
1842 (should
1843 (equal "https"
1844 (org-test-with-temp-text
1845 "#+LINK: orgmode https://www.orgmode.org/\n* H [[orgmode:#docs]]"
1846 (progn (org-mode-restart)
1847 (org-element-map (org-element-parse-buffer) 'link
1848 (lambda (link) (org-element-property :type link))
1849 nil t nil t))))))
1852 ;;;; Macro
1854 (ert-deftest test-org-element/macro-parser ()
1855 "Test `macro' parser."
1856 ;; Without arguments.
1857 (should
1858 (org-test-with-temp-text "{{{macro}}}"
1859 (org-element-map (org-element-parse-buffer) 'macro 'identity)))
1860 ;; With arguments.
1861 (should
1862 (org-test-with-temp-text "{{{macro(arg1,arg2)}}}"
1863 (org-element-map (org-element-parse-buffer) 'macro 'identity)))
1864 ;; Properly handle protected commas in arguments...
1865 (should
1866 (= 2
1867 (length
1868 (org-test-with-temp-text "{{{macro(arg1\\,arg1,arg2)}}}"
1869 (org-element-property :args (org-element-context))))))
1870 ;; ... even when last argument ends with a protected comma.
1871 (should
1872 (equal '("C-,")
1873 (org-test-with-temp-text "{{{macro(C-\\,)}}}"
1874 (org-element-property :args (org-element-context)))))
1875 ;; Allow to escape escaping character.
1876 (should
1877 (equal '("C-\\" "")
1878 (org-test-with-temp-text "{{{macro(C-\\\\,)}}}"
1879 (org-element-property :args (org-element-context)))))
1880 ;; No need to escape backslashes elsewhere.
1881 (should
1882 (equal '("\\")
1883 (org-test-with-temp-text "{{{macro(\\)}}}"
1884 (org-element-property :args (org-element-context))))))
1887 ;;;; Node Property
1889 (ert-deftest test-org-element/node-property ()
1890 "Test `node-property' parser."
1891 ;; Standard test.
1892 (should
1893 (equal '("abc" "value")
1894 (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:abc: value\n:END:"
1895 (let ((element (org-element-at-point)))
1896 (list (org-element-property :key element)
1897 (org-element-property :value element))))))
1898 ;; Value should be trimmed.
1899 (should
1900 (equal "value"
1901 (org-test-with-temp-text
1902 "* H\n:PROPERTIES:\n<point>:abc: value \n:END:"
1903 (org-element-property :value (org-element-at-point)))))
1904 ;; A node property requires to be wrapped within a property drawer.
1905 (should-not
1906 (eq 'node-property
1907 (org-test-with-temp-text ":abc: value"
1908 (org-element-type (org-element-at-point)))))
1909 ;; Accept empty properties.
1910 (should
1911 (equal '(("foo" "value") ("bar" ""))
1912 (org-test-with-temp-text "* H\n:PROPERTIES:\n:foo: value\n:bar:\n:END:"
1913 (org-element-map (org-element-parse-buffer) 'node-property
1914 (lambda (p)
1915 (list (org-element-property :key p)
1916 (org-element-property :value p))))))))
1919 ;;;; Paragraph
1921 (ert-deftest test-org-element/paragraph-parser ()
1922 "Test `paragraph' parser."
1923 ;; Standard test.
1924 (should
1925 (org-test-with-temp-text "Paragraph"
1926 (org-element-map (org-element-parse-buffer) 'paragraph 'identity nil t)))
1927 ;; Property find end of a paragraph stuck to another element.
1928 (should
1929 (eq ?#
1930 (org-test-with-temp-text "Paragraph\n# Comment"
1931 (org-element-map (org-element-parse-buffer) 'paragraph
1932 (lambda (p) (char-after (org-element-property :end p)))
1933 nil t))))
1934 ;; Include ill-formed Keywords.
1935 (should
1936 (org-test-with-temp-text "#+wrong_keyword something"
1937 (org-element-map (org-element-parse-buffer) 'paragraph 'identity)))
1938 ;; Include incomplete-drawers.
1939 (should
1940 (org-test-with-temp-text ":TEST:\nParagraph"
1941 (let ((elem (org-element-at-point)))
1942 (and (eq (org-element-type elem) 'paragraph)
1943 (= (point-max) (org-element-property :end elem))))))
1944 ;; Include incomplete blocks.
1945 (should
1946 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph"
1947 (let ((elem (org-element-at-point)))
1948 (and (eq (org-element-type elem) 'paragraph)
1949 (= (point-max) (org-element-property :end elem))))))
1950 ;; Include incomplete dynamic blocks.
1951 (should
1952 (org-test-with-temp-text "#+BEGIN: \nParagraph"
1953 (let ((elem (org-element-at-point)))
1954 (and (eq (org-element-type elem) 'paragraph)
1955 (= (point-max) (org-element-property :end elem))))))
1956 ;; Include incomplete latex environments.
1957 (should
1958 (org-test-with-temp-text "\begin{equation}\nParagraph"
1959 (let ((elem (org-element-at-point)))
1960 (and (eq (org-element-type elem) 'paragraph)
1961 (= (point-max) (org-element-property :end elem))))))
1962 (should
1963 (org-test-with-temp-text "Paragraph\n\begin{equation}"
1964 (let ((elem (org-element-at-point)))
1965 (and (eq (org-element-type elem) 'paragraph)
1966 (= (point-max) (org-element-property :end elem))))))
1967 ;; Stop at affiliated keywords.
1968 (should
1969 (org-test-with-temp-text "Paragraph\n#+NAME: test\n| table |"
1970 (let ((elem (org-element-at-point)))
1971 (and (eq (org-element-type elem) 'paragraph)
1972 (not (org-element-property :name elem))
1973 (= (org-element-property :end elem) (line-beginning-position 2))))))
1974 (should
1975 (org-test-with-temp-text
1976 "Paragraph\n#+CAPTION[with short caption]: test\n| table |"
1977 (let ((elem (org-element-at-point)))
1978 (and (eq (org-element-type elem) 'paragraph)
1979 (not (org-element-property :name elem))
1980 (= (org-element-property :end elem) (line-beginning-position 2))))))
1981 ;; Do not steal affiliated keywords from container.
1982 (should
1983 (org-test-with-temp-text "#+ATTR_LATEX: test\n- item<point> 1"
1984 (let ((elem (org-element-at-point)))
1985 (and (eq (org-element-type elem) 'paragraph)
1986 (not (org-element-property :attr_latex elem))
1987 (/= (org-element-property :begin elem) 1)))))
1988 ;; Handle non-empty blank line at the end of buffer.
1989 (should
1990 (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
1991 (= (org-element-property :end (org-element-at-point)) (point-max)))))
1994 ;;;; Plain List
1996 (ert-deftest test-org-element/plain-list-parser ()
1997 "Test `plain-list' parser."
1998 (org-test-with-temp-text "- item"
1999 (should (org-element-map (org-element-parse-buffer) 'plain-list 'identity)))
2000 ;; Blank lines after a list or sub-list belongs to that list.
2001 (should
2002 (= 1
2003 (org-test-with-temp-text "- A\n\n- B\n\nEnd list"
2004 (org-element-property :post-blank (org-element-at-point)))))
2005 (should
2006 (= 1
2007 (org-test-with-temp-text "- A\n\n<point> - B\n\n - C\n\n End sub-list"
2008 (org-element-property :post-blank (org-element-at-point)))))
2009 ;; Blank lines after the list only belong to outer plain list,
2010 ;; however.
2011 (should
2012 (equal
2013 '(t t)
2014 (org-test-with-temp-text "
2015 - outer
2016 - inner
2018 Outside list"
2019 (let ((endings (org-element-map (org-element-parse-buffer) 'plain-list
2020 (lambda (pl) (org-element-property :end pl)))))
2021 (list
2022 ;; Move to ending of outer list.
2023 (progn (goto-char (car endings)) (looking-at "Outside list"))
2024 ;; Move to ending of inner list.
2025 (progn (goto-char (nth 1 endings)) (looking-at "^$")))))))
2026 ;; Correctly compute end of list if it doesn't end at a line
2027 ;; beginning.
2028 (should
2029 (org-test-with-temp-text "- list\n \n "
2030 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2033 ;;;; Planning
2035 (ert-deftest test-org-element/planning-parser ()
2036 "Test `planning' parser."
2037 ;; Test various keywords.
2038 (should
2039 (org-element-property
2040 :closed
2041 (org-test-with-temp-text "* H\n<point>CLOSED: [2012-03-29 thu.]"
2042 (org-element-at-point))))
2043 (should
2044 (org-element-property
2045 :deadline
2046 (org-test-with-temp-text "* H\n<point>DEADLINE: <2012-03-29 thu.>"
2047 (org-element-at-point))))
2048 (should
2049 (org-element-property
2050 :scheduled
2051 (org-test-with-temp-text "* H\n<point>SCHEDULED: <2012-03-29 thu.>"
2052 (org-element-at-point))))
2053 ;; Planning line only exists right after a headline.
2054 (should-not
2055 (eq 'planning
2056 (org-test-with-temp-text "DEADLINE: <2012-03-29 thu.>"
2057 (org-element-type (org-element-at-point)))))
2058 (should-not
2059 (eq 'planning
2060 (org-test-with-temp-text
2061 "* H\n# Comment\n<point>DEADLINE: <2012-03-29 thu.>"
2062 (org-element-type (org-element-at-point)))))
2063 (should-not
2064 (eq 'planning
2065 (org-test-with-temp-text
2066 "* H\n\n<point>DEADLINE: <2012-03-29 thu.>"
2067 (org-element-type (org-element-at-point))))))
2070 ;;;; Property Drawer
2072 (ert-deftest test-org-element/property-drawer-parser ()
2073 "Test `property-drawer' parser."
2074 ;; Standard test.
2075 (should
2076 (eq 'property-drawer
2077 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value\n:END:"
2078 (org-element-type (org-element-at-point)))))
2079 (should
2080 (eq 'property-drawer
2081 (org-test-with-temp-text
2082 "* H\nDEADLINE: <2014-03-04 tue.>\n<point>:PROPERTIES:\n:prop: value\n:END:"
2083 (org-element-type (org-element-at-point)))))
2084 ;; Allow properties without value and no property at all.
2085 (should
2086 (eq 'property-drawer
2087 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop:\n:END:"
2088 (org-element-type (org-element-at-point)))))
2089 (should
2090 (eq 'property-drawer
2091 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:"
2092 (org-element-type (org-element-at-point)))))
2093 ;; Ignore incomplete drawer, drawer at a wrong location or with
2094 ;; wrong contents.
2095 (should-not
2096 (eq 'property-drawer
2097 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value"
2098 (org-element-type (org-element-at-point)))))
2099 (should-not
2100 (eq 'property-drawer
2101 (org-test-with-temp-text
2102 "* H\nParagraph\n<point>:PROPERTIES:\n:prop: value\n:END:"
2103 (org-element-type (org-element-at-point)))))
2104 (should-not
2105 (eq 'property-drawer
2106 (org-test-with-temp-text
2107 "* H\nParagraph\n<point>:PROPERTIES:\nparagraph\n:END:"
2108 (org-element-type (org-element-at-point)))))
2109 (should-not
2110 (eq 'property-drawer
2111 (org-test-with-temp-text
2112 "* H\n\n<point>:PROPERTIES:\n:prop: value\n:END:"
2113 (org-element-type (org-element-at-point)))))
2114 ;; Handle non-empty blank line at the end of buffer.
2115 (should
2116 (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:\n "
2117 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2120 ;;;; Quote Block
2122 (ert-deftest test-org-element/quote-block-parser ()
2123 "Test `quote-block' parser."
2124 ;; Regular test.
2125 (should
2126 (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
2127 (org-element-map (org-element-parse-buffer) 'quote-block 'identity)))
2128 ;; Ignore incomplete block.
2129 (should-not
2130 (org-test-with-temp-text "#+BEGIN_QUOTE"
2131 (org-element-map (org-element-parse-buffer) 'quote-block 'identity nil t)))
2132 ;; Handle non-empty blank line at the end of buffer.
2133 (should
2134 (org-test-with-temp-text "#+BEGIN_QUOTE\nC\n#+END_QUOTE\n "
2135 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2138 ;;;; Radio Target
2140 (ert-deftest test-org-element/radio-target-parser ()
2141 "Test `radio-target' parser."
2142 ;; Standard test.
2143 (should
2144 (eq 'radio-target
2145 (org-test-with-temp-text "<<<radio>>>"
2146 (org-element-type (org-element-context)))))
2147 ;; Radio targets with objects.
2148 (should
2149 (eq 'radio-target
2150 (org-test-with-temp-text "<<<radio \\alpha>>>"
2151 (org-element-type (org-element-context)))))
2152 ;; Radio targets starting with an object.
2153 (should
2154 (eq 'radio-target
2155 (org-test-with-temp-text "<<<\\alpha radio>>>"
2156 (org-element-type (org-element-context)))))
2157 ;; Radio targets cannot begin or end with white space.
2158 (should-not
2159 (eq 'radio-target
2160 (org-test-with-temp-text "<<< radio>>>"
2161 (org-element-type (org-element-context)))))
2162 (should-not
2163 (eq 'radio-target
2164 (org-test-with-temp-text "<<<radio >>>"
2165 (org-element-type (org-element-context))))))
2168 ;;;; Section
2170 (ert-deftest test-org-element/section-parser ()
2171 "Test `section' parser."
2172 ;; Standard test.
2173 (should
2174 (org-test-with-temp-text "* Headline\nText"
2175 (org-element-map (org-element-parse-buffer) 'section 'identity)))
2176 ;; There's a section before the first headline.
2177 (should
2178 (org-test-with-temp-text "Text"
2179 (org-element-map (org-element-parse-buffer) 'section 'identity)))
2180 ;; A section cannot be empty.
2181 (should-not
2182 (org-test-with-temp-text "* Headline 1\n* Headline 2"
2183 (org-element-map (org-element-parse-buffer) 'section 'identity)))
2184 ;; A section doesn't contain sub-trees.
2185 (should-not
2186 (org-test-with-temp-text "* Head\nText\n** Sub-Head"
2187 (org-element-map
2188 (org-element-map (org-element-parse-buffer) 'section 'identity nil t)
2189 'headline 'identity))))
2192 ;;;; Special Block
2194 (ert-deftest test-org-element/special-block-parser ()
2195 "Test `special-block' parser."
2196 ;; Standard test.
2197 (should
2198 (equal "SPECIAL"
2199 (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
2200 (org-element-property :type (org-element-at-point)))))
2201 ;; Special blocks are case sensitive.
2202 (should
2203 (equal "CaSe"
2204 (org-test-with-temp-text "#+BEGIN_CaSe\nText\n#+END_CaSe"
2205 (org-element-property :type (org-element-at-point)))))
2206 ;; Special blocks can contain paragraphs.
2207 (should
2208 (eq 'paragraph
2209 (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
2210 (forward-line)
2211 (org-element-type (org-element-at-point)))))
2212 ;; Ignore incomplete block.
2213 (should-not
2214 (eq 'special-block
2215 (org-test-with-temp-text "#+BEGIN_SPECIAL"
2216 (org-element-type (org-element-at-point)))))
2217 ;; Allow special characters in type.
2218 (should
2219 (equal '(special-block "SPECIAL*")
2220 (org-test-with-temp-text "#+BEGIN_SPECIAL*\nContents\n#+END_SPECIAL*"
2221 (let ((element (org-element-at-point)))
2222 (list (org-element-type element)
2223 (org-element-property :type element))))))
2224 ;; Handle non-empty blank line at the end of buffer.
2225 (should
2226 (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n "
2227 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2230 ;;;; Src Block
2232 (ert-deftest test-org-element/src-block-parser ()
2233 "Test `src-block' parser."
2234 ;; Regular tests.
2235 (should
2236 (org-test-with-temp-text "#+BEGIN_SRC org\nText\n#+END_SRC"
2237 (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
2238 ;; Ignore incomplete block.
2239 (should-not
2240 (org-test-with-temp-text "#+BEGIN_SRC"
2241 (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
2242 ;; Properly un-escape code.
2243 (should
2244 (equal "* Headline\n #+keyword\nText\n"
2245 (org-test-with-temp-text
2246 "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
2247 (org-element-property :value (org-element-at-point)))))
2248 ;; Handle non-empty blank line at the end of buffer.
2249 (should
2250 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\nC\n#+END_SRC\n "
2251 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2254 ;;;; Statistics Cookie
2256 (ert-deftest test-org-element/statistics-cookie ()
2257 "Test `statistics-cookie' parser."
2258 ;; With numbers.
2259 (should
2260 (org-test-with-temp-text "[1/2]"
2261 (org-element-map (org-element-parse-buffer) 'statistics-cookie 'identity)))
2262 ;; With percents.
2263 (should
2264 (org-test-with-temp-text "[33%]"
2265 (org-element-map
2266 (org-element-parse-buffer) 'statistics-cookie 'identity))))
2269 ;;;; Strike Through
2271 (ert-deftest test-org-element/strike-through-parser ()
2272 "Test `strike-through' parser."
2273 ;; Regular test.
2274 (should
2275 (org-test-with-temp-text "+strike-through+"
2276 (org-element-map (org-element-parse-buffer) 'strike-through #'identity)))
2277 ;; Multi-line markup.
2278 (should
2279 (equal
2280 (org-element-contents
2281 (org-test-with-temp-text "+first line\nsecond line+"
2282 (org-element-map
2283 (org-element-parse-buffer) 'strike-through #'identity nil t)))
2284 '("first line\nsecond line"))))
2287 ;;;; Subscript
2289 (ert-deftest test-org-element/subscript-parser ()
2290 "Test `subscript' parser."
2291 ;; Without braces.
2292 (should
2293 (org-test-with-temp-text "a_b"
2294 (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
2295 ;; With braces.
2296 (should
2297 (org-test-with-temp-text "a_{b}"
2298 (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
2299 ;; Multiple subscripts in a paragraph.
2300 (should
2301 (= 2
2302 (org-test-with-temp-text "a_b and c_d"
2303 (length
2304 (org-element-map (org-element-parse-buffer) 'subscript 'identity))))))
2307 ;;;; Superscript
2309 (ert-deftest test-org-element/superscript-parser ()
2310 "Test `superscript' parser."
2311 ;; Without braces.
2312 (should
2313 (org-test-with-temp-text "a^b"
2314 (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
2315 ;; With braces.
2316 (should
2317 (org-test-with-temp-text "a^{b}"
2318 (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
2319 ;; Multiple superscript in a paragraph.
2320 (should
2321 (= 2
2322 (org-test-with-temp-text "a^b and c^d"
2323 (length
2324 (org-element-map
2325 (org-element-parse-buffer) 'superscript 'identity))))))
2328 ;;;; Table
2330 (ert-deftest test-org-element/table-parser ()
2331 "Test `table' parser."
2332 (should
2333 (org-test-with-temp-text "| a |"
2334 (org-element-map (org-element-parse-buffer) 'table 'identity)))
2335 ;; TBLFM keyword is case insensitive.
2336 (should
2337 (org-test-with-temp-text "| a |\n#+tblfm: test"
2338 (org-element-property
2339 :tblfm
2340 (org-element-map (org-element-parse-buffer) 'table 'identity nil t))))
2341 ;; Handle multiple TBLFM lines.
2342 (should
2343 (= 2
2344 (org-test-with-temp-text "| a |\n#+TBLFM: test1\n#+TBLFM: test2"
2345 (length (org-element-property
2346 :tblfm
2347 (org-element-map
2348 (org-element-parse-buffer) 'table 'identity nil t))))))
2349 ;; Handle non-empty blank line at the end of buffer.
2350 (should
2351 (org-test-with-temp-text "| a |\n "
2352 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2355 ;;;; Table Cell
2357 (ert-deftest test-org-element/table-cell-parser ()
2358 "Test `table-cell' parser."
2359 ;; Regular table cell.
2360 (should
2361 (org-test-with-temp-text "| a |"
2362 (org-element-map (org-element-parse-buffer) 'table-cell 'identity)))
2363 ;; Last vertical bar may be omitted.
2364 (should
2365 (org-test-with-temp-text "| a "
2366 (org-element-map (org-element-parse-buffer) 'table-cell 'identity))))
2369 ;;;; Table Row
2371 (ert-deftest test-org-element/table-row-parser ()
2372 "Test `table-row' parser."
2373 (should
2374 (equal '(standard rule)
2375 (org-test-with-temp-text "| a |\n|---|"
2376 (org-element-map
2377 (org-element-parse-buffer) 'table-row
2378 (lambda (row) (org-element-property :type row)))))))
2381 ;;;; Target
2383 (ert-deftest test-org-element/target-parser ()
2384 "Test `target' parser."
2385 (should
2386 (org-test-with-temp-text "<<target>>"
2387 (org-element-map (org-element-parse-buffer) 'target 'identity))))
2390 ;;;; Timestamp
2392 (ert-deftest test-org-element/timestamp ()
2393 "Test `timestamp' parser."
2394 ;; Active timestamp.
2395 (should
2396 (org-test-with-temp-text "<2012-03-29 16:40>"
2397 (eq (org-element-property :type (org-element-context)) 'active)))
2398 (should-not
2399 (org-test-with-temp-text "<2012-03-29 Thu>"
2400 (let ((timestamp (org-element-context)))
2401 (or (org-element-property :hour-start timestamp)
2402 (org-element-property :minute-start timestamp)))))
2403 (should
2404 (equal '(2012 3 29 16 40)
2405 (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
2406 (let ((object (org-element-context)))
2407 (list (org-element-property :year-start object)
2408 (org-element-property :month-start object)
2409 (org-element-property :day-start object)
2410 (org-element-property :hour-start object)
2411 (org-element-property :minute-start object))))))
2412 ;; Inactive timestamp.
2413 (should
2414 (org-test-with-temp-text "[2012-03-29 Thu 16:40]"
2415 (eq (org-element-property :type (org-element-context)) 'inactive)))
2416 ;; Time range.
2417 (should
2418 (equal '(2012 3 29 16 40 7 30)
2419 (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
2420 (let ((object (org-element-context)))
2421 (list (org-element-property :year-end object)
2422 (org-element-property :month-end object)
2423 (org-element-property :day-end object)
2424 (org-element-property :hour-end object)
2425 (org-element-property :minute-end object)
2426 (org-element-property :hour-start object)
2427 (org-element-property :minute-start object))))))
2428 (should
2429 (eq 'active-range
2430 (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
2431 (org-element-property :type (org-element-context)))))
2432 ;; Date range.
2433 (should
2434 (org-test-with-temp-text "[2012-03-29 Thu 16:40]--[2012-03-29 Thu 16:41]"
2435 (eq (org-element-property :type (org-element-context)) 'inactive-range)))
2436 (should-not
2437 (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
2438 (let ((timestamp (org-element-context)))
2439 (or (org-element-property :hour-end timestamp)
2440 (org-element-property :minute-end timestamp)))))
2441 ;; With repeater, warning delay and both.
2442 (should
2443 (eq 'catch-up
2444 (org-test-with-temp-text "<2012-03-29 Thu ++1y>"
2445 (org-element-property :repeater-type (org-element-context)))))
2446 (should
2447 (eq 'first
2448 (org-test-with-temp-text "<2012-03-29 Thu --1y>"
2449 (org-element-property :warning-type (org-element-context)))))
2450 (should
2451 (equal '(cumulate all)
2452 (org-test-with-temp-text "<2012-03-29 Thu +1y -1y>"
2453 (let ((ts (org-element-context)))
2454 (list (org-element-property :repeater-type ts)
2455 (org-element-property :warning-type ts)))))))
2458 ;;;; Underline
2460 (ert-deftest test-org-element/underline-parser ()
2461 "Test `underline' parser."
2462 ;; Regular test.
2463 (should
2464 (org-test-with-temp-text "_underline_"
2465 (org-element-map (org-element-parse-buffer) 'underline #'identity)))
2466 ;; Multi-line markup.
2467 (should
2468 (equal
2469 (org-element-contents
2470 (org-test-with-temp-text "_first line\nsecond line_"
2471 (org-element-map
2472 (org-element-parse-buffer) 'underline #'identity nil t)))
2473 '("first line\nsecond line"))))
2476 ;;;; Verbatim
2478 (ert-deftest test-org-element/verbatim-parser ()
2479 "Test `verbatim' parser."
2480 ;; Regular test.
2481 (should
2482 (org-test-with-temp-text "=verbatim="
2483 (org-element-map (org-element-parse-buffer) 'verbatim #'identity)))
2484 ;; Multi-line markup.
2485 (should
2486 (equal
2487 (org-element-property
2488 :value
2489 (org-test-with-temp-text "=first line\nsecond line="
2490 (org-element-map (org-element-parse-buffer) 'verbatim #'identity nil t)))
2491 "first line\nsecond line")))
2494 ;;;; Verse Block
2496 (ert-deftest test-org-element/verse-block-parser ()
2497 "Test `verse-block' parser."
2498 ;; Standard test.
2499 (should
2500 (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
2501 (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
2502 ;; Ignore case.
2503 (should
2504 (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
2505 (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
2506 ;; Parse objects in verse blocks.
2507 (should
2508 (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
2509 (org-element-map (org-element-parse-buffer) 'entity 'identity)))
2510 ;; Ignore incomplete verse block.
2511 (should-not
2512 (org-test-with-temp-text "#+BEGIN_VERSE"
2513 (org-element-map (org-element-parse-buffer) 'verse-block 'identity nil t)))
2514 ;; Handle non-empty blank line at the end of buffer.
2515 (should
2516 (org-test-with-temp-text "#+BEGIN_VERSE\nC\n#+END_VERSE\n "
2517 (= (org-element-property :end (org-element-at-point)) (point-max)))))
2521 ;;; Test Interpreters.
2523 (ert-deftest test-org-element/interpret-data ()
2524 "Test `org-element-interpret-data' specifications."
2525 ;; Interpret simple affiliated keywords.
2526 (should
2527 (equal
2528 (org-element-interpret-data
2529 '(org-data nil (paragraph (:name "para") "Paragraph")))
2530 "#+name: para\nParagraph\n"))
2531 ;; Interpret multiple affiliated keywords.
2532 (should
2533 (equal
2534 (org-element-interpret-data
2535 '(org-data nil (paragraph (:attr_ascii ("line2" "line1")) "Paragraph")))
2536 "#+attr_ascii: line1\n#+attr_ascii: line2\nParagraph\n"))
2537 ;; Interpret parsed affiliated keywords.
2538 (should
2539 (equal
2540 (org-element-interpret-data
2541 '(org-data nil (paragraph (:caption (("caption"))) "Paragraph")))
2542 "#+caption: caption\nParagraph\n"))
2543 ;; Interpret dual affiliated keywords.
2544 (should
2545 (equal
2546 (org-element-interpret-data
2547 '(org-data nil (paragraph (:caption ((("long") "short"))) "Paragraph")))
2548 "#+caption[short]: long\nParagraph\n"))
2549 ;; Interpret multiple parsed dual keywords.
2550 (should
2551 (equal
2552 (org-element-interpret-data
2553 '(org-data nil (paragraph
2554 (:caption ((("l2") "s2") (("l1") "s1"))) "Paragraph")))
2555 "#+caption[s1]: l1\n#+caption[s2]: l2\nParagraph\n"))
2556 ;; Pseudo objects and elements are transparent.
2557 (should
2558 (equal "A B"
2559 (org-trim
2560 (org-element-interpret-data
2561 '(paragraph nil (pseudo-object (:post-blank 1) "A") "B")))))
2562 (should
2563 (equal "A\n\nB\n"
2564 (org-element-interpret-data
2565 '(center nil
2566 (pseudo-element (:post-blank 1) (paragraph nil "A"))
2567 (paragraph nil "B"))))))
2569 (ert-deftest test-org-element/center-block-interpreter ()
2570 "Test center block interpreter."
2571 (should
2572 (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
2573 "#+begin_center\nTest\n#+end_center\n")))
2575 (ert-deftest test-org-element/drawer-interpreter ()
2576 "Test drawer interpreter."
2577 (should
2578 (equal (org-test-parse-and-interpret ":TEST:\nTest\n:END:")
2579 ":TEST:\nTest\n:END:\n")))
2581 (ert-deftest test-org-element/dynamic-block-interpreter ()
2582 "Test dynamic block interpreter."
2583 (should
2584 (equal (org-test-parse-and-interpret
2585 "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
2586 "#+begin: myblock :parameter value1\nTest\n#+end:\n")))
2588 (ert-deftest test-org-element/footnote-definition-interpreter ()
2589 "Test footnote definition interpreter."
2590 (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n"))
2591 ;; Handle `:pre-blank' in definitions.
2592 (should
2593 (equal (org-test-parse-and-interpret "[fn:1]\nparagraph")
2594 "[fn:1]\nparagraph\n"))
2595 (should
2596 (equal (org-test-parse-and-interpret "[fn:1]\n\nparagraph")
2597 "[fn:1]\n\nparagraph\n")))
2599 (ert-deftest test-org-element/headline-interpreter ()
2600 "Test headline and section interpreters."
2601 ;; 1. Standard test.
2602 (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
2603 ;; 2. With TODO keywords.
2604 (should
2605 (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
2606 (org-test-parse-and-interpret "* TODO Headline"))
2607 "* TODO Headline\n"))
2608 ;; 3. With tags...
2610 ;; 3.1. ... and a positive `org-tags-column' value.
2611 (should
2612 (equal (let ((org-tags-column 20))
2613 (org-test-parse-and-interpret "* Headline :tag:"))
2614 "* Headline :tag:\n"))
2615 ;; 3.2. ... and a negative `org-tags-column' value.
2616 (should
2617 (equal (let ((org-tags-column -20))
2618 (org-test-parse-and-interpret "* Headline :tag:"))
2619 "* Headline :tag:\n"))
2620 ;; 3.3. ... and a null `org-tags-column' value.
2621 (should
2622 (equal (let ((org-tags-column 0))
2623 (org-test-parse-and-interpret "* Headline :tag:"))
2624 "* Headline :tag:\n"))
2625 ;; 4. With priority cookie.
2626 (should
2627 (equal (org-test-parse-and-interpret "* [#B] Headline")
2628 "* [#B] Headline\n"))
2629 ;; 5. With comment keyword.
2630 (should
2631 (equal (let ((org-comment-string "COMMENT"))
2632 (org-test-parse-and-interpret "* COMMENT Headline"))
2633 "* COMMENT Headline\n"))
2634 ;; 6. Keep same number of blank lines before body.
2635 (should
2636 (equal (org-test-parse-and-interpret
2637 "* Headline\n\n\nText after two blank lines.")
2638 "* Headline\n\n\nText after two blank lines.\n"))
2639 ;; 8. Preserve `org-odd-levels-only' state.
2640 (should
2641 (equal "* H\n*** H2\n"
2642 (let ((org-odd-levels-only t))
2643 (org-test-parse-and-interpret "* H\n*** H2")))))
2645 (ert-deftest test-org-element/inlinetask-interpreter ()
2646 "Test inlinetask interpretation."
2647 (when (featurep 'org-inlinetask)
2648 (let ((org-inlinetask-min-level 15))
2649 ;; 1. Regular inlinetask.
2650 (should (equal (org-test-parse-and-interpret
2651 "*************** Task\nTest\n*************** END")
2652 "*************** Task\nTest\n*************** end\n"))
2653 ;; 2. Degenerate inlinetask.
2654 (should (equal (org-test-parse-and-interpret "*************** Task")
2655 "*************** Task\n"))
2656 ;; 3. Prefer degenerate form when there are no contents.
2657 (should (equal (org-test-parse-and-interpret
2658 "*************** Task\n*************** end")
2659 "*************** Task\n"))
2660 ;; 4. With TODO keywords.
2661 (should
2662 (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
2663 (org-test-parse-and-interpret "*************** TODO Task"))
2664 "*************** TODO Task\n"))
2665 ;; 5. With tags...
2667 ;; 5.1. ... and a positive `org-tags-column' value.
2668 (should
2669 (equal (let ((org-tags-column 30))
2670 (org-test-parse-and-interpret "*************** Task :tag:"))
2671 "*************** Task :tag:\n"))
2672 ;; 5.2. ... and a negative `org-tags-column' value.
2673 (should
2674 (equal (let ((org-tags-column -30))
2675 (org-test-parse-and-interpret "*************** Task :tag:"))
2676 "*************** Task :tag:\n"))
2677 ;; 5.3. ... and a null `org-tags-column' value.
2678 (should
2679 (equal (let ((org-tags-column 0))
2680 (org-test-parse-and-interpret "*************** Task :tag:"))
2681 "*************** Task :tag:\n"))
2682 ;; 6. With priority cookie.
2683 (should
2684 (equal (org-test-parse-and-interpret "*************** [#B] Task")
2685 "*************** [#B] Task\n")))))
2687 (ert-deftest test-org-element/plain-list-interpreter ()
2688 "Test plain-list and item interpreters."
2689 (let ((org-list-two-spaces-after-bullet-regexp nil))
2690 ;; Unordered list.
2691 (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
2692 ;; Description list.
2693 (should
2694 (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
2695 ;; Ordered list.
2696 (should
2697 (equal (let ((org-plain-list-ordered-item-terminator t))
2698 (org-test-parse-and-interpret "1. Item"))
2699 "1. Item\n"))
2700 (should
2701 (equal (let ((org-plain-list-ordered-item-terminator ?\)))
2702 (org-test-parse-and-interpret "1) Item"))
2703 "1) Item\n"))
2704 ;; Ordered list with counter.
2705 (should
2706 (equal (let ((org-plain-list-ordered-item-terminator t))
2707 (org-test-parse-and-interpret "1. [@5] Item"))
2708 "5. [@5] Item\n"))
2709 ;; List with check-boxes.
2710 (should
2711 (equal (org-test-parse-and-interpret
2712 "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3")
2713 "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3\n"))
2714 ;; Item not starting with a paragraph.
2715 (should
2716 (equal (org-test-parse-and-interpret "-\n | a | b |")
2717 "- \n | a | b |\n"))
2718 ;; Handle `:pre-blank' in items.
2719 (should
2720 (equal (org-test-parse-and-interpret "-\n paragraph")
2721 "- \n paragraph\n"))
2722 (should
2723 (equal (org-test-parse-and-interpret "-\n\n paragraph")
2724 "- \n\n paragraph\n"))
2725 ;; Special case: correctly handle "*" bullets.
2726 (should (org-test-parse-and-interpret " * item"))
2727 ;; Special case: correctly handle empty items.
2728 (should (org-test-parse-and-interpret "-"))))
2730 (ert-deftest test-org-element/quote-block-interpreter ()
2731 "Test quote block interpreter."
2732 (should (equal (org-test-parse-and-interpret
2733 "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
2734 "#+begin_quote\nTest\n#+end_quote\n")))
2736 (ert-deftest test-org-element/special-block-interpreter ()
2737 "Test special block interpreter."
2738 (should (equal (org-test-parse-and-interpret
2739 "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
2740 "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n")))
2742 (ert-deftest test-org-element/babel-call-interpreter ()
2743 "Test Babel call interpreter."
2744 ;; Without argument.
2745 (should (equal (org-test-parse-and-interpret "#+CALL: test()")
2746 "#+call: test()\n"))
2747 ;; With argument.
2748 (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
2749 "#+call: test(x=2)\n"))
2750 ;; With header arguments.
2751 (should (equal (org-test-parse-and-interpret
2752 "#+CALL: test[:results output]() :results html")
2753 "#+call: test[:results output]() :results html\n")))
2755 (ert-deftest test-org-element/clock-interpreter ()
2756 "Test clock interpreter."
2757 ;; Running clock.
2758 (should
2759 (string-match
2760 "CLOCK: \\[2012-01-01 .* 00:01\\]"
2761 (org-test-parse-and-interpret "CLOCK: [2012-01-01 sun. 00:01]")))
2762 ;; Closed clock.
2763 (should
2764 (string-match
2765 "CLOCK: \\[2012-01-01 .* 00:01\\]--\\[2012-01-01 .* 00:02\\] => 0:01"
2766 (org-test-parse-and-interpret "
2767 CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))))
2769 (ert-deftest test-org-element/comment-interpreter ()
2770 "Test comment interpreter."
2771 ;; Regular comment.
2772 (should (equal (org-test-parse-and-interpret "# Comment") "# Comment\n"))
2773 ;; Inline comment.
2774 (should (equal (org-test-parse-and-interpret " # Comment")
2775 "# Comment\n"))
2776 ;; Preserve indentation.
2777 (should (equal (org-test-parse-and-interpret " # No blank\n# One blank")
2778 "# No blank\n# One blank\n")))
2780 (ert-deftest test-org-element/comment-block-interpreter ()
2781 "Test comment block interpreter."
2782 (should (equal (org-test-parse-and-interpret
2783 "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
2784 "#+begin_comment\nTest\n#+end_comment\n"))
2785 ;; Accept missing final newline in value.
2786 (should
2787 (equal "#+begin_comment\nTest\n#+end_comment\n"
2788 (org-element-interpret-data '(comment-block (:value "Test"))))))
2790 (ert-deftest test-org-element/diary-sexp ()
2791 "Test diary-sexp interpreter."
2792 (should
2793 (equal
2794 (org-test-parse-and-interpret
2795 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old")
2796 "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old\n")))
2798 (ert-deftest test-org-element/example-block-interpreter ()
2799 "Test example block interpreter."
2800 ;; Without switches.
2801 (should (equal "#+begin_example\nTest\n#+end_example\n"
2802 (let ((org-src-preserve-indentation t))
2803 (org-test-parse-and-interpret
2804 "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"))))
2805 ;; With switches.
2806 (should
2807 (equal "#+begin_example -n -k\n(+ 1 1)\n#+end_example\n"
2808 (let ((org-src-preserve-indentation t))
2809 (org-test-parse-and-interpret
2810 "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE"))))
2811 ;; Preserve code escaping.
2812 (should
2813 (equal
2814 (let ((org-src-preserve-indentation t))
2815 (org-test-parse-and-interpret
2816 "#+BEGIN_EXAMPLE\n,* Headline\n,#+KEYWORD: value\nText\n#+END_EXAMPLE"))
2817 "#+begin_example\n,* Headline\n,#+KEYWORD: value\nText\n#+end_example\n"))
2818 ;; Accept missing final newline in value.
2819 (should
2820 (equal
2821 "#+begin_example\nTest\n#+end_example\n"
2822 (let ((org-src-preserve-indentation t))
2823 (org-element-interpret-data '(example-block (:value "Test"))))))
2824 ;; Handle indentation.
2825 (should (equal "#+begin_example\n Test\n#+end_example\n"
2826 (let ((org-src-preserve-indentation nil)
2827 (org-edit-src-content-indentation 2))
2828 (org-test-parse-and-interpret
2829 "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"))))
2830 (should (equal "#+begin_example\n Test\n#+end_example\n"
2831 (let ((org-src-preserve-indentation t)
2832 (org-edit-src-content-indentation 2))
2833 (org-test-parse-and-interpret
2834 "#+BEGIN_EXAMPLE\n Test\n#+END_EXAMPLE")))))
2836 (ert-deftest test-org-element/export-block-interpreter ()
2837 "Test export block interpreter."
2838 (should (equal (org-test-parse-and-interpret
2839 "#+begin_export HTML\nTest\n#+end_export")
2840 "#+begin_export HTML\nTest\n#+end_export\n")))
2842 (ert-deftest test-org-element/fixed-width-interpreter ()
2843 "Test fixed width interpreter."
2844 ;; Standard test.
2845 (should (equal (org-test-parse-and-interpret ": Test") ": Test\n"))
2846 ;; Preserve indentation.
2847 (should (equal (org-test-parse-and-interpret ": 2 blanks\n: 1 blank")
2848 ": 2 blanks\n: 1 blank\n"))
2849 ;; Remove last newline character
2850 (should
2851 (equal (org-element-fixed-width-interpreter
2852 '(fixed-width (:value "Test\n")) nil)
2853 ": Test"))
2854 (should
2855 (equal (org-element-fixed-width-interpreter
2856 '(fixed-width (:value "Test")) nil)
2857 ": Test"))
2858 ;; Handle empty string.
2859 (should
2860 (equal (org-element-fixed-width-interpreter
2861 '(fixed-width (:value "")) nil)
2862 ""))
2863 ;; Handle nil value.
2864 (should-not
2865 (org-element-fixed-width-interpreter
2866 '(fixed-width (:value nil)) nil)))
2868 (ert-deftest test-org-element/horizontal-rule-interpreter ()
2869 "Test horizontal rule interpreter."
2870 (should (equal (org-test-parse-and-interpret "-------") "-----\n")))
2872 (ert-deftest test-org-element/keyword-interpreter ()
2873 "Test keyword interpreter."
2874 (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
2875 "#+keyword: value\n")))
2877 (ert-deftest test-org-element/latex-environment-interpreter ()
2878 "Test latex environment interpreter."
2879 (should (equal (org-test-parse-and-interpret
2880 "\\begin{equation}\n1+1=2\n\\end{equation}")
2881 "\\begin{equation}\n1+1=2\n\\end{equation}\n"))
2882 (should (equal (org-test-parse-and-interpret
2883 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}")
2884 "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}\n")))
2886 (ert-deftest test-org-element/planning-interpreter ()
2887 "Test planning interpreter."
2888 (should
2889 (string-match
2890 "\\* Headline
2891 DEADLINE: <2012-03-29 .*?> SCHEDULED: <2012-03-29 .*?> CLOSED: \\[2012-03-29 .*?\\]"
2892 (org-test-parse-and-interpret
2893 "* Headline
2894 DEADLINE: <2012-03-29 thu.> SCHEDULED: <2012-03-29 thu.> CLOSED: [2012-03-29 thu.]"))))
2896 (ert-deftest test-org-element/property-drawer-interpreter ()
2897 "Test property drawer interpreter."
2898 (should (equal (let ((org-property-format "%-10s %s"))
2899 (org-test-parse-and-interpret
2900 "* H\n:PROPERTIES:\n:prop: value\n:END:"))
2901 "* H\n:PROPERTIES:\n:prop: value\n:END:\n")))
2903 (ert-deftest test-org-element/src-block-interpreter ()
2904 "Test src block interpreter."
2905 ;; With arguments.
2906 (should
2907 (equal (let ((org-edit-src-content-indentation 2)
2908 (org-src-preserve-indentation nil))
2909 (org-test-parse-and-interpret
2910 "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC"))
2911 "#+begin_src emacs-lisp :results silent\n (+ 1 1)\n#+end_src\n"))
2912 ;; With switches.
2913 (should
2914 (equal (let ((org-edit-src-content-indentation 2)
2915 (org-src-preserve-indentation nil))
2916 (org-test-parse-and-interpret
2917 "#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
2918 "#+begin_src emacs-lisp -n -k\n (+ 1 1)\n#+end_src\n"))
2919 ;; Preserve code escaping.
2920 (should
2921 (equal
2922 (let ((org-edit-src-content-indentation 2)
2923 (org-src-preserve-indentation nil))
2924 (org-test-parse-and-interpret
2925 "#+BEGIN_SRC org\n,* Headline\n,#+KEYWORD: value\nText\n#+END_SRC"))
2926 "#+begin_src org\n ,* Headline\n ,#+KEYWORD: value\n Text\n#+end_src\n"))
2927 ;; Do not apply `org-edit-src-content-indentation' when preserving
2928 ;; indentation.
2929 (should
2930 (equal (let ((org-edit-src-content-indentation 2)
2931 (org-src-preserve-indentation t))
2932 (org-test-parse-and-interpret
2933 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"))
2934 "#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src\n"))
2935 (should
2936 (equal (let ((org-edit-src-content-indentation 2)
2937 (org-src-preserve-indentation nil))
2938 (org-test-parse-and-interpret
2939 "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"))
2940 "#+begin_src emacs-lisp -i\n(+ 1 1)\n#+end_src\n"))
2941 ;; Accept missing final newline in value.
2942 (should
2943 (equal
2944 "#+begin_src emacs-lisp\n Test\n#+end_src\n"
2945 (let ((org-edit-src-content-indentation 2)
2946 (org-src-preserve-indentation nil))
2947 (org-element-interpret-data
2948 '(src-block (:language "emacs-lisp" :value "Test")))))))
2950 (ert-deftest test-org-element/table-interpreter ()
2951 "Test table, table-row and table-cell interpreters."
2952 ;; 1. Simple table.
2953 (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
2954 "| a | b |\n| c | d |\n"))
2955 ;; 2. With horizontal rules.
2956 (should (equal (org-test-parse-and-interpret
2957 "| a | b |\n|---+---|\n| c | d |")
2958 "| a | b |\n|---+---|\n| c | d |\n"))
2959 ;; 3. With meta-data.
2960 (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
2961 "| / | < | > |\n| * | 1 | 2 |\n"))
2962 ;; 4. With a formula.
2963 (should
2964 (equal (org-test-parse-and-interpret
2965 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)")
2966 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)\n"))
2967 ;; 5. With multiple formulas.
2968 (should
2969 (equal (org-test-parse-and-interpret
2970 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2")
2971 "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2\n")))
2973 (ert-deftest test-org-element/timestamp-interpreter ()
2974 "Test timestamp interpreter."
2975 ;; Active.
2976 (should
2977 (string-match "<2012-03-29 .* 16:40>"
2978 (org-test-parse-and-interpret "<2012-03-29 thu. 16:40>")))
2979 (should
2980 (string-match "<2012-03-29 .* 16:40>"
2981 (org-element-timestamp-interpreter
2982 '(timestamp
2983 (:type active :year-start 2012 :month-start 3 :day-start 29
2984 :hour-start 16 :minute-start 40)) nil)))
2985 ;; Inactive.
2986 (should
2987 (string-match "\\[2012-03-29 .* 16:40\\]"
2988 (org-test-parse-and-interpret "[2012-03-29 thu. 16:40]")))
2989 (should
2990 (string-match
2991 "\\[2012-03-29 .* 16:40\\]"
2992 (org-element-timestamp-interpreter
2993 '(timestamp
2994 (:type inactive :year-start 2012 :month-start 3 :day-start 29
2995 :hour-start 16 :minute-start 40)) nil)))
2996 ;; Active range.
2997 (should
2998 (string-match "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
2999 (org-test-parse-and-interpret
3000 "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>")))
3001 (should
3002 (string-match
3003 "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
3004 (org-element-timestamp-interpreter
3005 '(timestamp
3006 (:type active-range :year-start 2012 :month-start 3 :day-start 29
3007 :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
3008 :day-end 29 :hour-end 16 :minute-end 41)) nil)))
3009 ;; Inactive range.
3010 (should
3011 (string-match "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
3012 (org-test-parse-and-interpret
3013 "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]")))
3014 (should
3015 (string-match
3016 "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
3017 (org-element-timestamp-interpreter
3018 '(timestamp
3019 (:type inactive-range :year-start 2012 :month-start 3 :day-start 29
3020 :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
3021 :day-end 29 :hour-end 16 :minute-end 41)) nil)))
3022 ;; Diary.
3023 (should (equal (org-test-parse-and-interpret "<%%diary-float t 4 2>")
3024 "<%%diary-float t 4 2>\n"))
3025 ;; Timestamp with repeater interval, with delay, with both.
3026 (should
3027 (string-match "<2012-03-29 .* \\+1y>"
3028 (org-test-parse-and-interpret "<2012-03-29 thu. +1y>")))
3029 (should
3030 (string-match
3031 "<2012-03-29 .* \\+1y>"
3032 (org-element-timestamp-interpreter
3033 '(timestamp
3034 (:type active :year-start 2012 :month-start 3 :day-start 29
3035 :repeater-type cumulate :repeater-value 1 :repeater-unit year))
3036 nil)))
3037 (should
3038 (string-match
3039 "<2012-03-29 .* -1y>"
3040 (org-element-timestamp-interpreter
3041 '(timestamp
3042 (:type active :year-start 2012 :month-start 3 :day-start 29
3043 :warning-type all :warning-value 1 :warning-unit year))
3044 nil)))
3045 (should
3046 (string-match
3047 "<2012-03-29 .* \\+1y -1y>"
3048 (org-element-timestamp-interpreter
3049 '(timestamp
3050 (:type active :year-start 2012 :month-start 3 :day-start 29
3051 :warning-type all :warning-value 1 :warning-unit year
3052 :repeater-type cumulate :repeater-value 1 :repeater-unit year))
3053 nil)))
3054 ;; Timestamp range with repeater interval
3055 (should
3056 (string-match "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
3057 (org-test-parse-and-interpret
3058 "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>")))
3059 (should
3060 (string-match
3061 "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
3062 (org-element-timestamp-interpreter
3063 '(timestamp
3064 (:type active-range :year-start 2012 :month-start 3 :day-start 29
3065 :year-end 2012 :month-end 3 :day-end 30 :repeater-type cumulate
3066 :repeater-value 1 :repeater-unit year))
3067 nil))))
3069 (ert-deftest test-org-element/verse-block-interpreter ()
3070 "Test verse block interpretation."
3071 (should
3072 (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
3073 "#+begin_verse\nTest\n#+end_verse\n")))
3075 (ert-deftest test-org-element/bold-interpreter ()
3076 "Test bold interpreter."
3077 (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))
3079 (ert-deftest test-org-element/code-interpreter ()
3080 "Test code interpreter."
3081 (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))
3083 (ert-deftest test-org-element/entity-interpreter ()
3084 "Test entity interpreter."
3085 ;; 1. Without brackets.
3086 (should
3087 (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
3088 ;; 2. With brackets.
3089 (should
3090 (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))
3092 (ert-deftest test-org-element/export-snippet-interpreter ()
3093 "Test export snippet interpreter."
3094 (should (equal (org-test-parse-and-interpret "@@back-end:contents@@")
3095 "@@back-end:contents@@\n")))
3097 (ert-deftest test-org-element/footnote-reference-interpreter ()
3098 "Test footnote reference interpreter."
3099 ;; Regular reference.
3100 (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
3101 ;; Named reference.
3102 (should (equal (org-test-parse-and-interpret "Text[fn:label]")
3103 "Text[fn:label]\n"))
3104 ;; Inline reference.
3105 (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
3106 "Text[fn:label:def]\n"))
3107 ;; Anonymous reference.
3108 (should (equal (org-test-parse-and-interpret "Text[fn::def]")
3109 "Text[fn::def]\n")))
3111 (ert-deftest test-org-element/inline-babel-call-interpreter ()
3112 "Test inline babel call interpreter."
3113 ;; Without arguments.
3114 (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
3115 ;; With arguments.
3116 (should (equal (org-test-parse-and-interpret "call_test(x=2)")
3117 "call_test(x=2)\n"))
3118 ;; With header arguments.
3119 (should (equal (org-test-parse-and-interpret
3120 "call_test[:results output]()[:results html]")
3121 "call_test[:results output]()[:results html]\n")))
3123 (ert-deftest test-org-element/inline-src-block-interpreter ()
3124 "Test inline src block interpreter."
3125 ;; 1. Without header argument.
3126 (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
3127 "src_emacs-lisp{(+ 1 1)}\n"))
3128 ;; 2. With header arguments.
3129 (should (equal (org-test-parse-and-interpret
3130 "src_emacs-lisp[:results silent]{(+ 1 1)}")
3131 "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))
3133 (ert-deftest test-org-element/italic-interpreter ()
3134 "Test italic interpreter."
3135 (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))
3137 (ert-deftest test-org-element/latex-fragment-interpreter ()
3138 "Test latex fragment interpreter."
3139 (should (equal (org-test-parse-and-interpret "\\command{}") "\\command{}\n"))
3140 (should (equal (org-test-parse-and-interpret "$x$") "$x$\n"))
3141 (should (equal (org-test-parse-and-interpret "$x+y$") "$x+y$\n"))
3142 (should (equal (org-test-parse-and-interpret "$$x+y$$") "$$x+y$$\n"))
3143 (should (equal (org-test-parse-and-interpret "\\(x+y\\)") "\\(x+y\\)\n"))
3144 (should (equal (org-test-parse-and-interpret "\\[x+y\\]") "\\[x+y\\]\n")))
3146 (ert-deftest test-org-element/line-break-interpreter ()
3147 "Test line break interpreter."
3148 (should (equal (org-test-parse-and-interpret "First line \\\\ \nSecond line")
3149 "First line \\\\\nSecond line\n")))
3151 (ert-deftest test-org-element/link-interpreter ()
3152 "Test link interpreter."
3153 ;; Links targeted from a radio target.
3154 (should (equal (let ((org-target-link-regexp "radio-target"))
3155 (org-test-parse-and-interpret "a radio-target"))
3156 "a radio-target\n"))
3157 ;; Links without description.
3158 (should (equal (org-test-parse-and-interpret "[[https://orgmode.org]]")
3159 "[[https://orgmode.org]]\n"))
3160 ;; Links with a description, even one containing a link.
3161 (should (equal (org-test-parse-and-interpret
3162 "[[https://orgmode.org][Org mode]]")
3163 "[[https://orgmode.org][Org mode]]\n"))
3164 (should (equal (org-test-parse-and-interpret
3165 "[[https://orgmode.org][https://orgmode.org]]")
3166 "[[https://orgmode.org][https://orgmode.org]]\n"))
3167 ;; File links.
3168 (should
3169 (equal (org-test-parse-and-interpret "[[file+emacs:todo.org]]")
3170 "[[file+emacs:todo.org]]\n"))
3171 (should
3172 (equal (org-test-parse-and-interpret "[[file:todo.org::*task]]")
3173 "[[file:todo.org::*task]]\n"))
3174 ;; Id links.
3175 (should (equal (org-test-parse-and-interpret "[[id:aaaa]]") "[[id:aaaa]]\n"))
3176 ;; Custom-id links.
3177 (should (equal (org-test-parse-and-interpret "[[#id]]") "[[#id]]\n"))
3178 ;; Code-ref links.
3179 (should (equal (org-test-parse-and-interpret "[[(ref)]]") "[[(ref)]]\n"))
3180 ;; Plain links.
3181 (should (equal (org-test-parse-and-interpret "https://orgmode.org")
3182 "https://orgmode.org\n"))
3183 ;; Angular links.
3184 (should (equal (org-test-parse-and-interpret "<https://orgmode.org>")
3185 "<https://orgmode.org>\n"))
3186 ;; Pathological case: link with a %-sign in description.
3187 (should (equal (org-test-parse-and-interpret "[[file://path][%s]]")
3188 "[[file://path][%s]]\n")))
3190 (ert-deftest test-org-element/macro-interpreter ()
3191 "Test macro interpreter."
3192 ;; 1. Without argument.
3193 (should (equal (org-test-parse-and-interpret "{{{test}}}") "{{{test}}}\n"))
3194 ;; 2. With arguments.
3195 (should (equal (org-test-parse-and-interpret "{{{test(arg1,arg2)}}}")
3196 "{{{test(arg1,arg2)}}}\n")))
3198 (ert-deftest test-org-element/radio-target-interpreter ()
3199 "Test radio target interpreter."
3200 (should (equal (org-test-parse-and-interpret "<<<some text>>>")
3201 "<<<some text>>>\n")))
3203 (ert-deftest test-org-element/statistics-cookie-interpreter ()
3204 "Test statistics cookie interpreter."
3205 ;; 1. Without percent
3206 (should (equal (org-test-parse-and-interpret "[0/1]") "[0/1]\n"))
3207 ;; 2. With percent.
3208 (should (equal (org-test-parse-and-interpret "[66%]") "[66%]\n")))
3210 (ert-deftest test-org-element/strike-through-interpreter ()
3211 "Test strike through interpreter."
3212 (should (equal (org-test-parse-and-interpret "+target+") "+target+\n")))
3214 (ert-deftest test-org-element/subscript-interpreter ()
3215 "Test subscript interpreter."
3216 ;; 1. Without brackets.
3217 (should (equal (org-test-parse-and-interpret "a_b") "a_b\n"))
3218 ;; 2. With brackets.
3219 (should (equal (org-test-parse-and-interpret "a_{b}") "a_{b}\n")))
3221 (ert-deftest test-org-element/superscript-interpreter ()
3222 "Test superscript interpreter."
3223 ;; 1. Without brackets.
3224 (should (equal (org-test-parse-and-interpret "a^b") "a^b\n"))
3225 ;; 2. With brackets.
3226 (should (equal (org-test-parse-and-interpret "a^{b}") "a^{b}\n")))
3228 (ert-deftest test-org-element/target-interpreter ()
3229 "Test target interpreter."
3230 (should (equal (org-test-parse-and-interpret "<<target>>") "<<target>>\n")))
3232 (ert-deftest test-org-element/underline-interpreter ()
3233 "Test underline interpreter."
3234 (should (equal (org-test-parse-and-interpret "_text_") "_text_\n")))
3236 (ert-deftest test-org-element/verbatim-interpreter ()
3237 "Test verbatim interpreter."
3238 (should (equal (org-test-parse-and-interpret "=text=") "=text=\n")))
3242 ;;; Test Granularity
3244 (ert-deftest test-org-element/granularity ()
3245 "Test granularity impact on buffer parsing."
3246 (org-test-with-temp-text "
3247 * Head 1
3248 ** Head 2
3249 #+BEGIN_CENTER
3250 Centered paragraph.
3251 #+END_CENTER
3252 Paragraph \\alpha."
3253 ;; 1.1. Granularity set to `headline' should parse every headline
3254 ;; in buffer, and only them.
3255 (let ((tree (org-element-parse-buffer 'headline)))
3256 (should (= 2 (length (org-element-map tree 'headline 'identity))))
3257 (should-not (org-element-map tree 'paragraph 'identity)))
3258 ;; 1.2. Granularity set to `greater-element' should not enter
3259 ;; greater elements excepted headlines and sections.
3260 (let ((tree (org-element-parse-buffer 'greater-element)))
3261 (should (= 1 (length (org-element-map tree 'center-block 'identity))))
3262 (should (= 1 (length (org-element-map tree 'paragraph 'identity))))
3263 (should-not (org-element-map tree 'entity 'identity)))
3264 ;; 1.3. Granularity set to `element' should enter every
3265 ;; greater-element.
3266 (let ((tree (org-element-parse-buffer 'element)))
3267 (should (= 2 (length (org-element-map tree 'paragraph 'identity))))
3268 (should-not (org-element-map tree 'entity 'identity)))
3269 ;; 1.4. Granularity set to `object' can see everything.
3270 (let ((tree (org-element-parse-buffer 'object)))
3271 (should (= 1 (length (org-element-map tree 'entity 'identity)))))))
3273 (ert-deftest test-org-element/secondary-string-parsing ()
3274 "Test if granularity correctly toggles secondary strings parsing."
3275 ;; With a granularity bigger than `object', no secondary string
3276 ;; should be parsed.
3277 (should
3278 (stringp
3279 (org-test-with-temp-text "* Headline"
3280 (let ((headline
3281 (org-element-map (org-element-parse-buffer 'headline) 'headline
3282 #'identity nil 'first-match)))
3283 (org-element-property :title headline)))))
3284 (should
3285 (stringp
3286 (org-test-with-temp-text "* Headline\n- tag :: item"
3287 (let ((item (org-element-map (org-element-parse-buffer 'element) 'item
3288 #'identity nil 'first-match)))
3289 (org-element-property :tag item)))))
3290 (when (featurep 'org-inlinetask)
3291 (should
3292 (stringp
3293 (let ((org-inlinetask-min-level 15))
3294 (org-test-with-temp-text "*************** Inlinetask"
3295 (let ((inlinetask (org-element-map (org-element-parse-buffer 'element)
3296 'inlinetask
3297 #'identity nil 'first-match)))
3298 (org-element-property :title inlinetask)))))))
3299 ;; With a default granularity, secondary strings should be parsed.
3300 (should
3301 (listp
3302 (org-test-with-temp-text "* Headline"
3303 (let ((headline
3304 (org-element-map (org-element-parse-buffer) 'headline
3305 #'identity nil 'first-match)))
3306 (org-element-property :title headline)))))
3307 ;; `org-element-at-point' should never parse a secondary string.
3308 (should-not
3309 (listp
3310 (org-test-with-temp-text "* Headline"
3311 (org-element-property :title (org-element-at-point)))))
3312 ;; Preserve current local variables when parsing a secondary string.
3313 (should
3314 (let ((org-entities nil)
3315 (org-entities-user nil))
3316 (org-test-with-temp-text "
3317 #+CAPTION: \\foo
3318 Text
3319 # Local Variables:
3320 # org-entities-user: ((\"foo\"))
3321 # End:"
3322 (let ((safe-local-variable-values '((org-entities-user . (("foo"))))))
3323 (hack-local-variables))
3324 (org-element-map (org-element-parse-buffer) 'entity
3325 #'identity nil nil nil t)))))
3329 ;;; Test Visible Only Parsing
3331 (ert-deftest test-org-element/parse-buffer-visible ()
3332 "Test `org-element-parse-buffer' with visible only argument."
3333 (should
3334 (equal '("H1" "H3" "H5")
3335 (org-test-with-temp-text
3336 "* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:"
3337 (org-occur ":visible:")
3338 (org-element-map (org-element-parse-buffer nil t) 'headline
3339 (lambda (hl) (org-element-property :raw-value hl)))))))
3343 ;;; Test `:parent' Property
3345 (ert-deftest test-org-element/parent-property ()
3346 "Test `:parent' property."
3347 ;; Elements.
3348 (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
3349 (let* ((tree (org-element-parse-buffer))
3350 (parent (org-element-property
3351 :parent
3352 (org-element-map tree 'paragraph 'identity nil t))))
3353 (should parent)
3354 (should (eq (org-element-map tree 'center-block 'identity nil t)
3355 parent))))
3356 ;; Objects.
3357 (org-test-with-temp-text "a_{/b/}"
3358 (let* ((tree (org-element-parse-buffer))
3359 (parent (org-element-property
3360 :parent
3361 (org-element-map tree 'italic 'identity nil t))))
3362 (should parent)
3363 (should (eq parent
3364 (org-element-map tree 'subscript 'identity nil t)))))
3365 ;; Secondary strings
3366 (org-test-with-temp-text "* /italic/"
3367 (let* ((tree (org-element-parse-buffer))
3368 (parent (org-element-property
3369 :parent (org-element-map tree 'italic 'identity nil t))))
3370 (should parent)
3371 (should (eq parent
3372 (org-element-map tree 'headline 'identity nil t))))))
3376 ;;; Test Normalize Contents
3378 (ert-deftest test-org-element/normalize-contents ()
3379 "Test `org-element-normalize-contents' specifications."
3380 ;; Remove maximum common indentation from element's contents.
3381 (should
3382 (equal
3383 (org-element-normalize-contents
3384 '(paragraph nil " Two spaces\n Three spaces"))
3385 '(paragraph nil "Two spaces\n Three spaces")))
3386 (should
3387 (equal
3388 (org-element-normalize-contents
3389 '(paragraph nil " Two spaces\nNo space"))
3390 '(paragraph nil " Two spaces\nNo space")))
3391 ;; Ignore objects within contents when computing maximum common
3392 ;; indentation. However, if contents start with an object, common
3393 ;; indentation is 0.
3394 (should
3395 (equal
3396 (org-element-normalize-contents
3397 '(paragraph nil " One " (emphasis nil "space") "\n Two spaces"))
3398 '(paragraph nil "One " (emphasis nil "space") "\n Two spaces")))
3399 (should
3400 (equal
3401 (org-element-normalize-contents
3402 '(paragraph nil (verbatim nil "V") "No space\n Two\n Three"))
3403 '(paragraph nil (verbatim nil "V") "No space\n Two\n Three")))
3404 ;; Ignore blank lines.
3405 (should
3406 (equal
3407 (org-element-normalize-contents
3408 '(paragraph nil " Two spaces\n\n \n Two spaces"))
3409 '(paragraph nil "Two spaces\n\n\nTwo spaces")))
3410 (should
3411 (equal
3412 '(paragraph nil " Two spaces\n" (verbatim nil "V") "\n Two spaces")
3413 (org-element-normalize-contents
3414 '(paragraph nil " Two spaces\n " (verbatim nil "V") "\n Two spaces"))))
3415 (should
3416 (equal
3417 '(verse-block nil "line 1\n\nline 2")
3418 (org-element-normalize-contents
3419 '(verse-block nil " line 1\n\n line 2"))))
3420 ;; Recursively enter objects in order to compute common indentation.
3421 (should
3422 (equal
3423 (org-element-normalize-contents
3424 '(paragraph nil " Two spaces " (bold nil " and\n One space")))
3425 '(paragraph nil " Two spaces " (bold nil " and\nOne space"))))
3426 ;; When optional argument is provided, ignore first line
3427 ;; indentation.
3428 (should
3429 (equal
3430 (org-element-normalize-contents
3431 '(paragraph nil "No space\n Two spaces\n Three spaces") t)
3432 '(paragraph nil "No space\nTwo spaces\n Three spaces")))
3433 (should
3434 (equal
3435 (org-element-normalize-contents
3436 '(paragraph nil (verbatim nil "V") "No space\n Two\n Three") t)
3437 '(paragraph nil (verbatim nil "V") "No space\nTwo\n Three")))
3438 ;; Corner case: do not ignore indentation of string right after
3439 ;; a line break.
3440 (should
3441 (equal
3442 (org-element-normalize-contents
3443 '(paragraph nil " 1 space" (line-break) " 2 spaces"))
3444 '(paragraph nil "1 space" (line-break) " 2 spaces"))))
3448 ;;; Test Navigation Tools.
3450 (ert-deftest test-org-element/at-point ()
3451 "Test `org-element-at-point' specifications."
3452 ;; Return closest element containing point.
3453 (should
3454 (eq 'paragraph
3455 (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
3456 (progn (search-forward "A")
3457 (org-element-type (org-element-at-point))))))
3458 ;; Correctly set `:parent' property.
3459 (should
3460 (eq 'center-block
3461 (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
3462 (progn (search-forward "A")
3463 (org-element-type
3464 (org-element-property :parent (org-element-at-point)))))))
3465 ;; Special case: at a blank line just below a headline, return that
3466 ;; headline.
3467 (should
3468 (equal "H1" (org-test-with-temp-text "* H1\n \n* H2\n"
3469 (forward-line)
3470 (org-element-property :title (org-element-at-point)))))
3471 ;; Special case: at the very beginning of a table, return `table'
3472 ;; object instead of `table-row'.
3473 (should
3474 (eq 'table
3475 (org-test-with-temp-text "| a | b |"
3476 (org-element-type (org-element-at-point)))))
3477 ;; Special case: at the very beginning of a list or sub-list, return
3478 ;; `plain-list' object instead of `item'.
3479 (should
3480 (eq 'plain-list
3481 (org-test-with-temp-text "- item"
3482 (org-element-type (org-element-at-point)))))
3483 ;; Special case: at the closing line of a greater element, be sure
3484 ;; to return it instead of the last element in its contents.
3485 (should
3486 (eq 'center-block
3487 (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph\n#+END_CENTER"
3488 (progn (forward-line 2)
3489 (org-element-type (org-element-at-point))))))
3490 ;; Special case: at a blank line between two items, be sure to
3491 ;; return item above instead of the last element of its contents.
3492 (should
3493 (eq 'item
3494 (org-test-with-temp-text "- Para1\n\n- Para2"
3495 (forward-line)
3496 (org-element-type (org-element-at-point)))))
3497 ;; Special case: at the last blank line in a plain list, return it
3498 ;; instead of the last item.
3499 (should
3500 (eq 'plain-list
3501 (org-test-with-temp-text "- Para1\n- Para2\n\nPara3"
3502 (progn (forward-line 2)
3503 (org-element-type (org-element-at-point))))))
3504 ;; Special case: when a list ends at the end of buffer and there's
3505 ;; no final newline, return last element in last item.
3506 (should
3507 (eq 'paragraph
3508 (org-test-with-temp-text "- a"
3509 (end-of-line)
3510 (org-element-type (org-element-at-point)))))
3511 ;; Parse a list within a block itself contained in a list.
3512 (should
3513 (eq 'plain-list
3514 (org-test-with-temp-text
3515 "- outer\n #+begin_center\n - inner\n #+end_center"
3516 (search-forward "inner")
3517 (beginning-of-line)
3518 (org-element-type (org-element-at-point)))))
3519 ;; Do not error at eob on an empty line.
3520 (should
3521 (org-test-with-temp-text "* H\n"
3522 (forward-line)
3523 (or (org-element-at-point) t))))
3525 (ert-deftest test-org-element/context ()
3526 "Test `org-element-context' specifications."
3527 ;; Return closest object containing point.
3528 (should
3529 (eq 'underline
3530 (org-test-with-temp-text "Some *text with _under<point>line_ text*"
3531 (org-element-type (org-element-context)))))
3532 ;; Find objects in secondary strings.
3533 (should
3534 (eq 'underline
3535 (org-test-with-temp-text "* Headline _<point>with_ underlining"
3536 (org-element-type (org-element-context)))))
3537 ;; Find objects in objects.
3538 (should
3539 (eq 'macro
3540 (org-test-with-temp-text "| a | {<point>{{macro}}} |"
3541 (org-element-type (org-element-context)))))
3542 (should
3543 (eq 'table-cell
3544 (org-test-with-temp-text "| a | b<point> {{{macro}}} |"
3545 (org-element-type (org-element-context)))))
3546 ;; Find objects in item tags.
3547 (should
3548 (eq 'bold
3549 (org-test-with-temp-text "- *bo<point>ld* ::"
3550 (org-element-type (org-element-context)))))
3551 (should-not
3552 (eq 'bold
3553 (org-test-with-temp-text "- *bold* ::<point>"
3554 (org-element-type (org-element-context)))))
3555 (should-not
3556 (eq 'bold
3557 (org-test-with-temp-text "- *bold* ::\n<point>"
3558 (org-element-type (org-element-context)))))
3559 ;; Do not find objects in table rules.
3560 (should
3561 (eq 'table-row
3562 (org-test-with-temp-text "| a | b |\n|-<point>--|---|\n| c | d |"
3563 (org-element-type (org-element-context)))))
3564 ;; Find objects in parsed affiliated keywords.
3565 (should
3566 (eq 'macro
3567 (org-test-with-temp-text "#+CAPTION: {<point>{{macro}}}\n| a | b |"
3568 (org-element-type (org-element-context)))))
3569 (should
3570 (eq 'bold
3571 (org-test-with-temp-text "#+caption: *<point>bold*\nParagraph"
3572 (org-element-type (org-element-context)))))
3573 ;; Find objects at the end of buffer.
3574 (should
3575 (eq 'bold
3576 (org-test-with-temp-text "*bold*"
3577 (goto-char (point-max))
3578 (org-element-type (org-element-context)))))
3579 ;; Correctly set `:parent' property.
3580 (should
3581 (eq 'paragraph
3582 (org-test-with-temp-text "Some *bold<point>* text"
3583 (org-element-type
3584 (org-element-property :parent (org-element-context))))))
3585 ;; Between two objects, return the second one.
3586 (should
3587 (eq 'macro
3588 (org-test-with-temp-text "<<target>><point>{{{test}}}"
3589 (org-element-type (org-element-context)))))
3590 ;; Test optional argument.
3591 (should
3592 (eq 'underline
3593 (org-test-with-temp-text "Some *text with _under<point>line_ text*"
3594 (org-element-type (org-element-context (org-element-at-point))))))
3595 ;; Special case: bold object at the beginning of a headline.
3596 (should
3597 (eq 'bold
3598 (org-test-with-temp-text "* *bo<point>ld*"
3599 (org-element-type (org-element-context)))))
3600 ;; Special case: incomplete cell at the end of a table row.
3601 (should
3602 (eq 'table-cell
3603 (org-test-with-temp-text "|a|b|c<point>"
3604 (org-element-type (org-element-context)))))
3605 ;; Special case: objects in inline footnotes.
3606 (should
3607 (eq 'link
3608 (org-test-with-temp-text "[fn::[[<point>https://orgmode.org]]]"
3609 (org-element-type (org-element-context)))))
3610 ;; Special case: tags looking like a link.
3611 (should-not
3612 (eq 'link
3613 (org-test-with-temp-text "* Headline :file<point>:tags:"
3614 (org-element-type (org-element-context)))))
3615 (should
3616 (eq 'link
3617 (org-test-with-temp-text "* Headline :file<point>:tags: :real:tag:"
3618 (org-element-type (org-element-context))))))
3622 ;;; Test Tools
3624 (ert-deftest test-org-element/lineage ()
3625 "Test `org-element-lineage' specifications."
3626 ;; Regular tests. When applied to an element or object returned by
3627 ;; `org-element-at-point' or `org-element-context', the list is
3628 ;; limited to the current section.
3629 (should
3630 (equal '(paragraph center-block)
3631 (org-test-with-temp-text
3632 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3633 (mapcar #'car (org-element-lineage (org-element-context))))))
3634 (should
3635 (equal '(paragraph center-block section headline headline org-data)
3636 (org-test-with-temp-text
3637 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3638 (mapcar #'car
3639 (org-element-lineage
3640 (org-element-map (org-element-parse-buffer) 'bold
3641 #'identity nil t))))))
3642 ;; Test TYPES optional argument.
3643 (should
3644 (eq 'center-block
3645 (org-test-with-temp-text
3646 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3647 (org-element-type
3648 (org-element-lineage (org-element-context) '(center-block))))))
3649 (should-not
3650 (org-test-with-temp-text
3651 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3652 (org-element-lineage (org-element-context) '(example-block))))
3653 ;; Test WITH-SELF optional argument.
3654 (should
3655 (equal '(bold paragraph center-block)
3656 (org-test-with-temp-text
3657 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3658 (mapcar #'car (org-element-lineage (org-element-context) nil t)))))
3659 ;; When TYPES and WITH-SELF are provided, the latter is also checked
3660 ;; against the former.
3661 (should
3662 (org-test-with-temp-text
3663 "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
3664 (org-element-lineage (org-element-context) '(bold) t))))
3668 ;;; Test Cache.
3670 (ert-deftest test-org-element/cache ()
3671 "Test basic expectations and common pitfalls for cache."
3672 ;; Shift positions.
3673 (should
3674 (equal '(18 . 23)
3675 (org-test-with-temp-text "para1\n\npara2\n\npara3"
3676 (let ((org-element-use-cache t))
3677 (save-excursion (goto-char (point-max)) (org-element-at-point))
3678 (insert "add")
3679 (forward-line 4)
3680 (let ((element (org-element-at-point)))
3681 (cons (org-element-property :begin element)
3682 (org-element-property :end element)))))))
3683 ;; Partial shifting: when the contents of a greater element are
3684 ;; modified, only shift ending positions.
3685 (should
3686 (org-test-with-temp-text
3687 "#+BEGIN_CENTER\nPara1\n\nPara2\n\nPara3\n#+END_CENTER"
3688 (let ((org-element-use-cache t))
3689 (save-excursion (search-forward "3") (org-element-at-point))
3690 (search-forward "Para2")
3691 (insert " ")
3692 (let ((element (org-element-property :parent (org-element-at-point))))
3693 (equal (cons (org-element-property :begin element)
3694 (org-element-property :end element))
3695 (cons (point-min) (point-max)))))))
3696 ;; Re-parent shifted elements.
3697 (should
3698 (eq 'item
3699 (org-test-with-temp-text "- item\n\n\n para1\n para2"
3700 (let ((org-element-use-cache t))
3701 (end-of-line)
3702 (org-element-at-point)
3703 (save-excursion (goto-char (point-max)) (org-element-at-point))
3704 (forward-line)
3705 (delete-char 1)
3706 (goto-char (point-max))
3707 (org-element-type
3708 (org-element-property :parent (org-element-at-point)))))))
3709 ;; Preserve local structures when re-parenting.
3710 (should
3711 (eq 'table
3712 (let ((org-element-use-cache t))
3713 (org-test-with-temp-text
3714 "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n| c | d |\n#+end_center"
3715 (save-excursion (search-forward "| c |") (org-element-at-point))
3716 (insert "- item")
3717 (search-forward "| c |")
3718 (beginning-of-line)
3719 (org-element-type
3720 (org-element-property :parent (org-element-at-point)))))))
3721 (should-not
3722 (eq 'center-block
3723 (org-test-with-temp-text
3724 "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n#+end_center"
3725 (let ((org-element-use-cache t))
3726 (save-excursion (search-forward "| a |") (org-element-at-point))
3727 (insert "- item")
3728 (search-forward "| a |")
3729 (beginning-of-line)
3730 (org-element-type
3731 (org-element-property :parent (org-element-at-point)))))))
3732 ;; When re-parenting, also propagate changes to list structures.
3733 (should
3734 (= 2
3735 (org-test-with-temp-text "\n Para\n - item<point>"
3736 (let ((org-element-use-cache t))
3737 (org-element-at-point)
3738 (goto-char (point-min))
3739 (insert "- Top\n")
3740 (search-forward "- item")
3741 (beginning-of-line)
3742 (length (org-element-property :structure (org-element-at-point)))))))
3743 ;; Modifying the last line of an element alters the element below.
3744 (should
3745 (org-test-with-temp-text "para1\n\npara2"
3746 (let ((org-element-use-cache t))
3747 (goto-char (point-max))
3748 (org-element-at-point)
3749 (forward-line -1)
3750 (insert "merge")
3751 (let ((element (org-element-at-point)))
3752 (equal (cons (org-element-property :begin element)
3753 (org-element-property :end element))
3754 (cons (point-min) (point-max)))))))
3755 ;; Modifying the first line of an element alters the element above.
3756 (should
3757 (org-test-with-temp-text ": fixed-width\n:not-fixed-width"
3758 (let ((org-element-use-cache t))
3759 (goto-char (point-max))
3760 (org-element-at-point)
3761 (search-backward ":")
3762 (forward-char)
3763 (insert " ")
3764 (let ((element (org-element-at-point)))
3765 (equal (cons (org-element-property :begin element)
3766 (org-element-property :end element))
3767 (cons (point-min) (point-max)))))))
3768 ;; Sensitive change: adding a line alters document structure both
3769 ;; above and below.
3770 (should
3771 (eq 'example-block
3772 (org-test-with-temp-text "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n"
3773 (let ((org-element-use-cache t))
3774 (goto-char (point-max))
3775 (org-element-at-point)
3776 (insert "#+END_EXAMPLE")
3777 (search-backward "Para1")
3778 (org-element-type (org-element-at-point))))))
3779 (should
3780 (eq 'example-block
3781 (org-test-with-temp-text "Para1\n\nPara2\n#+END_EXAMPLE"
3782 (let ((org-element-use-cache t))
3783 (save-excursion (goto-char (point-max)) (org-element-at-point))
3784 (insert "#+BEGIN_EXAMPLE\n")
3785 (search-forward "Para2")
3786 (org-element-type (org-element-at-point))))))
3787 ;; Sensitive change: removing a line alters document structure both
3788 ;; above and below.
3789 (should
3790 (eq 'example-block
3791 (org-test-with-temp-text
3792 "# +BEGIN_EXAMPLE\nPara1\n\nPara2\n#+END_EXAMPLE"
3793 (let ((org-element-use-cache t))
3794 (save-excursion (goto-char (point-max)) (org-element-at-point))
3795 (forward-char)
3796 (delete-char 1)
3797 (search-forward "Para2")
3798 (org-element-type (org-element-at-point))))))
3799 (should
3800 (eq 'example-block
3801 (org-test-with-temp-text
3802 "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n# +END_EXAMPLE"
3803 (let ((org-element-use-cache t))
3804 (save-excursion (goto-char (point-max)) (org-element-at-point))
3805 (search-forward "# ")
3806 (delete-char -1)
3807 (search-backward "Para1")
3808 (org-element-type (org-element-at-point))))))
3809 ;; Corner case: watch out drawers named "PROPERTIES" as they are
3810 ;; fragile, unlike to other drawers.
3811 (should
3812 (eq 'node-property
3813 (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A<point>\n:END:"
3814 (let ((org-element-use-cache t))
3815 (org-element-at-point)
3816 (insert "+:")
3817 (org-element-type (org-element-at-point))))))
3818 ;; Properly handle elements not altered by modifications but whose
3819 ;; parents were removed from cache.
3820 (should
3821 (org-test-with-temp-text
3822 "Paragraph\n\n\n\n#+begin_center\n<point>contents\n#+end_center"
3823 (let ((org-element-use-cache t)
3824 (parent-end (point-max)))
3825 (org-element-at-point)
3826 (save-excursion (search-backward "Paragraph")
3827 (forward-line 2)
3828 (insert "\n "))
3829 (eq (org-element-property
3830 :end (org-element-property :parent (org-element-at-point)))
3831 (+ parent-end 3))))))
3834 (provide 'test-org-element)
3836 ;;; test-org-element.el ends here