GNU bug report logs - #23729
[PATCH] Remove ‘ert-with-function-mocked’ macro in favour of ‘cl-letf’ macro

Previous Next

Package: emacs;

Reported by: Michal Nazarewicz <mina86 <at> mina86.com>

Date: Wed, 8 Jun 2016 12:46:01 UTC

Severity: wishlist

Tags: patch

Done: Michal Nazarewicz <mina86 <at> mina86.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 23729 in the body.
You can then email your comments to 23729 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#23729; Package emacs. (Wed, 08 Jun 2016 12:46:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Michal Nazarewicz <mina86 <at> mina86.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 08 Jun 2016 12:46:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Michal Nazarewicz <mina86 <at> mina86.com>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH] Remove ‘ert-with-function-mocked’ macro in favour of ‘cl-letf’ macro
Date: Wed,  8 Jun 2016 14:44:39 +0200
* lisp/emacs-lisp/ert-x.el (ert-with-function-mocked): Remove macro
in favour of ‘cl-letf’ macro which is more generic.  All existing
uses are migrated accordingly.  The macro has not been included in
an official release yet so it should be fine to delete it.
---
 etc/NEWS                              |  3 ---
 lisp/emacs-lisp/ert-x.el              | 40 --------------------------------
 test/lisp/calendar/icalendar-tests.el |  3 +--
 test/lisp/emacs-lisp/ert-x-tests.el   | 43 -----------------------------------
 test/lisp/gnus/message-tests.el       |  2 +-
 test/lisp/vc/vc-bzr-tests.el          |  3 +--
 6 files changed, 3 insertions(+), 91 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 1ae8ff6..d8583cf 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -418,9 +418,6 @@ by setting 'autoload-timestamps' to nil.
 FIXME As an experiment, nil is the current default.
 If no insurmountable problems before next release, it can stay that way.
 
-** 'ert-with-function-mocked' of 'ert-x package allows mocking of functions
-in unit tests.
-
 ---
 ** 'gnutls-boot' now takes a parameter :complete-negotiation that says
 that negotiation should complete even on non-blocking sockets.
diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el
index 67cb102..2a2418f 100644
--- a/lisp/emacs-lisp/ert-x.el
+++ b/lisp/emacs-lisp/ert-x.el
@@ -285,46 +285,6 @@ ert-buffer-string-reindented
             (kill-buffer clone)))))))
 
 
-(defmacro ert-with-function-mocked (name mock &rest body)
-  "Mocks function NAME with MOCK and run BODY.
-
-Once BODY finishes (be it normally by returning a value or
-abnormally by throwing or signaling), the old definition of
-function NAME is restored.
-
-BODY may further change the mock with `fset'.
-
-If MOCK is nil, the function NAME is mocked with a function
-`ert-fail'ing when called.
-
-For example:
-
-    ;; Regular use, function is mocked inside the BODY:
-    (should (eq 2 (+ 1 1)))
-    (ert-with-function-mocked ((+ (lambda (a b) (- a b))))
-      (should (eq 0 (+ 1 1))))
-    (should (eq 2 (+ 1 1)))
-
-    ;; Macro correctly recovers from a throw or signal:
-    (should
-      (catch 'done
-        (ert-with-function-mocked ((+ (lambda (a b) (- a b))))
-          (should (eq 0 (+ 1 1))))
-          (throw 'done t)))
-    (should (eq 2 (+ 1 1)))
-"
-  (declare (indent 2))
-  (let ((old-var (make-symbol "old-var"))
-        (mock-var (make-symbol "mock-var")))
-    `(let ((,old-var (symbol-function (quote ,name))) (,mock-var ,mock))
-       (fset (quote ,name)
-             (or ,mock-var (lambda (&rest _)
-                             (ert-fail (concat "`" ,(symbol-name name)
-                                               "' unexpectedly called.")))))
-       (unwind-protect
-           (progn ,@body)
-         (fset (quote ,name) ,old-var)))))
-
 (provide 'ert-x)
 
 ;;; ert-x.el ends here
diff --git a/test/lisp/calendar/icalendar-tests.el b/test/lisp/calendar/icalendar-tests.el
index 20d8834..6db4222 100644
--- a/test/lisp/calendar/icalendar-tests.el
+++ b/test/lisp/calendar/icalendar-tests.el
@@ -32,7 +32,6 @@
 ;;; Code:
 
 (require 'ert)
-(require 'ert-x)
 (require 'icalendar)
 
 ;; ======================================================================
@@ -64,7 +63,7 @@ icalendar-tests--trim
          (hash (format "%d" (abs (sxhash entry-full))))
          (contents "DTSTART:19640630T070100\nblahblah")
          (username (or user-login-name "UNKNOWN_USER")))
-    (ert-with-function-mocked current-time (lambda () '(1 2 3))
+    (cl-letf (((symbol-function 'current-time) (lambda () '(1 2 3))))
       (should (= 77 icalendar--uid-count))
       (should (string=  (concat "xxx-123-77-" hash "-" username "-19640630")
                         (icalendar--create-uid entry-full contents)))
diff --git a/test/lisp/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el
index a2665e7..ef8642a 100644
--- a/test/lisp/emacs-lisp/ert-x-tests.el
+++ b/test/lisp/emacs-lisp/ert-x-tests.el
@@ -275,49 +275,6 @@ ert--hash-table-to-alist
              (should (equal (c x) (lisp x))))))
 
 
-(defun ert--dummy-id (a)
-  "Identity function.  Used for tests only."
-  a)
-
-(ert-deftest ert-with-function-mocked ()
-  (let ((mock-id  (lambda (_) 21)))
-    (should (eq 42 (ert--dummy-id 42)))
-
-    (ert-with-function-mocked ert--dummy-id nil
-       (fset 'ert--dummy-id mock-id)
-       (should (eq 21 (ert--dummy-id 42))))
-    (should (eq 42 (ert--dummy-id 42)))
-
-    (ert-with-function-mocked ert--dummy-id mock-id
-       (should (eq 21 (ert--dummy-id 42))))
-    (should (eq 42 (ert--dummy-id 42)))
-
-    (should
-     (catch 'exit
-       (ert-with-function-mocked ert--dummy-id mock-id
-         (should (eq 21 (ert--dummy-id 42))))
-         (throw 'exit t)))
-    (should (eq 42 (ert--dummy-id 42)))
-
-    (should
-     (string= "Foo"
-              (condition-case err
-                  (progn
-                    (ert-with-function-mocked ert--dummy-id mock-id
-                      (should (eq 21 (ert--dummy-id 42))))
-                    (user-error "Foo"))
-                (user-error (cadr err)))))
-    (should (eq 42 (ert--dummy-id 42)))
-
-    (should
-     (string= "`ert--dummy-id' unexpectedly called."
-              (condition-case err
-                  (ert-with-function-mocked ert--dummy-id nil
-                    (ert--dummy-id 42))
-                (ert-test-failed (cadr err)))))
-    (should (eq 42 (ert--dummy-id 42)))))
-
-
 (provide 'ert-x-tests)
 
 ;;; ert-x-tests.el ends here
diff --git a/test/lisp/gnus/message-tests.el b/test/lisp/gnus/message-tests.el
index ae34f24..13c15e3 100644
--- a/test/lisp/gnus/message-tests.el
+++ b/test/lisp/gnus/message-tests.el
@@ -57,7 +57,7 @@
 
 
 (ert-deftest message-strip-subject-trailing-was ()
-  (ert-with-function-mocked message-talkative-question nil
+  (cl-letf (((symbol-function 'message-talkative-question) nil))
     (with-temp-buffer
       (let ((no-was "Re: Foo ")
             (with-was "Re: Foo \t (was: Bar ) ")
diff --git a/test/lisp/vc/vc-bzr-tests.el b/test/lisp/vc/vc-bzr-tests.el
index 98d176c..f27e658 100644
--- a/test/lisp/vc/vc-bzr-tests.el
+++ b/test/lisp/vc/vc-bzr-tests.el
@@ -25,7 +25,6 @@
 ;;; Code:
 
 (require 'ert)
-(require 'ert-x)
 (require 'vc-bzr)
 (require 'vc-dir)
 
@@ -102,7 +101,7 @@
           (while (vc-dir-busy)
             (sit-for 0.1))
           (vc-dir-mark-all-files t)
-          (ert-with-function-mocked y-or-n-p (lambda (_) t)
+          (cl-letf (((symbol-function 'y-or-n-p) (lambda (_) t)))
             (vc-next-action nil))
           (should (get-buffer "*vc-log*")))
       (delete-directory homedir t))))
-- 
2.8.0.rc3.226.g39d4020





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#23729; Package emacs. (Wed, 08 Jun 2016 16:07:01 GMT) Full text and rfc822 format available.

Message #8 received at 23729 <at> debbugs.gnu.org (full text, mbox):

From: Glenn Morris <rgm <at> gnu.org>
To: Michal Nazarewicz <mina86 <at> mina86.com>
Cc: 23729 <at> debbugs.gnu.org
Subject: Re: bug#23729: [PATCH] Remove ‘ert-with-function-mocked’ macro in favour of ‘cl-letf’ macro
Date: Wed, 08 Jun 2016 12:05:42 -0400
I see you added it in the first place?
So I guess you should feel free to remove it again. :)




Added indication that bug 23729 blocks21966 Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Wed, 08 Jun 2016 16:07:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#23729; Package emacs. (Wed, 08 Jun 2016 17:16:01 GMT) Full text and rfc822 format available.

Message #13 received at 23729 <at> debbugs.gnu.org (full text, mbox):

From: Michal Nazarewicz <mina86 <at> mina86.com>
To: Glenn Morris <rgm <at> gnu.org>
Cc: 23729 <at> debbugs.gnu.org
Subject: Re: bug#23729: [PATCH] Remove ‘ert-with-function-mocked’ macro in favour of ‘cl-letf’ macro
Date: Wed, 08 Jun 2016 19:15:21 +0200
On Wed, Jun 08 2016, Glenn Morris wrote:
> I see you added it in the first place?
> So I guess you should feel free to remove it again. :)

The write access seems to be misbehaving though (I’ve already tried with
the NEWS typo fix):

Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (17/17), 1.59 KiB | 0 bytes/s, done.
Total 17 (delta 14), reused 0 (delta 0)
error: unpack failed: unpack-objects abnormal exit
To git://git.sv.gnu.org/emacs.git
 ! [remote rejected] ert -> master (n/a (unpacker error))
error: failed to push some refs to 'git://git.sv.gnu.org/emacs.git'

-- 
Best regards
ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴイツ
«If at first you don’t succeed, give up skydiving»




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#23729; Package emacs. (Wed, 08 Jun 2016 17:19:01 GMT) Full text and rfc822 format available.

Message #16 received at 23729 <at> debbugs.gnu.org (full text, mbox):

From: Michal Nazarewicz <mina86 <at> mina86.com>
To: Glenn Morris <rgm <at> gnu.org>
Cc: 23729 <at> debbugs.gnu.org
Subject: Re: bug#23729: [PATCH] Remove ‘ert-with-function-mocked’ macro in favour of ‘cl-letf’ macro
Date: Wed, 08 Jun 2016 19:17:56 +0200
> On Wed, Jun 08 2016, Glenn Morris wrote:
>> I see you added it in the first place?
>> So I guess you should feel free to remove it again. :)

On Wed, Jun 08 2016, Michal Nazarewicz wrote:
> The write access seems to be misbehaving though (I’ve already tried with
> the NEWS typo fix):

Disregard, I’m an idiot.  Got it working.

> Counting objects: 17, done.
> Delta compression using up to 4 threads.
> Compressing objects: 100% (17/17), done.
> Writing objects: 100% (17/17), 1.59 KiB | 0 bytes/s, done.
> Total 17 (delta 14), reused 0 (delta 0)
> error: unpack failed: unpack-objects abnormal exit
> To git://git.sv.gnu.org/emacs.git
>  ! [remote rejected] ert -> master (n/a (unpacker error))
> error: failed to push some refs to 'git://git.sv.gnu.org/emacs.git'

-- 
Best regards
ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴイツ
«If at first you don’t succeed, give up skydiving»




Reply sent to Michal Nazarewicz <mina86 <at> mina86.com>:
You have taken responsibility. (Wed, 08 Jun 2016 17:21:01 GMT) Full text and rfc822 format available.

Notification sent to Michal Nazarewicz <mina86 <at> mina86.com>:
bug acknowledged by developer. (Wed, 08 Jun 2016 17:21:01 GMT) Full text and rfc822 format available.

Message #21 received at 23729-done <at> debbugs.gnu.org (full text, mbox):

From: Michal Nazarewicz <mina86 <at> mina86.com>
To: 23729-done <at> debbugs.gnu.org
Subject: Re: bug#23729: [PATCH] Remove ‘ert-with-function-mocked’ macro in favour of ‘cl-letf’ macro
Date: Wed, 08 Jun 2016 19:20:41 +0200
Patch applied.

-- 
Best regards
ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴイツ
«If at first you don’t succeed, give up skydiving»

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 07 Jul 2016 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 9 years and 41 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.