aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-06-21 08:31:24 -0400
committerCraig Jennings <c@cjennings.net>2026-06-21 08:31:24 -0400
commit55c60f025e834c6bc60523542644ac0ec03d035c (patch)
treef8bce1933679fd46674996ef7da6a6ce68fb2f1e
parent021b5bb771bff834bc67d82fa61b870e175bdf71 (diff)
downloademacs-wttrin-55c60f025e834c6bc60523542644ac0ec03d035c.tar.gz
emacs-wttrin-55c60f025e834c6bc60523542644ac0ec03d035c.zip
test: extract message-capture helper into testutil
Five test files hand-rolled the same scaffolding to capture the echo-area message: a nil-initialized var, a cl-letf on message, and a lambda that stored the formatted string. Centralize it as testutil-wttrin-with-captured-message, which binds the var and captures the last message shown. Behavior is unchanged. The full suite stays green. Sites that mock message for a different reason are left alone: the ones that silence it with #'ignore, and the one that accumulates every message into a list.
-rw-r--r--tests/test-wttrin--display-weather.el14
-rw-r--r--tests/test-wttrin--handle-fetch-callback.el21
-rw-r--r--tests/test-wttrin-clear-cache.el11
-rw-r--r--tests/test-wttrin-query.el25
-rw-r--r--tests/test-wttrin-requery-force.el15
-rw-r--r--tests/testutil-wttrin.el14
6 files changed, 45 insertions, 55 deletions
diff --git a/tests/test-wttrin--display-weather.el b/tests/test-wttrin--display-weather.el
index bca6a6c..4cc4269 100644
--- a/tests/test-wttrin--display-weather.el
+++ b/tests/test-wttrin--display-weather.el
@@ -159,11 +159,8 @@ Empty string does not match ERROR pattern, so it's processed as data."
(test-wttrin--display-weather-setup)
(unwind-protect
(progn
- (let ((message-log-max t)
- (message-displayed nil))
- (cl-letf (((symbol-function 'message)
- (lambda (format-string &rest args)
- (setq message-displayed (apply #'format format-string args)))))
+ (let ((message-log-max t))
+ (testutil-wttrin-with-captured-message message-displayed
(wttrin--display-weather "InvalidCity" nil)
;; Should display error message
@@ -176,11 +173,8 @@ Empty string does not match ERROR pattern, so it's processed as data."
(test-wttrin--display-weather-setup)
(unwind-protect
(progn
- (let ((message-log-max t)
- (message-displayed nil))
- (cl-letf (((symbol-function 'message)
- (lambda (format-string &rest args)
- (setq message-displayed (apply #'format format-string args)))))
+ (let ((message-log-max t))
+ (testutil-wttrin-with-captured-message message-displayed
(wttrin--display-weather "BadLocation" testutil-wttrin-sample-error-response)
;; Should display error message
diff --git a/tests/test-wttrin--handle-fetch-callback.el b/tests/test-wttrin--handle-fetch-callback.el
index d4158ac..e50c61d 100644
--- a/tests/test-wttrin--handle-fetch-callback.el
+++ b/tests/test-wttrin--handle-fetch-callback.el
@@ -226,12 +226,9 @@
(ert-deftest test-wttrin--handle-fetch-callback-error-network-shows-message ()
"Network errors should show a specific message in the echo area,
not leave the user guessing."
- (let ((displayed-message nil))
+ (testutil-wttrin-with-captured-message displayed-message
(cl-letf (((symbol-function 'wttrin--extract-response-body)
- (lambda () nil))
- ((symbol-function 'message)
- (lambda (fmt &rest args)
- (setq displayed-message (apply #'format fmt args)))))
+ (lambda () nil)))
(wttrin--handle-fetch-callback
'(:error (error "Network unreachable"))
#'ignore)
@@ -240,14 +237,11 @@ not leave the user guessing."
(ert-deftest test-wttrin--handle-fetch-callback-error-http-404-shows-message ()
"HTTP 404 should tell the user the location wasn't found."
- (let ((displayed-message nil))
+ (testutil-wttrin-with-captured-message displayed-message
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () nil))
((symbol-function 'wttrin--extract-http-status)
- (lambda () 404))
- ((symbol-function 'message)
- (lambda (fmt &rest args)
- (setq displayed-message (apply #'format fmt args)))))
+ (lambda () 404)))
;; No :error in status — url-retrieve succeeded but server returned 404
(wttrin--handle-fetch-callback nil #'ignore)
(should displayed-message)
@@ -255,14 +249,11 @@ not leave the user guessing."
(ert-deftest test-wttrin--handle-fetch-callback-error-http-500-shows-message ()
"HTTP 500 should tell the user the weather service had an error."
- (let ((displayed-message nil))
+ (testutil-wttrin-with-captured-message displayed-message
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () nil))
((symbol-function 'wttrin--extract-http-status)
- (lambda () 500))
- ((symbol-function 'message)
- (lambda (fmt &rest args)
- (setq displayed-message (apply #'format fmt args)))))
+ (lambda () 500)))
(wttrin--handle-fetch-callback nil #'ignore)
(should displayed-message)
(should (string-match-p "service\\|server\\|500" (downcase displayed-message))))))
diff --git a/tests/test-wttrin-clear-cache.el b/tests/test-wttrin-clear-cache.el
index 8185c5f..784be4c 100644
--- a/tests/test-wttrin-clear-cache.el
+++ b/tests/test-wttrin-clear-cache.el
@@ -43,13 +43,10 @@
"User should be told the cache was cleared."
(test-wttrin-clear-cache-setup)
(unwind-protect
- (let ((displayed-message nil))
- (cl-letf (((symbol-function 'message)
- (lambda (fmt &rest args)
- (setq displayed-message (apply #'format fmt args)))))
- (wttrin-clear-cache)
- (should displayed-message)
- (should (string-match-p "cache cleared" displayed-message))))
+ (testutil-wttrin-with-captured-message displayed-message
+ (wttrin-clear-cache)
+ (should displayed-message)
+ (should (string-match-p "cache cleared" displayed-message)))
(test-wttrin-clear-cache-teardown)))
;;; Boundary Cases
diff --git a/tests/test-wttrin-query.el b/tests/test-wttrin-query.el
index 396507b..4c4de87 100644
--- a/tests/test-wttrin-query.el
+++ b/tests/test-wttrin-query.el
@@ -102,20 +102,17 @@
"When fetch returns nil, the user should see an error message, not a crash."
(test-wttrin-query-setup)
(unwind-protect
- (let ((saved-callback nil)
- (displayed-message nil))
- (cl-letf (((symbol-function 'wttrin--get-cached-or-fetch)
- (lambda (_location callback)
- (setq saved-callback callback)))
- ((symbol-function 'message)
- (lambda (fmt &rest args)
- (setq displayed-message (apply #'format fmt args)))))
- (wttrin-query "BadLocation")
- ;; Simulate fetch returning nil
- (funcall saved-callback nil)
- ;; Should have shown error message (from wttrin--display-weather validation)
- (should displayed-message)
- (should (string-match-p "Cannot retrieve" displayed-message))))
+ (let ((saved-callback nil))
+ (testutil-wttrin-with-captured-message displayed-message
+ (cl-letf (((symbol-function 'wttrin--get-cached-or-fetch)
+ (lambda (_location callback)
+ (setq saved-callback callback))))
+ (wttrin-query "BadLocation")
+ ;; Simulate fetch returning nil
+ (funcall saved-callback nil)
+ ;; Should have shown error message (from wttrin--display-weather validation)
+ (should displayed-message)
+ (should (string-match-p "Cannot retrieve" displayed-message)))))
(test-wttrin-query-teardown)))
(provide 'test-wttrin-query)
diff --git a/tests/test-wttrin-requery-force.el b/tests/test-wttrin-requery-force.el
index d572348..171166d 100644
--- a/tests/test-wttrin-requery-force.el
+++ b/tests/test-wttrin-requery-force.el
@@ -63,15 +63,12 @@
"When no current location is set, user should be told there's nothing to refresh."
(test-wttrin-requery-force-setup)
(unwind-protect
- (let ((displayed-message nil))
- (cl-letf (((symbol-function 'message)
- (lambda (fmt &rest args)
- (setq displayed-message (apply #'format fmt args)))))
- (with-current-buffer (get-buffer-create "*wttr.in*")
- ;; wttrin--current-location is nil (buffer-local default)
- (wttrin-requery-force)
- (should displayed-message)
- (should (string-match-p "No location" displayed-message)))))
+ (testutil-wttrin-with-captured-message displayed-message
+ (with-current-buffer (get-buffer-create "*wttr.in*")
+ ;; wttrin--current-location is nil (buffer-local default)
+ (wttrin-requery-force)
+ (should displayed-message)
+ (should (string-match-p "No location" displayed-message))))
(test-wttrin-requery-force-teardown)))
(ert-deftest test-wttrin-requery-force-boundary-force-flag-does-not-leak ()
diff --git a/tests/testutil-wttrin.el b/tests/testutil-wttrin.el
index 7c71a84..e4e2e4e 100644
--- a/tests/testutil-wttrin.el
+++ b/tests/testutil-wttrin.el
@@ -111,6 +111,20 @@
(funcall callback nil)))))
,@body))
+;;; Message Capture Helpers
+
+(defmacro testutil-wttrin-with-captured-message (msg-var &rest body)
+ "Run BODY with `message' captured into MSG-VAR.
+MSG-VAR starts nil and is set to each formatted message string as `message'
+is called, so after BODY it holds the last message shown (or nil if none).
+Other mocks BODY needs can be set in a nested `cl-letf'."
+ (declare (indent 1))
+ `(let ((,msg-var nil))
+ (cl-letf (((symbol-function 'message)
+ (lambda (fmt &rest args)
+ (setq ,msg-var (apply #'format fmt args)))))
+ ,@body)))
+
;;; Mode-line Cache Helpers
(defun testutil-wttrin-set-mode-line-cache (data &optional age-seconds)