[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/with-simulated-input ddd7bd71b1 011/134: Add "wsi-simulate
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/with-simulated-input ddd7bd71b1 011/134: Add "wsi-simulate-idle-time" and tests for it |
Date: |
Mon, 10 Jan 2022 22:59:59 -0500 (EST) |
branch: elpa/with-simulated-input
commit ddd7bd71b178435035e7339e785eb8f30ee11277
Author: Ryan C. Thompson <rct@thompsonclan.org>
Commit: Ryan C. Thompson <rct@thompsonclan.org>
Add "wsi-simulate-idle-time" and tests for it
Fixes #1.
---
tests/test-with-simulated-input.el | 44 ++++++++++++++++++++++++++++++++
with-simulated-input.el | 52 ++++++++++++++++++++++++++++++++++++++
2 files changed, 96 insertions(+)
diff --git a/tests/test-with-simulated-input.el
b/tests/test-with-simulated-input.el
index 6524e68a4a..273ccffc3d 100644
--- a/tests/test-with-simulated-input.el
+++ b/tests/test-with-simulated-input.el
@@ -90,4 +90,48 @@
(read-string "Enter a string: "))
:to-equal "hello world"))))
+(defun idle-canary ())
+(defvar timers-to-cancel nil)
+(defvar orig-timer--activate (symbol-function 'timer--activate))
+
+(describe "`wsi-simulate-idle-time'"
+ (spy-on 'idle-canary)
+ (spy-on 'timer--activate
+ :and-call-fake
+ (lambda (timer &rest args)
+ (push timer timers-to-cancel)
+ (apply orig-timer--activate timer args)))
+ (after-each
+ (mapcar #'cancel-timer timers-to-cancel)
+ (setq timers-to-cancel nil)
+ (spy-calls-reset 'idle-canary))
+ (it "should run idle timers"
+ (run-with-idle-timer 500 nil 'idle-canary)
+ (wsi-simulate-idle-time 501)
+ (expect 'idle-canary :to-have-been-called))
+ (it "should not run idle times with longer times"
+ (run-with-idle-timer 500 nil 'set 'idle-canary)
+ (wsi-simulate-idle-time 100)
+ (expect 'idle-canary :not :to-have-been-called))
+ (it "should run idle timers added by other idle timers"
+ (run-with-idle-timer
+ 100 nil 'run-with-idle-timer
+ 200 nil 'idle-canary)
+ (wsi-simulate-idle-time 500)
+ (expect 'idle-canary :to-have-been-called))
+ (it "should run idle timers added by other idle timers when the new timer is
in the past"
+ (run-with-idle-timer
+ 100 nil 'run-with-idle-timer
+ 50 nil 'idle-canary)
+ (wsi-simulate-idle-time 500)
+ (expect 'idle-canary :to-have-been-called))
+
+ (describe "used within `with-simulated-input'"
+ (it "should allow idle timers to trigger during simulated input"
+ (run-with-idle-timer 500 nil 'insert "world")
+ (expect
+ (with-simulated-input '("hello SPC" (wsi-simulate-idle-time 501) "RET")
+ (read-string "Enter a string: "))
+ :to-equal "hello world"))))
+
;;; test-with-simulated-input.el ends here
diff --git a/with-simulated-input.el b/with-simulated-input.el
index 83c352ff3e..ae3c07558b 100644
--- a/with-simulated-input.el
+++ b/with-simulated-input.el
@@ -208,6 +208,58 @@ in `progn'."
(error "Reached end of simulated input while evaluating body")
result)))
+(defvar wsi-simulated-idle-time nil)
+
+(defadvice current-idle-time (around simulat-idle-time activate)
+ "Return the faked value while simulating idle time.
+
+While executing `wsi-simulate-idle-time', this advice causes the
+simulated idle time to be returned instead of the real value."
+ (if wsi-simulated-idle-time
+ (setq ad-return-value
+ (when (> (float-time wsi-simulated-idle-time) 0)
+ (seconds-to-time wsi-simulated-idle-time)))
+ ad-do-it))
+
+(cl-defun wsi-simulate-idle-time (secs &optional actually-wait)
+ "Run all idle timers with delay less than SECS.
+
+This simulates resetting the idle time to zero and then being
+idle for SECS seconds. If ACTUALLY-WAIT is non-nil, this function
+will also wait for the specified amount of time before running
+each timers.
+
+While each timer is running, `current-idle-time' will be
+overridden to return the current simulated idle time."
+ (interactive
+ "nSeconds of idle time: \nP")
+ (cl-loop
+ with already-run-timers = nil
+ with stop-time = (float-time secs)
+ with wsi-simulated-idle-time = 0.0
+ ;; We have to search `timer-idle-list' from the beginning each time
+ ;; through the loop because each timer that runs might add more
+ ;; timers to the list, and picking up at the same list position
+ ;; would ignore those new timers.
+ for next-timer = (car (cl-member-if-not
+ (lambda (timer) (memq timer already-run-timers))
+ timer-idle-list))
+ while next-timer
+ for previous-idle-time = wsi-simulated-idle-time
+ maximize (float-time (timer--time next-timer))
+ into wsi-simulated-idle-time
+ when actually-wait
+ do (sleep-for (float-time (time-subtract wsi-simulated-idle-time
+ previous-idle-time)))
+ while (time-less-p wsi-simulated-idle-time stop-time)
+ when (not (timer--triggered next-timer))
+ do (timer-event-handler next-timer)
+ do (push next-timer already-run-timers)
+ finally do
+ (when actually-wait
+ (sleep-for (float-time (time-subtract stop-time
+ wsi-simulated-idle-time))))))
+
(provide 'with-simulated-input)
;;; with-simulated-input.el ends here
- [nongnu] elpa/with-simulated-input 39dbadff0b 120/134: Update the README to reflect current supported syntax, (continued)
- [nongnu] elpa/with-simulated-input 39dbadff0b 120/134: Update the README to reflect current supported syntax, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input e7a7a6c39d 109/134: Don't use "define-advice" in tests, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 334f9f244f 119/134: Implement support for characters in KEYS, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 00209c2be3 128/134: Test on 27.2 instead of 27.1, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 1006e93000 125/134: Add more tests to improve coverage, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 9c160b2d81 106/134: Add warnings for quoted list KEYS and nil/constant BODY, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 4eedb0eee8 126/134: Use an alternate method to disable eager macro-expansion for testing, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 9d082a372a 113/134: Be more selective about which file changes trigger a CI run, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 681397b90d 130/134: Also test variables containing characters, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 9ecf7c3f84 107/134: Improve code for detecting warnings in tests, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input ddd7bd71b1 011/134: Add "wsi-simulate-idle-time" and tests for it,
ELPA Syncer <=
- [nongnu] elpa/with-simulated-input 662055c0a5 029/134: Handle non-lexical contexts as well as lexical, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 7f37451dcf 019/134: Use closures instead of quoted expressions, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input e7e8829eee 020/134: Make SECS arg to wsi-simulated-idle-time optional, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 47aa40dbe1 021/134: Handle the case where SECS exactly equals a timer's time, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 5ce98adda7 022/134: Add test for SECS = nil and for repeated idle simulations, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input e860a39bba 024/134: Version 2.1, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input c57ba13c17 043/134: Disable Travis testing emacs snapshot because it hangs, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 9fc83ac95d 047/134: Put spies in before-each form, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 2917358875 054/134: Minor CI tweaks, ELPA Syncer, 2022/01/10
- [nongnu] elpa/with-simulated-input 7bbe0ff680 056/134: Make sure buttercup and undercover are loadable in "eldev emacs", ELPA Syncer, 2022/01/10