summaryrefslogtreecommitdiff
path: root/test/lisp/emacs-lisp
diff options
context:
space:
mode:
authorMichal Nazarewicz <mina86@mina86.com>2016-02-23 14:44:56 +1100
committerLars Ingebrigtsen <larsi@gnus.org>2016-02-23 14:44:56 +1100
commit6f0498bf290cbe066036f8bfe0d09ad99ddaab03 (patch)
tree391e4f25adbbe7d88e1f968bbef54ddeb1dcda84 /test/lisp/emacs-lisp
parent96e32bbb736ec6e0a7278ae864098c7c812b05a4 (diff)
downloademacs-6f0498bf290cbe066036f8bfe0d09ad99ddaab03.tar.gz
ert-with-function-mocked: New macro
* 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.
Diffstat (limited to 'test/lisp/emacs-lisp')
-rw-r--r--test/lisp/emacs-lisp/ert-x-tests.el43
1 files changed, 43 insertions, 0 deletions
diff --git a/test/lisp/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el
index ef8642aebfb..a2665e7c390 100644
--- a/test/lisp/emacs-lisp/ert-x-tests.el
+++ b/test/lisp/emacs-lisp/ert-x-tests.el
@@ -275,6 +275,49 @@ desired effect."
(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