]> git.eshelyaron.com Git - emacs.git/commitdiff
time-stamp-tests: Hygienic macros
authorStephen Gildea <stepheng+git-config-global@gildea.com>
Sun, 1 Jun 2025 15:06:55 +0000 (08:06 -0700)
committerEshel Yaron <me@eshelyaron.com>
Sat, 7 Jun 2025 19:59:19 +0000 (21:59 +0200)
* test/lisp/time-stamp-test.el (with-time-stamp-test-time,
time-stamp-should-warn, formatz-should-equal): Use cl-with-gensyms.

(cherry picked from commit 3fa8c36f235d7a26e397f357dc7110dbf6650644)

test/lisp/time-stamp-tests.el

index c479806639cdb1ce2f23338ac62b4ef42169d822..2e85f63f44f9dc206ff7977e03e7f65dbf00ffa1 100644 (file)
 (defmacro with-time-stamp-test-time (reference-time &rest body)
   "Force `time-stamp' to use time REFERENCE-TIME while evaluating BODY."
   (declare (indent 1) (debug t))
-  `(cl-letf*
-         ((orig-time-stamp-string-fn (symbol-function 'time-stamp-string))
-         ((symbol-function 'time-stamp-string)
-          (lambda (ts-format)
-            (apply orig-time-stamp-string-fn ts-format ,reference-time nil))))
-     ,@body))
+  (cl-with-gensyms (g-orig-time-stamp-string-fn)
+    `(cl-letf*
+         ((,g-orig-time-stamp-string-fn (symbol-function 'time-stamp-string))
+          ((symbol-function 'time-stamp-string)
+           (lambda (ts-format)
+             (funcall ,g-orig-time-stamp-string-fn ts-format ,reference-time))))
+       ,@body)))
 
 (defmacro with-time-stamp-system-name (name &rest body)
   "Force function `system-name' to return NAME while evaluating BODY."
 (defmacro time-stamp-should-warn (form)
   "Similar to `should' and also verify that FORM generates a format warning."
   (declare (debug t))
-  `(let ((warning-count 0))
-     (cl-letf (((symbol-function 'time-stamp-conv-warn)
-                (lambda (_old _new &optional _newer)
-                  (setq warning-count (1+ warning-count)))))
-       (should ,form)
-       (unless (= warning-count 1)
-         (ert-fail (format "Should have warned about format: %S" ',form))))))
+  (cl-with-gensyms (g-warning-count)
+    `(let ((,g-warning-count 0))
+       (cl-letf (((symbol-function 'time-stamp-conv-warn)
+                  (lambda (_old _new &optional _newer)
+                    (incf ,g-warning-count))))
+         (should ,form)
+         (unless (= ,g-warning-count 1)
+           (ert-fail (format "Should have warned about format: %S" ',form)))))))
 
 ;;; Tests:
 
       (should (equal (time-stamp-string "%:a" ref-time1) Monday))
       ;; recommended 1997-2019, warned since 2024, will change
       (time-stamp-should-warn
-       (should (equal (time-stamp-string "%3A" ref-time1) MON)))
+       (equal (time-stamp-string "%3A" ref-time1) MON))
       (time-stamp-should-warn
-       (should (equal (time-stamp-string "%10A" ref-time1) p10-MONDAY)))
+       (equal (time-stamp-string "%10A" ref-time1) p10-MONDAY))
       ;; implemented since 2001, recommended since 2019
       (should (equal (time-stamp-string "%#a" ref-time1) MON))
       (should (equal (time-stamp-string "%#3a" ref-time1) MON))
       (should (equal (time-stamp-string "%:b" ref-time1) January))
       ;; recommended 1997-2019, warned since 2024, will change
       (time-stamp-should-warn
-       (should (equal (time-stamp-string "%3B" ref-time1) JAN)))
+       (equal (time-stamp-string "%3B" ref-time1) JAN))
       (time-stamp-should-warn
-       (should (equal (time-stamp-string "%10B" ref-time1) p10-JANUARY)))
+       (equal (time-stamp-string "%10B" ref-time1) p10-JANUARY))
       ;; implemented since 2001, recommended since 2019
       (should (equal (time-stamp-string "%#b" ref-time1) JAN))
       (should (equal (time-stamp-string "%#3b" ref-time1) JAN))
     (should (equal (time-stamp-string "%02y" ref-time2) "16"))
     ;; recommended 1997-2019, warned since 2024
     (time-stamp-should-warn
-     (should (equal (time-stamp-string "%:y" ref-time1) "2006")))
+     (equal (time-stamp-string "%:y" ref-time1) "2006"))
     (time-stamp-should-warn
-     (should (equal (time-stamp-string "%:y" ref-time2) "2016")))
+     (equal (time-stamp-string "%:y" ref-time2) "2016"))
     ;; %-y and %_y warned 1997-2019, changed in 2019
     ;; (We don't expect these forms to be useful,
     ;; but we test here so that we can confidently state that
                        "test-system-name.example.org")))
       ;; recommended 1997-2019, warned since 2024
       (time-stamp-should-warn
-       (should (equal (time-stamp-string "%s" ref-time1)
-                      "test-system-name.example.org")))
+       (equal (time-stamp-string "%s" ref-time1)
+              "test-system-name.example.org"))
       (time-stamp-should-warn
-       (should (equal (time-stamp-string "%U" ref-time1) "100%d Tester")))
+       (equal (time-stamp-string "%U" ref-time1) "100%d Tester"))
       (time-stamp-should-warn
-       (should (equal (time-stamp-string "%u" ref-time1) "test-logname")))
+       (equal (time-stamp-string "%u" ref-time1) "test-logname"))
       ;; implemented since 2001, recommended since 2019
       (should (equal (time-stamp-string "%L" ref-time1) "100%d Tester"))
       (should (equal (time-stamp-string "%l" ref-time1) "test-logname"))
@@ -964,10 +966,11 @@ The interval arguments H M and S are all non-negative."
 Use the free variables `form-string' and `pattern-mod'.
 The functions in `pattern-mod' are composed left to right."
   (declare (debug t))
-  `(let ((result ,expect))
-     (dolist (fn pattern-mod)
-       (setq result (funcall fn result)))
-     (should (equal (formatz form-string ,zone) result))))
+  (cl-with-gensyms (g-result g-fn)
+    `(let ((,g-result ,expect))
+       (dolist (,g-fn pattern-mod)
+         (setq ,g-result (funcall ,g-fn ,g-result)))
+       (should (equal (formatz form-string ,zone) ,g-result)))))
 
 ;; These test cases have zeros in all places (first, last, none, both)
 ;; for hours, minutes, and seconds.
@@ -1318,4 +1321,14 @@ the other expected results for hours greater than 99 with non-zero seconds."
   (should (equal "" (formatz "%#z" 0)))
   )
 
+
+;;; Repeat the indent properties declared by the macros above,
+;;; so that we can indent code before we eval this buffer.
+;; Local variables:
+;; eval: (put 'with-time-stamp-test-env 'lisp-indent-function 0)
+;; eval: (put 'with-time-stamp-test-time 'lisp-indent-function 1)
+;; eval: (put 'with-time-stamp-system-name 'lisp-indent-function 1)
+;; eval: (put 'define-formatz-tests 'lisp-indent-function 1)
+;; End:
+
 ;;; time-stamp-tests.el ends here