3

I am trying to write a test around a flow that uses transient. Here is the sample code:

;; -*- lexical-binding: t; coding: utf-8 -*-

(require 'transient)
(require 'buttercup)
(require 'with-simulated-input)

(defun ogt-print-a () (interactive) (insert "a"))
(defun ogt-print-b () (interactive) (insert "b"))

(transient-define-prefix ogt-transient ()
  ["X"
   ("f" "test f" ogt-print-a)
   ("g" "test g" ogt-print-b)])

(describe
 "Transient"
 (it "takes keys"
     (with-temp-buffer
       (with-simulated-input "f"
         (ogt-transient)
         (expect (buffer-string)
                 :to-match "a")))))

Now, I would expect the library "with-simulated-input" to press "f" when the transient comes up and to have that put "a" in the temp buffer, as that's what the function called should do.

However, it does not add anything, and the temp buffer remains empty.

I could use some help, I really would like to know for a fact that my package works.

I wondered if with-temp-buffer could be the culprit but changing this to a set-buffer call did not seem to help.

Trevoke
  • 2,375
  • 21
  • 34

1 Answers1

2

Author of with-simulated-input here. This happens because with-simulated-input is expecting BODY to read input, and ogt-transient doesn't read input. It only sets up the appropriate key bindings to implement the transient menu and then returns, letting Emacs handle those key bindings as normal. However, when it returns, with-simulated-input assumes it is "done" and continues evaluating BODY. In this case, since ogt-transient sets up the appropriate bindings and returns without blocking on input, you don't actually need with-simulated-input at all. You can simply follow it by the appropriate execute-kbd-macro form, e.g.

(progn 
  (ogt-transient)
  (execute-kbd-macro "f"))

For more details, see the issue here.

Ryan C. Thompson
  • 435
  • 2
  • 10