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