Skip to content

Commit

Permalink
Fix filter in AWK indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
l3kn committed Mar 12, 2024
1 parent 0163a79 commit cf70a39
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
9 changes: 3 additions & 6 deletions org-fc-awk.el
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ ITAGS and LTAGS are strings `\":tag1:tag2:\"'"
(org-fc-file
:path (plist-get pfile :path)
:title (plist-get pfile :title)))
(filtered-cards
(if filter
(cl-remove-if-not filter (plist-get pfile :cards))
(plist-get pfile :cards)))
(ocards
(mapcar
(lambda (pcard)
Expand All @@ -130,8 +126,9 @@ ITAGS and LTAGS are strings `\":tag1:tag2:\"'"
(plist-get pcard :inherited-tags)
(plist-get pcard :local-tags)))
ofile))
filtered-cards)))
(oset ofile cards ocards)
(plist-get pfile :cards))))
(oset ofile cards
(if filter (cl-remove-if-not filter ocards) ocards))
ofile))
(read (concat "(" output ")")))
(error "Org-fc shell error: %s" output))))
Expand Down
32 changes: 13 additions & 19 deletions tests/org-fc-filter-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,61 +20,55 @@
(equal (sort ids1 #'string-lessp)
(sort ids2 #'string-lessp))))

(defun org-fc-test-filter-index (index filter)
(cl-remove-if-not
(org-fc--compile-filter filter)
index))

(ert-deftest org-fc-filter-test ()
(let* ((index
(org-fc-index-flatten-file
(org-fc-awk-index (list (org-fc-test-fixture "filter/"))))))
(let* ((paths (list (org-fc-test-fixture "filter/")))
(org-fc-index-function #'org-fc-awk-index))
;; Index of all cards
(should (org-fc-test-compare-ids
index
(org-fc-index `(:paths ,paths))
'(a-normal a-double b-normal1 b-normal2 c-double c-cloze)))

;; Filter by type
(should
(org-fc-test-compare-ids
(org-fc-test-filter-index index '(type double))
(org-fc-index `(:paths ,paths :filter (type double)))
'(a-double c-double)))

;; Filter by type, or
(should
(org-fc-test-compare-ids
(org-fc-test-filter-index index '(or (type cloze) (type double)))
(org-fc-index `(:paths ,paths :filter (or (type cloze) (type double))))
'(a-double c-double c-cloze)))

;; Filter by tag, direct
(should
(org-fc-test-compare-ids
(org-fc-test-filter-index index '(tag "tag1"))
(org-fc-index `(:paths ,paths :filter (tag "tag1")))
'(a-normal a-double)))

;; Filter by tag, inherited
(should
(org-fc-test-compare-ids
(org-fc-test-filter-index index '(tag "tag2"))
(org-fc-index `(:paths ,paths :filter (tag "tag2")))
'(a-double b-normal1)))

;; Filter by tag, filetag
(should
(org-fc-test-compare-ids
(org-fc-test-filter-index index '(and (tag "file1")
(tag "file2")
(tag "file3")))
(org-fc-index `(:paths ,paths :filter (and (tag "file1")
(tag "file2")
(tag "file3"))))
'(c-double c-cloze)))

;; Negation
(should
(org-fc-test-compare-ids
(org-fc-test-filter-index index '(not (type normal)))
(org-fc-index `(:paths ,paths :filter (not (type normal))))
'(a-double c-double c-cloze)))

;; Combined
(should
(org-fc-test-compare-ids
(org-fc-test-filter-index index '(and (not (type normal))
(tag "file1")))
(org-fc-index `(:paths ,paths :filter (and (not (type normal))
(tag "file1"))))
'(c-double c-cloze)))))

0 comments on commit cf70a39

Please sign in to comment.