Skip to content

Commit

Permalink
Fix to keep the children in the order they were added
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCassou committed Apr 24, 2019
1 parent 06f21d3 commit 4a97f3a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion hierarchy.el
Expand Up @@ -101,7 +101,8 @@ should be an item of the hierarchy."
(error "An item (%s) can only have one parent: '%s' vs '%s'"
item existing-parent parent))
((not has-parent-p)
(push item (map-elt (hierarchy--children hierarchy) parent (list)))
(let ((existing-children (map-elt (hierarchy--children hierarchy) parent (list))))
(map-put (hierarchy--children hierarchy) parent (append existing-children (list item))))
(map-put (hierarchy--parents hierarchy) item parent)))))

(defun hierarchy--set-equal (list1 list2 &rest cl-keys)
Expand Down
8 changes: 4 additions & 4 deletions test/hierarchy-test.el
Expand Up @@ -124,7 +124,7 @@
(hierarchy-add-tree hierarchy 'dove parentfn)
(hierarchy-add-tree hierarchy 'pigeon parentfn)
(expect (hierarchy-roots hierarchy) :to-equal '(bird))
(expect (hierarchy-children hierarchy 'bird) :to-equal '(pigeon dove))))
(expect (hierarchy-children hierarchy 'bird) :to-equal '(dove pigeon))))

(it "add-two-hierarchies"
(let ((parentfn (lambda (item)
Expand All @@ -147,7 +147,7 @@
(hierarchy-add-tree hierarchy 'animal nil childrenfn)
(expect (hierarchy-roots hierarchy) :to-equal '(animal))
(expect (hierarchy-children hierarchy 'animal) :to-equal '(bird))
(expect (hierarchy-children hierarchy 'bird) :to-equal '(pigeon dove))))
(expect (hierarchy-children hierarchy 'bird) :to-equal '(dove pigeon))))

(it "add-with-parentfn-and-childrenfn"
(let ((parentfn (lambda (item)
Expand All @@ -163,7 +163,7 @@
(expect (hierarchy-roots hierarchy) :to-equal '(life-form))
(expect (hierarchy-children hierarchy 'life-form) :to-equal '(animal))
(expect (hierarchy-children hierarchy 'animal) :to-equal '(bird))
(expect (hierarchy-children hierarchy 'bird) :to-equal '(pigeon dove))
(expect (hierarchy-children hierarchy 'bird) :to-equal '(dove pigeon))
(expect (hierarchy-children hierarchy 'pigeon) :to-equal '(ashy-wood-pigeon))))

(it "add-twice-with-parentfn-and-childrenfn"
Expand All @@ -190,7 +190,7 @@
(hierarchy-add-trees hierarchy '(dove pigeon) parentfn)
(expect (hierarchy-roots hierarchy) :to-equal '(animal))
(expect (hierarchy-children hierarchy 'animal) :to-equal '(bird))
(expect (hierarchy-children hierarchy 'bird) :to-equal '(pigeon dove))))
(expect (hierarchy-children hierarchy 'bird) :to-equal '(dove pigeon))))

(it "from-list"
(let ((hierarchy (hierarchy-from-list
Expand Down

0 comments on commit 4a97f3a

Please sign in to comment.