diff options
| author | Michal Nazarewicz | 2016-02-23 14:44:56 +1100 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2016-02-23 14:44:56 +1100 |
| commit | 6f0498bf290cbe066036f8bfe0d09ad99ddaab03 (patch) | |
| tree | 391e4f25adbbe7d88e1f968bbef54ddeb1dcda84 /lisp | |
| parent | 96e32bbb736ec6e0a7278ae864098c7c812b05a4 (diff) | |
| download | emacs-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 'lisp')
| -rw-r--r-- | lisp/emacs-lisp/ert-x.el | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el index 2a2418fa7d2..eb10c845d3f 100644 --- a/lisp/emacs-lisp/ert-x.el +++ b/lisp/emacs-lisp/ert-x.el | |||
| @@ -285,6 +285,46 @@ 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 | |||
| 291 | Once BODY finishes (be it normally by returning a value or | ||
| 292 | abnormally by throwing or signalling), the old definition of | ||
| 293 | function NAME is restored. | ||
| 294 | |||
| 295 | BODY may further change the mock with `fset'. | ||
| 296 | |||
| 297 | If MOCK is nil, the function NAME is mocked with a function | ||
| 298 | `ert-fail'ing when called. | ||
| 299 | |||
| 300 | For 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 | |||
| 288 | (provide 'ert-x) | 328 | (provide 'ert-x) |
| 289 | 329 | ||
| 290 | ;;; ert-x.el ends here | 330 | ;;; ert-x.el ends here |