From a495247b42d3cfc78002062c87c14fbb90666b7c Mon Sep 17 00:00:00 2001 From: vemv Date: Fri, 21 Jul 2023 17:58:33 +0200 Subject: [PATCH 1/5] `cider-test`: don't render a newline between expected and actual --- cider-test.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cider-test.el b/cider-test.el index ef5714416..63c1a5049 100644 --- a/cider-test.el +++ b/cider-test.el @@ -426,8 +426,7 @@ With the actual value, the outermost '(not ...)' s-expression is removed." (when message (cider-insert message 'font-lock-string-face t)) (when expected (insert-label "expected") - (insert-rect expected) - (insert "\n")) + (insert-rect expected)) (if diffs (dolist (d diffs) (cl-destructuring-bind (actual (removed added)) d From e18c50425b197589b97e3ea8e282a2fa28c5e3fe Mon Sep 17 00:00:00 2001 From: vemv Date: Wed, 26 Jul 2023 15:39:58 +0200 Subject: [PATCH 2/5] Add newline conditionally --- cider-test.el | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cider-test.el b/cider-test.el index 63c1a5049..f1b47ae28 100644 --- a/cider-test.el +++ b/cider-test.el @@ -426,7 +426,12 @@ With the actual value, the outermost '(not ...)' s-expression is removed." (when message (cider-insert message 'font-lock-string-face t)) (when expected (insert-label "expected") - (insert-rect expected)) + (insert-rect expected) + ;; insert a newline between expected and actual only when both values are large enough + ;; to justify the readability improvement. + ;; our heuristic for a 'large enough' object is the presence of diffs: + (when diffs + (insert "\n"))) (if diffs (dolist (d diffs) (cl-destructuring-bind (actual (removed added)) d From 154f05044a77f3ac6291045588f25587fced03aa Mon Sep 17 00:00:00 2001 From: vemv Date: Wed, 26 Jul 2023 17:56:02 +0200 Subject: [PATCH 3/5] Always insert a newline between `actual` and `diffs` --- cider-test.el | 1 + 1 file changed, 1 insertion(+) diff --git a/cider-test.el b/cider-test.el index f1b47ae28..e0c7962ab 100644 --- a/cider-test.el +++ b/cider-test.el @@ -437,6 +437,7 @@ With the actual value, the outermost '(not ...)' s-expression is removed." (cl-destructuring-bind (actual (removed added)) d (insert-label "actual") (insert-rect actual) + (insert "\n") (insert-label "diff") (insert "- ") (insert-rect removed) From c23b45215bc4da8a20a91f2514f75493434f5f84 Mon Sep 17 00:00:00 2001 From: vemv Date: Fri, 4 Aug 2023 07:50:03 +0200 Subject: [PATCH 4/5] PR feedback --- CHANGELOG.md | 1 + cider-test.el | 16 +++++++++++---- test/cider-test-tests.el | 43 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 test/cider-test-tests.el diff --git a/CHANGELOG.md b/CHANGELOG.md index 3238753fe..098f03335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - Bump the injected `cider-nrepl` to [0.34](https://github.com/clojure-emacs/cider-nrepl/blob/v0.34.0/CHANGELOG.md#0340-2023-08-03). - Improve `nrepl-dict` error reporting. - Preserve the `:cljs-repl-type` more reliably. +- [#3375](https://github.com/clojure-emacs/cider/pull/3375): `cider-test`: don't render a newline between expected and actual, most times. ## 1.7.0 (2023-03-23) diff --git a/cider-test.el b/cider-test.el index e0c7962ab..42d9476e9 100644 --- a/cider-test.el +++ b/cider-test.el @@ -400,6 +400,14 @@ With the actual value, the outermost '(not ...)' s-expression is removed." (cider-insert "t" 'font-lock-constant-face t)) (insert "\n\n")))) +(defun cider-test--string-contains-newline (input-string) + "Returns whether INPUT-STRING contains a newline. +Ignores any newlines at the end of the string." + (when (stringp input-string) + (let ((trimmed-string (replace-regexp-in-string "\n\\'" "" input-string))) + (and (string-match-p "\n" trimmed-string) + t)))) + (defun cider-test-render-assertion (buffer test) "Emit into BUFFER report detail for the TEST assertion." (with-current-buffer buffer @@ -427,10 +435,10 @@ With the actual value, the outermost '(not ...)' s-expression is removed." (when expected (insert-label "expected") (insert-rect expected) - ;; insert a newline between expected and actual only when both values are large enough - ;; to justify the readability improvement. - ;; our heuristic for a 'large enough' object is the presence of diffs: - (when diffs + ;; Only place a newline between expected and actual when the values are deemed 'dense', + ;; otherwise favor compact output: + (when (or (cider-test--string-contains-newline expected) + (cider-test--string-contains-newline actual)) (insert "\n"))) (if diffs (dolist (d diffs) diff --git a/test/cider-test-tests.el b/test/cider-test-tests.el new file mode 100644 index 000000000..f036b0b24 --- /dev/null +++ b/test/cider-test-tests.el @@ -0,0 +1,43 @@ +;;; cider-test-tests.el -*- lexical-binding: t; -*- + +;; Copyright © 2023 Bozhidar Batsov + +;; Author: Bozhidar Batsov + +;; This file is NOT part of GNU Emacs. + +;; This program is free software: you can redistribute it and/or +;; modify it under the terms of the GNU General Public License as +;; published by the Free Software Foundation, either version 3 of the +;; License, or (at your option) any later version. +;; +;; This program is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see `http://www.gnu.org/licenses/'. + +;;; Commentary: + +;; This file is part of CIDER + +;;; Code: + +(require 'buttercup) +(require 'cider-test) + +(describe "cider-test--string-contains-newline" + (expect (cider-test--string-contains-newline "Hello World") + :to-equal + nil) + (expect (cider-test--string-contains-newline "Hello World\n") + :to-equal + nil) + (expect (cider-test--string-contains-newline "Hello\nWorld") + :to-equal + t) + (expect (cider-test--string-contains-newline "Hello\nWorld\n") + :to-equal + t)) From 924db50e3f6fd9455031ea09a59d2c48692b6206 Mon Sep 17 00:00:00 2001 From: vemv Date: Fri, 4 Aug 2023 07:56:21 +0200 Subject: [PATCH 5/5] Check for escaped strings instead --- cider-test.el | 8 +++----- test/cider-test-tests.el | 10 ++-------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/cider-test.el b/cider-test.el index 42d9476e9..7c083c3e2 100644 --- a/cider-test.el +++ b/cider-test.el @@ -401,12 +401,10 @@ With the actual value, the outermost '(not ...)' s-expression is removed." (insert "\n\n")))) (defun cider-test--string-contains-newline (input-string) - "Returns whether INPUT-STRING contains a newline. -Ignores any newlines at the end of the string." + "Returns whether INPUT-STRING contains an escaped newline." (when (stringp input-string) - (let ((trimmed-string (replace-regexp-in-string "\n\\'" "" input-string))) - (and (string-match-p "\n" trimmed-string) - t)))) + (and (string-match-p "\\n" input-string) + t))) (defun cider-test-render-assertion (buffer test) "Emit into BUFFER report detail for the TEST assertion." diff --git a/test/cider-test-tests.el b/test/cider-test-tests.el index f036b0b24..3531c6739 100644 --- a/test/cider-test-tests.el +++ b/test/cider-test-tests.el @@ -29,15 +29,9 @@ (require 'cider-test) (describe "cider-test--string-contains-newline" - (expect (cider-test--string-contains-newline "Hello World") - :to-equal - nil) - (expect (cider-test--string-contains-newline "Hello World\n") - :to-equal - nil) (expect (cider-test--string-contains-newline "Hello\nWorld") :to-equal - t) - (expect (cider-test--string-contains-newline "Hello\nWorld\n") + nil) + (expect (cider-test--string-contains-newline "Hello\\nWorld") :to-equal t))