Skip to content

Commit

Permalink
class-star: Add original-class helper function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambrevar committed Aug 25, 2020
1 parent 19f518b commit 184b0de
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions libraries/class-star/class-star.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ The class is restored when exiting BODY."
#-SBCL
(deftype ,class-sym () ',class-sym)))))

(defun original-class (class-sym)
"Return the parent class with the same name, or nil if there is none.
This is useful to retrieve the original class of a class that was overridden,
e.g. with (define-class foo (foo) ...)."
(find class-sym (mopu:superclasses (find-class class-sym))
:key #'class-name))

(defun name-identity (name definition)
(declare (ignore definition))
name)
Expand Down
1 change: 1 addition & 0 deletions libraries/class-star/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
(:import-from :hu.dwim.defclass-star)
(:export #:with-class
#:replace-class
#:original-class
#:define-class
#:name-identity))
8 changes: 8 additions & 0 deletions libraries/class-star/tests/tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
(fboundp 'name-no-acc-of)))
nil))

(prove:subtest "Original class"
(defclass foo () ())
(defclass bar (foo) ())
(setf (find-class 'foo) (find-class 'bar))
(prove:isnt (class*:original-class 'foo) nil)
(prove:is (class-name (class*:original-class 'foo)) 'foo)
(prove:isnt (class*:original-class 'foo) (find-class 'foo)))

;; TODO: These cycle tests work if run at the top-level, but not within prove:subtest.

;; (prove:subtest "Cycle"
Expand Down

0 comments on commit 184b0de

Please sign in to comment.