aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorStephen Gildea2019-12-23 11:51:47 -0800
committerStephen Gildea2019-12-23 11:52:42 -0800
commit17c19817f7f4ec918a3a9bb3c957b1aa0463da82 (patch)
tree88b6793832552c7de83b2129cc553ac6f8d838d9 /test
parente7edba42c8a525722cbd40f782b0df68e4976a62 (diff)
downloademacs-17c19817f7f4ec918a3a9bb3c957b1aa0463da82.tar.gz
emacs-17c19817f7f4ec918a3a9bb3c957b1aa0463da82.zip
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.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/time-stamp-tests.el110
1 files changed, 86 insertions, 24 deletions
diff --git a/test/lisp/time-stamp-tests.el b/test/lisp/time-stamp-tests.el
index fb2780af2de..5326d26e1df 100644
--- a/test/lisp/time-stamp-tests.el
+++ b/test/lisp/time-stamp-tests.el
@@ -38,9 +38,7 @@
38 (cl-letf (((symbol-function 'time-stamp-conv-warn) 38 (cl-letf (((symbol-function 'time-stamp-conv-warn)
39 (lambda (old-format _new) 39 (lambda (old-format _new)
40 (ert-fail 40 (ert-fail
41 (format "Unexpected format warning for '%s'" old-format)))) 41 (format "Unexpected format warning for '%s'" old-format)))))
42 ((symbol-function 'system-name)
43 (lambda () "test-system-name.example.org")))
44 ;; Not all reference times are used in all tests; 42 ;; Not all reference times are used in all tests;
45 ;; suppress the byte compiler's "unused" warning. 43 ;; suppress the byte compiler's "unused" warning.
46 (list ref-time1 ref-time2 ref-time3) 44 (list ref-time1 ref-time2 ref-time3)
@@ -56,6 +54,13 @@
56 (apply orig-time-stamp-string-fn ts-format ,reference-time nil)))) 54 (apply orig-time-stamp-string-fn ts-format ,reference-time nil))))
57 ,@body)) 55 ,@body))
58 56
57(defmacro with-time-stamp-system-name (name &rest body)
58 "Force (system-name) to return NAME while evaluating BODY."
59 (declare (indent defun))
60 `(cl-letf (((symbol-function 'system-name)
61 (lambda () ,name)))
62 ,@body))
63
59(defmacro time-stamp-should-warn (form) 64(defmacro time-stamp-should-warn (form)
60 "Similar to `should' but verifies that a format warning is generated." 65 "Similar to `should' but verifies that a format warning is generated."
61 `(let ((warning-count 0)) 66 `(let ((warning-count 0))
@@ -170,6 +175,20 @@
170 ;; triggering the tests above. 175 ;; triggering the tests above.
171 (time-stamp))))))) 176 (time-stamp)))))))
172 177
178(ert-deftest time-stamp-custom-format-tabs-expand ()
179 "Test that Tab characters expand in the format but not elsewhere."
180 (with-time-stamp-test-env
181 (let ((time-stamp-start "Updated in: <\t")
182 ;; Tabs in the format should expand
183 (time-stamp-format "\t%Y\t")
184 (time-stamp-end "\t>"))
185 (with-time-stamp-test-time ref-time1
186 (with-temp-buffer
187 (insert "Updated in: <\t\t>")
188 (time-stamp)
189 (should (equal (buffer-string)
190 "Updated in: <\t 2006 \t>")))))))
191
173(ert-deftest time-stamp-custom-inserts-lines () 192(ert-deftest time-stamp-custom-inserts-lines ()
174 "Test that time-stamp inserts lines or not, as directed." 193 "Test that time-stamp inserts lines or not, as directed."
175 (with-time-stamp-test-env 194 (with-time-stamp-test-env
@@ -194,19 +213,46 @@
194 (time-stamp) 213 (time-stamp)
195 (should (equal (buffer-string) buffer-expected-2line))))))) 214 (should (equal (buffer-string) buffer-expected-2line)))))))
196 215
216(ert-deftest time-stamp-custom-end ()
217 "Test that time-stamp finds the end pattern on the correct line."
218 (with-time-stamp-test-env
219 (let ((time-stamp-start "Updated on: <")
220 (time-stamp-format "%Y-%m-%d")
221 (time-stamp-end ">") ;changed later in the test
222 (buffer-original-contents "Updated on: <\n>\n")
223 (buffer-expected-time-stamped "Updated on: <2006-01-02\n>\n"))
224 (with-time-stamp-test-time ref-time1
225 (with-temp-buffer
226 (insert buffer-original-contents)
227 ;; time-stamp-end is not on same line, should not be seen
228 (time-stamp)
229 (should (equal (buffer-string) buffer-original-contents))
230
231 ;; add a newline to time-stamp-end, so it starts on same line
232 (setq time-stamp-end "\n>")
233 (time-stamp)
234 (should (equal (buffer-string) buffer-expected-time-stamped)))))))
235
197(ert-deftest time-stamp-custom-count () 236(ert-deftest time-stamp-custom-count ()
198 "Test that time-stamp updates no more than time-stamp-count templates." 237 "Test that time-stamp updates no more than time-stamp-count templates."
199 (with-time-stamp-test-env 238 (with-time-stamp-test-env
200 (let ((time-stamp-start "TS: <") 239 (let ((time-stamp-start "TS: <")
201 (time-stamp-format "%Y-%m-%d") 240 (time-stamp-format "%Y-%m-%d")
202 (time-stamp-count 1) ;changed later in the test 241 (time-stamp-count 0) ;changed later in the test
203 (buffer-expected-once "TS: <2006-01-02>\nTS: <>") 242 (buffer-expected-once "TS: <2006-01-02>\nTS: <>")
204 (buffer-expected-twice "TS: <2006-01-02>\nTS: <2006-01-02>")) 243 (buffer-expected-twice "TS: <2006-01-02>\nTS: <2006-01-02>"))
205 (with-time-stamp-test-time ref-time1 244 (with-time-stamp-test-time ref-time1
206 (with-temp-buffer 245 (with-temp-buffer
207 (insert "TS: <>\nTS: <>") 246 (insert "TS: <>\nTS: <>")
208 (time-stamp) 247 (time-stamp)
248 ;; even with count = 0, expect one time stamp
249 (should (equal (buffer-string) buffer-expected-once)))
250 (with-temp-buffer
251 (setq time-stamp-count 1)
252 (insert "TS: <>\nTS: <>")
253 (time-stamp)
209 (should (equal (buffer-string) buffer-expected-once)) 254 (should (equal (buffer-string) buffer-expected-once))
255
210 (setq time-stamp-count 2) 256 (setq time-stamp-count 2)
211 (time-stamp) 257 (time-stamp)
212 (should (equal (buffer-string) buffer-expected-twice))))))) 258 (should (equal (buffer-string) buffer-expected-twice)))))))
@@ -488,26 +534,35 @@
488(ert-deftest time-stamp-format-non-date-conversions () 534(ert-deftest time-stamp-format-non-date-conversions ()
489 "Test time-stamp formats for non-date items." 535 "Test time-stamp formats for non-date items."
490 (with-time-stamp-test-env 536 (with-time-stamp-test-env
491 ;; implemented and documented since 1995 537 (with-time-stamp-system-name "test-system-name.example.org"
492 (should (equal (time-stamp-string "%%" ref-time1) "%")) ;% last char 538 ;; implemented and documented since 1995
493 (should (equal (time-stamp-string "%%P" ref-time1) "%P")) ;% not last char 539 (should (equal (time-stamp-string "%%" ref-time1) "%")) ;% last char
494 (should (equal (time-stamp-string "%f" ref-time1) "time-stamped-file")) 540 (should (equal (time-stamp-string "%%P" ref-time1) "%P")) ;% not last char
495 (should 541 (should (equal (time-stamp-string "%f" ref-time1) "time-stamped-file"))
496 (equal (time-stamp-string "%F" ref-time1) "/emacs/test/time-stamped-file")) 542 (should (equal (time-stamp-string "%F" ref-time1)
497 (should (equal (time-stamp-string "%h" ref-time1) "test-mail-host-name")) 543 "/emacs/test/time-stamped-file"))
498 ;; documented 1995-2019 544 (with-temp-buffer
499 (should (equal 545 (should (equal (time-stamp-string "%f" ref-time1) "(no file)"))
500 (time-stamp-string "%s" ref-time1) "test-system-name.example.org")) 546 (should (equal (time-stamp-string "%F" ref-time1) "(no file)")))
501 (should (equal (time-stamp-string "%U" ref-time1) "100%d Tester")) 547 (should (equal (time-stamp-string "%h" ref-time1) "test-mail-host-name"))
502 (should (equal (time-stamp-string "%u" ref-time1) "test-logname")) 548 (let ((mail-host-address nil))
503 ;; implemented since 2001, documented since 2019 549 (should (equal (time-stamp-string "%h" ref-time1)
504 (should (equal (time-stamp-string "%L" ref-time1) "100%d Tester")) 550 "test-system-name.example.org")))
505 (should (equal (time-stamp-string "%l" ref-time1) "test-logname")) 551 ;; documented 1995-2019
506 ;; implemented since 2007, documented since 2019 552 (should (equal (time-stamp-string "%s" ref-time1)
507 (should (equal 553 "test-system-name.example.org"))
508 (time-stamp-string "%Q" ref-time1) "test-system-name.example.org")) 554 (should (equal (time-stamp-string "%U" ref-time1) "100%d Tester"))
509 (should (equal 555 (should (equal (time-stamp-string "%u" ref-time1) "test-logname"))
510 (time-stamp-string "%q" ref-time1) "test-system-name")))) 556 ;; implemented since 2001, documented since 2019
557 (should (equal (time-stamp-string "%L" ref-time1) "100%d Tester"))
558 (should (equal (time-stamp-string "%l" ref-time1) "test-logname"))
559 ;; implemented since 2007, documented since 2019
560 (should (equal (time-stamp-string "%Q" ref-time1)
561 "test-system-name.example.org"))
562 (should (equal (time-stamp-string "%q" ref-time1) "test-system-name")))
563 (with-time-stamp-system-name "sysname-no-dots"
564 (should (equal (time-stamp-string "%Q" ref-time1) "sysname-no-dots"))
565 (should (equal (time-stamp-string "%q" ref-time1) "sysname-no-dots")))))
511 566
512(ert-deftest time-stamp-format-ignored-modifiers () 567(ert-deftest time-stamp-format-ignored-modifiers ()
513 "Test additional args allowed (but ignored) to allow for future expansion." 568 "Test additional args allowed (but ignored) to allow for future expansion."
@@ -538,6 +593,13 @@
538 593
539;;; Tests of helper functions 594;;; Tests of helper functions
540 595
596(ert-deftest time-stamp-helper-string-defaults ()
597 "Test that time-stamp-string defaults its format to time-stamp-format."
598 (with-time-stamp-test-env
599 (should (equal (time-stamp-string nil ref-time1)
600 (time-stamp-string time-stamp-format ref-time1)))
601 (should (equal (time-stamp-string 'not-a-string ref-time1) nil))))
602
541(ert-deftest time-stamp-helper-zone-type-p () 603(ert-deftest time-stamp-helper-zone-type-p ()
542 "Test time-stamp-zone-type-p." 604 "Test time-stamp-zone-type-p."
543 (should (time-stamp-zone-type-p t)) 605 (should (time-stamp-zone-type-p t))