Skip to content

Commit

Permalink
Add items and empty-p
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCassou committed Apr 16, 2017
1 parent e5ed17c commit 2de3460
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions hierarchy.el
Expand Up @@ -183,10 +183,18 @@ default, SORTFN is `string-lessp'."
;; Querying
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun hierarchy-items (hierarchy)
"Return a list of all items of HIERARCHY."
(map-keys (hierarchy--seen-items hierarchy)))

(defun hierarchy-has-item (hierarchy item)
"Return t if HIERARCHY includes ITEM."
(map-contains-key (hierarchy--seen-items hierarchy) item))

(defun hierarchy-empty-p (hierarchy)
"Return t if HIERARCHY is empty."
(= 0 (hierarchy-length hierarchy)))

(defun hierarchy-length (hierarchy)
"Return the number of items in HIERARCHY."
(hash-table-count (hierarchy--seen-items hierarchy)))
Expand Down
19 changes: 19 additions & 0 deletions test/hierarchy-test.el
Expand Up @@ -168,6 +168,12 @@
(should-error
(hierarchy--add-relation hierarchy 'bird 'cow #'identity))))

(ert-deftest hierarchy-empty-p-return-non-nil-for-empty ()
(should (hierarchy-empty-p (hierarchy-new))))

(ert-deftest hierarchy-empty-p-return-nil-for-non-empty ()
(should-not (hierarchy-empty-p (test-helper-animals))))

(ert-deftest hierarchy-length-of-empty-is-0 ()
(should (equal (hierarchy-length (hierarchy-new)) 0)))

Expand Down Expand Up @@ -312,6 +318,19 @@
(let* ((animals (test-helper-animals)))
(should-not (hierarchy-extract-tree animals 'foobar))))

(ert-deftest hierarchy-items-of-empty-hierarchy-is-empty ()
(should (seq-empty-p (hierarchy-items (hierarchy-new)))))

(ert-deftest hierarchy-items-returns-sequence-of-same-length ()
(let* ((animals (test-helper-animals))
(result (hierarchy-items animals)))
(should (= (seq-length result) (hierarchy-length animals)))))

(ert-deftest hierarchy-items-return-all-elements-of-hierarchy ()
(let* ((animals (test-helper-animals))
(result (hierarchy-items animals)))
(should (equal (seq-sort #'string< result) '(animal bird cow dolphin dove pigeon)))))

(ert-deftest hierarchy-labelfn-indent-no-indent-if-0 ()
(let* ((labelfn-base (lambda (_item _indent) (insert "foo")))
(labelfn (hierarchy-labelfn-indent labelfn-base)))
Expand Down

0 comments on commit 2de3460

Please sign in to comment.