unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: 30219@debbugs.gnu.org
Cc: Lars Ingebrigtsen <larsi@gnus.org>, Philipp <p.stephani2@gmail.com>
Subject: bug#30219: 27.0.50; (should (equal ...)) bug for string equality
Date: Thu, 10 Oct 2019 21:29:01 +0200	[thread overview]
Message-ID: <92BD15D2-9962-4760-9CF5-A82F46226808@acm.org> (raw)
In-Reply-To: <CEAA5F54-E7E5-458A-A7D4-6235AF1A469C@acm.org>

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

> There are several solutions, none perfect. I'll be back with a patch later today or tomorrow.

Please try this patch.


[-- Attachment #2: 0001-Correctly-explain-test-failures-with-mixed-uni-multi.patch --]
[-- Type: application/octet-stream, Size: 3827 bytes --]

From dc8570aee0fd01ae55c2214160fb609626fbcf4e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Thu, 10 Oct 2019 21:20:20 +0200
Subject: [PATCH] Correctly explain test failures with mixed uni/multibyte
 strings

* lisp/emacs-lisp/ert.el (ert--explain-equal-rec)
(ert--explain-equal-including-properties):
* test/lisp/emacs-lisp/ert-tests.el (ert-test-explain-equal):
When explaining a difference between a unibyte and a multibyte string,
first convert both to multibyte.  Otherwise, we might end up comparing
unibyte char C to multibyte char C, 127<C<256, and not detect the
difference (bug#30219).
---
 lisp/emacs-lisp/ert.el            |  9 +++++++++
 test/lisp/emacs-lisp/ert-tests.el | 23 +++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el
index da241e6304..880a40dafd 100644
--- a/lisp/emacs-lisp/ert.el
+++ b/lisp/emacs-lisp/ert.el
@@ -512,6 +512,11 @@ ert--explain-equal-rec
                      (cl-assert (equal a b) t)
                      nil))))))))
       ((pred arrayp)
+       ;; For mixed unibyte/multibyte string comparisons, make both multibyte.
+       (when (and (stringp a)
+                  (xor (multibyte-string-p a) (multibyte-string-p b)))
+         (setq a (string-to-multibyte a))
+         (setq b (string-to-multibyte b)))
        (if (/= (length a) (length b))
            `(arrays-of-different-length ,(length a) ,(length b)
                                         ,a ,b
@@ -606,6 +611,10 @@ ert--explain-equal-including-properties
     (cl-assert (stringp a) t)
     (cl-assert (stringp b) t)
     (cl-assert (eql (length a) (length b)) t)
+    ;; For mixed unibyte/multibyte comparisons, make both multibyte.
+    (when (xor (multibyte-string-p a) (multibyte-string-p b))
+      (setq a (string-to-multibyte a))
+      (setq b (string-to-multibyte b)))
     (cl-loop for i from 0 to (length a)
              for props-a = (text-properties-at i a)
              for props-b = (text-properties-at i b)
diff --git a/test/lisp/emacs-lisp/ert-tests.el b/test/lisp/emacs-lisp/ert-tests.el
index 36db1eeb42..3a9e81595b 100644
--- a/test/lisp/emacs-lisp/ert-tests.el
+++ b/test/lisp/emacs-lisp/ert-tests.el
@@ -627,6 +627,29 @@ ert-test-explain-equal
     (should (equal (ert--explain-equal 'a sym)
                    `(different-symbols-with-the-same-name a ,sym)))))
 
+(ert-deftest ert-test-explain-equal-strings ()
+  (should (equal (ert--explain-equal "abc" "axc")
+                 '(array-elt 1 (different-atoms
+                                (?b "#x62" "?b")
+                                (?x "#x78" "?x")))))
+  (should (equal (ert--explain-equal "abc" "abxc")
+                 '(arrays-of-different-length
+                   3 4 "abc" "abxc" first-mismatch-at 2)))
+  (should (equal (ert--explain-equal "xyA" "xyÅ")
+                 '(array-elt 2 (different-atoms
+                                (?A "#x41" "?A")
+                                (?Å "#xc5" "?Å")))))
+  (should (equal (ert--explain-equal "m\xff" "m\u00ff")
+                 `(array-elt
+                   1 (different-atoms
+                      (#x3fffff "#x3fffff" ,(string-to-multibyte "?\xff"))
+                      (#xff "#xff" "?ÿ")))))
+  (should (equal (ert--explain-equal (string-to-multibyte "m\xff") "m\u00ff")
+                 `(array-elt
+                   1 (different-atoms
+                      (#x3fffff "#x3fffff" ,(string-to-multibyte "?\xff"))
+                      (#xff "#xff" "?ÿ"))))))
+
 (ert-deftest ert-test-explain-equal-improper-list ()
   (should (equal (ert--explain-equal '(a . b) '(a . c))
                  '(cdr (different-atoms b c)))))
-- 
2.21.0 (Apple Git-122)


  reply	other threads:[~2019-10-10 19:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-22 22:55 bug#30219: 27.0.50; (should (equal ...)) bug for string equality Philipp
2019-10-09 21:29 ` Lars Ingebrigtsen
2019-10-10 14:34 ` Mattias Engdegård
2019-10-10 19:29   ` Mattias Engdegård [this message]
2019-10-11  5:43     ` Lars Ingebrigtsen
2019-10-11  9:21       ` Mattias Engdegård
2019-10-13 18:10         ` Lars Ingebrigtsen
2019-10-13 18:30           ` Mattias Engdegård

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=92BD15D2-9962-4760-9CF5-A82F46226808@acm.org \
    --to=mattiase@acm.org \
    --cc=30219@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    --cc=p.stephani2@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).