aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre Téchoueyres2017-12-15 21:42:21 +0100
committerEric Abrahamsen2018-03-22 10:46:07 +0800
commit4ec935dc5bc5d6e6ad5c9eb8027412b333b4b9ea (patch)
tree8a65e51f17c37ce9375cd37a4f13e2b1c55afd3f
parent47917d8f4dbb711435fe46765fd0290e1f4a16b3 (diff)
downloademacs-4ec935dc5bc5d6e6ad5c9eb8027412b333b4b9ea.tar.gz
emacs-4ec935dc5bc5d6e6ad5c9eb8027412b333b4b9ea.zip
Add new tests for eieio persistence
* test/lisp/emacs-lisp/eieio-tests/eieio-test-persist.el: (hash-equal): New comparison test for hash-tables. (persist-test-save-and-compare): Use test for hash-tables. (eieio-test-persist-hash-and-vector, eieio-test-persist-interior-lists): New tests.
-rw-r--r--test/lisp/emacs-lisp/eieio-tests/eieio-test-persist.el113
1 files changed, 103 insertions, 10 deletions
diff --git a/test/lisp/emacs-lisp/eieio-tests/eieio-test-persist.el b/test/lisp/emacs-lisp/eieio-tests/eieio-test-persist.el
index a3ab3834899..ff4aaf7aeb8 100644
--- a/test/lisp/emacs-lisp/eieio-tests/eieio-test-persist.el
+++ b/test/lisp/emacs-lisp/eieio-tests/eieio-test-persist.el
@@ -1,4 +1,4 @@
1;;; eieio-persist.el --- Tests for eieio-persistent class 1;;; eieio-test-persist.el --- Tests for eieio-persistent class
2 2
3;; Copyright (C) 2011-2018 Free Software Foundation, Inc. 3;; Copyright (C) 2011-2018 Free Software Foundation, Inc.
4 4
@@ -40,6 +40,17 @@ This is usually a symbol that starts with `:'."
40 (car tuple) 40 (car tuple)
41 nil))) 41 nil)))
42 42
43(defun hash-equal (hash1 hash2)
44 "Compare two hash tables to see whether they are equal."
45 (and (= (hash-table-count hash1)
46 (hash-table-count hash2))
47 (catch 'flag
48 (maphash (lambda (x y)
49 (or (equal (gethash x hash2) y)
50 (throw 'flag nil)))
51 hash1)
52 (throw 'flag t))))
53
43(defun persist-test-save-and-compare (original) 54(defun persist-test-save-and-compare (original)
44 "Compare the object ORIGINAL against the one read fromdisk." 55 "Compare the object ORIGINAL against the one read fromdisk."
45 56
@@ -49,8 +60,8 @@ This is usually a symbol that starts with `:'."
49 (class (eieio-object-class original)) 60 (class (eieio-object-class original))
50 (fromdisk (eieio-persistent-read file class)) 61 (fromdisk (eieio-persistent-read file class))
51 (cv (cl--find-class class)) 62 (cv (cl--find-class class))
52 (slots (eieio--class-slots cv)) 63 (slots (eieio--class-slots cv)))
53 ) 64
54 (unless (object-of-class-p fromdisk class) 65 (unless (object-of-class-p fromdisk class)
55 (error "Persistent class %S != original class %S" 66 (error "Persistent class %S != original class %S"
56 (eieio-object-class fromdisk) 67 (eieio-object-class fromdisk)
@@ -62,18 +73,24 @@ This is usually a symbol that starts with `:'."
62 (origvalue (eieio-oref original oneslot)) 73 (origvalue (eieio-oref original oneslot))
63 (fromdiskvalue (eieio-oref fromdisk oneslot)) 74 (fromdiskvalue (eieio-oref fromdisk oneslot))
64 (initarg-p (eieio--attribute-to-initarg 75 (initarg-p (eieio--attribute-to-initarg
65 (cl--find-class class) oneslot)) 76 (cl--find-class class) oneslot)))
66 )
67 77
68 (if initarg-p 78 (if initarg-p
69 (unless (equal origvalue fromdiskvalue) 79 (unless
80 (cond ((and (hash-table-p origvalue) (hash-table-p fromdiskvalue))
81 (hash-equal origvalue fromdiskvalue))
82 (t (equal origvalue fromdiskvalue)))
70 (error "Slot %S Original Val %S != Persistent Val %S" 83 (error "Slot %S Original Val %S != Persistent Val %S"
71 oneslot origvalue fromdiskvalue)) 84 oneslot origvalue fromdiskvalue))
72 ;; Else !initarg-p 85 ;; Else !initarg-p
73 (unless (equal (cl--slot-descriptor-initform slot) fromdiskvalue) 86 (let ((origval (cl--slot-descriptor-initform slot))
87 (diskval fromdiskvalue))
88 (unless
89 (cond ((and (hash-table-p origval) (hash-table-p diskval))
90 (hash-equal origval diskval))
91 (t (equal origval diskval)))
74 (error "Slot %S Persistent Val %S != Default Value %S" 92 (error "Slot %S Persistent Val %S != Default Value %S"
75 oneslot fromdiskvalue (cl--slot-descriptor-initform slot)))) 93 oneslot diskval origvalue))))))))
76 ))))
77 94
78;;; Simple Case 95;;; Simple Case
79;; 96;;
@@ -205,13 +222,16 @@ persistent class.")
205 ((slot1 :initarg :slot1 222 ((slot1 :initarg :slot1
206 :type (or persistent-random-class null persist-not-persistent)) 223 :type (or persistent-random-class null persist-not-persistent))
207 (slot2 :initarg :slot2 224 (slot2 :initarg :slot2
208 :type (or persist-not-persistent persist-random-class null)))) 225 :type (or persist-not-persistent persistent-random-class null))
226 (slot3 :initarg :slot3
227 :type persistent-random-class)))
209 228
210(ert-deftest eieio-test-multiple-class-slot () 229(ert-deftest eieio-test-multiple-class-slot ()
211 (let ((persist 230 (let ((persist
212 (persistent-multiclass-slot "random string" 231 (persistent-multiclass-slot "random string"
213 :slot1 (persistent-random-class) 232 :slot1 (persistent-random-class)
214 :slot2 (persist-not-persistent) 233 :slot2 (persist-not-persistent)
234 :slot3 (persistent-random-class)
215 :file (concat default-directory "test-ps5.pt")))) 235 :file (concat default-directory "test-ps5.pt"))))
216 (unwind-protect 236 (unwind-protect
217 (persist-test-save-and-compare persist) 237 (persist-test-save-and-compare persist)
@@ -238,4 +258,77 @@ persistent class.")
238 (persist-test-save-and-compare persist-wols) 258 (persist-test-save-and-compare persist-wols)
239 (delete-file (oref persist-wols file)))) 259 (delete-file (oref persist-wols file))))
240 260
261;;; Tests targeted at popular libraries in the wild.
262
263;; Objects inside hash tables and vectors (pcache), see bug#29220.
264(defclass person ()
265 ((name :type string :initarg :name)))
266
267(defclass classy (eieio-persistent)
268 ((teacher
269 :type person
270 :initarg :teacher)
271 (students
272 :initarg :students :initform (make-hash-table :test 'equal))
273 (janitors
274 :type list
275 :initarg :janitors)
276 (random-vector
277 :type vector
278 :initarg :random-vector)))
279
280(ert-deftest eieio-test-persist-hash-and-vector ()
281 (let* ((jane (make-instance 'person :name "Jane"))
282 (bob (make-instance 'person :name "Bob"))
283 (hans (make-instance 'person :name "Hans"))
284 (dierdre (make-instance 'person :name "Dierdre"))
285 (class (make-instance 'classy
286 :teacher jane
287 :janitors (list [tuesday nil]
288 [friday nil])
289 :random-vector [nil]
290 :file (concat default-directory "classy-" emacs-version ".eieio"))))
291 (puthash "Bob" bob (slot-value class 'students))
292 (aset (slot-value class 'random-vector) 0
293 (make-instance 'persistent-random-class))
294 (aset (car (slot-value class 'janitor)) 1 hans)
295 (aset (nth 1 (slot-value class 'janitor)) 1 dierdre)
296 (unwind-protect
297 (persist-test-save-and-compare class)
298 (delete-file (oref class file)))))
299
300;; Extra quotation of lists inside other objects (Gnus registry), also
301;; bug#29220.
302
303(defclass eieio-container (eieio-persistent)
304 ((alist
305 :initarg :alist
306 :type list)
307 (vec
308 :initarg :vec
309 :type vector)
310 (htab
311 :initarg :htab
312 :type hash-table)))
313
314(ert-deftest eieio-test-persist-interior-lists ()
315 (let* ((thing (make-instance
316 'eieio-container
317 :vec [nil]
318 :htab (make-hash-table :test #'equal)
319 :file (concat default-directory
320 "container-" emacs-version ".eieio")))
321 (john (make-instance 'person :name "John"))
322 (alexie (make-instance 'person :name "Alexie"))
323 (alst '(("first" (one two three))
324 ("second" (four five six)))))
325 (setf (nth 2 (cadar alst)) john
326 (nth 2 (cadadr alst)) alexie)
327 (setf (slot-value thing 'alist) alst)
328 (puthash "alst" alst (slot-value thing 'htab))
329 (aset (slot-value thing 'vec) 0 alst)
330 (unwind-protect
331 (persist-test-save-and-compare thing)
332 (delete-file (slot-value thing 'file)))))
333
241;;; eieio-test-persist.el ends here 334;;; eieio-test-persist.el ends here