Re: master e4df903f6b5 2/2: ; Fix last change

Michael Albinus <[email protected]> Fri, 31 Jul 2026 08:58:46 +0200
Newsgroups gmane.emacs.devel
Message-ID <[email protected]>
Richard Stallman <[email protected]> writes:

Hi Richard,

>   > Writing tests in *-tests.el. For example, if I want to test a function,
>   > which calls yes-or-no-p internally, I'll test both code paths like
>
>   > --8<---------------cut here---------------start------------->8---
>   > (cl-letf (((symbol-function #'yes-or-no-p) #'always))
>   >   ...)
>
>   > (cl-letf (((symbol-function #'yes-or-no-p) #'ignore))
>   >   ...)
>   > --8<---------------cut here---------------end--------------->8---
>
> This is interesting.
>
> Is there a short list of functions which it is useful for tests to
> force?  Or is that technique potentially useful for very many functions?
>
> I think it could be be cleaner, and help discourage uncontrolled uses
> of cl-letf, to define a macro that would be used like this:
>
> (testing-force-value (yes-or-no-p t)
>   ...tests...)
>
> The name of this macro, and other features useful for tests, could
> avoid suggesting using this in other clever ways that are not wise design.

I like this idea. We could add the following macro to ERT:

--8<---------------cut here---------------start------------->8---
(defmacro ert-with-forced-function-value (name value &rest body)
  "Bind function NAME to return always VALUE, and evaluate BODY."
  (declare (indent 2) (debug (functionp sexp body)))
  `(cl-letf (((symbol-function ',name) (lambda (&rest _) ,value)))
     ,@body))
--8<---------------cut here---------------end--------------->8---

OTOH, in the test/lisp subdirectory there are 432 hits when looking for
'(symbol-function'. Perhaps it's already too late ...

Best regards, Michael.