From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Lars Ingebrigtsen Newsgroups: gmane.emacs.bugs Subject: bug#35018: 26.1; Use diff as en ert-explainer for string= Date: Wed, 23 Jun 2021 15:42:11 +0200 Message-ID: <87wnqklmto.fsf@gnus.org> References: <87a7hgpqlg.fsf@ambrevar.xyz> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33021"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: 35018@debbugs.gnu.org, Stefan Monnier To: Pierre Neidhardt Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jun 23 15:43:14 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lw39s-0008Iu-7Q for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 23 Jun 2021 15:43:12 +0200 Original-Received: from localhost ([::1]:47058 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lw39q-0006Le-GA for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 23 Jun 2021 09:43:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37816) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lw39i-0006LM-HE for bug-gnu-emacs@gnu.org; Wed, 23 Jun 2021 09:43:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:57327) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lw39i-0002SV-61 for bug-gnu-emacs@gnu.org; Wed, 23 Jun 2021 09:43:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lw39i-00026J-4j for bug-gnu-emacs@gnu.org; Wed, 23 Jun 2021 09:43:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Lars Ingebrigtsen Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 23 Jun 2021 13:43:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 35018 X-GNU-PR-Package: emacs Original-Received: via spool by 35018-submit@debbugs.gnu.org id=B35018.16244557458007 (code B ref 35018); Wed, 23 Jun 2021 13:43:02 +0000 Original-Received: (at 35018) by debbugs.gnu.org; 23 Jun 2021 13:42:25 +0000 Original-Received: from localhost ([127.0.0.1]:40637 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lw396-000254-Ol for submit@debbugs.gnu.org; Wed, 23 Jun 2021 09:42:25 -0400 Original-Received: from quimby.gnus.org ([95.216.78.240]:48860) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lw394-00024m-Hz for 35018@debbugs.gnu.org; Wed, 23 Jun 2021 09:42:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnus.org; s=20200322; h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date: References:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Se7dLmip6XDwfSStS7jhMPLuvzFKVHjS18bzDXDldqg=; b=ha8p+iHPqaF093cGPWFQPtCNgY JgujFX/LS469PTozfDWtUFM1dP7Vp1XxII9+mhRrbbEqaMRk5nS+Yq2q7HjrWSkyjyZiYz93qLmtY Gy5feslKcG6wK+BQ9jsrvwlSsP4ZbAHFZHINYZ/uTo0XqHH21j7RvQx3zh9i51TBLkWY=; Original-Received: from cm-84.212.220.105.getinternet.no ([84.212.220.105] helo=elva) by quimby.gnus.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lw38v-0005EE-0E; Wed, 23 Jun 2021 15:42:15 +0200 X-Now-Playing: The Clash's _Sandinista!_: "Corner Soul" In-Reply-To: <87a7hgpqlg.fsf@ambrevar.xyz> (Pierre Neidhardt's message of "Wed, 27 Mar 2019 11:19:07 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:208897 Archived-At: Pierre Neidhardt writes: > I've just committed webfeeder.el to ELPA. In his review, Stefan Monnier > suggested we merged the following code snippet upstream: I've adapted this to ert to see how it would look, and... I'm not sure it's all that helpful as is. The following test: (ert-deftest test-test-test () (should (string= "foo\nbar" "zot\nbar"))) then gives this output: (ert-test-failed ((should (string= "foo\12bar" "zot\12bar")) :form (string= "foo\12bar" "zot\12bar") :value nil :explanation "diff -u /tmp/webfeeder1kS4io /tmp/webfeederwYoKdx\12--- /tmp/webfeeder1kS4io\0112021-06-23 15:37:54.407053381 +0200\12+++ /tmp/webfeederwYoKdx\0112021-06-23 15:37:54.407053381 +0200\12@@ -1,2 +1,2 @@\12-foo\12+zot\12 bar\12\\ No newline at end of file\12\12Diff finished. Wed Jun 23 15:37:54 2021\12")) Which isn't all that nice. But I guess we could massage the output. Removing the header and trailer, we get: (ert-test-failed ((should (string= "foo\12bar" "zot\12bar")) :form (string= "foo\12bar" "zot\12bar") :value nil :explanation "@@ -1,2 +1,2 @@\12-foo\12+zot\12 bar\12\\ ")) But... that's still not exactly readable. So I don't know. Anybody got an opinion here, or an easy fix that'll make this readable in this context? diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el index 6793b374ee..004bc81b4a 100644 --- a/lisp/emacs-lisp/ert.el +++ b/lisp/emacs-lisp/ert.el @@ -63,6 +63,7 @@ (require 'ewoc) (require 'find-func) (require 'pp) +(require 'diff) ;;; UI customization options. @@ -535,6 +536,34 @@ ert--explain-equal (ert--explain-equal-rec a b))) (put 'equal 'ert-explainer 'ert--explain-equal) +(defun ert--explain-string (string-a string-b) + "Return the diff output of STRING-A and STRING-B" + (unless (or (string= string-a string-b) + ;; Only do diffs if there are newlines. + (not (string-match-p "\n" string-a)) + (not (string-match-p "\n" string-b))) + (let (file-a file-b) + (unwind-protect + (let (result) + (setq file-a (make-temp-file "ert") + file-b (make-temp-file "ert")) + (with-temp-file file-a + (insert string-a)) + (with-temp-file file-b + (insert string-b)) + (setq result + (with-temp-buffer + ;; The following generates a *Diff* buffer which is + ;; convenient for coloration. + (diff-no-select file-a file-b nil 'no-async) + (diff-no-select file-a file-b nil 'no-async + (current-buffer)) + (buffer-string))) + result) + (delete-file file-a) + (delete-file file-b))))) +(put 'string= 'ert-explainer #'ert--explain-string) + (defun ert--significant-plist-keys (plist) "Return the keys of PLIST that have non-null values, in order." (cl-assert (zerop (mod (length plist) 2)) t) -- (domestic pets only, the antidote for overdose, milk.) bloggy blog: http://lars.ingebrigtsen.no