aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorStefan Monnier2025-05-05 23:18:56 -0400
committerStefan Monnier2025-05-05 23:18:56 -0400
commitfc4d8ce9514dd45ab34dbef6f023347b42ee9fef (patch)
tree77ebb9ff9b1bdf8b13a93980b54a4a3446f60c54 /test
parent68a50324a70bd794d7f3228290310093f1515f7b (diff)
downloademacs-fc4d8ce9514dd45ab34dbef6f023347b42ee9fef.tar.gz
emacs-fc4d8ce9514dd45ab34dbef6f023347b42ee9fef.zip
cl-types: Integrate into CL-Lib
* lisp/emacs-lisp/cl-extra.el (cl--type-unique, cl-types-of) (cl--type-dispatch-list, cl--type-generalizer): Move to `cl-extra.el`. (cl--type-generalizers): New function extracted from "cl-types-of" method of `cl-generic-generalizers`. * lisp/emacs-lisp/cl-lib.el (cl-generic-generalizers): New method to dispatch on derived types. Use `cl--type-generalizers`. * lisp/emacs-lisp/cl-macs.el (cl-deftype): Move from `cl-types.el` and rename from `cl-deftype2`. (extended-char): Tweak definition to fix bootstrapping issues. * lisp/emacs-lisp/cl-preloaded.el (cl--type-list, cl-type-class) (cl--type-deftype): Move from `cl-types.el`. * lisp/emacs-lisp/oclosure.el (oclosure): Don't abuse `cl-deftype` to register the predicate function. * test/lisp/emacs-lisp/cl-extra-tests.el: Move tests from `cl-type-tests.el`.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/emacs-lisp/cl-extra-tests.el92
-rw-r--r--test/lisp/emacs-lisp/cl-types-tests.el96
2 files changed, 92 insertions, 96 deletions
diff --git a/test/lisp/emacs-lisp/cl-extra-tests.el b/test/lisp/emacs-lisp/cl-extra-tests.el
index 20d1e532a6f..1f94d71e567 100644
--- a/test/lisp/emacs-lisp/cl-extra-tests.el
+++ b/test/lisp/emacs-lisp/cl-extra-tests.el
@@ -348,4 +348,96 @@
348 (should (cl-tailp l l)) 348 (should (cl-tailp l l))
349 (should (not (cl-tailp '(4 5) l))))) 349 (should (not (cl-tailp '(4 5) l)))))
350 350
351;;;; Method dispatch for derived types.
352
353(cl-deftype multiples-of (&optional m)
354 (let ((multiplep (if (eq m '*)
355 #'ignore
356 (lambda (n) (= 0 (% n m))))))
357 `(and integer (satisfies ,multiplep))))
358
359(cl-deftype multiples-of-2 ()
360 '(multiples-of 2))
361
362(cl-deftype multiples-of-3 ()
363 '(multiples-of 3))
364
365(cl-deftype multiples-of-4 ()
366 (declare (parents multiples-of-2))
367 '(and multiples-of-2 (multiples-of 4)))
368
369(cl-deftype unsigned-byte (&optional bits)
370 "Unsigned integer."
371 `(integer 0 ,(if (eq bits '*) bits (1- (ash 1 bits)))))
372
373(cl-deftype unsigned-16bits ()
374 "Unsigned 16-bits integer."
375 (declare (parents unsigned-byte))
376 '(unsigned-byte 16))
377
378(cl-deftype unsigned-8bits ()
379 "Unsigned 8-bits integer."
380 (declare (parents unsigned-16bits))
381 '(unsigned-byte 8))
382
383(cl-defmethod my-foo ((_n unsigned-byte))
384 (format "unsigned"))
385
386(cl-defmethod my-foo ((_n unsigned-16bits))
387 (format "unsigned 16bits - also %s"
388 (cl-call-next-method)))
389
390(cl-defmethod my-foo ((_n unsigned-8bits))
391 (format "unsigned 8bits - also %s"
392 (cl-call-next-method)))
393
394(ert-deftest cl-types-test ()
395 "Test types definition, cl-types-of and method dispatching."
396
397 ;; Invalid DAG error
398 ;; FIXME: We don't test that any more.
399 ;; (should-error
400 ;; (eval
401 ;; '(cl-deftype unsigned-16bits ()
402 ;; "Unsigned 16-bits integer."
403 ;; (declare (parents unsigned-8bits))
404 ;; '(unsigned-byte 16))
405 ;; lexical-binding
406 ;; ))
407
408 ;; Test that (cl-types-of 4) is (multiples-of-4 multiples-of-2 ...)
409 ;; Test that (cl-types-of 6) is (multiples-of-3 multiples-of-2 ...)
410 ;; Test that (cl-types-of 12) is (multiples-of-4 multiples-of-3 multiples-of-2 ...)
411 (let ((types '(multiples-of-2 multiples-of-3 multiples-of-4)))
412 (should (equal '(multiples-of-2)
413 (seq-intersection (cl-types-of 2) types)))
414
415 (should (equal '(multiples-of-4 multiples-of-2)
416 (seq-intersection (cl-types-of 4) types)))
417
418 (should (equal '(multiples-of-3 multiples-of-2)
419 (seq-intersection (cl-types-of 6) types)))
420
421 (should (member (seq-intersection (cl-types-of 12) types)
422 ;; Order between 3 and 4/2 is undefined.
423 '((multiples-of-3 multiples-of-4 multiples-of-2)
424 (multiples-of-4 multiples-of-2 multiples-of-3))))
425
426 (should (equal '()
427 (seq-intersection (cl-types-of 5) types)))
428 )
429
430 ;;; Method dispatching.
431 (should (equal "unsigned 8bits - also unsigned 16bits - also unsigned"
432 (my-foo 100)))
433
434 (should (equal "unsigned 16bits - also unsigned"
435 (my-foo 256)))
436
437 (should (equal "unsigned"
438 (my-foo most-positive-fixnum)))
439 )
440
441
442
351;;; cl-extra-tests.el ends here 443;;; cl-extra-tests.el ends here
diff --git a/test/lisp/emacs-lisp/cl-types-tests.el b/test/lisp/emacs-lisp/cl-types-tests.el
deleted file mode 100644
index 746270578e7..00000000000
--- a/test/lisp/emacs-lisp/cl-types-tests.el
+++ /dev/null
@@ -1,96 +0,0 @@
1;;; Test `cl-typedef' -*- lexical-binding: t; -*-
2;;
3(require 'ert)
4(require 'cl-types)
5
6(cl-deftype2 multiples-of (&optional m)
7 (let ((multiplep (if (eq m '*)
8 #'ignore
9 (lambda (n) (= 0 (% n m))))))
10 `(and integer (satisfies ,multiplep))))
11
12(cl-deftype2 multiples-of-2 ()
13 '(multiples-of 2))
14
15(cl-deftype2 multiples-of-3 ()
16 '(multiples-of 3))
17
18(cl-deftype2 multiples-of-4 ()
19 (declare (parents multiples-of-2))
20 '(and multiples-of-2 (multiples-of 4)))
21
22(cl-deftype2 unsigned-byte (&optional bits)
23 "Unsigned integer."
24 `(integer 0 ,(if (eq bits '*) bits (1- (ash 1 bits)))))
25
26(cl-deftype2 unsigned-16bits ()
27 "Unsigned 16-bits integer."
28 (declare (parents unsigned-byte))
29 '(unsigned-byte 16))
30
31(cl-deftype2 unsigned-8bits ()
32 "Unsigned 8-bits integer."
33 (declare (parents unsigned-16bits))
34 '(unsigned-byte 8))
35
36(cl-defmethod my-foo ((_n unsigned-byte))
37 (format "unsigned"))
38
39(cl-defmethod my-foo ((_n unsigned-16bits))
40 (format "unsigned 16bits - also %s"
41 (cl-call-next-method)))
42
43(cl-defmethod my-foo ((_n unsigned-8bits))
44 (format "unsigned 8bits - also %s"
45 (cl-call-next-method)))
46
47(ert-deftest cl-types-test ()
48 "Test types definition, cl-types-of and method dispatching."
49
50 ;; Invalid DAG error
51 ;; FIXME: We don't test that any more.
52 ;; (should-error
53 ;; (eval
54 ;; '(cl-deftype2 unsigned-16bits ()
55 ;; "Unsigned 16-bits integer."
56 ;; (declare (parents unsigned-8bits))
57 ;; '(unsigned-byte 16))
58 ;; lexical-binding
59 ;; ))
60
61 ;; Test that (cl-types-of 4) is (multiples-of-4 multiples-of-2 ...)
62 ;; Test that (cl-types-of 6) is (multiples-of-3 multiples-of-2 ...)
63 ;; Test that (cl-types-of 12) is (multiples-of-4 multiples-of-3 multiples-of-2 ...)
64 (let ((types '(multiples-of-2 multiples-of-3 multiples-of-4)))
65 (should (equal '(multiples-of-2)
66 (seq-intersection (cl-types-of 2) types)))
67
68 (should (equal '(multiples-of-4 multiples-of-2)
69 (seq-intersection (cl-types-of 4) types)))
70
71 (should (equal '(multiples-of-3 multiples-of-2)
72 (seq-intersection (cl-types-of 6) types)))
73
74 (should (member (seq-intersection (cl-types-of 12) types)
75 ;; Order between 3 and 4/2 is undefined.
76 '((multiples-of-3 multiples-of-4 multiples-of-2)
77 (multiples-of-4 multiples-of-2 multiples-of-3))))
78
79 (should (equal '()
80 (seq-intersection (cl-types-of 5) types)))
81 )
82
83 ;;; Method dispatching.
84 (should (equal "unsigned 8bits - also unsigned 16bits - also unsigned"
85 (my-foo 100)))
86
87 (should (equal "unsigned 16bits - also unsigned"
88 (my-foo 256)))
89
90 (should (equal "unsigned"
91 (my-foo most-positive-fixnum)))
92 )
93
94(provide 'cl-types-tests)
95
96;;; cl-types-tests.el ends here