1 ;;; eieio-testsinvoke.el -- eieio tests for method invocation
3 ;; Copyright (C) 2005, 2008, 2010, 2013-2014 Free Software Foundation,
6 ;; Author: Eric. M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
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.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Test method invocation order. From the common lisp reference
29 ;; - All the :before methods are called, in most-specific-first
30 ;; order. Their values are ignored. An error is signaled if
31 ;; call-next-method is used in a :before method.
33 ;; - The most specific primary method is called. Inside the body of a
34 ;; primary method, call-next-method may be used to call the next
35 ;; most specific primary method. When that method returns, the
36 ;; previous primary method can execute more code, perhaps based on
37 ;; the returned value or values. The generic function no-next-method
38 ;; is invoked if call-next-method is used and there are no more
39 ;; applicable primary methods. The function next-method-p may be
40 ;; used to determine whether a next method exists. If
41 ;; call-next-method is not used, only the most specific primary
44 ;; - All the :after methods are called, in most-specific-last order.
45 ;; Their values are ignored. An error is signaled if
46 ;; call-next-method is used in a :after method.
49 ;; Also test behavior of `call-next-method'. From clos.org:
52 ;; When call-next-method is called with no arguments, it passes the
53 ;; current method's original arguments to the next method.
58 (defvar eieio-test-method-order-list nil
59 "List of symbols stored during method invocation.")
61 (defun eieio-test-method-store ()
62 "Store current invocation class symbol in the invocation order list."
63 (let* ((keysym (aref [ :STATIC
:BEFORE
:PRIMARY
:AFTER
]
64 (or eieio-generic-call-key
0)))
65 (c (list eieio-generic-call-methodname keysym
(eieio--scoped-class))))
66 (setq eieio-test-method-order-list
67 (cons c eieio-test-method-order-list
))))
69 (defun eieio-test-match (rightanswer)
71 (if (equal rightanswer eieio-test-method-order-list
)
73 (error "eieio-test-methodinvoke.el: Test Failed!")))
75 (defvar eieio-test-call-next-method-arguments nil
76 "List of passed to methods during execution of `call-next-method'.")
78 (defun eieio-test-arguments-for (class)
79 "Returns arguments passed to method of CLASS during `call-next-method'."
80 (cdr (assoc class eieio-test-call-next-method-arguments
)))
82 (defclass eitest-A
() ())
83 (defclass eitest-AA
(eitest-A) ())
84 (defclass eitest-AAA
(eitest-AA) ())
85 (defclass eitest-B-base1
() ())
86 (defclass eitest-B-base2
() ())
87 (defclass eitest-B
(eitest-B-base1 eitest-B-base2
) ())
89 (defmethod eitest-F :BEFORE
((p eitest-B-base1
))
90 (eieio-test-method-store))
92 (defmethod eitest-F :BEFORE
((p eitest-B-base2
))
93 (eieio-test-method-store))
95 (defmethod eitest-F :BEFORE
((p eitest-B
))
96 (eieio-test-method-store))
98 (defmethod eitest-F ((p eitest-B
))
99 (eieio-test-method-store)
102 (defmethod eitest-F ((p eitest-B-base1
))
103 (eieio-test-method-store)
106 (defmethod eitest-F ((p eitest-B-base2
))
107 (eieio-test-method-store)
108 (when (next-method-p)
112 (defmethod eitest-F :AFTER
((p eitest-B-base1
))
113 (eieio-test-method-store))
115 (defmethod eitest-F :AFTER
((p eitest-B-base2
))
116 (eieio-test-method-store))
118 (defmethod eitest-F :AFTER
((p eitest-B
))
119 (eieio-test-method-store))
121 (ert-deftest eieio-test-method-order-list-3
()
122 (let ((eieio-test-method-order-list nil
)
124 (eitest-F :BEFORE eitest-B
)
125 (eitest-F :BEFORE eitest-B-base1
)
126 (eitest-F :BEFORE eitest-B-base2
)
128 (eitest-F :PRIMARY eitest-B
)
129 (eitest-F :PRIMARY eitest-B-base1
)
130 (eitest-F :PRIMARY eitest-B-base2
)
132 (eitest-F :AFTER eitest-B-base2
)
133 (eitest-F :AFTER eitest-B-base1
)
134 (eitest-F :AFTER eitest-B
)
136 (eitest-F (eitest-B nil
))
137 (setq eieio-test-method-order-list
(nreverse eieio-test-method-order-list
))
138 (eieio-test-match ans
)))
140 ;;; Test static invocation
142 (defmethod eitest-H :STATIC
((class eitest-A
))
143 "No need to do work in here."
146 (ert-deftest eieio-test-method-order-list-4
()
147 ;; Both of these situations should succeed.
148 (should (eitest-H eitest-A
))
149 (should (eitest-H (eitest-A nil
))))
151 ;;; Return value from :PRIMARY
153 (defmethod eitest-I :BEFORE
((a eitest-A
))
154 (eieio-test-method-store)
157 (defmethod eitest-I :PRIMARY
((a eitest-A
))
158 (eieio-test-method-store)
161 (defmethod eitest-I :AFTER
((a eitest-A
))
162 (eieio-test-method-store)
165 (ert-deftest eieio-test-method-order-list-5
()
166 (let ((eieio-test-method-order-list nil
)
167 (ans (eitest-I (eitest-A nil
))))
168 (should (string= ans
":primary"))))
170 ;;; Multiple inheritance and the 'constructor' method.
172 ;; Constructor is a static method, so this is really testing
173 ;; static method invocation and multiple inheritance.
175 (defclass C-base1
() ())
176 (defclass C-base2
() ())
177 (defclass C
(C-base1 C-base2
) ())
179 (defmethod constructor :STATIC
((p C-base1
) &rest args
)
180 (eieio-test-method-store)
181 (if (next-method-p) (call-next-method))
184 (defmethod constructor :STATIC
((p C-base2
) &rest args
)
185 (eieio-test-method-store)
186 (if (next-method-p) (call-next-method))
189 (defmethod constructor :STATIC
((p C
) &rest args
)
190 (eieio-test-method-store)
194 (ert-deftest eieio-test-method-order-list-6
()
195 (let ((eieio-test-method-order-list nil
)
197 (constructor :STATIC C
)
198 (constructor :STATIC C-base1
)
199 (constructor :STATIC C-base2
)
202 (setq eieio-test-method-order-list
(nreverse eieio-test-method-order-list
))
203 (eieio-test-match ans
)))
207 ;; For a diamond shaped inheritance structure, (call-next-method) can break.
208 ;; As such, there are two possible orders.
210 (defclass D-base0
() () :method-invocation-order
:depth-first
)
211 (defclass D-base1
(D-base0) () :method-invocation-order
:depth-first
)
212 (defclass D-base2
(D-base0) () :method-invocation-order
:depth-first
)
213 (defclass D
(D-base1 D-base2
) () :method-invocation-order
:depth-first
)
215 (defmethod eitest-F ((p D
))
217 (eieio-test-method-store)
220 (defmethod eitest-F ((p D-base0
))
222 (eieio-test-method-store)
223 ;; This should have no next
224 ;; (when (next-method-p) (call-next-method))
227 (defmethod eitest-F ((p D-base1
))
229 (eieio-test-method-store)
232 (defmethod eitest-F ((p D-base2
))
234 (eieio-test-method-store)
235 (when (next-method-p)
239 (ert-deftest eieio-test-method-order-list-7
()
240 (let ((eieio-test-method-order-list nil
)
242 (eitest-F :PRIMARY D
)
243 (eitest-F :PRIMARY D-base1
)
244 ;; (eitest-F :PRIMARY D-base2)
245 (eitest-F :PRIMARY D-base0
)
248 (setq eieio-test-method-order-list
(nreverse eieio-test-method-order-list
))
249 (eieio-test-match ans
)))
251 ;;; Other invocation order
253 (defclass E-base0
() () :method-invocation-order
:breadth-first
)
254 (defclass E-base1
(E-base0) () :method-invocation-order
:breadth-first
)
255 (defclass E-base2
(E-base0) () :method-invocation-order
:breadth-first
)
256 (defclass E
(E-base1 E-base2
) () :method-invocation-order
:breadth-first
)
258 (defmethod eitest-F ((p E
))
259 (eieio-test-method-store)
262 (defmethod eitest-F ((p E-base0
))
263 (eieio-test-method-store)
264 ;; This should have no next
265 ;; (when (next-method-p) (call-next-method))
268 (defmethod eitest-F ((p E-base1
))
269 (eieio-test-method-store)
272 (defmethod eitest-F ((p E-base2
))
273 (eieio-test-method-store)
274 (when (next-method-p)
278 (ert-deftest eieio-test-method-order-list-8
()
279 (let ((eieio-test-method-order-list nil
)
281 (eitest-F :PRIMARY E
)
282 (eitest-F :PRIMARY E-base1
)
283 (eitest-F :PRIMARY E-base2
)
284 (eitest-F :PRIMARY E-base0
)
287 (setq eieio-test-method-order-list
(nreverse eieio-test-method-order-list
))
288 (eieio-test-match ans
)))
290 ;;; Jan's methodinvoke order w/ multiple inheritance and :after methods.
292 (defclass eitest-Ja
()
295 (defmethod initialize-instance :after
((this eitest-Ja
) &rest slots
)
297 (when (next-method-p)
302 (defclass eitest-Jb
()
305 (defmethod initialize-instance :after
((this eitest-Jb
) &rest slots
)
307 (when (next-method-p)
312 (defclass eitest-Jc
(eitest-Jb)
315 (defclass eitest-Jd
(eitest-Jc eitest-Ja
)
318 (defmethod initialize-instance ((this eitest-Jd
) &rest slots
)
320 (when (next-method-p)
325 (ert-deftest eieio-test-method-order-list-9
()
326 (should (eitest-Jd "test")))
328 ;;; call-next-method with replacement arguments across a simple class hierarchy.
334 (defclass CNM-1-1
(CNM-0)
337 (defclass CNM-1-2
(CNM-0)
340 (defclass CNM-2
(CNM-1-1 CNM-1-2
)
343 (defmethod CNM-M ((this CNM-0
) args
)
344 (push (cons 'CNM-0
(copy-sequence args
))
345 eieio-test-call-next-method-arguments
)
346 (when (next-method-p)
348 this
(cons 'CNM-0 args
))))
350 (defmethod CNM-M ((this CNM-1-1
) args
)
351 (push (cons 'CNM-1-1
(copy-sequence args
))
352 eieio-test-call-next-method-arguments
)
353 (when (next-method-p)
355 this
(cons 'CNM-1-1 args
))))
357 (defmethod CNM-M ((this CNM-1-2
) args
)
358 (push (cons 'CNM-1-2
(copy-sequence args
))
359 eieio-test-call-next-method-arguments
)
360 (when (next-method-p)
363 (defmethod CNM-M ((this CNM-2
) args
)
364 (push (cons 'CNM-2
(copy-sequence args
))
365 eieio-test-call-next-method-arguments
)
366 (when (next-method-p)
368 this
(cons 'CNM-2 args
))))
370 (ert-deftest eieio-test-method-order-list-10
()
371 (let ((eieio-test-call-next-method-arguments nil
))
372 (CNM-M (CNM-2 "") '(INIT))
373 (should (equal (eieio-test-arguments-for 'CNM-0
)
374 '(CNM-1-1 CNM-2 INIT
)))
375 (should (equal (eieio-test-arguments-for 'CNM-1-1
)
377 (should (equal (eieio-test-arguments-for 'CNM-1-2
)
378 '(CNM-1-1 CNM-2 INIT
)))
379 (should (equal (eieio-test-arguments-for 'CNM-2
)