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

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

[nongnu] elpa/with-simulated-input 9c160b2d81 106/134: Add warnings for


From: ELPA Syncer
Subject: [nongnu] elpa/with-simulated-input 9c160b2d81 106/134: Add warnings for quoted list KEYS and nil/constant BODY
Date: Mon, 10 Jan 2022 23:00:10 -0500 (EST)

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

    Add warnings for quoted list KEYS and nil/constant BODY
    
    The former is deprecated and the latter is a likely the unintended
    result of a typo.
---
 tests/test-with-simulated-input.el | 55 ++++++++++++++++++++++----
 with-simulated-input.el            | 79 +++++++++++++++++++++++---------------
 2 files changed, 97 insertions(+), 37 deletions(-)

diff --git a/tests/test-with-simulated-input.el 
b/tests/test-with-simulated-input.el
index 8aada68dfe..6c3c5bd24f 100644
--- a/tests/test-with-simulated-input.el
+++ b/tests/test-with-simulated-input.el
@@ -26,6 +26,9 @@
 
 (describe "`with-simulated-input'"
 
+  (before-each
+    (spy-on 'display-warning :and-call-through))
+
   (describe "should work when KEYS"
 
     (it "is a literal string"
@@ -178,6 +181,31 @@
         (expect my-non-lexical-var
                 :to-be-truthy))))
 
+  (describe "should display a deprecation warning when KEYS"
+
+    ;; We need `eval' in these tests to ensure the macro is evalauted
+    ;; during the test, not while loading the file.
+    (it "is a quoted list of literal strings"
+      (eval '(with-simulated-input '("hello" "RET")
+               (read-string "Enter a string: ")))
+      (expect #'display-warning :to-have-been-called))
+
+    (it "is a quoted list of lisp forms"
+      (eval '(with-simulated-input '((insert "hello") (exit-minibuffer))
+               (read-string "Enter a string: ")))
+      (expect #'display-warning :to-have-been-called))
+
+    (it "is a quoted list of strings and lisp forms"
+      (eval
+       '(progn
+          (with-simulated-input '((insert "hello") "RET")
+            (read-string "Enter a string: "))
+          (with-simulated-input '("hello" (exit-minibuffer))
+            (read-string "Enter a string: "))
+          (with-simulated-input '("hello SPC" (insert "world") "RET")
+            (read-string "Enter a string: "))))
+      (expect #'display-warning :to-have-been-called-times 3)))
+
   (describe "should throw an error when KEYS"
 
     (it "is an invalid literal expression"
@@ -258,30 +286,30 @@
     (expect
      (with-simulated-input "hello"      ; No RET
        (read-string "Enter a string: "))
-     :to-throw 'error '("Reached end of simulated input while evaluating 
body")))
+     :to-throw 'error))
 
   (it "should throw an error if the input is empty and BODY reads input"
     (expect
      (with-simulated-input nil
        (read-string "Enter a string: "))
-     :to-throw 'error '("Reached end of simulated input while evaluating 
body"))
+     :to-throw 'error)
     (expect
      (with-simulated-input '()
        (read-string "Enter a string: "))
-     :to-throw 'error '("Reached end of simulated input while evaluating 
body"))
+     :to-throw 'error)
     (expect
      (with-simulated-input ()
        (read-string "Enter a string: "))
-     :to-throw 'error '("Reached end of simulated input while evaluating 
body"))
+     :to-throw 'error)
     (expect
      (with-simulated-input '(nil)
        (read-string "Enter a string: "))
-     :to-throw 'error '("Reached end of simulated input while evaluating 
body"))
+     :to-throw 'error)
     (let ((my-input nil))
       (expect
        (with-simulated-input my-input
          (read-string "Enter a string: "))
-       :to-throw 'error '("Reached end of simulated input while evaluating 
body"))))
+       :to-throw 'error)))
 
   (it "should not throw an error if the input is empty unless BODY reads input"
     (expect
@@ -325,6 +353,19 @@
              (read-string "Second word: ")))
      :to-equal '("hello" "world")))
 
+  (it "should allow an empty/constant BODY, with a warning"
+    ;; We need `eval' to ensure the macro is evalauted during the
+    ;; test, not while loading the file.
+    (eval
+     '(progn
+        (expect (with-simulated-input "Is SPC anybody SPC listening? RET")
+                :to-be nil)
+        (expect (with-simulated-input "Is SPC anybody SPC listening? RET" t)
+                :to-be t)
+        (expect (with-simulated-input "Is SPC anybody SPC listening? RET" 1 2 
3)
+                :to-equal 3)))
+    (expect #'display-warning :to-have-been-called-times 3))
+
   (describe "used with `completing-read'"
 
     :var (completing-read-function)
@@ -353,7 +394,7 @@
       (expect
        (with-simulated-input "bl TAB C-j"
          (completing-read "Choose: " my-collection nil t))
-       :to-throw 'error '("Reached end of simulated input while evaluating 
body"))))
+       :to-throw 'error)))
 
   (describe "should not reproduce past issues:"
     ;; https://github.com/DarwinAwardWinner/with-simulated-input/issues/4
diff --git a/with-simulated-input.el b/with-simulated-input.el
index b193ce9f58..ea94a0565a 100644
--- a/with-simulated-input.el
+++ b/with-simulated-input.el
@@ -44,6 +44,7 @@
 ;;
 ;;; Code:
 
+(require 'files)
 (require 'cl-lib)
 
 (cl-defun wsi-key-bound-p (key)
@@ -188,9 +189,18 @@ in `progn'."
                 ([&or functionp macrop] &rest form) ; arbitrary lisp function 
call
                 ]
            body)))
-  ;; TODO Warn on empty body
   ;; TODO Support integers (i.e. single characters) in KEYS
   (cond
+   ((null body)
+    (display-warning 'with-simulated-input
+                     "BODY is empty; KEYS will not be used")
+    nil)
+   ((not (cl-find-if-not #'hack-one-local-variable-constantp body))
+    (display-warning 'with-simulated-input
+                     "BODY consists of only constant expressions; KEYS will 
not be used")
+    ;; Since all the expressions are constant, there's no point in
+    ;; evaluating any of them except the last one.
+    (car (last body)))
    ((null keys)
     `(with-simulated-input-1
       (lambda ()
@@ -198,42 +208,51 @@ in `progn'."
       nil))
    ((and keys (symbolp keys))
     `(cond
-     ((null ,keys)
-      (with-simulated-input-1
-       (lambda ()
-         ,@body)
-       nil))
-     ((stringp ,keys)
-      (with-simulated-input-1
-       (lambda ()
-         ,@body)
-       ,keys))
-     ((listp ,keys)
-      (apply
-       #'with-simulated-input-1
-       (lambda ()
-         ,@body)
-       (cl-loop for key in ,keys collect (if (stringp key) key `(lambda () 
,key)))))
-     (t
-      (error "KEYS must be a string or list, not %s: %s = %S"
-             (type-of ,keys) ',keys ,keys))))
-   ((and (listp keys)
-         (not (eq (car keys) 'quote))
-         (or (functionp (car keys))
-             (macrop (car keys))))
-    `(let ((evaluated-keys (,@keys)))
-       (pcase evaluated-keys
-         (`(quote ,x) (setq evaluated-keys x))
-         ((guard (not (listp evaluated-keys))) (cl-callf list evaluated-keys)))
+      ((null ,keys)
+       (with-simulated-input-1
+        (lambda ()
+          ,@body)
+        nil))
+      ((stringp ,keys)
+       (with-simulated-input-1
+        (lambda ()
+          ,@body)
+        ,keys))
+      ((listp ,keys)
        (apply
         #'with-simulated-input-1
         (lambda ()
           ,@body)
-        (cl-loop for key in evaluated-keys collect (if (stringp key) key 
`(lambda () ,key))))))
+        (cl-loop for key in ,keys collect (if (stringp key) key `(lambda () 
,key)))))
+      (t
+       (error "KEYS must be a string or list, not %s: %s = %S"
+              (type-of ,keys) ',keys ,keys))))
+   ((and (listp keys)
+         (not (eq (car keys) 'quote))
+         (or (functionp (car keys))
+             (macrop (car keys))))
+    (let ((evaluated-keys-sym (make-symbol "evaluated-keys")))
+      `(let ((,evaluated-keys-sym (,@keys)))
+         (pcase ,evaluated-keys-sym
+           (`(quote ,x)
+            (prog1 (setq ,evaluated-keys-sym x)
+              (display-warning
+               'with-simulated-input
+               "Passing KEYS as a quoted list is deprecated.")))
+           ((guard (not (listp ,evaluated-keys-sym))) (cl-callf list 
,evaluated-keys-sym)))
+         (apply
+          #'with-simulated-input-1
+          (lambda ()
+            ,@body)
+          (cl-loop for key in ,evaluated-keys-sym collect (if (stringp key) 
key `(lambda () ,key)))))))
    (t
     ;; (message "Keys is something else: %S" keys)
     (pcase keys
-      (`(quote ,x) (setq keys x))
+      (`(quote ,x)
+       (prog1 (setq keys x)
+         (display-warning
+          'with-simulated-input
+          "Passing KEYS as a quoted list is deprecated.")))
       ((guard (not (listp keys))) (cl-callf list keys)))
     `(with-simulated-input-1
       (lambda ()



reply via email to

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