all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#40562: [patch] Treat records as arrays in ert object comparisons and add support for cl-structs
@ 2020-04-11 20:34 Clément Pit-Claudel
  2020-04-11 23:26 ` Štěpán Němec
  0 siblings, 1 reply; 8+ messages in thread
From: Clément Pit-Claudel @ 2020-04-11 20:34 UTC (permalink / raw)
  To: 40562; +Cc: lars

[-- Attachment #1: Type: text/plain, Size: 1016 bytes --]

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" "?\x02") (4 "#x4" "?\x04"))))

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" "?\x02") (4 "#x4" "?\x04"))))

Clément.

[-- Attachment #2: 0001-lisp-emacs-lisp-ert.el-ert-explain-equal-rec-Treat-r.patch --]
[-- Type: text/x-patch, Size: 942 bytes --]

From 1f9936f1f1f8428b9ddebec04a4a865719543cf4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Pit-Claudel?= <clement.pitclaudel@live.com>
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


[-- Attachment #3: 0002-lisp-emacs-lisp-ert.el-ert-explain-equal-rec-Add-sup.patch --]
[-- Type: text/x-patch, Size: 1189 bytes --]

From 47eb414118ab41b32c613fcdbe86369b2efd4c56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Pit-Claudel?= <clement.pitclaudel@live.com>
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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-08-18 14:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-11 20:34 bug#40562: [patch] Treat records as arrays in ert object comparisons and add support for cl-structs Clément Pit-Claudel
2020-04-11 23:26 ` Štěpán Němec
2020-04-12  3:06   ` Clément Pit-Claudel
2020-04-12  9:24     ` Štěpán Němec
2020-04-12 15:50       ` Clément Pit-Claudel
2020-04-25 21:27         ` Clément Pit-Claudel
2020-08-08 13:02           ` Lars Ingebrigtsen
2020-08-18 14:07             ` Lars Ingebrigtsen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.