Avoid failing tests for Emacs without lexical binding.
[gnus] / lisp / registry.el
index 3da83d1..3c402cb 100644 (file)
@@ -1,22 +1,24 @@
 ;;; registry.el --- Track and remember data items by various fields
 
-;; Copyright (C) 2011  Teodor Zlatanov
+;; Copyright (C) 2011  Free Software Foundation, Inc.
 
 ;; Author: Teodor Zlatanov <tzz@lifelogs.com>
 ;; Keywords: data
 
-;; This program is free software; you can redistribute it and/or modify
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation, either version 3 of the License, or
 ;; (at your option) any later version.
 
-;; This program is distributed in the hope that it will be useful,
+;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 
 ;;; Code:
 
-(eval-when-compile (require 'ert))
+(eval-when-compile
+  (when (null (ignore-errors (require 'ert)))
+    (defmacro* ert-deftest (name () &body docstring-keys-and-body))))
+
+(ignore-errors
+  (require 'ert))
+
 (eval-when-compile (require 'cl))
 (eval-and-compile
   (or (ignore-errors (progn
              :type t
              :documentation "The precious fields, a list of symbols.")
    (tracker :initarg :tracker
-            :initform (make-hash-table :size 100 :rehash-size 2.0)
-            :type t
+            :type hash-table
             :documentation "The field tracking hashtable.")
    (data :initarg :data
-         :initform (make-hash-table :size 10000 :rehash-size 2.0 :test 'equal)
-         :type t
+         :type hash-table
          :documentation "The data hashtable.")))
 
-;; (defmethod initialize-instance :after ((this registry-db) slots)
-;;   "Set value of data slot of THIS after initialization."
-;;   (with-slots (data tracker max-hard max-soft tracked precious version) this
-;;     (setq data (make-hash-table :size 10000 :rehash-size 2.0 :test 'equal)
-;;           tracker (make-hash-table :size 100 :rehash-size 2.0)
-;;           max-hard 5000000
-;;           max-soft 50000
-;;           tracked nil
-;;           precious nil
-;;           version 0.1)))
+(defmethod initialize-instance :AFTER ((this registry-db) slots)
+  "Set value of data slot of THIS after initialization."
+  (with-slots (data tracker) this
+    (unless (member :data slots)
+      (setq data (make-hash-table :size 10000 :rehash-size 2.0 :test 'equal)))
+    (unless (member :tracker slots)
+      (setq tracker (make-hash-table :size 100 :rehash-size 2.0)))))
 
 (defmethod registry-lookup ((db registry-db) keys)
   "Search for KEYS in the registry-db THIS.
@@ -147,6 +150,15 @@ Returns a alist of the key followed by the entry in a list, not a cons cell."
                (list k (gethash k data))))
            keys))))
 
+(defmethod registry-lookup-breaks-before-lexbind ((db registry-db) keys)
+  "Search for KEYS in the registry-db THIS.
+Returns a alist of the key followed by the entry in a list, not a cons cell."
+  (let ((data (oref db :data)))
+    (delq nil
+          (loop for key in keys
+                when (gethash key data)
+                collect (list key (gethash key data))))))
+
 (defmethod registry-lookup-secondary ((db registry-db) tracksym
                                       &optional create)
   "Search for TRACKSYM in the registry-db THIS.
@@ -345,6 +357,13 @@ Removes only entries without the :precious keys."
     (should (= 58 (caadr (registry-lookup db '(1 58 99)))))
     (message "Grouped individual lookup")
     (should (= 3 (length (registry-lookup db '(1 58 99)))))
+    (when (boundp 'lexical-binding)
+      (message "Individual lookup (breaks before lexbind)")
+      (should (= 58
+                (caadr (registry-lookup-breaks-before-lexbind db '(1 58 99)))))
+      (message "Grouped individual lookup (breaks before lexbind)")
+      (should (= 3
+                (length (registry-lookup-breaks-before-lexbind db '(1 58 99))))))
     (message "Search")
     (should (= n (length (registry-search db :all t))))
     (should (= n (length (registry-search db :member '((sender "me"))))))