diff options
| author | Damien Cassou | 2020-08-09 14:48:22 +0200 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2020-08-09 14:48:22 +0200 |
| commit | 8e82baf5a730ff542118ddba5b76afdc1db643f6 (patch) | |
| tree | 6870659b38a168709c2f98f571c35a6451ecb64f /test | |
| parent | d586bae501a3d6ec8e6a8088d05b0abfa541dece (diff) | |
| download | emacs-8e82baf5a730ff542118ddba5b76afdc1db643f6.tar.gz emacs-8e82baf5a730ff542118ddba5b76afdc1db643f6.zip | |
Add the new library hierarchy.el
* lisp/emacs-lisp/hierarchy.el: New file.
Diffstat (limited to 'test')
| -rw-r--r-- | test/lisp/emacs-lisp/hierarchy-tests.el | 556 |
1 files changed, 556 insertions, 0 deletions
diff --git a/test/lisp/emacs-lisp/hierarchy-tests.el b/test/lisp/emacs-lisp/hierarchy-tests.el new file mode 100644 index 00000000000..23cfc79d848 --- /dev/null +++ b/test/lisp/emacs-lisp/hierarchy-tests.el | |||
| @@ -0,0 +1,556 @@ | |||
| 1 | ;;; hierarchy-tests.el --- Tests for hierarchy.el | ||
| 2 | |||
| 3 | ;; Copyright (C) 2017-2019 Damien Cassou | ||
| 4 | |||
| 5 | ;; Author: Damien Cassou <damien@cassou.me> | ||
| 6 | ;; Maintainer: emacs-devel@gnu.org | ||
| 7 | |||
| 8 | ;; This file is part of GNU Emacs. | ||
| 9 | |||
| 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 11 | ;; it under the terms of the GNU General Public License as published by | ||
| 12 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 13 | ;; (at your option) any later version. | ||
| 14 | |||
| 15 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | ;; GNU General Public License for more details. | ||
| 19 | |||
| 20 | ;; You should have received a copy of the GNU General Public License | ||
| 21 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 22 | |||
| 23 | ;;; Commentary: | ||
| 24 | |||
| 25 | ;; Tests for hierarchy.el | ||
| 26 | |||
| 27 | ;;; Code: | ||
| 28 | |||
| 29 | (require 'ert) | ||
| 30 | (require 'hierarchy) | ||
| 31 | |||
| 32 | (defun hierarchy-animals () | ||
| 33 | "Create a sorted animal hierarchy." | ||
| 34 | (let ((parentfn (lambda (item) (cl-case item | ||
| 35 | (dove 'bird) | ||
| 36 | (pigeon 'bird) | ||
| 37 | (bird 'animal) | ||
| 38 | (dolphin 'animal) | ||
| 39 | (cow 'animal)))) | ||
| 40 | (hierarchy (hierarchy-new))) | ||
| 41 | (hierarchy-add-tree hierarchy 'dove parentfn) | ||
| 42 | (hierarchy-add-tree hierarchy 'pigeon parentfn) | ||
| 43 | (hierarchy-add-tree hierarchy 'dolphin parentfn) | ||
| 44 | (hierarchy-add-tree hierarchy 'cow parentfn) | ||
| 45 | (hierarchy-sort hierarchy) | ||
| 46 | hierarchy)) | ||
| 47 | |||
| 48 | (ert-deftest hierarchy-add-one-root () | ||
| 49 | (let ((parentfn (lambda (_) nil)) | ||
| 50 | (hierarchy (hierarchy-new))) | ||
| 51 | (hierarchy-add-tree hierarchy 'animal parentfn) | ||
| 52 | (should (equal (hierarchy-roots hierarchy) '(animal))))) | ||
| 53 | |||
| 54 | (ert-deftest hierarchy-add-one-item-with-parent () | ||
| 55 | (let ((parentfn (lambda (item) | ||
| 56 | (cl-case item | ||
| 57 | (bird 'animal)))) | ||
| 58 | (hierarchy (hierarchy-new))) | ||
| 59 | (hierarchy-add-tree hierarchy 'bird parentfn) | ||
| 60 | (should (equal (hierarchy-roots hierarchy) '(animal))) | ||
| 61 | (should (equal (hierarchy-children hierarchy 'animal) '(bird))))) | ||
| 62 | |||
| 63 | (ert-deftest hierarchy-add-one-item-with-parent-and-grand-parent () | ||
| 64 | (let ((parentfn (lambda (item) | ||
| 65 | (cl-case item | ||
| 66 | (dove 'bird) | ||
| 67 | (bird 'animal)))) | ||
| 68 | (hierarchy (hierarchy-new))) | ||
| 69 | (hierarchy-add-tree hierarchy 'dove parentfn) | ||
| 70 | (should (equal (hierarchy-roots hierarchy) '(animal))) | ||
| 71 | (should (equal (hierarchy-children hierarchy 'animal) '(bird))) | ||
| 72 | (should (equal (hierarchy-children hierarchy 'bird) '(dove))))) | ||
| 73 | |||
| 74 | (ert-deftest hierarchy-add-same-root-twice () | ||
| 75 | (let ((parentfn (lambda (_) nil)) | ||
| 76 | (hierarchy (hierarchy-new))) | ||
| 77 | (hierarchy-add-tree hierarchy 'animal parentfn) | ||
| 78 | (hierarchy-add-tree hierarchy 'animal parentfn) | ||
| 79 | (should (equal (hierarchy-roots hierarchy) '(animal))))) | ||
| 80 | |||
| 81 | (ert-deftest hierarchy-add-same-child-twice () | ||
| 82 | (let ((parentfn (lambda (item) | ||
| 83 | (cl-case item | ||
| 84 | (bird 'animal)))) | ||
| 85 | (hierarchy (hierarchy-new))) | ||
| 86 | (hierarchy-add-tree hierarchy 'bird parentfn) | ||
| 87 | (hierarchy-add-tree hierarchy 'bird parentfn) | ||
| 88 | (should (equal (hierarchy-roots hierarchy) '(animal))) | ||
| 89 | (should (equal (hierarchy-children hierarchy 'animal) '(bird))))) | ||
| 90 | |||
| 91 | (ert-deftest hierarchy-add-item-and-its-parent () | ||
| 92 | (let ((parentfn (lambda (item) | ||
| 93 | (cl-case item | ||
| 94 | (bird 'animal)))) | ||
| 95 | (hierarchy (hierarchy-new))) | ||
| 96 | (hierarchy-add-tree hierarchy 'bird parentfn) | ||
| 97 | (hierarchy-add-tree hierarchy 'animal parentfn) | ||
| 98 | (should (equal (hierarchy-roots hierarchy) '(animal))) | ||
| 99 | (should (equal (hierarchy-children hierarchy 'animal) '(bird))))) | ||
| 100 | |||
| 101 | (ert-deftest hierarchy-add-item-and-its-child () | ||
| 102 | (let ((parentfn (lambda (item) | ||
| 103 | (cl-case item | ||
| 104 | (bird 'animal)))) | ||
| 105 | (hierarchy (hierarchy-new))) | ||
| 106 | (hierarchy-add-tree hierarchy 'animal parentfn) | ||
| 107 | (hierarchy-add-tree hierarchy 'bird parentfn) | ||
| 108 | (should (equal (hierarchy-roots hierarchy) '(animal))) | ||
| 109 | (should (equal (hierarchy-children hierarchy 'animal) '(bird))))) | ||
| 110 | |||
| 111 | (ert-deftest hierarchy-add-two-items-sharing-parent () | ||
| 112 | (let ((parentfn (lambda (item) | ||
| 113 | (cl-case item | ||
| 114 | (dove 'bird) | ||
| 115 | (pigeon 'bird)))) | ||
| 116 | (hierarchy (hierarchy-new))) | ||
| 117 | (hierarchy-add-tree hierarchy 'dove parentfn) | ||
| 118 | (hierarchy-add-tree hierarchy 'pigeon parentfn) | ||
| 119 | (should (equal (hierarchy-roots hierarchy) '(bird))) | ||
| 120 | (should (equal (hierarchy-children hierarchy 'bird) '(dove pigeon))))) | ||
| 121 | |||
| 122 | (ert-deftest hierarchy-add-two-hierarchies () | ||
| 123 | (let ((parentfn (lambda (item) | ||
| 124 | (cl-case item | ||
| 125 | (dove 'bird) | ||
| 126 | (circle 'shape)))) | ||
| 127 | (hierarchy (hierarchy-new))) | ||
| 128 | (hierarchy-add-tree hierarchy 'dove parentfn) | ||
| 129 | (hierarchy-add-tree hierarchy 'circle parentfn) | ||
| 130 | (should (equal (hierarchy-roots hierarchy) '(bird shape))) | ||
| 131 | (should (equal (hierarchy-children hierarchy 'bird) '(dove))) | ||
| 132 | (should (equal (hierarchy-children hierarchy 'shape) '(circle))))) | ||
| 133 | |||
| 134 | (ert-deftest hierarchy-add-with-childrenfn () | ||
| 135 | (let ((childrenfn (lambda (item) | ||
| 136 | (cl-case item | ||
| 137 | (animal '(bird)) | ||
| 138 | (bird '(dove pigeon))))) | ||
| 139 | (hierarchy (hierarchy-new))) | ||
| 140 | (hierarchy-add-tree hierarchy 'animal nil childrenfn) | ||
| 141 | (should (equal (hierarchy-roots hierarchy) '(animal))) | ||
| 142 | (should (equal (hierarchy-children hierarchy 'animal) '(bird))) | ||
| 143 | (should (equal (hierarchy-children hierarchy 'bird) '(dove pigeon))))) | ||
| 144 | |||
| 145 | (ert-deftest hierarchy-add-with-parentfn-and-childrenfn () | ||
| 146 | (let ((parentfn (lambda (item) | ||
| 147 | (cl-case item | ||
| 148 | (bird 'animal) | ||
| 149 | (animal 'life-form)))) | ||
| 150 | (childrenfn (lambda (item) | ||
| 151 | (cl-case item | ||
| 152 | (bird '(dove pigeon)) | ||
| 153 | (pigeon '(ashy-wood-pigeon))))) | ||
| 154 | (hierarchy (hierarchy-new))) | ||
| 155 | (hierarchy-add-tree hierarchy 'bird parentfn childrenfn) | ||
| 156 | (should (equal (hierarchy-roots hierarchy) '(life-form))) | ||
| 157 | (should (equal (hierarchy-children hierarchy 'life-form) '(animal))) | ||
| 158 | (should (equal (hierarchy-children hierarchy 'animal) '(bird))) | ||
| 159 | (should (equal (hierarchy-children hierarchy 'bird) '(dove pigeon))) | ||
| 160 | (should (equal (hierarchy-children hierarchy 'pigeon) '(ashy-wood-pigeon))))) | ||
| 161 | |||
| 162 | (ert-deftest hierarchy-add-twice-with-parentfn-and-childrenfn () | ||
| 163 | (let* ((parentfn (lambda (item) | ||
| 164 | (cl-case item | ||
| 165 | (dove 'bird) | ||
| 166 | (bird 'animal)))) | ||
| 167 | (childrenfn (lambda (item) | ||
| 168 | (cl-case item | ||
| 169 | (animal '(bird)) | ||
| 170 | (bird '(dove))))) | ||
| 171 | (hierarchy (hierarchy-new))) | ||
| 172 | (hierarchy-add-tree hierarchy 'bird parentfn childrenfn) | ||
| 173 | (should (equal (hierarchy-children hierarchy 'animal) '(bird))) | ||
| 174 | (should (equal (hierarchy-children hierarchy 'bird) '(dove))))) | ||
| 175 | |||
| 176 | (ert-deftest hierarchy-add-trees () | ||
| 177 | (let ((parentfn (lambda (item) | ||
| 178 | (cl-case item | ||
| 179 | (dove 'bird) | ||
| 180 | (pigeon 'bird) | ||
| 181 | (bird 'animal)))) | ||
| 182 | (hierarchy (hierarchy-new))) | ||
| 183 | (hierarchy-add-trees hierarchy '(dove pigeon) parentfn) | ||
| 184 | (should (equal (hierarchy-roots hierarchy) '(animal))) | ||
| 185 | (should (equal (hierarchy-children hierarchy 'animal) '(bird))) | ||
| 186 | (should (equal (hierarchy-children hierarchy 'bird) '(dove pigeon))))) | ||
| 187 | |||
| 188 | (ert-deftest hierarchy-from-list () | ||
| 189 | (let ((hierarchy (hierarchy-from-list | ||
| 190 | '(animal (bird (dove) | ||
| 191 | (pigeon)) | ||
| 192 | (cow) | ||
| 193 | (dolphin))))) | ||
| 194 | (hierarchy-sort hierarchy (lambda (item1 item2) | ||
| 195 | (string< (car item1) | ||
| 196 | (car item2)))) | ||
| 197 | (should (equal (hierarchy-to-string hierarchy (lambda (item) (symbol-name (car item)))) | ||
| 198 | "animal\n bird\n dove\n pigeon\n cow\n dolphin\n")))) | ||
| 199 | |||
| 200 | (ert-deftest hierarchy-from-list-with-duplicates () | ||
| 201 | (let ((hierarchy (hierarchy-from-list | ||
| 202 | '(a (b) (b)) | ||
| 203 | t))) | ||
| 204 | (hierarchy-sort hierarchy (lambda (item1 item2) | ||
| 205 | ;; sort by ID | ||
| 206 | (< (car item1) (car item2)))) | ||
| 207 | (should (equal (hierarchy-length hierarchy) 3)) | ||
| 208 | (should (equal (hierarchy-to-string | ||
| 209 | hierarchy | ||
| 210 | (lambda (item) | ||
| 211 | (format "%s(%s)" | ||
| 212 | (cadr item) | ||
| 213 | (car item)))) | ||
| 214 | "a(1)\n b(2)\n b(3)\n")))) | ||
| 215 | |||
| 216 | (ert-deftest hierarchy-from-list-with-childrenfn () | ||
| 217 | (let ((hierarchy (hierarchy-from-list | ||
| 218 | "abc" | ||
| 219 | nil | ||
| 220 | (lambda (item) | ||
| 221 | (when (string= item "abc") | ||
| 222 | (split-string item "" t)))))) | ||
| 223 | (hierarchy-sort hierarchy (lambda (item1 item2) (string< item1 item2))) | ||
| 224 | (should (equal (hierarchy-length hierarchy) 4)) | ||
| 225 | (should (equal (hierarchy-to-string hierarchy) | ||
| 226 | "abc\n a\n b\n c\n")))) | ||
| 227 | |||
| 228 | (ert-deftest hierarchy-add-relation-check-error-when-different-parent () | ||
| 229 | (let ((parentfn (lambda (item) | ||
| 230 | (cl-case item | ||
| 231 | (bird 'animal)))) | ||
| 232 | (hierarchy (hierarchy-new))) | ||
| 233 | (hierarchy-add-tree hierarchy 'bird parentfn) | ||
| 234 | (should-error | ||
| 235 | (hierarchy--add-relation hierarchy 'bird 'cow #'identity)))) | ||
| 236 | |||
| 237 | (ert-deftest hierarchy-empty-p-return-non-nil-for-empty () | ||
| 238 | (should (hierarchy-empty-p (hierarchy-new)))) | ||
| 239 | |||
| 240 | (ert-deftest hierarchy-empty-p-return-nil-for-non-empty () | ||
| 241 | (should-not (hierarchy-empty-p (hierarchy-animals)))) | ||
| 242 | |||
| 243 | (ert-deftest hierarchy-length-of-empty-is-0 () | ||
| 244 | (should (equal (hierarchy-length (hierarchy-new)) 0))) | ||
| 245 | |||
| 246 | (ert-deftest hierarchy-length-of-non-empty-counts-items () | ||
| 247 | (let ((parentfn (lambda (item) | ||
| 248 | (cl-case item | ||
| 249 | (bird 'animal) | ||
| 250 | (dove 'bird) | ||
| 251 | (pigeon 'bird)))) | ||
| 252 | (hierarchy (hierarchy-new))) | ||
| 253 | (hierarchy-add-tree hierarchy 'dove parentfn) | ||
| 254 | (hierarchy-add-tree hierarchy 'pigeon parentfn) | ||
| 255 | (should (equal (hierarchy-length hierarchy) 4)))) | ||
| 256 | |||
| 257 | (ert-deftest hierarchy-has-root () | ||
| 258 | (let ((parentfn (lambda (item) | ||
| 259 | (cl-case item | ||
| 260 | (bird 'animal) | ||
| 261 | (dove 'bird) | ||
| 262 | (pigeon 'bird)))) | ||
| 263 | (hierarchy (hierarchy-new))) | ||
| 264 | (should-not (hierarchy-has-root hierarchy 'animal)) | ||
| 265 | (should-not (hierarchy-has-root hierarchy 'bird)) | ||
| 266 | (hierarchy-add-tree hierarchy 'dove parentfn) | ||
| 267 | (hierarchy-add-tree hierarchy 'pigeon parentfn) | ||
| 268 | (should (hierarchy-has-root hierarchy 'animal)) | ||
| 269 | (should-not (hierarchy-has-root hierarchy 'bird)))) | ||
| 270 | |||
| 271 | (ert-deftest hierarchy-leafs () | ||
| 272 | (let ((animals (hierarchy-animals))) | ||
| 273 | (should (equal (hierarchy-leafs animals) | ||
| 274 | '(dove pigeon dolphin cow))))) | ||
| 275 | |||
| 276 | (ert-deftest hierarchy-leafs-includes-lonely-roots () | ||
| 277 | (let ((parentfn (lambda (item) nil)) | ||
| 278 | (hierarchy (hierarchy-new))) | ||
| 279 | (hierarchy-add-tree hierarchy 'foo parentfn) | ||
| 280 | (should (equal (hierarchy-leafs hierarchy) | ||
| 281 | '(foo))))) | ||
| 282 | |||
| 283 | (ert-deftest hierarchy-leafs-of-node () | ||
| 284 | (let ((animals (hierarchy-animals))) | ||
| 285 | (should (equal (hierarchy-leafs animals 'cow) '())) | ||
| 286 | (should (equal (hierarchy-leafs animals 'animal) '(dove pigeon dolphin cow))) | ||
| 287 | (should (equal (hierarchy-leafs animals 'bird) '(dove pigeon))) | ||
| 288 | (should (equal (hierarchy-leafs animals 'dove) '())))) | ||
| 289 | |||
| 290 | (ert-deftest hierarchy-child-p () | ||
| 291 | (let ((animals (hierarchy-animals))) | ||
| 292 | (should (hierarchy-child-p animals 'dove 'bird)) | ||
| 293 | (should (hierarchy-child-p animals 'bird 'animal)) | ||
| 294 | (should (hierarchy-child-p animals 'cow 'animal)) | ||
| 295 | (should-not (hierarchy-child-p animals 'cow 'bird)) | ||
| 296 | (should-not (hierarchy-child-p animals 'bird 'cow)) | ||
| 297 | (should-not (hierarchy-child-p animals 'animal 'dove)) | ||
| 298 | (should-not (hierarchy-child-p animals 'animal 'bird)))) | ||
| 299 | |||
| 300 | (ert-deftest hierarchy-descendant () | ||
| 301 | (let ((animals (hierarchy-animals))) | ||
| 302 | (should (hierarchy-descendant-p animals 'dove 'animal)) | ||
| 303 | (should (hierarchy-descendant-p animals 'dove 'bird)) | ||
| 304 | (should (hierarchy-descendant-p animals 'bird 'animal)) | ||
| 305 | (should (hierarchy-descendant-p animals 'cow 'animal)) | ||
| 306 | (should-not (hierarchy-descendant-p animals 'cow 'bird)) | ||
| 307 | (should-not (hierarchy-descendant-p animals 'bird 'cow)) | ||
| 308 | (should-not (hierarchy-descendant-p animals 'animal 'dove)) | ||
| 309 | (should-not (hierarchy-descendant-p animals 'animal 'bird)))) | ||
| 310 | |||
| 311 | (ert-deftest hierarchy-descendant-if-not-same () | ||
| 312 | (let ((animals (hierarchy-animals))) | ||
| 313 | (should-not (hierarchy-descendant-p animals 'cow 'cow)) | ||
| 314 | (should-not (hierarchy-descendant-p animals 'dove 'dove)) | ||
| 315 | (should-not (hierarchy-descendant-p animals 'bird 'bird)) | ||
| 316 | (should-not (hierarchy-descendant-p animals 'animal 'animal)))) | ||
| 317 | |||
| 318 | ;; keywords supported: :test :key | ||
| 319 | (ert-deftest hierarchy--set-equal () | ||
| 320 | (should (hierarchy--set-equal '(1 2 3) '(1 2 3))) | ||
| 321 | (should (hierarchy--set-equal '(1 2 3) '(3 2 1))) | ||
| 322 | (should (hierarchy--set-equal '(3 2 1) '(1 2 3))) | ||
| 323 | (should-not (hierarchy--set-equal '(2 3) '(3 2 1))) | ||
| 324 | (should-not (hierarchy--set-equal '(1 2 3) '(2 3))) | ||
| 325 | (should-not (hierarchy--set-equal '("1" "2") '("2" "1") :test #'eq)) | ||
| 326 | (should (hierarchy--set-equal '("1" "2") '("2" "1") :test #'equal)) | ||
| 327 | (should-not (hierarchy--set-equal '(1 2) '(-1 -2))) | ||
| 328 | (should (hierarchy--set-equal '(1 2) '(-1 -2) :key #'abs)) | ||
| 329 | (should-not (hierarchy--set-equal '(("1" 1) ("2" 1)) '(("1" 2) ("2" 2)))) | ||
| 330 | (should-not (hierarchy--set-equal '(("1" 1) ("2" 1)) '(("1" 2) ("2" 2)) :key #'car)) | ||
| 331 | (should-not (hierarchy--set-equal '(("1" 1) ("2" 1)) '(("1" 2) ("2" 2)) :test #'equal)) | ||
| 332 | (should (hierarchy--set-equal '(("1" 1) ("2" 1)) '(("1" 2) ("2" 2)) :key #'car :test #'equal))) | ||
| 333 | |||
| 334 | (ert-deftest hierarchy-equal-returns-true-for-same-hierarchy () | ||
| 335 | (let ((animals (hierarchy-animals))) | ||
| 336 | (should (hierarchy-equal animals animals)) | ||
| 337 | (should (hierarchy-equal (hierarchy-animals) animals)))) | ||
| 338 | |||
| 339 | (ert-deftest hierarchy-equal-returns-true-for-hierarchy-copies () | ||
| 340 | (let ((animals (hierarchy-animals))) | ||
| 341 | (should (hierarchy-equal animals (hierarchy-copy animals))))) | ||
| 342 | |||
| 343 | (ert-deftest hierarchy-map-item-on-leaf () | ||
| 344 | (let* ((animals (hierarchy-animals)) | ||
| 345 | (result (hierarchy-map-item (lambda (item indent) (cons item indent)) | ||
| 346 | 'cow | ||
| 347 | animals))) | ||
| 348 | (should (equal result '((cow . 0)))))) | ||
| 349 | |||
| 350 | (ert-deftest hierarchy-map-item-on-leaf-with-indent () | ||
| 351 | (let* ((animals (hierarchy-animals)) | ||
| 352 | (result (hierarchy-map-item (lambda (item indent) (cons item indent)) | ||
| 353 | 'cow | ||
| 354 | animals | ||
| 355 | 2))) | ||
| 356 | (should (equal result '((cow . 2)))))) | ||
| 357 | |||
| 358 | (ert-deftest hierarchy-map-item-on-parent () | ||
| 359 | (let* ((animals (hierarchy-animals)) | ||
| 360 | (result (hierarchy-map-item (lambda (item indent) (cons item indent)) | ||
| 361 | 'bird | ||
| 362 | animals))) | ||
| 363 | (should (equal result '((bird . 0) (dove . 1) (pigeon . 1)))))) | ||
| 364 | |||
| 365 | (ert-deftest hierarchy-map-item-on-grand-parent () | ||
| 366 | (let* ((animals (hierarchy-animals)) | ||
| 367 | (result (hierarchy-map-item (lambda (item indent) (cons item indent)) | ||
| 368 | 'animal | ||
| 369 | animals))) | ||
| 370 | (should (equal result '((animal . 0) (bird . 1) (dove . 2) (pigeon . 2) | ||
| 371 | (cow . 1) (dolphin . 1)))))) | ||
| 372 | |||
| 373 | (ert-deftest hierarchy-map-conses () | ||
| 374 | (let* ((animals (hierarchy-animals)) | ||
| 375 | (result (hierarchy-map (lambda (item indent) | ||
| 376 | (cons item indent)) | ||
| 377 | animals))) | ||
| 378 | (should (equal result '((animal . 0) | ||
| 379 | (bird . 1) | ||
| 380 | (dove . 2) | ||
| 381 | (pigeon . 2) | ||
| 382 | (cow . 1) | ||
| 383 | (dolphin . 1)))))) | ||
| 384 | |||
| 385 | (ert-deftest hierarchy-map-tree () | ||
| 386 | (let ((animals (hierarchy-animals))) | ||
| 387 | (should (equal (hierarchy-map-tree (lambda (item indent children) | ||
| 388 | (list item indent children)) | ||
| 389 | animals) | ||
| 390 | '(animal | ||
| 391 | 0 | ||
| 392 | ((bird 1 ((dove 2 nil) (pigeon 2 nil))) | ||
| 393 | (cow 1 nil) | ||
| 394 | (dolphin 1 nil))))))) | ||
| 395 | |||
| 396 | (ert-deftest hierarchy-map-hierarchy-keeps-hierarchy () | ||
| 397 | (let* ((animals (hierarchy-animals)) | ||
| 398 | (result (hierarchy-map-hierarchy (lambda (item _) (identity item)) | ||
| 399 | animals))) | ||
| 400 | (should (hierarchy-equal animals result)))) | ||
| 401 | |||
| 402 | (ert-deftest hierarchy-map-applies-function () | ||
| 403 | (let* ((animals (hierarchy-animals)) | ||
| 404 | (parentfn (lambda (item) | ||
| 405 | (cond | ||
| 406 | ((equal item "bird") "animal") | ||
| 407 | ((equal item "dove") "bird") | ||
| 408 | ((equal item "pigeon") "bird") | ||
| 409 | ((equal item "cow") "animal") | ||
| 410 | ((equal item "dolphin") "animal")))) | ||
| 411 | (expected (hierarchy-new))) | ||
| 412 | (hierarchy-add-tree expected "dove" parentfn) | ||
| 413 | (hierarchy-add-tree expected "pigeon" parentfn) | ||
| 414 | (hierarchy-add-tree expected "cow" parentfn) | ||
| 415 | (hierarchy-add-tree expected "dolphin" parentfn) | ||
| 416 | (should (hierarchy-equal | ||
| 417 | (hierarchy-map-hierarchy (lambda (item _) (symbol-name item)) animals) | ||
| 418 | expected)))) | ||
| 419 | |||
| 420 | (ert-deftest hierarchy-extract-tree () | ||
| 421 | (let* ((animals (hierarchy-animals)) | ||
| 422 | (birds (hierarchy-extract-tree animals 'bird))) | ||
| 423 | (hierarchy-sort birds) | ||
| 424 | (should (equal (hierarchy-roots birds) '(animal))) | ||
| 425 | (should (equal (hierarchy-children birds 'animal) '(bird))) | ||
| 426 | (should (equal (hierarchy-children birds 'bird) '(dove pigeon))))) | ||
| 427 | |||
| 428 | (ert-deftest hierarchy-extract-tree-nil-if-not-in-hierarchy () | ||
| 429 | (let* ((animals (hierarchy-animals))) | ||
| 430 | (should-not (hierarchy-extract-tree animals 'foobar)))) | ||
| 431 | |||
| 432 | (ert-deftest hierarchy-items-of-empty-hierarchy-is-empty () | ||
| 433 | (should (seq-empty-p (hierarchy-items (hierarchy-new))))) | ||
| 434 | |||
| 435 | (ert-deftest hierarchy-items-returns-sequence-of-same-length () | ||
| 436 | (let* ((animals (hierarchy-animals)) | ||
| 437 | (result (hierarchy-items animals))) | ||
| 438 | (should (= (seq-length result) (hierarchy-length animals))))) | ||
| 439 | |||
| 440 | (ert-deftest hierarchy-items-return-all-elements-of-hierarchy () | ||
| 441 | (let* ((animals (hierarchy-animals)) | ||
| 442 | (result (hierarchy-items animals))) | ||
| 443 | (should (equal (seq-sort #'string< result) '(animal bird cow dolphin dove pigeon))))) | ||
| 444 | |||
| 445 | (ert-deftest hierarchy-labelfn-indent-no-indent-if-0 () | ||
| 446 | (let* ((labelfn-base (lambda (_item _indent) (insert "foo"))) | ||
| 447 | (labelfn (hierarchy-labelfn-indent labelfn-base))) | ||
| 448 | (should (equal | ||
| 449 | (with-temp-buffer | ||
| 450 | (funcall labelfn "bar" 0) | ||
| 451 | (buffer-substring (point-min) (point-max))) | ||
| 452 | "foo")))) | ||
| 453 | |||
| 454 | (ert-deftest hierarchy-labelfn-indent-three-times-if-3 () | ||
| 455 | (let* ((labelfn-base (lambda (_item _indent) (insert "foo"))) | ||
| 456 | (labelfn (hierarchy-labelfn-indent labelfn-base))) | ||
| 457 | (should (equal | ||
| 458 | (with-temp-buffer | ||
| 459 | (funcall labelfn "bar" 3) | ||
| 460 | (buffer-substring (point-min) (point-max))) | ||
| 461 | " foo")))) | ||
| 462 | |||
| 463 | (ert-deftest hierarchy-labelfn-indent-default-indent-string () | ||
| 464 | (let* ((labelfn-base (lambda (_item _indent) (insert "foo"))) | ||
| 465 | (labelfn (hierarchy-labelfn-indent labelfn-base))) | ||
| 466 | (should (equal | ||
| 467 | (with-temp-buffer | ||
| 468 | (funcall labelfn "bar" 1) | ||
| 469 | (buffer-substring (point-min) (point-max))) | ||
| 470 | " foo")))) | ||
| 471 | |||
| 472 | (ert-deftest hierarchy-labelfn-indent-custom-indent-string () | ||
| 473 | (let* ((labelfn-base (lambda (_item _indent) (insert "foo"))) | ||
| 474 | (labelfn (hierarchy-labelfn-indent labelfn-base "###")) | ||
| 475 | (content (with-temp-buffer | ||
| 476 | (funcall labelfn "bar" 1) | ||
| 477 | (buffer-substring (point-min) (point-max))))) | ||
| 478 | (should (equal content "###foo")))) | ||
| 479 | |||
| 480 | (ert-deftest hierarchy-labelfn-button-propertize () | ||
| 481 | (let* ((labelfn-base (lambda (_item _indent) (insert "foo"))) | ||
| 482 | (actionfn #'identity) | ||
| 483 | (labelfn (hierarchy-labelfn-button labelfn-base actionfn)) | ||
| 484 | (properties (with-temp-buffer | ||
| 485 | (funcall labelfn "bar" 1) | ||
| 486 | (text-properties-at 1)))) | ||
| 487 | (should (equal (car properties) 'action)))) | ||
| 488 | |||
| 489 | (ert-deftest hierarchy-labelfn-button-execute-labelfn () | ||
| 490 | (let* ((labelfn-base (lambda (_item _indent) (insert "foo"))) | ||
| 491 | (actionfn #'identity) | ||
| 492 | (labelfn (hierarchy-labelfn-button labelfn-base actionfn)) | ||
| 493 | (content (with-temp-buffer | ||
| 494 | (funcall labelfn "bar" 1) | ||
| 495 | (buffer-substring-no-properties (point-min) (point-max))))) | ||
| 496 | (should (equal content "foo")))) | ||
| 497 | |||
| 498 | (ert-deftest hierarchy-labelfn-button-if-does-not-button-unless-condition () | ||
| 499 | (let ((labelfn-base (lambda (_item _indent) (insert "foo"))) | ||
| 500 | (spy-count 0) | ||
| 501 | (condition (lambda (_item _indent) nil))) | ||
| 502 | (cl-letf (((symbol-function 'hierarchy-labelfn-button) (lambda (_labelfn _actionfn) (lambda (_item _indent) (cl-incf spy-count))))) | ||
| 503 | (funcall (hierarchy-labelfn-button-if labelfn-base condition #'identity) nil nil) | ||
| 504 | (should (equal spy-count 0))))) | ||
| 505 | |||
| 506 | (ert-deftest hierarchy-labelfn-button-if-does-button-when-condition () | ||
| 507 | (let ((labelfn-base (lambda (_item _indent) (insert "foo"))) | ||
| 508 | (spy-count 0) | ||
| 509 | (condition (lambda (_item _indent) t))) | ||
| 510 | (cl-letf (((symbol-function 'hierarchy-labelfn-button) (lambda (_labelfn _actionfn) (lambda (_item _indent) (cl-incf spy-count))))) | ||
| 511 | (funcall (hierarchy-labelfn-button-if labelfn-base condition #'identity) nil nil) | ||
| 512 | (should (equal spy-count 1))))) | ||
| 513 | |||
| 514 | (ert-deftest hierarchy-labelfn-to-string () | ||
| 515 | (let ((labelfn (lambda (item _indent) (insert item)))) | ||
| 516 | (should (equal (hierarchy-labelfn-to-string labelfn "foo" 1) "foo")))) | ||
| 517 | |||
| 518 | (ert-deftest hierarchy-print () | ||
| 519 | (let* ((animals (hierarchy-animals)) | ||
| 520 | (result (with-temp-buffer | ||
| 521 | (hierarchy-print animals) | ||
| 522 | (buffer-substring-no-properties (point-min) (point-max))))) | ||
| 523 | (should (equal result "animal\n bird\n dove\n pigeon\n cow\n dolphin\n")))) | ||
| 524 | |||
| 525 | (ert-deftest hierarchy-to-string () | ||
| 526 | (let* ((animals (hierarchy-animals)) | ||
| 527 | (result (hierarchy-to-string animals))) | ||
| 528 | (should (equal result "animal\n bird\n dove\n pigeon\n cow\n dolphin\n")))) | ||
| 529 | |||
| 530 | (ert-deftest hierarchy-tabulated-display () | ||
| 531 | (let* ((animals (hierarchy-animals)) | ||
| 532 | (labelfn (lambda (item _indent) (insert (symbol-name item)))) | ||
| 533 | (contents (with-temp-buffer | ||
| 534 | (hierarchy-tabulated-display animals labelfn (current-buffer)) | ||
| 535 | (buffer-substring-no-properties (point-min) (point-max))))) | ||
| 536 | (should (equal contents "animal\nbird\ndove\npigeon\ncow\ndolphin\n")))) | ||
| 537 | |||
| 538 | (ert-deftest hierarchy-sort-non-root-nodes () | ||
| 539 | (let* ((animals (hierarchy-animals))) | ||
| 540 | (should (equal (hierarchy-roots animals) '(animal))) | ||
| 541 | (should (equal (hierarchy-children animals 'animal) '(bird cow dolphin))) | ||
| 542 | (should (equal (hierarchy-children animals 'bird) '(dove pigeon))))) | ||
| 543 | |||
| 544 | (ert-deftest hierarchy-sort-roots () | ||
| 545 | (let* ((organisms (hierarchy-new)) | ||
| 546 | (parentfn (lambda (item) | ||
| 547 | (cl-case item | ||
| 548 | (oak 'plant) | ||
| 549 | (bird 'animal))))) | ||
| 550 | (hierarchy-add-tree organisms 'oak parentfn) | ||
| 551 | (hierarchy-add-tree organisms 'bird parentfn) | ||
| 552 | (hierarchy-sort organisms) | ||
| 553 | (should (equal (hierarchy-roots organisms) '(animal plant))))) | ||
| 554 | |||
| 555 | (provide 'hierarchy-tests) | ||
| 556 | ;;; hierarchy-tests.el ends here | ||