Browse Source

Test `org-map-entries'

* lisp/org.el (org-make-tags-matcher): Small refactoring.
* testing/lisp/test-org.el (test-org/map-entries): New test.
Nicolas Goaziou 9 years ago
parent
commit
2520fd654a
2 changed files with 94 additions and 10 deletions
  1. 4 10
      lisp/org.el
  2. 90 0
      testing/lisp/test-org.el

+ 4 - 10
lisp/org.el

@@ -14429,8 +14429,7 @@ See also `org-scan-tags'."
       (setq todomatch nil))
 
     ;; Make the tags matcher.
-    (if (not (org-string-nw-p tagsmatch))
-	(setq tagsmatcher t)
+    (when (org-string-nw-p tagsmatch)
       (let ((orlist nil)
 	    (orterms (org-split-string tagsmatch "|"))
 	    term)
@@ -14479,17 +14478,12 @@ See also `org-scan-tags'."
 		     (t `(member ,tag tags-list)))))
 	      (push (if minus `(not ,mm) mm) tagsmatcher)
 	      (setq term rest)))
-	  (push (if (> (length tagsmatcher) 1)
-		    (cons 'and tagsmatcher)
-		  (car tagsmatcher))
-		orlist)
+	  (push `(and ,@tagsmatcher) orlist)
 	  (setq tagsmatcher nil))
-	(setq tagsmatcher
-	      `(progn (setq org-cached-props nil) ,(cons 'or orlist)))))
+	(setq tagsmatcher `(progn (setq org-cached-props nil) (or ,@orlist)))))
 
     ;; Make the TODO matcher.
-    (if (not (org-string-nw-p todomatch))
-	(setq todomatcher t)
+    (when (org-string-nw-p todomatch)
       (let ((orlist nil))
 	(dolist (term (org-split-string todomatch "|"))
 	  (while (string-match re term)

+ 90 - 0
testing/lisp/test-org.el

@@ -1442,6 +1442,96 @@
 				     ">>>>>>>>>>")
 	    ">>>>>>>>..")))
 
+(ert-deftest test-org/map-entries ()
+  "Test `org-map-entries' specifications."
+  ;; Full match.
+  (should
+   (equal '(1 11)
+	  (org-test-with-temp-text "* Level 1\n** Level 2"
+	    (org-map-entries #'point))))
+  ;; Level match.
+  (should
+   (equal '(1)
+	  (org-test-with-temp-text "* Level 1\n** Level 2"
+	    (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
+  (should
+   (equal '(11)
+	  (org-test-with-temp-text "* Level 1\n** Level 2"
+	    (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
+  ;; Tag match.
+  (should
+   (equal '(11)
+	  (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
+	    (org-map-entries #'point "yes"))))
+  (should
+   (equal '(14)
+	  (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
+	    (org-map-entries #'point "+yes-a"))))
+  (should
+   (equal '(11 23)
+	  (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
+	    (org-map-entries #'point "{yes?}"))))
+  ;; Priority match.
+  (should
+   (equal '(1)
+	  (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
+	    (org-map-entries #'point "PRIORITY=\"A\""))))
+  ;; Date match.
+  (should
+   (equal '(36)
+	  (org-test-with-temp-text "
+* H1
+SCHEDULED: <2012-03-29 thu.>
+* H2
+SCHEDULED: <2014-03-04 tue.>"
+	    (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
+  (should
+   (equal '(2)
+	  (org-test-with-temp-text "
+* H1
+SCHEDULED: <2012-03-29 thu.>
+* H2
+SCHEDULED: <2014-03-04 tue.>"
+	    (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
+  ;; Regular property match.
+  (should
+   (equal '(2)
+	  (org-test-with-temp-text "
+* H1
+:PROPERTIES:
+:TEST: 1
+:END:
+* H2
+:PROPERTIES:
+:TEST: 2
+:END:"
+	    (org-map-entries #'point "TEST=1"))))
+  ;; Multiple criteria.
+  (should
+   (equal '(23)
+	  (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
+	    (let (org-odd-levels-only
+		  (org-use-tag-inheritance nil))
+	      (org-map-entries #'point "yes+LEVEL=1")))))
+  ;; "or" criteria.
+  (should
+   (equal '(12 24)
+	  (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
+	    (let (org-odd-levels-only)
+	      (org-map-entries #'point "LEVEL=2|no")))))
+  (should
+   (equal '(1 12)
+	  (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
+	    (let (org-odd-levels-only)
+	      (org-map-entries #'point "yes|no")))))
+  ;; "and" criteria.
+  (should
+   (equal '(22)
+	  (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
+	    (let (org-odd-levels-only)
+	      (org-map-entries #'point "yes&no"))))))
+
+
 
 ;;; Keywords