1

Is it possible to automate testing user input?

An example of this could be multiple editing operations, undo, saving, reverting the buffer etc. Then checking the buffer contents is what the test expects.

The user input could be keyboard shortcuts or (preferably) commands run as if they were bound to keys.

Hooks should run as would typically happen when running interactive commands, so the last-command, variable should be set, post-command-hook should run. etc.


To make this question concrete, is it possible to type 5 lines, undo 4, redo 2 times, then ensure 3 of the 5 lines were entered?

NickD
  • 27,023
  • 3
  • 23
  • 42
ideasman42
  • 8,375
  • 1
  • 28
  • 105
  • 1
    What's the question? Is it really whether it's possible? Too vague or too broad. Please ask a specific question that will solicit a specific answer. – Drew Feb 09 '20 at 17:36
  • Try `(execute-kbd-macro (kbd "..."))` for whatever arbitrary sequence of keys (in `kbd` syntax) is wanted for input. – phils Feb 10 '20 at 00:23

1 Answers1

3

I don't think there's a very satisfactory answer, but here's what I used today for testing the new undo-redo command I installed into master:

(defun simple-tests--exec (cmds)
  (dolist (cmd cmds)
    (setq last-command this-command)
    (setq this-command cmd)
    (run-hooks 'pre-command-hook)
    (command-execute cmd)
    (run-hooks 'post-command-hook)
    (undo-boundary)))

Another approach I used (in mule-cmds--test-universal-coding-system-argument):

(let ((enable-recursive-minibuffers t)
      (unread-command-events
       (append (kbd "C-x RET c u t f - 8 RET C-u C-u c a b RET") nil)))
  (read-string "prompt:")))))
Stefan
  • 26,154
  • 3
  • 46
  • 84