aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichal Nazarewicz2016-06-07 22:32:59 +0200
committerMichal Nazarewicz2016-06-08 19:10:59 +0200
commit7715ee54b3588cfdef03b5d45aaf44b73b422ec6 (patch)
tree176ae2e58b9018e0b761aea2bd31e2161e6c0481
parent027e6fbfe472bad1fd0464e070bc782c7e3e776a (diff)
downloademacs-7715ee54b3588cfdef03b5d45aaf44b73b422ec6.tar.gz
emacs-7715ee54b3588cfdef03b5d45aaf44b73b422ec6.zip
Remove ‘ert-with-function-mocked’ macro in favour of ‘cl-letf’ macro
* 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.
-rw-r--r--etc/NEWS3
-rw-r--r--lisp/emacs-lisp/ert-x.el40
-rw-r--r--test/lisp/calendar/icalendar-tests.el3
-rw-r--r--test/lisp/emacs-lisp/ert-x-tests.el43
-rw-r--r--test/lisp/gnus/message-tests.el2
-rw-r--r--test/lisp/vc/vc-bzr-tests.el3
6 files changed, 3 insertions, 91 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 7f917212059..e2c99a10275 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -411,9 +411,6 @@ by setting 'autoload-timestamps' to nil.
411FIXME As an experiment, nil is the current default. 411FIXME As an experiment, nil is the current default.
412If no insurmountable problems before next release, it can stay that way. 412If no insurmountable problems before next release, it can stay that way.
413 413
414** 'ert-with-function-mocked' of 'ert-x package allows mocking of functions
415in unit tests.
416
417--- 414---
418** 'gnutls-boot' now takes a parameter :complete-negotiation that says 415** 'gnutls-boot' now takes a parameter :complete-negotiation that says
419that negotiation should complete even on non-blocking sockets. 416that 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 67cb102a67c..2a2418fa7d2 100644
--- a/lisp/emacs-lisp/ert-x.el
+++ b/lisp/emacs-lisp/ert-x.el
@@ -285,46 +285,6 @@ BUFFER defaults to current buffer. Does not modify BUFFER."
285 (kill-buffer clone))))))) 285 (kill-buffer clone)))))))
286 286
287 287
288(defmacro ert-with-function-mocked (name mock &rest body)
289 "Mocks function NAME with MOCK and run BODY.
290
291Once BODY finishes (be it normally by returning a value or
292abnormally by throwing or signaling), the old definition of
293function NAME is restored.
294
295BODY may further change the mock with `fset'.
296
297If MOCK is nil, the function NAME is mocked with a function
298`ert-fail'ing when called.
299
300For example:
301
302 ;; Regular use, function is mocked inside the BODY:
303 (should (eq 2 (+ 1 1)))
304 (ert-with-function-mocked ((+ (lambda (a b) (- a b))))
305 (should (eq 0 (+ 1 1))))
306 (should (eq 2 (+ 1 1)))
307
308 ;; Macro correctly recovers from a throw or signal:
309 (should
310 (catch 'done
311 (ert-with-function-mocked ((+ (lambda (a b) (- a b))))
312 (should (eq 0 (+ 1 1))))
313 (throw 'done t)))
314 (should (eq 2 (+ 1 1)))
315"
316 (declare (indent 2))
317 (let ((old-var (make-symbol "old-var"))
318 (mock-var (make-symbol "mock-var")))
319 `(let ((,old-var (symbol-function (quote ,name))) (,mock-var ,mock))
320 (fset (quote ,name)
321 (or ,mock-var (lambda (&rest _)
322 (ert-fail (concat "`" ,(symbol-name name)
323 "' unexpectedly called.")))))
324 (unwind-protect
325 (progn ,@body)
326 (fset (quote ,name) ,old-var)))))
327
328(provide 'ert-x) 288(provide 'ert-x)
329 289
330;;; ert-x.el ends here 290;;; ert-x.el ends here
diff --git a/test/lisp/calendar/icalendar-tests.el b/test/lisp/calendar/icalendar-tests.el
index 20d88349bbc..6db4222697e 100644
--- a/test/lisp/calendar/icalendar-tests.el
+++ b/test/lisp/calendar/icalendar-tests.el
@@ -32,7 +32,6 @@
32;;; Code: 32;;; Code:
33 33
34(require 'ert) 34(require 'ert)
35(require 'ert-x)
36(require 'icalendar) 35(require 'icalendar)
37 36
38;; ====================================================================== 37;; ======================================================================
@@ -64,7 +63,7 @@
64 (hash (format "%d" (abs (sxhash entry-full)))) 63 (hash (format "%d" (abs (sxhash entry-full))))
65 (contents "DTSTART:19640630T070100\nblahblah") 64 (contents "DTSTART:19640630T070100\nblahblah")
66 (username (or user-login-name "UNKNOWN_USER"))) 65 (username (or user-login-name "UNKNOWN_USER")))
67 (ert-with-function-mocked current-time (lambda () '(1 2 3)) 66 (cl-letf (((symbol-function 'current-time) (lambda () '(1 2 3))))
68 (should (= 77 icalendar--uid-count)) 67 (should (= 77 icalendar--uid-count))
69 (should (string= (concat "xxx-123-77-" hash "-" username "-19640630") 68 (should (string= (concat "xxx-123-77-" hash "-" username "-19640630")
70 (icalendar--create-uid entry-full contents))) 69 (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 a2665e7c390..ef8642aebfb 100644
--- a/test/lisp/emacs-lisp/ert-x-tests.el
+++ b/test/lisp/emacs-lisp/ert-x-tests.el
@@ -275,49 +275,6 @@ 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
321(provide 'ert-x-tests) 278(provide 'ert-x-tests)
322 279
323;;; ert-x-tests.el ends here 280;;; ert-x-tests.el ends here
diff --git a/test/lisp/gnus/message-tests.el b/test/lisp/gnus/message-tests.el
index ae34f24d741..13c15e33b27 100644
--- a/test/lisp/gnus/message-tests.el
+++ b/test/lisp/gnus/message-tests.el
@@ -57,7 +57,7 @@
57 57
58 58
59(ert-deftest message-strip-subject-trailing-was () 59(ert-deftest message-strip-subject-trailing-was ()
60 (ert-with-function-mocked message-talkative-question nil 60 (cl-letf (((symbol-function 'message-talkative-question) nil))
61 (with-temp-buffer 61 (with-temp-buffer
62 (let ((no-was "Re: Foo ") 62 (let ((no-was "Re: Foo ")
63 (with-was "Re: Foo \t (was: Bar ) ") 63 (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 98d176ca1ee..f27e6588cf2 100644
--- a/test/lisp/vc/vc-bzr-tests.el
+++ b/test/lisp/vc/vc-bzr-tests.el
@@ -25,7 +25,6 @@
25;;; Code: 25;;; Code:
26 26
27(require 'ert) 27(require 'ert)
28(require 'ert-x)
29(require 'vc-bzr) 28(require 'vc-bzr)
30(require 'vc-dir) 29(require 'vc-dir)
31 30
@@ -102,7 +101,7 @@
102 (while (vc-dir-busy) 101 (while (vc-dir-busy)
103 (sit-for 0.1)) 102 (sit-for 0.1))
104 (vc-dir-mark-all-files t) 103 (vc-dir-mark-all-files t)
105 (ert-with-function-mocked y-or-n-p (lambda (_) t) 104 (cl-letf (((symbol-function 'y-or-n-p) (lambda (_) t)))
106 (vc-next-action nil)) 105 (vc-next-action nil))
107 (should (get-buffer "*vc-log*"))) 106 (should (get-buffer "*vc-log*")))
108 (delete-directory homedir t)))) 107 (delete-directory homedir t))))