emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] map 6287979: Fix a false negative in `map-elt' with alists


From: Nicolas Petton
Subject: [Emacs-diffs] map 6287979: Fix a false negative in `map-elt' with alists and values being nil
Date: Sat, 25 Apr 2015 10:07:39 +0000

branch: map
commit 62879799ea0272f2ed3067252f20afb910bce352
Author: Nicolas Petton <address@hidden>
Commit: Nicolas Petton <address@hidden>

    Fix a false negative in `map-elt' with alists and values being nil
    
    * lisp/emacs-lisp/map.el (map-elt): If map is an alist and key is
    found but its associated value is nil, do not return the default
    value.
    
    * test/automated/map-tests.el: Add a regression test.
---
 lisp/emacs-lisp/map.el      |   13 +++++++++++--
 test/automated/map-tests.el |    6 ++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 3984b08..ebf1fe9 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -50,7 +50,7 @@ If KEY is not found, return DEFAULT which defaults to nil.
 
 If MAP is a list, `equal' is used to lookup KEY."
   (map--dispatch map
-    :list (or (cdr (assoc key map)) default)
+    :list (map--elt-list map key default)
     :hash-table (gethash key map default)
     :array (map--elt-array map key default)))
 
@@ -253,8 +253,17 @@ form.
                   (setq index (1+ index))))
               map)))
 
+(defun map--elt-list (map key &optional default)
+  "Return the element of the list MAP at the index KEY.
+If KEY is not found, return DEFAULT which defaults to nil."
+  (let ((pair (assoc key map)))
+    (if pair
+        (cdr (assoc key map))
+      default)))
+
 (defun map--elt-array (map key &optional default)
-  "Return the element of the arary MAP at the index KEY, or DEFAULT if nil."
+  "Return the element of the array MAP at the index KEY.
+If KEY is not found, return DEFAULT which defaults to nil."
   (let ((len (seq-length map)))
     (or (and (>= key 0)
              (<= key len)
diff --git a/test/automated/map-tests.el b/test/automated/map-tests.el
index 5201116..e65af89 100644
--- a/test/automated/map-tests.el
+++ b/test/automated/map-tests.el
@@ -66,6 +66,12 @@ Evaluate BODY for each created map.
   (with-maps-do map
     (assert (= 5 (map-elt map 7 5)))))
 
+(ert-deftest test-map-elt-with-nil-value ()
+  (assert (null (map-elt '((a . 1)
+                           (b))
+                         'b
+                         '2))))
+
 (ert-deftest test-map-put ()
   (with-maps-do map
     (map-put map 2 'hello)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]