1 ;;; gnustest-registry.el --- Registry and Gnus registry testing for Gnus
2 ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
4 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
6 ;; This file is not part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
28 (when (null (ignore-errors (require 'ert)))
29 (defmacro* ert-deftest (name () &body docstring-keys-and-body))))
35 (require 'gnus-registry)
37 (ert-deftest gnustest-registry-instantiation-test ()
38 (should (registry-db "Testing")))
40 (ert-deftest gnustest-registry-match-test ()
41 (let ((entry '((hello "goodbye" "bye") (blank))))
43 (message "Testing :regex matching")
44 (should (registry--match :regex entry '((hello "nye" "bye"))))
45 (should (registry--match :regex entry '((hello "good"))))
46 (should-not (registry--match :regex entry '((hello "nye"))))
47 (should-not (registry--match :regex entry '((hello))))
49 (message "Testing :member matching")
50 (should (registry--match :member entry '((hello "bye"))))
51 (should (registry--match :member entry '((hello "goodbye"))))
52 (should-not (registry--match :member entry '((hello "good"))))
53 (should-not (registry--match :member entry '((hello "nye"))))
54 (should-not (registry--match :member entry '((hello)))))
55 (message "Done with matching testing."))
57 (defun gnustest-registry-make-testable-db (n &optional name file)
58 (let* ((db (registry-db
60 :file (or file "unused")
62 :max-soft 0 ; keep nothing not precious
63 :precious '(extra more-extra)
64 :tracked '(sender subject groups))))
66 (registry-insert db i `((sender "me")
68 (more-extra) ; empty data key should be pruned
69 ;; first 5 entries will NOT have this extra data
70 ,@(when (< 5 i) (list (list 'extra "more data")))
71 (groups ,(number-to-string i)))))
74 (ert-deftest gnustest-registry-usage-test ()
76 (db (gnustest-registry-make-testable-db n)))
78 (should (= n (registry-size db)))
79 (message "max-hard test")
80 (should-error (registry-insert db "new" '()))
81 (message "Individual lookup")
82 (should (= 58 (caadr (registry-lookup db '(1 58 99)))))
83 (message "Grouped individual lookup")
84 (should (= 3 (length (registry-lookup db '(1 58 99)))))
85 (when (boundp 'lexical-binding)
86 (message "Individual lookup (breaks before lexbind)")
88 (caadr (registry-lookup-breaks-before-lexbind db '(1 58 99)))))
89 (message "Grouped individual lookup (breaks before lexbind)")
91 (length (registry-lookup-breaks-before-lexbind db
94 (should (= n (length (registry-search db :all t))))
95 (should (= n (length (registry-search db :member '((sender "me"))))))
96 (message "Secondary index search")
97 (should (= n (length (registry-lookup-secondary-value db 'sender "me"))))
98 (should (equal '(74) (registry-lookup-secondary-value db 'groups "74")))
100 (should (registry-delete db '(1) t))
102 (message "Search after delete")
103 (should (= n (length (registry-search db :all t))))
104 (message "Secondary search after delete")
105 (should (= n (length (registry-lookup-secondary-value db 'sender "me"))))
106 ;; (message "Pruning")
107 ;; (let* ((tokeep (registry-search db :member '((extra "more data"))))
108 ;; (count (- n (length tokeep)))
109 ;; (pruned (registry-prune db))
110 ;; (prune-count (length pruned)))
111 ;; (message "Expecting to prune %d entries and pruned %d"
112 ;; count prune-count)
113 ;; (should (and (= count 5)
114 ;; (= count prune-count))))
115 (message "Done with usage testing.")))
117 (ert-deftest gnustest-registry-persistence-test ()
119 (tempfile (make-temp-file "registry-persistence-"))
120 (name "persistence tester")
121 (db (gnustest-registry-make-testable-db n name tempfile))
123 (message "Saving to %s" tempfile)
124 (eieio-persistent-save db)
125 (setq size (nth 7 (file-attributes tempfile)))
126 (message "Saved to %s: size %d" tempfile size)
129 (insert-file-contents-literally tempfile)
130 (should (looking-at (concat ";; Object "
132 "\n;; EIEIO PERSISTENT OBJECT"))))
133 (message "Reading object back")
134 (setq back (eieio-persistent-read tempfile))
136 (message "Read object back: %d keys, expected %d==%d"
137 (registry-size back) n (registry-size db))
138 (should (= (registry-size back) n))
139 (should (= (registry-size back) (registry-size db)))
140 (delete-file tempfile))
141 (message "Done with persistence testing."))
143 (ert-deftest gnustest-gnus-registry-misc-test ()
144 (should-error (gnus-registry-extract-addresses '("" "")))
146 (should (equal '("Ted Zlatanov <tzz@lifelogs.com>"
148 "noname <cyd@stupidchicken.com>"
149 "noname <tzz@lifelogs.com>")
150 (gnus-registry-extract-addresses
151 (concat "Ted Zlatanov <tzz@lifelogs.com>, "
152 "ed <ed@you.me>, " ; "ed" is not a valid name here
153 "cyd@stupidchicken.com, "
154 "tzz@lifelogs.com")))))
156 (ert-deftest gnustest-gnus-registry-usage-test ()
158 (tempfile (make-temp-file "gnus-registry-persist"))
159 (db (gnus-registry-make-db tempfile))
160 (gnus-registry-db db)
162 (message "Adding %d keys to the test Gnus registry" n)
164 (let ((id (number-to-string i)))
165 (gnus-registry-handle-action id
166 (if (>= 50 i) "fromgroup" nil)
169 (format "subject %d" (mod i 10)))
171 (format "sender %d" (mod i 10))))))
172 (message "Testing Gnus registry size is %d" n)
173 (should (= n (registry-size db)))
174 (message "Looking up individual keys (registry-lookup)")
175 (should (equal (loop for e
177 (registry-lookup db '("20" "83" "72")))
178 collect (assq 'subject e)
179 collect (assq 'sender e)
180 collect (assq 'group e))
181 '((subject "subject 0") (sender "sender 0") (group "togroup")
182 (subject) (sender) (group "togroup")
183 (subject) (sender "sender 2") (group "togroup"))))
185 (message "Looking up individual keys (gnus-registry-id-key)")
186 (should (equal (gnus-registry-get-id-key "34" 'group) '("togroup")))
187 (should (equal (gnus-registry-get-id-key "34" 'subject) '("subject 4")))
188 (message "Trying to insert a duplicate key")
189 (should-error (gnus-registry-insert db "55" '()))
190 (message "Looking up individual keys (gnus-registry-get-or-make-entry)")
191 (should (gnus-registry-get-or-make-entry "22"))
192 (message "Saving the Gnus registry to %s" tempfile)
193 (should (gnus-registry-save tempfile db))
194 (setq size (nth 7 (file-attributes tempfile)))
195 (message "Saving the Gnus registry to %s: size %d" tempfile size)
198 (insert-file-contents-literally tempfile)
199 (should (looking-at (concat ";; Object "
201 "\n;; EIEIO PERSISTENT OBJECT"))))
202 (message "Reading Gnus registry back")
203 (setq back (eieio-persistent-read tempfile))
205 (message "Read Gnus registry back: %d keys, expected %d==%d"
206 (registry-size back) n (registry-size db))
207 (should (= (registry-size back) n))
208 (should (= (registry-size back) (registry-size db)))
209 (delete-file tempfile)
210 (message "Pruning Gnus registry to 0 by setting :max-soft")
211 (oset db :max-soft 0)
213 (should (= (registry-size db) 0)))
214 (message "Done with Gnus registry usage testing."))
216 (provide 'gnustest-registry)