]> git.eshelyaron.com Git - emacs.git/commitdiff
Add an ERT explainer for 'time-equal-p'.
authorPhilipp Stephani <p.stephani2@gmail.com>
Mon, 24 Mar 2025 23:12:20 +0000 (00:12 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 25 Mar 2025 18:19:42 +0000 (19:19 +0100)
* lisp/emacs-lisp/ert.el (ert--explain-time-equal-p): New explainer
function.

* test/lisp/emacs-lisp/ert-tests.el (ert-test-explain-time-equal-p): New
test.

(cherry picked from commit 8be7e98557df8ba708b3f7e285a29f279b609e46)

lisp/emacs-lisp/ert.el
test/lisp/emacs-lisp/ert-tests.el

index ebad69f301dffa5bc2c61e44dda99f516e523727..f2d0a175b55ef594d97fb85eff15e335da4dbdbc 100644 (file)
@@ -662,6 +662,19 @@ Return nil if they are."
 (put 'equal-including-properties 'ert-explainer
      'ert--explain-equal-including-properties)
 
+(defun ert--explain-time-equal-p (a b)
+  "Explainer function for `time-equal-p'.
+A and B are the time values to compare."
+  (declare (ftype (function (t t) list))
+           (side-effect-free t))
+  (unless (time-equal-p a b)
+    `(different-time-values
+      ,(format-time-string "%F %T.%N %Z" a t)
+      ,(format-time-string "%F %T.%N %Z" b t)
+      difference
+      ,(format-time-string "%s.%N" (time-subtract a b) t))))
+(function-put #'time-equal-p 'ert-explainer #'ert--explain-time-equal-p)
+
 ;;; Implementation of `ert-info'.
 
 ;; TODO(ohler): The name `info' clashes with
index c729028279af96ecad25c00bb9972a23e02a6769..32066d42154fa6ea1f9b6ae00f539ea8e3c39cef 100644 (file)
@@ -792,6 +792,14 @@ This macro is used to test if macroexpansion in `should' works."
                  '(char 1 "o" (different-properties-for-key a (different-atoms b foo))
                         context-before "f" context-after "o"))))
 
+(ert-deftest ert-test-explain-time-equal-p ()
+  (should-not (ert--explain-time-equal-p 123 '(0 123 0 0)))
+  (should (equal (ert--explain-time-equal-p 123 '(0 120 0 0))
+                 '(different-time-values
+                   "1970-01-01 00:02:03.000000000 UTC"
+                   "1970-01-01 00:02:00.000000000 UTC"
+                   difference "3.000000000"))))
+
 (ert-deftest ert-test-stats-set-test-and-result ()
   (let* ((test-1 (make-ert-test :name 'test-1
                                 :body (lambda () nil)))