Further expand coverage of unit tests for time-stamp

* test/lisp/time-stamp-tests.el (time-stamp-custom-format-tabs-expand,
time-stamp-custom-end, time-stamp-helper-string-defaults): New tests.
(time-stamp-custom-count): Test 0 case.
(time-stamp-format-non-date-conversions): Test different system values.

Development of these new tests was guided by the "testcover" library.
This commit is contained in:
Stephen Gildea 2019-12-23 11:51:47 -08:00
parent e7edba42c8
commit 17c19817f7

View file

@ -38,9 +38,7 @@
(cl-letf (((symbol-function 'time-stamp-conv-warn)
(lambda (old-format _new)
(ert-fail
(format "Unexpected format warning for '%s'" old-format))))
((symbol-function 'system-name)
(lambda () "test-system-name.example.org")))
(format "Unexpected format warning for '%s'" old-format)))))
;; Not all reference times are used in all tests;
;; suppress the byte compiler's "unused" warning.
(list ref-time1 ref-time2 ref-time3)
@ -56,6 +54,13 @@
(apply orig-time-stamp-string-fn ts-format ,reference-time nil))))
,@body))
(defmacro with-time-stamp-system-name (name &rest body)
"Force (system-name) to return NAME while evaluating BODY."
(declare (indent defun))
`(cl-letf (((symbol-function 'system-name)
(lambda () ,name)))
,@body))
(defmacro time-stamp-should-warn (form)
"Similar to `should' but verifies that a format warning is generated."
`(let ((warning-count 0))
@ -170,6 +175,20 @@
;; triggering the tests above.
(time-stamp)))))))
(ert-deftest time-stamp-custom-format-tabs-expand ()
"Test that Tab characters expand in the format but not elsewhere."
(with-time-stamp-test-env
(let ((time-stamp-start "Updated in: <\t")
;; Tabs in the format should expand
(time-stamp-format "\t%Y\t")
(time-stamp-end "\t>"))
(with-time-stamp-test-time ref-time1
(with-temp-buffer
(insert "Updated in: <\t\t>")
(time-stamp)
(should (equal (buffer-string)
"Updated in: <\t 2006 \t>")))))))
(ert-deftest time-stamp-custom-inserts-lines ()
"Test that time-stamp inserts lines or not, as directed."
(with-time-stamp-test-env
@ -194,19 +213,46 @@
(time-stamp)
(should (equal (buffer-string) buffer-expected-2line)))))))
(ert-deftest time-stamp-custom-end ()
"Test that time-stamp finds the end pattern on the correct line."
(with-time-stamp-test-env
(let ((time-stamp-start "Updated on: <")
(time-stamp-format "%Y-%m-%d")
(time-stamp-end ">") ;changed later in the test
(buffer-original-contents "Updated on: <\n>\n")
(buffer-expected-time-stamped "Updated on: <2006-01-02\n>\n"))
(with-time-stamp-test-time ref-time1
(with-temp-buffer
(insert buffer-original-contents)
;; time-stamp-end is not on same line, should not be seen
(time-stamp)
(should (equal (buffer-string) buffer-original-contents))
;; add a newline to time-stamp-end, so it starts on same line
(setq time-stamp-end "\n>")
(time-stamp)
(should (equal (buffer-string) buffer-expected-time-stamped)))))))
(ert-deftest time-stamp-custom-count ()
"Test that time-stamp updates no more than time-stamp-count templates."
(with-time-stamp-test-env
(let ((time-stamp-start "TS: <")
(time-stamp-format "%Y-%m-%d")
(time-stamp-count 1) ;changed later in the test
(time-stamp-count 0) ;changed later in the test
(buffer-expected-once "TS: <2006-01-02>\nTS: <>")
(buffer-expected-twice "TS: <2006-01-02>\nTS: <2006-01-02>"))
(with-time-stamp-test-time ref-time1
(with-temp-buffer
(insert "TS: <>\nTS: <>")
(time-stamp)
;; even with count = 0, expect one time stamp
(should (equal (buffer-string) buffer-expected-once)))
(with-temp-buffer
(setq time-stamp-count 1)
(insert "TS: <>\nTS: <>")
(time-stamp)
(should (equal (buffer-string) buffer-expected-once))
(setq time-stamp-count 2)
(time-stamp)
(should (equal (buffer-string) buffer-expected-twice)))))))
@ -488,26 +534,35 @@
(ert-deftest time-stamp-format-non-date-conversions ()
"Test time-stamp formats for non-date items."
(with-time-stamp-test-env
;; implemented and documented since 1995
(should (equal (time-stamp-string "%%" ref-time1) "%")) ;% last char
(should (equal (time-stamp-string "%%P" ref-time1) "%P")) ;% not last char
(should (equal (time-stamp-string "%f" ref-time1) "time-stamped-file"))
(should
(equal (time-stamp-string "%F" ref-time1) "/emacs/test/time-stamped-file"))
(should (equal (time-stamp-string "%h" ref-time1) "test-mail-host-name"))
;; documented 1995-2019
(should (equal
(time-stamp-string "%s" ref-time1) "test-system-name.example.org"))
(should (equal (time-stamp-string "%U" ref-time1) "100%d Tester"))
(should (equal (time-stamp-string "%u" ref-time1) "test-logname"))
;; implemented since 2001, documented since 2019
(should (equal (time-stamp-string "%L" ref-time1) "100%d Tester"))
(should (equal (time-stamp-string "%l" ref-time1) "test-logname"))
;; implemented since 2007, documented since 2019
(should (equal
(time-stamp-string "%Q" ref-time1) "test-system-name.example.org"))
(should (equal
(time-stamp-string "%q" ref-time1) "test-system-name"))))
(with-time-stamp-system-name "test-system-name.example.org"
;; implemented and documented since 1995
(should (equal (time-stamp-string "%%" ref-time1) "%")) ;% last char
(should (equal (time-stamp-string "%%P" ref-time1) "%P")) ;% not last char
(should (equal (time-stamp-string "%f" ref-time1) "time-stamped-file"))
(should (equal (time-stamp-string "%F" ref-time1)
"/emacs/test/time-stamped-file"))
(with-temp-buffer
(should (equal (time-stamp-string "%f" ref-time1) "(no file)"))
(should (equal (time-stamp-string "%F" ref-time1) "(no file)")))
(should (equal (time-stamp-string "%h" ref-time1) "test-mail-host-name"))
(let ((mail-host-address nil))
(should (equal (time-stamp-string "%h" ref-time1)
"test-system-name.example.org")))
;; documented 1995-2019
(should (equal (time-stamp-string "%s" ref-time1)
"test-system-name.example.org"))
(should (equal (time-stamp-string "%U" ref-time1) "100%d Tester"))
(should (equal (time-stamp-string "%u" ref-time1) "test-logname"))
;; implemented since 2001, documented since 2019
(should (equal (time-stamp-string "%L" ref-time1) "100%d Tester"))
(should (equal (time-stamp-string "%l" ref-time1) "test-logname"))
;; implemented since 2007, documented since 2019
(should (equal (time-stamp-string "%Q" ref-time1)
"test-system-name.example.org"))
(should (equal (time-stamp-string "%q" ref-time1) "test-system-name")))
(with-time-stamp-system-name "sysname-no-dots"
(should (equal (time-stamp-string "%Q" ref-time1) "sysname-no-dots"))
(should (equal (time-stamp-string "%q" ref-time1) "sysname-no-dots")))))
(ert-deftest time-stamp-format-ignored-modifiers ()
"Test additional args allowed (but ignored) to allow for future expansion."
@ -538,6 +593,13 @@
;;; Tests of helper functions
(ert-deftest time-stamp-helper-string-defaults ()
"Test that time-stamp-string defaults its format to time-stamp-format."
(with-time-stamp-test-env
(should (equal (time-stamp-string nil ref-time1)
(time-stamp-string time-stamp-format ref-time1)))
(should (equal (time-stamp-string 'not-a-string ref-time1) nil))))
(ert-deftest time-stamp-helper-zone-type-p ()
"Test time-stamp-zone-type-p."
(should (time-stamp-zone-type-p t))