Add tests for org-refile-get-targets

* testing/lisp/test-org.el (test-org/refile-get-targets): New test.

Based on a patch from Sebastian Reuße <seb@wirrsal.net>.
This commit is contained in:
Nicolas Goaziou 2017-05-22 02:17:32 +02:00
parent 23ac37da7c
commit f335c3517d
1 changed files with 105 additions and 0 deletions

View File

@ -5340,6 +5340,111 @@ Paragraph<point>"
(insert "new")
(org-element-type (org-element-context))))))
;;; Refile
(ert-deftest test-org/refile-get-targets ()
"Test `org-refile-get-targets' specifications."
;; :maxlevel includes all headings above specified value.
(should
(equal '("H1" "H2" "H3")
(org-test-with-temp-text "* H1\n** H2\n*** H3"
(let ((org-refile-use-outline-path nil)
(org-refile-targets `((nil :maxlevel . 3))))
(mapcar #'car (org-refile-get-targets))))))
(should
(equal '("H1" "H2")
(org-test-with-temp-text "* H1\n** H2\n*** H3"
(let ((org-refile-use-outline-path nil)
(org-refile-targets `((nil :maxlevel . 2))))
(mapcar #'car (org-refile-get-targets))))))
;; :level limits targets to headlines with the specified level.
(should
(equal '("H2")
(org-test-with-temp-text "* H1\n** H2\n*** H3"
(let ((org-refile-use-outline-path nil)
(org-refile-targets `((nil :level . 2))))
(mapcar #'car (org-refile-get-targets))))))
;; :tag limits targets to headlines with specified tag.
(should
(equal '("H1")
(org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
(let ((org-refile-use-outline-path nil)
(org-refile-targets `((nil :tag . "foo"))))
(mapcar #'car (org-refile-get-targets))))))
;; :todo limits targets to headlines with specified TODO keyword.
(should
(equal '("H2")
(org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
(let ((org-refile-use-outline-path nil)
(org-refile-targets `((nil :todo . "TODO"))))
(mapcar #'car (org-refile-get-targets))))))
;; :regexp filters targets matching provided regexp.
(should
(equal '("F2" "F3")
(org-test-with-temp-text "* H1\n** F2\n*** F3"
(let ((org-refile-use-outline-path nil)
(org-refile-targets `((nil :regexp . "F"))))
(mapcar #'car (org-refile-get-targets))))))
;; A nil `org-refile-targets' includes only top level headlines in
;; current buffer.
(should
(equal '("H1" "H2")
(org-test-with-temp-text "* H1\n** S1\n* H2"
(let ((org-refile-use-outline-path nil)
(org-refile-targets nil))
(mapcar #'car (org-refile-get-targets))))))
;; Return value is the union of the targets according to all the
;; defined rules.
(should
(equal '("F2" "F3" "H1")
(org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
(let ((org-refile-use-outline-path nil)
(org-refile-targets `((nil :regexp . "F")
(nil :todo . "TODO"))))
(mapcar #'car (org-refile-get-targets))))))
;; When `org-refile-use-outline-path' is non-nil, provide targets as
;; paths.
(should
(equal '("H1" "H1/H2" "H1/H2/H3")
(org-test-with-temp-text "* H1\n** H2\n*** H3"
(let ((org-refile-use-outline-path t)
(org-refile-targets `((nil :maxlevel . 3))))
(mapcar #'car (org-refile-get-targets))))))
;; When providing targets as paths, escape forward slashes in
;; headings with backslashes.
(should
(equal '("H1\\/foo")
(org-test-with-temp-text "* H1/foo"
(let ((org-refile-use-outline-path t)
(org-refile-targets `((nil :maxlevel . 1))))
(mapcar #'car (org-refile-get-targets))))))
;; When `org-refile-use-outline-path' is `file', include file name
;; without directory in targets.
(should
(org-test-with-temp-text-in-file "* H1"
(let* ((filename (buffer-file-name))
(org-refile-use-outline-path 'file)
(org-refile-targets `(((,filename) :level . 1))))
(member (file-name-nondirectory filename)
(mapcar #'car (org-refile-get-targets))))))
;; When `org-refile-use-outline-path' is `full-file-path', include
;; full file name.
(should
(org-test-with-temp-text-in-file "* H1"
(let* ((filename (buffer-file-name))
(org-refile-use-outline-path 'full-file-path)
(org-refile-targets `(((,filename) :level . 1))))
(member filename (mapcar #'car (org-refile-get-targets))))))
;; When `org-refile-use-outline-path' is `buffer-name', include
;; buffer name.
(should
(org-test-with-temp-text "* H1"
(let* ((org-refile-use-outline-path 'buffer-name)
(org-refile-targets `((nil :level . 1))))
(member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
;;; Sparse trees