DarwinAwardWinner / with-simulated-input

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

strange when it's in a (with-current-buffer) expression. #4

Closed ssskkkky closed 4 years ago

ssskkkky commented 4 years ago

It seems like that it didn't change the buffer when it's in a (with-current buffer) expression try this:

(defun test()
  (interactive)
  (let ((file "~/test.org"))
    (setq org-agenda-buffer (if (file-exists-p file)
                                (org-get-agenda-file-buffer file)
                              (error "No such file %s" file)))
    (with-current-buffer org-agenda-buffer
      (with-simulated-input
          "a"
        (test2)))))

(defun test2()
  (and (read-char) (insert "test")))
DarwinAwardWinner commented 4 years ago

Can you give me an example that doesn't use an org agenda buffer? Unless you think the org agenda buffer is relevant?

DarwinAwardWinner commented 4 years ago

Also, can you describe what happens when you run the code, and how it is different from what you expected to happen?

ssskkkky commented 4 years ago
(defun test()
  (interactive)
  (let ((file "~/testt"))
    (setq test-buffer (if (file-exists-p file)
                          (get-file-buffer file)
                        (error "No such file %s" file)))
    (with-current-buffer test-buffer
      (with-simulated-input
          "a"
        (test2)))))

(defun test2()
  (and (read-char) (insert "test")))

When I call function test in a file buffer named abc or whatever, rather than insert the string "test" to the ~/testt file buffer which was expected , it insert the words in the current buffer.

DarwinAwardWinner commented 4 years ago

Ok, thank you. I'll take a look at this when I have the time.

DarwinAwardWinner commented 4 years ago

For my own reference, here is a more self-contained test case:

(with-temp-buffer 
  (list
   ;; Returns the temp buffer
   (current-buffer)
   (progn
     (with-simulated-input
         "a"
       (read-char))
     ;; Returns the buffer that was current when evaluation began
     (current-buffer))))
DarwinAwardWinner commented 4 years ago

Ok, it looks like this is a bug (or perhaps an intended behavior?) in execute-kbd-macro:

(with-temp-buffer 
  (list
   ;; Returns the temp buffer
   (current-buffer)
   (progn
     (execute-kbd-macro "hello")
     ;; Returns the buffer that was current when evaluation began
     (current-buffer))))
ssskkkky commented 4 years ago

don't know perhaps.

DarwinAwardWinner commented 4 years ago

I've fixed it with a workaround that sets the buffer correctly before executing BODY. Please let me know if the latest master works for you.

DarwinAwardWinner commented 4 years ago

And here's the root cause: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=37396#8

ssskkkky commented 4 years ago

It works. Thanks.