summaryrefslogtreecommitdiff
path: root/test/lisp/emacs-lisp/cl-macs-tests.el
diff options
context:
space:
mode:
authorEarl Hyatt <okamsn@protonmail.com>2025-03-12 23:01:49 -0400
committerStefan Monnier <monnier@iro.umontreal.ca>2025-03-31 14:29:48 -0400
commite04d1dafc700813c835ae4e45af4e104c49e8875 (patch)
tree405296248ee9da13e065213d8f88cfe317a7bc81 /test/lisp/emacs-lisp/cl-macs-tests.el
parenta97a61b630624f5a6ec917db92e2985c56b20aa0 (diff)
downloademacs-e04d1dafc700813c835ae4e45af4e104c49e8875.tar.gz
emacs-e04d1dafc700813c835ae4e45af4e104c49e8875.tar.bz2
emacs-e04d1dafc700813c835ae4e45af4e104c49e8875.zip
Add cl-with-accessors
* lisp/emacs-lisp/cl-macs.el (cl-with-accessors): New macro. * doc/misc/cl.texi (Structures): Mention the new macro. * test/lisp/emacs-lisp/cl-macs-tests.el (cl-lib-struct-with-accessors): New Test. * etc/NEWS (New macro 'cl-with-accessors'.): Mention the macro. This macro is useful when making repeated use of a structures accessor functions, such as reading from a slot and then writing to a slot. It is similar to 'with-slots' from EIEIO, but uses accessor functions instead of slot names.
Diffstat (limited to 'test/lisp/emacs-lisp/cl-macs-tests.el')
-rw-r--r--test/lisp/emacs-lisp/cl-macs-tests.el15
1 files changed, 15 insertions, 0 deletions
diff --git a/test/lisp/emacs-lisp/cl-macs-tests.el b/test/lisp/emacs-lisp/cl-macs-tests.el
index b4b939d3d31..ed6b1c2e4d4 100644
--- a/test/lisp/emacs-lisp/cl-macs-tests.el
+++ b/test/lisp/emacs-lisp/cl-macs-tests.el
@@ -541,6 +541,21 @@ collection clause."
(should (mystruct-p (cl-lib--con-1)))
(should (mystruct-p (cl-lib--con-2))))
+(ert-deftest cl-lib-struct-with-accessors ()
+ (let ((x (make-mystruct :abc 1 :def 2)))
+ (cl-with-accessors ((abc mystruct-abc)
+ (def mystruct-def))
+ x
+ (should (= abc 1))
+ (should-error (setf abc 99))
+ (should (= def 2))
+ (setf def 3)
+ (should (= def 3))
+ (setq def 4)
+ (should (= def 4)))
+ (should (= 4 (mystruct-def x)))
+ (should (= 1 (mystruct-abc x)))))
+
(ert-deftest cl-lib-arglist-performance ()
;; An `&aux' should not cause lambda's arglist to be turned into an &rest
;; that's parsed by hand.