DarwinAwardWinner / with-simulated-input

Test your interactive elisp functions non-interactively!
GNU General Public License v3.0
38 stars 4 forks source link

Document how to work when inside `with-temp-buffer` #11

Closed alhassy closed 3 years ago

alhassy commented 3 years ago

I expected the following test to show a string containing the word hello enclosed in a src block tagged emacs-lisp, but instead it errors out.

 (with-temp-buffer
   (insert "hello")
   (goto-char (point-min))
   (set-mark-command nil)
   (goto-char (point-max))
   (with-simulated-input
       "emacs-lisp"
     (org-babel-demarcate-block))
   (buffer-string))

In case the error is something to do with org-babel-demarcate-block, I've also tried with a home-backed function what-do and we obtain the same error.

 (defun what-do ()
   "Alter a region of text; if no selected text, do nothing.
 Action to be done is selected from a menu."
   (interactive)
   (when (use-region-p)
     (let ((it (intern (completing-read "What do? " '(upcase downcase capitalize))))
           (txt (buffer-substring-no-properties (region-beginning) (region-end))))
       (delete-region (region-beginning) (region-end))
       (insert (funcall it txt)))))

 (with-temp-buffer
   (insert "hello")
   (mark-whole-buffer)
   (with-simulated-input
       "upcase"
     (what-do))
   (buffer-string))

This is related to issue #4.

The tests show that the following should pass:

(it "should work inside code that switches buffer (issue #4)"
  (let ((orig-current-buffer (current-buffer)))
    (with-temp-buffer
      (let ((temp-buffer (current-buffer)))
        (with-simulated-input "a" (read-char))
        (expect (current-buffer) :to-equal temp-buffer)
        (expect (current-buffer) :not :to-equal orig-current-buffer)))))

But I could not get this to work for my uses cases above.

Since this issue of buffers has arisen multiple times, #4 and #11, it would seem prudent to document how to use with-simulated-input with different buffers in the README.

DarwinAwardWinner commented 3 years ago

I don't think this has anything to do with buffer switching. When I run these pieces of code, I get the error "Reached end of simulated input while evaluating body", which is the expected result because your input doesn't include RET at the end, so the function never returns. The following works fine and returns "HELLO":

(with-temp-buffer
  (insert "hello")
  (mark-whole-buffer)
  (with-simulated-input
      "upcase RET"
    (what-do))
  (buffer-string))
alhassy commented 3 years ago

Thanks for pointing that out; however, your code, with the RET, fails for me with the same error.


I'm using

DarwinAwardWinner commented 3 years ago

It looks like you posted an empty code block. For me, adding RET to the input sequence of either of the code examples above fixes them.

DarwinAwardWinner commented 3 years ago

There hasn't been a reply on this for over a year, so I'm closing it. If it's still a problem, feel free to provide additional information and reopen it.