branch: elpa/with-simulated-input
commit ea6735cf1b4c55d4eac165059fba662086aeca6e
Author: Nikita Bloshchanevich <[email protected]>
Commit: Nikita Bloshchanevich <[email protected]>
KEYS: support a single variable
`with-simulated-input' now supports a symbol for KEYS, handled like a single
string. The variable will be checked to be a string before being passed on
to
`with-simulated-input-1' (lambdas would be silently called).
---
tests/test-with-simulated-input.el | 10 ++++++++++
with-simulated-input.el | 24 ++++++++++++++++--------
2 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/tests/test-with-simulated-input.el
b/tests/test-with-simulated-input.el
index 058df890ed..b175de53a9 100644
--- a/tests/test-with-simulated-input.el
+++ b/tests/test-with-simulated-input.el
@@ -122,6 +122,16 @@
(read-string "Say hello: "))
:to-equal "hello world")))
+ (it "should allow a variable for KEYS"
+ (let ((keys "hello RET"))
+ (expect (with-simulated-input keys (read-string "Say hello: "))
+ :to-equal "hello")))
+
+ (it "should error for non-string variable KEYS"
+ (let ((keys (lambda () (insert "X"))))
+ (expect (with-simulated-input keys (read-string "Input: "))
+ :to-throw)))
+
(it "should allow lisp forms to throw errors"
(expect
diff --git a/with-simulated-input.el b/with-simulated-input.el
index 608c5950dd..afa7863529 100644
--- a/with-simulated-input.el
+++ b/with-simulated-input.el
@@ -176,15 +176,23 @@ are propagated normally.
The return value is the last form in BODY, as if it was wrapped
in `progn'."
(declare (indent 1) (debug ([&or ("quote" (&rest &or stringp def-form))
- (&rest &or stringp def-form)]
+ (&rest &or stringp def-form)
+ stringp symbolp]
def-body)))
- (pcase keys
- (`(quote ,x) (setq keys x))
- ((guard (not (listp keys))) (cl-callf list keys)))
- `(with-simulated-input-1
- (lambda ()
- ,@body)
- ,@(cl-loop for key in keys collect (if (stringp key) key `(lambda ()
,key)))))
+ (if (and (symbolp keys) keys)
+ `(progn
+ (cl-check-type ,keys string)
+ (with-simulated-input-1
+ (lambda ()
+ ,@body)
+ ,keys))
+ (pcase keys
+ (`(quote ,x) (setq keys x))
+ ((guard (not (listp keys))) (cl-callf list keys)))
+ `(with-simulated-input-1
+ (lambda ()
+ ,@body)
+ ,@(cl-loop for key in keys collect (if (stringp key) key `(lambda ()
,key))))))
(defvar wsi-simulated-idle-time nil
"The current simulated idle time.