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