Subject: bug#40562: [patch] Treat records as arrays in ert object comparisons and add support for cl-structs

From: Clément Pit-Claudel <[email-masked]>
Date: Sat, 11 Apr 2020 16:34:57 -0400

Hi all,

With the introduction of native records ert lost the ability to peek inside structures when comparing unequal values:

(require 'ert)
(cl-defstruct xyz a b c)
(defvar xyz123 (make-xyz :a 1 :b 2 :c 3))
(defvar xyz143 (make-xyz :a 1 :b 4 :c 3))
(should (equal xyz123 xyz143))

Emacs 25 said this:

Test failed: ((should (equal xyz123 xyz143)) 
  :form (equal [cl-struct-xyz 1 2 3 nil] [cl-struct-xyz 1 4 3 nil]) 
  :value nil
  :explanation (array-elt 2 (different-atoms (2 "#x2" "?") (4 "#x4" "?"))))

Emacs 26 says this:

Test failed: ((should (equal xyz123 xyz143))
  :form (equal #s(xyz 1 2 3) #s(xyz 1 4 3))
  :value nil 
  :explanation (different-atoms #s(xyz 1 2 3) #s(xyz 1 4 3)))

The first attached patch fixes this for all records.  The second patch adds special support for cl-structs, to get this:

Test failed: ((should (equal xyz123 xyz143)) 
  :form (equal #s(xyz 1 2 3) #s(xyz 1 4 3))
  :value nil
  :explanation (struct-field c (different-atoms (2 "#x2" "?") (4 "#x4" "?"))))

Clément.
>From 1f9936f1f1f8428b9ddebec04a4a865719543cf4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Pit-Claudel?= <[email-masked]>
Date: Sat, 11 Apr 2020 16:22:56 -0400
Subject: [PATCH 1/2] * lisp/emacs-lisp/ert.el (ert--explain-equal-rec): Treat
 records as arrays

---
 lisp/emacs-lisp/ert.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el
index 764354b03b..74202183cc 100644
--- a/lisp/emacs-lisp/ert.el
+++ b/lisp/emacs-lisp/ert.el
@@ -515,7 +515,7 @@ ert--explain-equal-rec
                        `(cdr ,cdr-x)
                      (cl-assert (equal a b) t)
                      nil))))))))
-      ((pred arrayp)
+      ((or (pred arrayp) (pred recordp))
        ;; For mixed unibyte/multibyte string comparisons, make both multibyte.
        (when (and (stringp a)
                   (xor (multibyte-string-p a) (multibyte-string-p b)))
-- 
2.17.1

>From 47eb414118ab41b32c613fcdbe86369b2efd4c56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Pit-Claudel?= <[email-masked]>
Date: Sat, 11 Apr 2020 16:23:47 -0400
Subject: [PATCH 2/2] * lisp/emacs-lisp/ert.el (ert--explain-equal-rec): Add
 support for cl-structs

---
 lisp/emacs-lisp/ert.el | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el
index 74202183cc..e3666a84c0 100644
--- a/lisp/emacs-lisp/ert.el
+++ b/lisp/emacs-lisp/ert.el
@@ -515,6 +515,13 @@ ert--explain-equal-rec
                        `(cdr ,cdr-x)
                      (cl-assert (equal a b) t)
                      nil))))))))
+      ((pred cl-struct-p)
+       (cl-loop for slot in (cdr (cl-struct-slot-info (type-of a)))
+                for ai across a
+                for bi across b
+                for xf = (ert--explain-equal-rec ai bi)
+                do (when xf (cl-return `(struct-field ,(car slot) ,xf)))
+                finally (cl-assert (equal a b) t)))
       ((or (pred arrayp) (pred recordp))
        ;; For mixed unibyte/multibyte string comparisons, make both multibyte.
        (when (and (stringp a)
-- 
2.17.1