2548 lines
88 KiB
EmacsLisp
2548 lines
88 KiB
EmacsLisp
;;; test-org-element.el --- Tests for org-element.el
|
||
|
||
;; Copyright (C) 2012 Nicolas Goaziou
|
||
|
||
;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
|
||
|
||
;; This program is free software; you can redistribute it and/or modify
|
||
;; it under the terms of the GNU General Public License as published by
|
||
;; the Free Software Foundation, either version 3 of the License, or
|
||
;; (at your option) any later version.
|
||
|
||
;; This program is distributed in the hope that it will be useful,
|
||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
;; GNU General Public License for more details.
|
||
|
||
;; You should have received a copy of the GNU General Public License
|
||
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
||
(unless (featurep 'org-element)
|
||
(signal 'missing-test-dependency "org-element"))
|
||
|
||
(defun org-test-parse-and-interpret (text)
|
||
"Parse TEXT as Org syntax and interpret it.
|
||
Return interpreted string."
|
||
(with-temp-buffer
|
||
(org-mode)
|
||
(insert text)
|
||
(org-element-interpret-data (org-element-parse-buffer))))
|
||
|
||
|
||
|
||
;;; Test `org-element-map'
|
||
|
||
(ert-deftest test-org-element/map ()
|
||
"Test `org-element-map'."
|
||
;; Can map to `plain-text' objects.
|
||
(should
|
||
(= 2
|
||
(org-test-with-temp-text "Some text \alpha
|
||
#+BEGIN_CENTER
|
||
Some other text
|
||
#+END_CENTER"
|
||
(let ((count 0))
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'plain-text
|
||
(lambda (s) (when (string-match "text" s) (incf count))))
|
||
count))))
|
||
;; Applies to secondary strings
|
||
(should
|
||
(org-element-map '("some " (bold nil "bold") "text") 'bold 'identity))
|
||
;; Enter secondary strings before entering contents.
|
||
(should
|
||
(equal
|
||
"alpha"
|
||
(org-element-property
|
||
:name
|
||
(org-test-with-temp-text "* Some \\alpha headline\n\\beta entity."
|
||
(org-element-map (org-element-parse-buffer) 'entity 'identity nil t)))))
|
||
;; Apply NO-RECURSION argument.
|
||
(should-not
|
||
(org-test-with-temp-text "#+BEGIN_CENTER\n\\alpha\n#+END_CENTER"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'entity 'identity nil nil 'center-block))))
|
||
|
||
|
||
|
||
;;; Test Setters
|
||
|
||
(ert-deftest test-org-element/put-property ()
|
||
"Test `org-element-put-property' specifications."
|
||
(org-test-with-temp-text "* Headline\n *a*"
|
||
(let ((tree (org-element-parse-buffer)))
|
||
(org-element-put-property
|
||
(org-element-map tree 'bold 'identity nil t) :test 1)
|
||
(should (org-element-property
|
||
:test (org-element-map tree 'bold 'identity nil t))))))
|
||
|
||
(ert-deftest test-org-element/set-contents ()
|
||
"Test `org-element-set-contents' specifications."
|
||
;; Accept multiple entries.
|
||
(should
|
||
(equal '("b" (italic nil "a"))
|
||
(org-test-with-temp-text "* Headline\n *a*"
|
||
(let ((tree (org-element-parse-buffer)))
|
||
(org-element-set-contents
|
||
(org-element-map tree 'bold 'identity nil t) "b" '(italic nil "a"))
|
||
(org-element-contents
|
||
(org-element-map tree 'bold 'identity nil t))))))
|
||
;; Accept atoms and elements.
|
||
(should
|
||
(equal '("b")
|
||
(org-test-with-temp-text "* Headline\n *a*"
|
||
(let ((tree (org-element-parse-buffer)))
|
||
(org-element-set-contents
|
||
(org-element-map tree 'bold 'identity nil t) "b")
|
||
(org-element-contents
|
||
(org-element-map tree 'bold 'identity nil t))))))
|
||
(should
|
||
(equal '((italic nil "b"))
|
||
(org-test-with-temp-text "* Headline\n *a*"
|
||
(let ((tree (org-element-parse-buffer)))
|
||
(org-element-set-contents
|
||
(org-element-map tree 'bold 'identity nil t) '(italic nil "b"))
|
||
(org-element-contents
|
||
(org-element-map tree 'bold 'identity nil t))))))
|
||
;; Allow nil contents.
|
||
(should-not
|
||
(org-test-with-temp-text "* Headline\n *a*"
|
||
(let ((tree (org-element-parse-buffer)))
|
||
(org-element-set-contents (org-element-map tree 'bold 'identity nil t))
|
||
(org-element-contents (org-element-map tree 'bold 'identity nil t))))))
|
||
|
||
(ert-deftest test-org-element/set-element ()
|
||
"Test `org-element-set-element' specifications."
|
||
(org-test-with-temp-text "* Headline\n*a*"
|
||
(let ((tree (org-element-parse-buffer)))
|
||
(org-element-set-element
|
||
(org-element-map tree 'bold 'identity nil t)
|
||
'(italic nil "b"))
|
||
;; Check if object is correctly replaced.
|
||
(should (org-element-map tree 'italic 'identity))
|
||
(should-not (org-element-map tree 'bold 'identity))
|
||
;; Check if new object's parent is correctly set.
|
||
(should
|
||
(equal
|
||
(org-element-property :parent
|
||
(org-element-map tree 'italic 'identity nil t))
|
||
(org-element-map tree 'paragraph 'identity nil t))))))
|
||
|
||
(ert-deftest test-org-element/adopt-element ()
|
||
"Test `org-element-adopt-element' specifications."
|
||
;; Adopt an element.
|
||
(should
|
||
(equal '(italic plain-text)
|
||
(org-test-with-temp-text "* Headline\n *a*"
|
||
(let ((tree (org-element-parse-buffer)))
|
||
(org-element-adopt-element
|
||
(org-element-map tree 'bold 'identity nil t) '(italic nil "a"))
|
||
(mapcar (lambda (blob) (org-element-type blob))
|
||
(org-element-contents
|
||
(org-element-map tree 'bold 'identity nil t)))))))
|
||
;; Adopt a string.
|
||
(should
|
||
(equal '("b" "a")
|
||
(org-test-with-temp-text "* Headline\n *a*"
|
||
(let ((tree (org-element-parse-buffer)))
|
||
(org-element-adopt-element
|
||
(org-element-map tree 'bold 'identity nil t) "b")
|
||
(org-element-contents
|
||
(org-element-map tree 'bold 'identity nil t))))))
|
||
;; Test APPEND optional argument.
|
||
(should
|
||
(equal '("a" "b")
|
||
(org-test-with-temp-text "* Headline\n *a*"
|
||
(let ((tree (org-element-parse-buffer)))
|
||
(org-element-adopt-element
|
||
(org-element-map tree 'bold 'identity nil t) "b" t)
|
||
(org-element-contents
|
||
(org-element-map tree 'bold 'identity nil t)))))))
|
||
|
||
|
||
|
||
;;; Test Parsers
|
||
|
||
;;;; Babel Call
|
||
|
||
(ert-deftest test-org-element/babel-call-parser ()
|
||
"Test `babel-call' parsing."
|
||
;; Standard test.
|
||
(should
|
||
(org-test-with-temp-text "#+CALL: test()"
|
||
(org-element-map (org-element-parse-buffer) 'babel-call 'identity)))
|
||
;; Ignore case.
|
||
(should
|
||
(org-test-with-temp-text "#+call: test()"
|
||
(org-element-map (org-element-parse-buffer) 'babel-call 'identity))))
|
||
|
||
|
||
;;;; Bold
|
||
|
||
(ert-deftest test-org-element/bold-parser ()
|
||
"Test `bold' parser."
|
||
;; Standard test.
|
||
(should
|
||
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
|
||
\n,\"']\\|[^
|
||
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
|
||
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
|
||
(org-test-with-temp-text "*bold*"
|
||
(org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
|
||
;; Multi-line markup.
|
||
(should
|
||
(equal
|
||
(org-element-contents
|
||
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
|
||
\n,\"']\\|[^
|
||
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
|
||
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
|
||
(org-test-with-temp-text "*first line\nsecond line*"
|
||
(org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
|
||
'("first line\nsecond line"))))
|
||
|
||
|
||
;;;; Center Block
|
||
|
||
(ert-deftest test-org-element/center-block-parser ()
|
||
"Test `center-block' parser."
|
||
;; Standard test.
|
||
(should
|
||
(org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
|
||
(org-element-map (org-element-parse-buffer) 'center-block 'identity)))
|
||
;; Ignore case.
|
||
(should
|
||
(org-test-with-temp-text "#+begin_center\nText\n#+end_center"
|
||
(org-element-map (org-element-parse-buffer) 'center-block 'identity)))
|
||
;; Test folded block.
|
||
(org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
|
||
(org-cycle)
|
||
(should
|
||
(org-element-property
|
||
:hiddenp
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'center-block 'identity nil t))))
|
||
;; Ignore incomplete block.
|
||
(should-not
|
||
(org-test-with-temp-text "#+BEGIN_CENTER"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'center-block 'identity nil t))))
|
||
|
||
|
||
;;;; Clock
|
||
|
||
(ert-deftest test-org-element/clock-parser ()
|
||
"Test `clock' parser."
|
||
;; Running clock.
|
||
(let* ((org-clock-string "CLOCK:")
|
||
(clock (org-test-with-temp-text "CLOCK: [2012-01-01 sun. 00:01]"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'clock 'identity nil t))))
|
||
(should (eq (org-element-property :status clock) 'running))
|
||
(should (equal (org-element-property :value clock)
|
||
"[2012-01-01 sun. 00:01]"))
|
||
(should-not (org-element-property :time clock)))
|
||
;; Closed clock.
|
||
(let* ((org-clock-string "CLOCK:")
|
||
(clock (org-test-with-temp-text "
|
||
CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'clock 'identity nil t))))
|
||
(should (eq (org-element-property :status clock) 'closed))
|
||
(should (equal (org-element-property :value clock)
|
||
"[2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02]"))
|
||
(should (equal (org-element-property :time clock) "0:01"))))
|
||
|
||
|
||
;;;; Code
|
||
|
||
(ert-deftest test-org-element/code-parser ()
|
||
"Test `code' parser."
|
||
;; Regular test.
|
||
(should
|
||
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
|
||
\n,\"']\\|[^
|
||
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
|
||
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
|
||
(org-test-with-temp-text "~code~"
|
||
(org-element-map (org-element-parse-buffer) 'code 'identity))))
|
||
;; Multi-line markup.
|
||
(should
|
||
(equal
|
||
(org-element-property
|
||
:value
|
||
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
|
||
\n,\"']\\|[^
|
||
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
|
||
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
|
||
(org-test-with-temp-text "~first line\nsecond line~"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'code 'identity nil t))))
|
||
"first line\nsecond line")))
|
||
|
||
|
||
;;;; Comment
|
||
|
||
(ert-deftest test-org-element/comment-parser ()
|
||
"Test `comment' parser."
|
||
;; Regular comment.
|
||
(should
|
||
(org-test-with-temp-text "# Comment"
|
||
(org-element-map (org-element-parse-buffer) 'comment 'identity)))
|
||
;; Inline comment.
|
||
(should
|
||
(org-test-with-temp-text "#+ Comment"
|
||
(org-element-map (org-element-parse-buffer) 'comment 'identity)))
|
||
;; Preserve indentation.
|
||
(should
|
||
(equal
|
||
(org-element-property
|
||
:value
|
||
(org-test-with-temp-text "#+ No blank\n#+ One blank"
|
||
(org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
|
||
"No blank\n One blank"))
|
||
;; Comment with blank lines.
|
||
(should
|
||
(equal
|
||
(org-element-property
|
||
:value
|
||
(org-test-with-temp-text "#+ First part\n#+ \n#+\n#+ Second part"
|
||
(org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
|
||
"First part\n\n\nSecond part"))
|
||
;; Keywords without colons are treated as comments.
|
||
(should
|
||
(org-test-with-temp-text "#+wrong_keyword something"
|
||
(org-element-map (org-element-parse-buffer) 'comment 'identity))))
|
||
|
||
|
||
;;;; Comment Block
|
||
|
||
(ert-deftest test-org-element/comment-block-parser ()
|
||
"Test `comment-block' parser."
|
||
;; Standard test.
|
||
(should
|
||
(org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'comment-block 'identity)))
|
||
;; Ignore case.
|
||
(should
|
||
(org-test-with-temp-text "#+begin_comment\nText\n#+end_comment"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'comment-block 'identity)))
|
||
;; Test folded block.
|
||
(org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
|
||
(org-cycle)
|
||
(should
|
||
(org-element-property
|
||
:hiddenp
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'comment-block 'identity nil t))))
|
||
;; Ignore incomplete block.
|
||
(should-not
|
||
(org-test-with-temp-text "#+BEGIN_COMMENT"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'comment-block 'identity nil t))))
|
||
|
||
|
||
;;;; Drawer
|
||
|
||
(ert-deftest test-org-element/drawer-parser ()
|
||
"Test `drawer' parser."
|
||
;; Standard test.
|
||
(should
|
||
(let ((org-drawers '("TEST")))
|
||
(org-test-with-temp-text ":TEST:\nText\n:END:"
|
||
(org-element-map (org-element-parse-buffer) 'drawer 'identity))))
|
||
;; Do not mix regular drawers and property drawers.
|
||
(should-not
|
||
(let ((org-drawers '("PROPERTIES")))
|
||
(org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'drawer 'identity nil t))))
|
||
;; Ignore incomplete drawer.
|
||
(should-not
|
||
(let ((org-drawers '("TEST")))
|
||
(org-test-with-temp-text ":TEST:"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'drawer 'identity nil t)))))
|
||
|
||
|
||
;;;; Dynamic Block
|
||
|
||
(ert-deftest test-org-element/dynamic-block-parser ()
|
||
"Test `dynamic-block' parser."
|
||
;; Standard test.
|
||
(should
|
||
(org-test-with-temp-text
|
||
"#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
|
||
(org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
|
||
;; Folded view
|
||
(org-test-with-temp-text
|
||
"#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
|
||
(org-cycle)
|
||
(should
|
||
(org-element-property
|
||
:hiddenp
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'dynamic-block 'identity nil t))))
|
||
;; Ignore case.
|
||
(should
|
||
(org-test-with-temp-text
|
||
"#+begin: myblock :param1 val1 :param2 val2\nText\n#+end:"
|
||
(org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
|
||
;; Ignore incomplete block.
|
||
(should-not
|
||
(org-test-with-temp-text "#+BEGIN: myblock :param1 val1 :param2 val2"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'dynamic-block 'identity nil t))))
|
||
|
||
|
||
;;;; Entity
|
||
|
||
(ert-deftest test-org-element/entity-parser ()
|
||
"Test `entity' parser."
|
||
;; Without brackets.
|
||
(should
|
||
(org-test-with-temp-text "\\sin"
|
||
(org-element-map (org-element-parse-buffer) 'entity 'identity)))
|
||
;; With brackets.
|
||
(should
|
||
(org-element-property
|
||
:use-brackets-p
|
||
(org-test-with-temp-text "\\alpha{}text"
|
||
(org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
|
||
;; User-defined entity.
|
||
(should
|
||
(equal
|
||
(org-element-property
|
||
:name
|
||
(let ((org-entities-user
|
||
'(("test" "test" nil "test" "test" "test" "test"))))
|
||
(org-test-with-temp-text "\\test"
|
||
(org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
|
||
"test")))
|
||
|
||
|
||
;;;; Example Block
|
||
|
||
(ert-deftest test-org-element/example-block-parser ()
|
||
"Test `example-block' parser."
|
||
;; Standard test.
|
||
(should
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
|
||
(org-element-map (org-element-parse-buffer) 'example-block 'identity)))
|
||
;; Test folded block.
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
|
||
(org-cycle)
|
||
(should
|
||
(org-element-property
|
||
:hiddenp
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'example-block 'identity nil t))))
|
||
;; Ignore incomplete block.
|
||
(should-not
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'example-block 'identity nil t))))
|
||
|
||
(ert-deftest test-org-element/block-switches ()
|
||
"Test `example-block' and `src-block' switches parsing."
|
||
(let ((org-coderef-label-format "(ref:%s)"))
|
||
;; 1. Test "-i" switch.
|
||
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should-not (org-element-property :preserve-indent element))))
|
||
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (org-element-property :preserve-indent element))))
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should-not (org-element-property :preserve-indent element))))
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (org-element-property :preserve-indent element))))
|
||
;; 2. "-n -r -k" combination should number lines, retain labels but
|
||
;; not use them in coderefs.
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (org-element-property :number-lines element)
|
||
(org-element-property :retain-labels element)
|
||
(not (org-element-property :use-labels element))))))
|
||
(org-test-with-temp-text
|
||
"#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (org-element-property :number-lines element)
|
||
(org-element-property :retain-labels element)
|
||
(not (org-element-property :use-labels element))))))
|
||
;; 3. "-n -r" combination should number-lines remove labels and not
|
||
;; use them in coderefs.
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (org-element-property :number-lines element)
|
||
(not (org-element-property :retain-labels element))
|
||
(not (org-element-property :use-labels element))))))
|
||
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (org-element-property :number-lines element)
|
||
(not (org-element-property :retain-labels element))
|
||
(not (org-element-property :use-labels element))))))
|
||
;; 4. "-n" or "+n" should number lines, retain labels and use them
|
||
;; in coderefs.
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (org-element-property :number-lines element)
|
||
(org-element-property :retain-labels element)
|
||
(org-element-property :use-labels element)))))
|
||
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (org-element-property :number-lines element)
|
||
(org-element-property :retain-labels element)
|
||
(org-element-property :use-labels element)))))
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (org-element-property :number-lines element)
|
||
(org-element-property :retain-labels element)
|
||
(org-element-property :use-labels element)))))
|
||
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (org-element-property :number-lines element)
|
||
(org-element-property :retain-labels element)
|
||
(org-element-property :use-labels element)))))
|
||
;; 5. No switch should not number lines, but retain labels and use
|
||
;; them in coderefs.
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (not (org-element-property :number-lines element))
|
||
(org-element-property :retain-labels element)
|
||
(org-element-property :use-labels element)))))
|
||
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (not (org-element-property :number-lines element))
|
||
(org-element-property :retain-labels element)
|
||
(org-element-property :use-labels element)))))
|
||
;; 6. "-r" switch only: do not number lines, remove labels, and
|
||
;; don't use labels in coderefs.
|
||
(org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (not (org-element-property :number-lines element))
|
||
(not (org-element-property :retain-labels element))
|
||
(not (org-element-property :use-labels element))))))
|
||
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should (and (not (org-element-property :number-lines element))
|
||
(not (org-element-property :retain-labels element))
|
||
(not (org-element-property :use-labels element))))))
|
||
;; 7. Recognize coderefs with user-defined syntax.
|
||
(org-test-with-temp-text
|
||
"#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should
|
||
(equal (org-element-property :label-fmt element) "[ref:%s]"))))
|
||
(org-test-with-temp-text
|
||
"#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
|
||
(let ((element (org-element-current-element (point-max))))
|
||
(should
|
||
(equal (org-element-property :label-fmt element) "[ref:%s]"))))))
|
||
|
||
|
||
;;;; Export Block
|
||
|
||
(ert-deftest test-org-element/export-block-parser ()
|
||
"Test `export-block' parser."
|
||
;; Standard test.
|
||
(should
|
||
(org-test-with-temp-text "#+BEGIN_LATEX\nText\n#+END_LATEX"
|
||
(org-element-map (org-element-parse-buffer) 'export-block 'identity)))
|
||
;; Test folded block.
|
||
(org-test-with-temp-text "#+BEGIN_LATEX\nText\n#+END_LATEX"
|
||
(org-cycle)
|
||
(should
|
||
(org-element-property
|
||
:hiddenp
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'export-block 'identity nil t))))
|
||
;; Ignore case.
|
||
(should
|
||
(org-test-with-temp-text "#+begin_latex\nText\n#+end_latex"
|
||
(org-element-map (org-element-parse-buffer) 'export-block 'identity)))
|
||
;; Ignore incomplete block.
|
||
(should-not
|
||
(org-test-with-temp-text "#+BEGIN_LATEX"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'export-block 'identity nil t))))
|
||
|
||
|
||
;;;; Export Snippet
|
||
|
||
(ert-deftest test-org-element/export-snippet-parser ()
|
||
"Test `export-snippet' parser."
|
||
(should
|
||
(equal
|
||
'("back-end" . "contents")
|
||
(org-test-with-temp-text "@@back-end:contents@@"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'export-snippet
|
||
(lambda (snippet) (cons (org-element-property :back-end snippet)
|
||
(org-element-property :value snippet)))
|
||
nil t)))))
|
||
|
||
|
||
;;;; Fixed Width
|
||
|
||
(ert-deftest test-org-element/fixed-width ()
|
||
"Test fixed-width area parsing."
|
||
;; Preserve indentation.
|
||
(should
|
||
(org-test-with-temp-text ": no blank\n: one blank"
|
||
(org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
|
||
;; Fixed-width with empty lines.
|
||
(should
|
||
(org-test-with-temp-text ": first part\n:\n: \n: second part"
|
||
(org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
|
||
;; Parse indented fixed-width markers.
|
||
(should
|
||
(org-test-with-temp-text "Text\n : no blank\n : one blank"
|
||
(org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
|
||
;; Distinguish fixed-width areas within a list and outside of it.
|
||
(should
|
||
(= 2
|
||
(length
|
||
(org-test-with-temp-text "
|
||
- Item
|
||
: fixed-width inside
|
||
: fixed-width outside"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'fixed-width 'identity))))))
|
||
|
||
|
||
;;;; Footnote Definition
|
||
|
||
(ert-deftest test-org-element/footnote-definition-parser ()
|
||
"Test `footnote-definition' parser."
|
||
(should
|
||
(org-test-with-temp-text "[fn:1] Definition"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'footnote-definition 'identity nil t)))
|
||
;; Footnote with more contents
|
||
(should
|
||
(= 28
|
||
(org-element-property
|
||
:end
|
||
(org-test-with-temp-text "[fn:1] Definition\n| a | b |"
|
||
(org-element-map
|
||
(org-element-parse-buffer)
|
||
'footnote-definition 'identity nil t))))))
|
||
|
||
|
||
;;;; Footnotes Reference.
|
||
|
||
(ert-deftest test-org-element/footnote-reference-parser ()
|
||
"Test `footnote-reference' parser."
|
||
;; 1. Parse a standard reference.
|
||
(org-test-with-temp-text "Text[fn:label]"
|
||
(should
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'footnote-reference 'identity)))
|
||
;; 2. Parse a normalized reference.
|
||
(org-test-with-temp-text "Text[1]"
|
||
(should
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'footnote-reference 'identity)))
|
||
;; 3. Parse an inline reference.
|
||
(org-test-with-temp-text "Text[fn:test:def]"
|
||
(should
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'footnote-reference 'identity)))
|
||
;; 4. Parse an anonymous reference.
|
||
(org-test-with-temp-text "Text[fn::def]"
|
||
(should
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'footnote-reference 'identity)))
|
||
;; 5. Parse nested footnotes.
|
||
(org-test-with-temp-text "Text[fn::def [fn:label]]"
|
||
(should
|
||
(= 2
|
||
(length
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'footnote-reference 'identity)))))
|
||
;; 6. Parse adjacent footnotes.
|
||
(org-test-with-temp-text "Text[fn:label1][fn:label2]"
|
||
(should
|
||
(= 2
|
||
(length
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'footnote-reference 'identity)))))
|
||
;; 7. Only properly closed footnotes are recognized as such.
|
||
(org-test-with-temp-text "Text[fn:label"
|
||
(should-not
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'footnote-reference 'identity))))
|
||
|
||
|
||
;;;; Headline
|
||
|
||
(ert-deftest test-org-element/headline-quote-keyword ()
|
||
"Test QUOTE keyword recognition."
|
||
;; Reference test.
|
||
(org-test-with-temp-text "* Headline"
|
||
(let ((org-quote-string "QUOTE"))
|
||
(should-not (org-element-property :quotedp (org-element-at-point)))))
|
||
;; Standard position.
|
||
(org-test-with-temp-text "* QUOTE Headline"
|
||
(let ((org-quote-string "QUOTE"))
|
||
(let ((headline (org-element-at-point)))
|
||
(should (org-element-property :quotedp headline))
|
||
;; Test removal from raw value.
|
||
(should (equal (org-element-property :raw-value headline) "Headline"))))
|
||
;; Case sensitivity.
|
||
(let ((org-quote-string "Quote"))
|
||
(should-not (org-element-property :quotedp (org-element-at-point)))))
|
||
;; With another keyword.
|
||
(org-test-with-temp-text "* TODO QUOTE Headline"
|
||
(let ((org-quote-string "QUOTE")
|
||
(org-todo-keywords '((sequence "TODO" "DONE"))))
|
||
(should (org-element-property :quotedp (org-element-at-point))))))
|
||
|
||
(ert-deftest test-org-element/headline-comment-keyword ()
|
||
"Test COMMENT keyword recognition."
|
||
;; Reference test.
|
||
(org-test-with-temp-text "* Headline"
|
||
(let ((org-comment-string "COMMENT"))
|
||
(should-not (org-element-property :commentedp (org-element-at-point)))))
|
||
;; Standard position.
|
||
(org-test-with-temp-text "* COMMENT Headline"
|
||
(let ((org-comment-string "COMMENT"))
|
||
(let ((headline (org-element-at-point)))
|
||
(should (org-element-property :commentedp headline))
|
||
;; Test removal from raw value.
|
||
(should (equal (org-element-property :raw-value headline) "Headline"))))
|
||
;; Case sensitivity.
|
||
(let ((org-comment-string "Comment"))
|
||
(should-not (org-element-property :commentedp (org-element-at-point)))))
|
||
;; With another keyword.
|
||
(org-test-with-temp-text "* TODO COMMENT Headline"
|
||
(let ((org-comment-string "COMMENT")
|
||
(org-todo-keywords '((sequence "TODO" "DONE"))))
|
||
(should (org-element-property :commentedp (org-element-at-point))))))
|
||
|
||
(ert-deftest test-org-element/headline-archive-tag ()
|
||
"Test ARCHIVE tag recognition."
|
||
;; Reference test.
|
||
(org-test-with-temp-text "* Headline"
|
||
(let ((org-archive-tag "ARCHIVE"))
|
||
(should-not (org-element-property :archivedp (org-element-at-point)))))
|
||
;; Single tag.
|
||
(org-test-with-temp-text "* Headline :ARCHIVE:"
|
||
(let ((org-archive-tag "ARCHIVE"))
|
||
(let ((headline (org-element-at-point)))
|
||
(should (org-element-property :archivedp headline))
|
||
;; Test tag removal.
|
||
(should-not (org-element-property :tags headline))))
|
||
(let ((org-archive-tag "Archive"))
|
||
(should-not (org-element-property :archivedp (org-element-at-point)))))
|
||
;; Multiple tags.
|
||
(org-test-with-temp-text "* Headline :test:ARCHIVE:"
|
||
(let ((org-archive-tag "ARCHIVE"))
|
||
(let ((headline (org-element-at-point)))
|
||
(should (org-element-property :archivedp headline))
|
||
;; Test tag removal.
|
||
(should (equal (org-element-property :tags headline) '("test")))))))
|
||
|
||
|
||
;;;; Horizontal Rule
|
||
|
||
(ert-deftest test-org-element/horizontal-rule-parser ()
|
||
"Test `horizontal-rule' parser."
|
||
;; Standard.
|
||
(should
|
||
(org-test-with-temp-text "-----"
|
||
(org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
|
||
;; Indented.
|
||
(should
|
||
(org-test-with-temp-text " -----"
|
||
(org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
|
||
;; Hyphen must be alone on the line.
|
||
(should-not
|
||
(org-test-with-temp-text "-----wrong"
|
||
(org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
|
||
;; 4 hyphens is too small.
|
||
(should-not
|
||
(org-test-with-temp-text "----"
|
||
(org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity))))
|
||
|
||
|
||
;;;; Inline Babel Call
|
||
|
||
(ert-deftest test-org-element/inline-babel-call-parser ()
|
||
"Test `inline-babel-call' parser."
|
||
(should
|
||
(org-test-with-temp-text "call_test()"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'inline-babel-call 'identity))))
|
||
|
||
|
||
;;;; Inline Src Block
|
||
|
||
(ert-deftest test-org-element/inline-src-block-parser ()
|
||
"Test `inline-src-block' parser."
|
||
(should
|
||
(org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
|
||
(org-element-map (org-element-parse-buffer) 'inline-src-block 'identity))))
|
||
|
||
|
||
;;;; Inlinetask
|
||
|
||
(ert-deftest test-org-element/inlinetask-parser ()
|
||
"Test `inlinetask' parser."
|
||
(when (featurep 'org-inlinetask)
|
||
(let ((org-inlinetask-min-level 15))
|
||
;; 1. Regular inlinetask.
|
||
(should
|
||
(org-test-with-temp-text
|
||
"*************** Task\nTest\n*************** END"
|
||
(org-element-map (org-element-parse-buffer) 'inlinetask 'identity)))
|
||
;; 2. Degenerate inlinetask.
|
||
(should
|
||
(org-test-with-temp-text "*************** Task"
|
||
(org-element-map (org-element-parse-buffer) 'inlinetask 'identity)))
|
||
;; TODO keyword.
|
||
(should
|
||
(equal
|
||
"TODO"
|
||
(let ((org-todo-keywords '((sequence "TODO" "DONE"))))
|
||
(org-test-with-temp-text "*************** TODO Task"
|
||
(org-element-property
|
||
:todo-keyword
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'inlinetask 'identity nil t))))))
|
||
;; Planning info.
|
||
(should
|
||
(equal
|
||
"2012-03-29 thu."
|
||
(org-test-with-temp-text "
|
||
*************** Task
|
||
DEADLINE: <2012-03-29 thu.>"
|
||
(org-element-property
|
||
:deadline
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'inlinetask 'identity nil t)))))
|
||
;; Priority.
|
||
(should
|
||
(equal
|
||
?A
|
||
(org-test-with-temp-text "
|
||
*************** [#A] Task"
|
||
(org-element-property
|
||
:priority
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'inlinetask 'identity nil t)))))
|
||
;; Tags.
|
||
(should
|
||
(equal
|
||
'("test")
|
||
(org-test-with-temp-text "
|
||
*************** Task :test:"
|
||
(org-element-property
|
||
:tags
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'inlinetask 'identity nil t))))))))
|
||
|
||
|
||
;;;; Italic
|
||
|
||
(ert-deftest test-org-element/italic-parser ()
|
||
"Test `italic' parser."
|
||
;; Regular test.
|
||
(should
|
||
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
|
||
\n,\"']\\|[^
|
||
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
|
||
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
|
||
(org-test-with-temp-text "/italic/"
|
||
(org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
|
||
;; Multi-line markup.
|
||
(should
|
||
(equal
|
||
(org-element-contents
|
||
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^
|
||
\n,\"']\\|[^
|
||
\n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^
|
||
\n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
|
||
(org-test-with-temp-text "/first line\nsecond line/"
|
||
(org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
|
||
'("first line\nsecond line"))))
|
||
|
||
|
||
;;;; Item
|
||
|
||
(ert-deftest test-org-element/item-parser ()
|
||
"Test `item' parser."
|
||
;; Standard test.
|
||
(should
|
||
(org-test-with-temp-text "- item"
|
||
(org-element-map (org-element-parse-buffer) 'item 'identity)))
|
||
;; Counter.
|
||
(should
|
||
(= 6
|
||
(org-element-property
|
||
:counter
|
||
(org-test-with-temp-text "6. [@6] item"
|
||
(org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
|
||
;; Tag
|
||
(should
|
||
(equal
|
||
'("tag")
|
||
(org-element-property
|
||
:tag
|
||
(org-test-with-temp-text "- tag :: description"
|
||
(org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
|
||
;; Check-boxes
|
||
(should
|
||
(equal
|
||
'(trans on off)
|
||
(org-test-with-temp-text "
|
||
- [-] item 1
|
||
- [X] item 1.1
|
||
- [ ] item 1.2"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'item
|
||
(lambda (item) (org-element-property :checkbox item))))))
|
||
;; Folded state.
|
||
(org-test-with-temp-text "* Headline
|
||
- item
|
||
|
||
paragraph below"
|
||
(forward-line)
|
||
(let ((org-cycle-include-plain-lists t)) (org-cycle))
|
||
(should
|
||
(org-element-property
|
||
:hiddenp
|
||
(org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
|
||
|
||
|
||
;;;; Keyword
|
||
|
||
(ert-deftest test-org-element/keyword-parser ()
|
||
"Test `keyword' parser."
|
||
;; Standard test.
|
||
(should
|
||
(org-test-with-temp-text "#+KEYWORD: value"
|
||
(org-element-map (org-element-parse-buffer) 'keyword 'identity)))
|
||
;; Keywords are case-insensitive.
|
||
(should
|
||
(org-test-with-temp-text "#+keyword: value"
|
||
(org-element-map (org-element-parse-buffer) 'keyword 'identity)))
|
||
;; Affiliated keywords are not keywords.
|
||
(should-not
|
||
(org-test-with-temp-text "#+NAME: value
|
||
Paragraph"
|
||
(org-element-map (org-element-parse-buffer) 'keyword 'identity)))
|
||
;; Do not mix keywords with Babel calls and dynamic blocks.
|
||
(should-not
|
||
(org-test-with-temp-text "#+CALL: fun()"
|
||
(org-element-map (org-element-parse-buffer) 'keyword 'identity)))
|
||
(should-not
|
||
(org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
|
||
(org-element-map (org-element-parse-buffer) 'keyword 'identity))))
|
||
|
||
|
||
;;;; Latex Environment
|
||
|
||
(ert-deftest test-org-element/latex-environment-parser ()
|
||
"Test `latex-environment' parser."
|
||
(should
|
||
(org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'latex-environment 'identity)))
|
||
;; Allow nested environments.
|
||
(should
|
||
(equal
|
||
"\\begin{outer}
|
||
\\begin{inner}
|
||
e^{i\\pi}+1=0
|
||
\\end{inner}
|
||
\\end{outer}"
|
||
(org-test-with-temp-text "
|
||
\\begin{outer}
|
||
\\begin{inner}
|
||
e^{i\\pi}+1=0
|
||
\\end{inner}
|
||
\\end{outer}"
|
||
(org-element-property
|
||
:value
|
||
(org-element-map
|
||
(org-element-parse-buffer) 'latex-environment 'identity nil t))))))
|
||
|
||
|
||
;;;; Latex Fragment
|
||
|
||
(ert-deftest test-org-element/latex-fragment-parser ()
|
||
"Test `latex-fragment' parser."
|
||
(let ((org-latex-regexps
|
||
'(("begin" "^[ ]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^ |