From: Michal Nazarewicz <mina86@mina86.com>
To: 22632@debbugs.gnu.org
Subject: bug#22632: [PATCH 1/4] Introduce `ert-with-function-mocked' macro
Date: Thu, 11 Feb 2016 16:02:32 +0100 [thread overview]
Message-ID: <1455202955-25751-1-git-send-email-mina86@mina86.com> (raw)
In-Reply-To: <1455202258-23963-1-git-send-email-mina86@mina86.com>
* lisp/emacs-lisp/ert-x.el (ert-with-function-mocked): New macro which
allows evaluating code while particular function is replaced with
a mock. The original definition of said function is restored once the
macro finishes.
---
etc/NEWS | 3 +++
lisp/emacs-lisp/ert-x.el | 40 ++++++++++++++++++++++++++++++++++
test/lisp/emacs-lisp/ert-x-tests.el | 43 +++++++++++++++++++++++++++++++++++++
3 files changed, 86 insertions(+)
diff --git a/etc/NEWS b/etc/NEWS
index 37eb2bc..ac418be7 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -88,6 +88,9 @@ different group ID.
** Autoload files can be generated without timestamps,
by setting `autoload-timestamps' to nil.
+** `ert-with-function-mocked' of 'ert-x package allows mocking of functions
+in unit tests.
+
\f
* Changes in Emacs 25.2 on Non-Free Operating Systems
diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el
index 2a2418f..eb10c84 100644
--- a/lisp/emacs-lisp/ert-x.el
+++ b/lisp/emacs-lisp/ert-x.el
@@ -285,6 +285,46 @@ 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 signalling), 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/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el
index ef8642a..a2665e7 100644
--- a/test/lisp/emacs-lisp/ert-x-tests.el
+++ b/test/lisp/emacs-lisp/ert-x-tests.el
@@ -275,6 +275,49 @@ 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
--
2.7.0.rc3.207.g0ac5344
next prev parent reply other threads:[~2016-02-11 15:02 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-11 14:50 bug#22632: [PATCH 0/4] `ert-with-function-mocked' and refactoring `message-strip-subject-trailing-was' Michal Nazarewicz
2016-02-11 15:02 ` Michal Nazarewicz [this message]
2016-02-11 15:02 ` bug#22632: [PATCH 2/4] Make use of the `ert-with-function-mocked' macro Michal Nazarewicz
2016-02-11 15:02 ` bug#22632: [PATCH 3/4] Add test for `message-strip-subject-trailing-was' Michal Nazarewicz
2016-02-11 15:02 ` bug#22632: [PATCH 4/4] Refactor `message-strip-subject-trailing-was' function Michal Nazarewicz
2016-02-23 3:49 ` bug#22632: [PATCH 0/4] `ert-with-function-mocked' and refactoring `message-strip-subject-trailing-was' Lars Ingebrigtsen
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1455202955-25751-1-git-send-email-mina86@mina86.com \
--to=mina86@mina86.com \
--cc=22632@debbugs.gnu.org \
/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 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.