I'd like to patch some elisp functions inside my unit tests. I've tried cl-labels
and cl-flet
, but neither achieve the result I want:
(defun return-number ()
1)
(defun calls-return-number ()
(return-number))
(defun patch-cl-labels ()
(cl-labels ((return-number () 2))
(calls-return-number)))
(message "After patching: %s" (patch-cl-labels))
I've looked at monkey-patching a function (but this permanently changes behaviour) and patching a third-party function (this uses advice and holds on to the old function).
return-number
to return 2.return-number
to return 2" then(defun return-number () 2)
achieves that. And as for temporary, just save the original definition (seesymbol-function
) and later restore it (seefset
ordefalias
). I sense that you want something more or different, but you don't specify what that is.