emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/with-simulated-input 5a67067ae0 002/134: Initial version


From: ELPA Syncer
Subject: [nongnu] elpa/with-simulated-input 5a67067ae0 002/134: Initial version
Date: Mon, 10 Jan 2022 22:59:57 -0500 (EST)

branch: elpa/with-simulated-input
commit 5a67067ae073d71cb1b39afd5edf54c07a5e3108
Author: Ryan C. Thompson <rct@thompsonclan.org>
Commit: Ryan C. Thompson <rct@thompsonclan.org>

    Initial version
---
 .gitignore                        |   1 +
 .travis.yml                       |  37 +++++++++++++
 Cask                              |  13 +++++
 setversion.sh                     |  14 +++++
 test/test-helper.el               |  40 +++++++++++++
 test/with-simulated-input-test.el |  30 ++++++++++
 with-simulated-input.el           | 114 ++++++++++++++++++++++++++++++++++++++
 7 files changed, 249 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000..176df11369
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/.cask
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000000..2ba30e4260
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,37 @@
+language: generic
+
+env:
+  matrix:
+    - EMACS_VERSION=emacs-23.4
+    - EMACS_VERSION=emacs-24.1
+    - EMACS_VERSION=emacs-24.2
+    - EMACS_VERSION=emacs-24.3
+    - EMACS_VERSION=emacs-24.4
+    - EMACS_VERSION=emacs-24.5
+    - EMACS_VERSION=emacs-25.1
+    - EMACS_VERSION=emacs-25.2
+    - EMACS_VERSION=emacs-git-snapshot
+    - EMACS_VERSION=remacs-git-snapshot
+
+matrix:
+  allow_failures:
+  - env: EMACS_VERSION=remacs-git-snapshot
+
+before_script:
+  # Install evm
+  - git clone https://github.com/rejeep/evm.git ~/.evm
+  - export PATH="$HOME/.evm/bin:$PATH"
+  - evm config path /tmp
+  - evm list
+  # use this version of emacs for tests
+  - evm install "${EMACS_VERSION}-travis" --use --skip
+  - evm list
+  - emacs --version
+  # Install cask
+  - curl -fsSkL https://raw.github.com/cask/cask/master/go | python
+  - export PATH="$HOME/.cask/bin:$PATH"
+  # Install elisp dependencies
+  - cask install
+
+script:
+  cask exec ert-runner
diff --git a/Cask b/Cask
new file mode 100644
index 0000000000..67c25d8267
--- /dev/null
+++ b/Cask
@@ -0,0 +1,13 @@
+;; -*- mode: emacs-lisp -*-
+
+(source gnu)
+(source melpa)
+
+(package "with-simulated-input" "1.0"
+         "A macro to simulate user input non-interactively.")
+
+(depends-on "seq")
+
+(development
+ (depends-on "f")
+ (depends-on "ert-runner"))
diff --git a/setversion.sh b/setversion.sh
new file mode 100755
index 0000000000..b87bd9d5e8
--- /dev/null
+++ b/setversion.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+PKG_NAME="with-simulated-input"
+
+TARGET_VERSION="$1"
+if [ -n "$TARGET_VERSION" ]; then
+    echo "Updating version to $TARGET_VERSION";
+    perl -i'orig_*' -lape "s/Version: [0-9.]+/Version: $TARGET_VERSION/g;" \
+         -e 
"s/((?:defconst|defvar|setq).*-version\s+)\"[0-9.]+\"/\${1}\"$TARGET_VERSION\"/g;"
 \
+         -e "s/\(package \"${PKG_NAME}\" \"[0-9.]+\"/(package \"${PKG_NAME}\" 
\"${TARGET_VERSION}\"/g" \
+         *.el Cask
+else
+    echo "Usage: $0 VERSION_NUMBER"
+fi
diff --git a/test/test-helper.el b/test/test-helper.el
new file mode 100644
index 0000000000..2cb7a30ccd
--- /dev/null
+++ b/test/test-helper.el
@@ -0,0 +1,40 @@
+;;; test-helper.el ---  -*- lexical-binding: t -*- 
+
+;; Copyright (C) 2015 Ryan C. Thompson
+
+;; Filename: test-helper.el
+;; Author: Ryan C. Thompson
+;; Created: Sat Nov 21 15:27:00 2015 (-0800)
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 
+;; This program is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or (at
+;; your option) any later version.
+;; 
+;; This program is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+;; 
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+;; 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 
+;;; Code:
+
+(require 'f)
+
+(defvar test-path
+  (f-dirname (f-this-file)))
+
+(defvar code-path
+  (f-parent test-path))
+
+(add-to-list 'load-path code-path)
+
+(require 'with-simulated-input)
+
+;;; test-helper.el ends here
diff --git a/test/with-simulated-input-test.el 
b/test/with-simulated-input-test.el
new file mode 100644
index 0000000000..be9d7a22e3
--- /dev/null
+++ b/test/with-simulated-input-test.el
@@ -0,0 +1,30 @@
+;;; -*- lexical-binding: t -*-
+
+(require 'with-simulated-input)
+(require 'ert)
+(require 'cl-lib)
+
+(ert-deftest simulate-input ()
+  "Tests for the basic functionality of the `with-simulated-input' macro."
+  ;; Basic string input
+  (should
+   (string= "hello"
+            (with-simulated-input "hello RET"
+              (read-string "Enter a string: "))))
+  ;; Error if RET is not pressed to finish the input
+  (should-error
+   (with-simulated-input "hello"
+     (read-string "Enter a string: ")))
+  ;; Can throw an error manually
+  (should-error
+   (with-simulated-input "(error SPC \"Manually SPC throwing SPC an SPC 
error\") RET"
+     (command-execute 'eval-expression)))
+  ;; Extra keys should not cause errors
+  (should
+   (string= "hello"
+            (with-simulated-input "hello RET M-x eval-expression (error SPC 
\"Manually SPC throwing SPC an SPC error\") RET"
+              (read-string "Enter a string: ")))))
+
+(provide 'with-simulated-input-test)
+
+;;; ido-ubiquitous-test.el ends here
diff --git a/with-simulated-input.el b/with-simulated-input.el
new file mode 100644
index 0000000000..de887c7683
--- /dev/null
+++ b/with-simulated-input.el
@@ -0,0 +1,114 @@
+;;; with-simulated-input.el --- A macro to simulate user input 
non-interactively -*- lexical-binding: t -*-
+
+;; Copyright (C) 2017 Ryan C. Thompson
+
+;; Filename: with-simulated-input.el
+;; Author: Ryan C. Thompson
+;; Created: Thu Jul 20 11:56:23 2017 (-0700)
+;; Version: 1.0
+;; Package-Requires: ((seq "0"))
+;; URL: 
+;; Keywords: 
+
+;; This file is NOT part of GNU Emacs.
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 
+;;; Commentary: 
+
+;; This package provides a single macro, `with-simulated-input', which
+;; evaluates one or more forms while simulating a sequence of input
+;; events for those forms to read. The result is the same as if you
+;; had evaluated the forms and then manually typed in the same input.
+;; This macro is useful for non-interactive testing of normally
+;; interactive commands and functions, such as `completing-read'.
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 
+;; This program is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or (at
+;; your option) any later version.
+;; 
+;; This program is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+;; 
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+;; 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 
+;;; Code:
+
+(require 'cl-lib)
+(require 'seq)
+
+(defmacro with-simulated-input (keys &rest body)
+  "Eval BODY forms with KEYS as simulated input.
+
+This macro is intended for automated testing of normally
+interactive functions by simulating input. If BODY tries to read
+user input (e.g. via `completing-read'), it will read input
+events from KEYS instead, as if the user had manually typed those
+keys after initiating evaluation of BODY.
+
+If BODY tries to read more input events than KEYS provides, an
+error is signalled. This is to ensure that BODY will never block
+waiting for input, since this macro is intended for
+non-interactive use. If BODY does not consume all the input
+events in KEYS, the remaining input events are discarded.
+
+The return value is the last form in BODY, as if it was wrapped
+in `progn'."
+  (declare (indent 1))
+  (let ((temp-cmd (cl-gensym "temp-cmd"))
+        (cmd-finished-tag (cl-gensym "cmd-finished"))
+        (canary-sym (cl-gensym "canary")))
+    `(cl-letf*
+         (;; Wrap BODY in a command that evaluates BODY and throws the
+          ;; result with `cmd-finished-tag'.
+          ((symbol-function ',temp-cmd)
+           (lambda ()
+             (interactive)
+             (throw ',cmd-finished-tag (progn ,@body))))
+          ;; Set up the keymap for invoking the temp command
+          (transient-map (make-sparse-keymap))
+          (command-invoke-key-sequence "C-c e")
+          (simulated-key-sequence ,keys)
+          (trailing-C-g-key-sequence
+           ;; We *really* want to trigger `keyboard-quit' if we reach
+           ;; the end of KEYS.
+           "C-g C-g C-g C-g C-g C-g C-g")
+          (full-key-sequence
+           (mapconcat #'identity
+                      (list
+                       command-invoke-key-sequence
+                       simulated-key-sequence
+                       trailing-C-g-key-sequence)
+                      " ")))
+       (when (seq-contains (kbd simulated-key-sequence) (elt (kbd "C-g") 0))
+         (error "KEYS must not include C-g"))
+       ;; Finish setting up the keymap for the temp command
+       (define-key transient-map (kbd command-invoke-key-sequence) ',temp-cmd)
+       (set-transient-map transient-map)
+       ;; Run the command followed by KEYS followed by C-g. The
+       ;; `catch' ensures that the keyboard macro stops executing as
+       ;; soon as BODY has finished evaluating, even if there are more
+       ;; keys to interpret.
+       (let ((result
+              (condition-case err
+                  (catch ',cmd-finished-tag
+                    (execute-kbd-macro (kbd full-key-sequence))
+                    ;; If the above doesn't throw, return the canary
+                    ',canary-sym)
+                ;; On `keyboard-quit', return canary
+                (quit ',canary-sym))))
+         (if (eq result ',canary-sym)
+             (error "Reached end of simulated input while evaluating body")
+           result)))))
+
+(provide 'with-simulated-input)
+
+;;; with-simulated-input.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]