aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMichal Nazarewicz2016-02-23 14:44:56 +1100
committerLars Ingebrigtsen2016-02-23 14:44:56 +1100
commit6f0498bf290cbe066036f8bfe0d09ad99ddaab03 (patch)
tree391e4f25adbbe7d88e1f968bbef54ddeb1dcda84 /test
parent96e32bbb736ec6e0a7278ae864098c7c812b05a4 (diff)
downloademacs-6f0498bf290cbe066036f8bfe0d09ad99ddaab03.tar.gz
emacs-6f0498bf290cbe066036f8bfe0d09ad99ddaab03.zip
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')
-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."
275 (should (equal (c x) (lisp x)))))) 275 (should (equal (c x) (lisp x))))))
276 276
277 277
278(defun ert--dummy-id (a)
279 "Identity function. Used for tests only."
280 a)
281
282(ert-deftest ert-with-function-mocked ()
283 (let ((mock-id (lambda (_) 21)))
284 (should (eq 42 (ert--dummy-id 42)))
285
286 (ert-with-function-mocked ert--dummy-id nil
287 (fset 'ert--dummy-id mock-id)
288 (should (eq 21 (ert--dummy-id 42))))
289 (should (eq 42 (ert--dummy-id 42)))
290
291 (ert-with-function-mocked ert--dummy-id mock-id
292 (should (eq 21 (ert--dummy-id 42))))
293 (should (eq 42 (ert--dummy-id 42)))
294
295 (should
296 (catch 'exit
297 (ert-with-function-mocked ert--dummy-id mock-id
298 (should (eq 21 (ert--dummy-id 42))))
299 (throw 'exit t)))
300 (should (eq 42 (ert--dummy-id 42)))
301
302 (should
303 (string= "Foo"
304 (condition-case err
305 (progn
306 (ert-with-function-mocked ert--dummy-id mock-id
307 (should (eq 21 (ert--dummy-id 42))))
308 (user-error "Foo"))
309 (user-error (cadr err)))))
310 (should (eq 42 (ert--dummy-id 42)))
311
312 (should
313 (string= "`ert--dummy-id' unexpectedly called."
314 (condition-case err
315 (ert-with-function-mocked ert--dummy-id nil
316 (ert--dummy-id 42))
317 (ert-test-failed (cadr err)))))
318 (should (eq 42 (ert--dummy-id 42)))))
319
320
278(provide 'ert-x-tests) 321(provide 'ert-x-tests)
279 322
280;;; ert-x-tests.el ends here 323;;; ert-x-tests.el ends here