branch: scratch/el-mock
commit 18cee97bf63ff2c2c5e63839f4709d1d0029d61e
Author: Stefan Monnier <[email protected]>
Commit: Stefan Monnier <[email protected]>
Don't expose the `-*-functions` vars in the macroexpanded code
Set those vars from the `mock--stub-setup` and `mock/setup` functions
instead. This has the benefit of simplifying the macroexpanded code
a bit, but it also causes the `mock` and `not-called` functions
to be added to `-stubbed-functions` which was not the case any more.
This has the advantage that we don't need to call
(mapc #'mock/teardown -mocked-functions) explicitly any more.
It has the subtle consequence of removing the mocks and not-called
*before* calling `mock-verify` rather than afterwards rather than
afterwards, but AFAICT, `mock-verify` does not need those mocks/not-called
to be still active, so it seems to be a safe change.
* el-mock.el (mock--stub-setup): Add func to `-stubbed-functions`.
(stub): Don't add func to `-stubbed-functions` any more
since it's done by `mock--stub-setup` now.
(mock/setup): Add func to `-mocked-functions`.
(mock): Don't add func to `-mocked-functions` any more since it's done
by `mock/setup` now.
(not-called): Don't add func to `-mocked-functions` any more since it
was useless anyway.
(mock/teardown): Delete (now) unused function.
---
el-mock.el | 26 +++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/el-mock.el b/el-mock.el
index 93b521ca5f..5e427e7473 100644
--- a/el-mock.el
+++ b/el-mock.el
@@ -72,6 +72,7 @@
(lambda ()
(when (fboundp funcsym)
(put funcsym 'mock-original-func (symbol-function funcsym)))
+ (cl-pushnew funcsym -stubbed-functions)
(fset funcsym function))))
(defun stub/setup (funcsym value)
@@ -81,6 +82,8 @@
(mock--stub-setup funcsym `(lambda (&rest x) ,value)))
(defun stub/teardown (funcsym)
+ ;; FIXME: Better not call this function by accident since it will
+ ;; merrily undefine the function you pass it :-(
(mock-suppress-redefinition-message
(lambda ()
(let ((func (get funcsym 'mock-original-func)))
@@ -94,6 +97,7 @@
(defun mock/setup (func-spec value times)
(let ((funcsym (car func-spec)))
(put funcsym 'mock-call-count 0)
+ (cl-pushnew funcsym -mocked-functions)
(mock--stub-setup funcsym
`(lambda (&rest actual-args)
(cl-incf (get ',funcsym 'mock-call-count))
@@ -108,8 +112,6 @@
(mock--stub-setup funcsym (lambda (&rest _actual-args)
(signal 'mock-error '(called)))))
-(defalias 'mock/teardown #'stub/teardown)
-
;;;; mock verify
(define-error 'mock-error "Mock error")
(defun mock-verify ()
@@ -155,11 +157,12 @@ wrap test method around this function."
(prog1
(funcall body-fn)
(setq any-error nil))
- (mapc #'stub/teardown -stubbed-functions)
- (unwind-protect
- (unless any-error
- (mock-verify))
- (mapc #'mock/teardown -mocked-functions)))))
+ ;; FIXME: `delete-dups' is for backward compatibility with `.elc'
+ ;; compiled with an old version of `el-mock' since those did
+ ;; (push ',function -stubbed-functions) after `stub/setup'.
+ (mapc #'stub/teardown (delete-dups -stubbed-functions))
+ (unless any-error
+ (mock-verify)))))
;;;; message hack
(defun mock-suppress-redefinition-message (func)
@@ -209,8 +212,7 @@ Example:
(t (signal 'mock-syntax-error '("Use `(stub FUNC)' or
`(stub FUNC => RETURN-VALUE)'"))))))
`(if (not in-mocking)
(error "Do not use `stub' outside")
- (mock--stub-setup ',function (lambda (&rest _) ,value))
- (push ',function -stubbed-functions))))
+ (mock--stub-setup ',function (lambda (&rest _) ,value)))))
(defmacro mock (func-spec &rest rest)
"Create a mock for function described by FUNC-SPEC.
@@ -255,8 +257,7 @@ Example:
((not times) (signal 'mock-syntax-error '("Use `(mock
FUNC-SPEC)' or `(mock FUNC-SPEC => RETURN-VALUE)'"))))))
`(if (not in-mocking)
(error "Do not use `mock' outside")
- (mock/setup ',func-spec ',value ,times)
- (push ',(car func-spec) -mocked-functions))))
+ (mock/setup ',func-spec ',value ,times))))
(defmacro not-called (function)
"Create a not-called mock for FUNCTION.
@@ -280,8 +281,7 @@ Example:
`(if (not in-mocking)
(error "Do not use `not-called' outside")
(mock--stub-setup ',function
- (lambda (&rest _) (signal 'mock-error '(called))))
- (push ',function -mocked-functions))))
+ (lambda (&rest _) (signal 'mock-error '(called)))))))
(defun mock-parse-spec (spec)