org-element: Refactoring

This commit is contained in:
Nicolas Goaziou 2012-07-11 14:17:02 +02:00
parent 7b6171735f
commit c75120aa71
2 changed files with 657 additions and 565 deletions

File diff suppressed because it is too large Load Diff

View File

@ -69,46 +69,52 @@ Some other text
(ert-deftest test-org-element/put-property () (ert-deftest test-org-element/put-property ()
"Test `org-element-put-property' specifications." "Test `org-element-put-property' specifications."
(org-test-with-parsed-data "* Headline\n *a*" (org-test-with-temp-text "* Headline\n *a*"
(let ((tree (org-element-parse-buffer)))
(org-element-put-property (org-element-put-property
(org-element-map tree 'bold 'identity nil t) :test 1) (org-element-map tree 'bold 'identity nil t) :test 1)
(should (org-element-property (should (org-element-property
:test (org-element-map tree 'bold 'identity nil t))))) :test (org-element-map tree 'bold 'identity nil t))))))
(ert-deftest test-org-element/set-contents () (ert-deftest test-org-element/set-contents ()
"Test `org-element-set-contents' specifications." "Test `org-element-set-contents' specifications."
;; Accept multiple entries. ;; Accept multiple entries.
(should (should
(equal '("b" (italic nil "a")) (equal '("b" (italic nil "a"))
(org-test-with-parsed-data "* Headline\n *a*" (org-test-with-temp-text "* Headline\n *a*"
(let ((tree (org-element-parse-buffer)))
(org-element-set-contents (org-element-set-contents
(org-element-map tree 'bold 'identity nil t) "b" '(italic nil "a")) (org-element-map tree 'bold 'identity nil t) "b" '(italic nil "a"))
(org-element-contents (org-element-contents
(org-element-map tree 'bold 'identity nil t))))) (org-element-map tree 'bold 'identity nil t))))))
;; Accept atoms and elements. ;; Accept atoms and elements.
(should (should
(equal '("b") (equal '("b")
(org-test-with-parsed-data "* Headline\n *a*" (org-test-with-temp-text "* Headline\n *a*"
(let ((tree (org-element-parse-buffer)))
(org-element-set-contents (org-element-set-contents
(org-element-map tree 'bold 'identity nil t) "b") (org-element-map tree 'bold 'identity nil t) "b")
(org-element-contents (org-element-contents
(org-element-map tree 'bold 'identity nil t))))) (org-element-map tree 'bold 'identity nil t))))))
(should (should
(equal '((italic nil "b")) (equal '((italic nil "b"))
(org-test-with-parsed-data "* Headline\n *a*" (org-test-with-temp-text "* Headline\n *a*"
(let ((tree (org-element-parse-buffer)))
(org-element-set-contents (org-element-set-contents
(org-element-map tree 'bold 'identity nil t) '(italic nil "b")) (org-element-map tree 'bold 'identity nil t) '(italic nil "b"))
(org-element-contents (org-element-contents
(org-element-map tree 'bold 'identity nil t))))) (org-element-map tree 'bold 'identity nil t))))))
;; Allow nil contents. ;; Allow nil contents.
(should-not (should-not
(org-test-with-parsed-data "* Headline\n *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)) (org-element-set-contents (org-element-map tree 'bold 'identity nil t))
(org-element-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 () (ert-deftest test-org-element/set-element ()
"Test `org-element-set-element' specifications." "Test `org-element-set-element' specifications."
(org-test-with-parsed-data "* Headline\n*a*" (org-test-with-temp-text "* Headline\n*a*"
(let ((tree (org-element-parse-buffer)))
(org-element-set-element (org-element-set-element
(org-element-map tree 'bold 'identity nil t) (org-element-map tree 'bold 'identity nil t)
'(italic nil "b")) '(italic nil "b"))
@ -120,35 +126,38 @@ Some other text
(equal (equal
(org-element-property :parent (org-element-property :parent
(org-element-map tree 'italic 'identity nil t)) (org-element-map tree 'italic 'identity nil t))
(org-element-map tree 'paragraph 'identity nil t))))) (org-element-map tree 'paragraph 'identity nil t))))))
(ert-deftest test-org-element/adopt-element () (ert-deftest test-org-element/adopt-element ()
"Test `org-element-adopt-element' specifications." "Test `org-element-adopt-element' specifications."
;; Adopt an element. ;; Adopt an element.
(should (should
(equal '(italic plain-text) (equal '(italic plain-text)
(org-test-with-parsed-data "* Headline\n *a*" (org-test-with-temp-text "* Headline\n *a*"
(let ((tree (org-element-parse-buffer)))
(org-element-adopt-element (org-element-adopt-element
(org-element-map tree 'bold 'identity nil t) '(italic nil "a")) (org-element-map tree 'bold 'identity nil t) '(italic nil "a"))
(mapcar (lambda (blob) (org-element-type blob)) (mapcar (lambda (blob) (org-element-type blob))
(org-element-contents (org-element-contents
(org-element-map tree 'bold 'identity nil t)))))) (org-element-map tree 'bold 'identity nil t)))))))
;; Adopt a string. ;; Adopt a string.
(should (should
(equal '("b" "a") (equal '("b" "a")
(org-test-with-parsed-data "* Headline\n *a*" (org-test-with-temp-text "* Headline\n *a*"
(let ((tree (org-element-parse-buffer)))
(org-element-adopt-element (org-element-adopt-element
(org-element-map tree 'bold 'identity nil t) "b") (org-element-map tree 'bold 'identity nil t) "b")
(org-element-contents (org-element-contents
(org-element-map tree 'bold 'identity nil t))))) (org-element-map tree 'bold 'identity nil t))))))
;; Test APPEND optional argument. ;; Test APPEND optional argument.
(should (should
(equal '("a" "b") (equal '("a" "b")
(org-test-with-parsed-data "* Headline\n *a*" (org-test-with-temp-text "* Headline\n *a*"
(let ((tree (org-element-parse-buffer)))
(org-element-adopt-element (org-element-adopt-element
(org-element-map tree 'bold 'identity nil t) "b" t) (org-element-map tree 'bold 'identity nil t) "b" t)
(org-element-contents (org-element-contents
(org-element-map tree 'bold 'identity nil t)))))) (org-element-map tree 'bold 'identity nil t)))))))
@ -429,97 +438,97 @@ CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
(let ((org-coderef-label-format "(ref:%s)")) (let ((org-coderef-label-format "(ref:%s)"))
;; 1. Test "-i" switch. ;; 1. Test "-i" switch.
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC" (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should-not (org-element-property :preserve-indent element)))) (should-not (org-element-property :preserve-indent element))))
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC" (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (org-element-property :preserve-indent element)))) (should (org-element-property :preserve-indent element))))
(org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE" (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should-not (org-element-property :preserve-indent element)))) (should-not (org-element-property :preserve-indent element))))
(org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE" (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (org-element-property :preserve-indent element)))) (should (org-element-property :preserve-indent element))))
;; 2. "-n -r -k" combination should number lines, retain labels but ;; 2. "-n -r -k" combination should number lines, retain labels but
;; not use them in coderefs. ;; not use them in coderefs.
(org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE" (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (org-element-property :number-lines element) (should (and (org-element-property :number-lines element)
(org-element-property :retain-labels element) (org-element-property :retain-labels element)
(not (org-element-property :use-labels element)))))) (not (org-element-property :use-labels element))))))
(org-test-with-temp-text (org-test-with-temp-text
"#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC" "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (org-element-property :number-lines element) (should (and (org-element-property :number-lines element)
(org-element-property :retain-labels element) (org-element-property :retain-labels element)
(not (org-element-property :use-labels element)))))) (not (org-element-property :use-labels element))))))
;; 3. "-n -r" combination should number-lines remove labels and not ;; 3. "-n -r" combination should number-lines remove labels and not
;; use them in coderefs. ;; use them in coderefs.
(org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE" (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (org-element-property :number-lines element) (should (and (org-element-property :number-lines element)
(not (org-element-property :retain-labels element)) (not (org-element-property :retain-labels element))
(not (org-element-property :use-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" (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (org-element-property :number-lines element) (should (and (org-element-property :number-lines element)
(not (org-element-property :retain-labels element)) (not (org-element-property :retain-labels element))
(not (org-element-property :use-labels element)))))) (not (org-element-property :use-labels element))))))
;; 4. "-n" or "+n" should number lines, retain labels and use them ;; 4. "-n" or "+n" should number lines, retain labels and use them
;; in coderefs. ;; in coderefs.
(org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE" (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (org-element-property :number-lines element) (should (and (org-element-property :number-lines element)
(org-element-property :retain-labels element) (org-element-property :retain-labels element)
(org-element-property :use-labels element))))) (org-element-property :use-labels element)))))
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC" (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (org-element-property :number-lines element) (should (and (org-element-property :number-lines element)
(org-element-property :retain-labels element) (org-element-property :retain-labels element)
(org-element-property :use-labels element))))) (org-element-property :use-labels element)))))
(org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE" (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (org-element-property :number-lines element) (should (and (org-element-property :number-lines element)
(org-element-property :retain-labels element) (org-element-property :retain-labels element)
(org-element-property :use-labels element))))) (org-element-property :use-labels element)))))
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC" (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (org-element-property :number-lines element) (should (and (org-element-property :number-lines element)
(org-element-property :retain-labels element) (org-element-property :retain-labels element)
(org-element-property :use-labels element))))) (org-element-property :use-labels element)))))
;; 5. No switch should not number lines, but retain labels and use ;; 5. No switch should not number lines, but retain labels and use
;; them in coderefs. ;; them in coderefs.
(org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE" (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (not (org-element-property :number-lines element)) (should (and (not (org-element-property :number-lines element))
(org-element-property :retain-labels element) (org-element-property :retain-labels element)
(org-element-property :use-labels element))))) (org-element-property :use-labels element)))))
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC" (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (not (org-element-property :number-lines element)) (should (and (not (org-element-property :number-lines element))
(org-element-property :retain-labels element) (org-element-property :retain-labels element)
(org-element-property :use-labels element))))) (org-element-property :use-labels element)))))
;; 6. "-r" switch only: do not number lines, remove labels, and ;; 6. "-r" switch only: do not number lines, remove labels, and
;; don't use labels in coderefs. ;; don't use labels in coderefs.
(org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE" (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (not (org-element-property :number-lines element)) (should (and (not (org-element-property :number-lines element))
(not (org-element-property :retain-labels element)) (not (org-element-property :retain-labels element))
(not (org-element-property :use-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" (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (and (not (org-element-property :number-lines element)) (should (and (not (org-element-property :number-lines element))
(not (org-element-property :retain-labels element)) (not (org-element-property :retain-labels element))
(not (org-element-property :use-labels element)))))) (not (org-element-property :use-labels element))))))
;; 7. Recognize coderefs with user-defined syntax. ;; 7. Recognize coderefs with user-defined syntax.
(org-test-with-temp-text (org-test-with-temp-text
"#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE" "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (should
(equal (org-element-property :label-fmt element) "[ref:%s]")))) (equal (org-element-property :label-fmt element) "[ref:%s]"))))
(org-test-with-temp-text (org-test-with-temp-text
"#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC" "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
(let ((element (org-element-current-element))) (let ((element (org-element-current-element (point-max))))
(should (should
(equal (org-element-property :label-fmt element) "[ref:%s]")))))) (equal (org-element-property :label-fmt element) "[ref:%s]"))))))