aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorStephen Gildea2019-09-22 20:27:53 -0700
committerStephen Gildea2019-09-22 20:31:59 -0700
commita33cda168e35cb93cdf115f7d12765f590226601 (patch)
tree0f668f2163a47da41ad6cde6ce39659478176b5e /test
parent4eac64fcf21fc54d562d9ac5903ed241c747372b (diff)
downloademacs-a33cda168e35cb93cdf115f7d12765f590226601.tar.gz
emacs-a33cda168e35cb93cdf115f7d12765f590226601.zip
Expand time-stamp unit tests to cover all formatting options
* time-stamp-tests.el: Expand unit tests to cover all formatting options. These tests validate time-stamp-pattern formatting that has existed since at least Emacs 22 (released in 2007). The tests cover both documented behavior and behavior implemented to support future migrations. * time-stamp.el (time-stamp-string): Add a second argument (TIME) to open a testing seam. Have the unit tests call this public function. * time-stamp.el (time-stamp-string, time-stamp-string-preprocess): Remove the second pass through time-string--format. (Previously both functions called it.) It was used only to handle "%", but this is now handled by having time-stamp-string-preprocess not double it. Not doubling the "%" in time-stamp-string-preprocess fixes the padding of "%2%", which was discovered by the new unit tests to be wrong.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/time-stamp-tests.el293
1 files changed, 284 insertions, 9 deletions
diff --git a/test/lisp/time-stamp-tests.el b/test/lisp/time-stamp-tests.el
index 3aa659e7a6d..165b4f13c2f 100644
--- a/test/lisp/time-stamp-tests.el
+++ b/test/lisp/time-stamp-tests.el
@@ -20,17 +20,292 @@
20;;; Code: 20;;; Code:
21 21
22(require 'ert) 22(require 'ert)
23(eval-when-compile (require 'cl-lib))
23(require 'time-stamp) 24(require 'time-stamp)
24 25
25(defun test-stamp (ts-format time) 26(defmacro with-time-stamp-test-env (&rest body)
26 (time-stamp--format (time-stamp-string-preprocess ts-format time) time)) 27 "Evaluates BODY with some standard time-stamp test variables bound."
28 `(let ((user-login-name "test-logname")
29 (user-full-name "Time Stamp Tester")
30 (buffer-file-name "/emacs/test/time-stamped-file")
31 (mail-host-address "test-mail-host-name")
32 (ref-time '(17337 16613)) ;Monday, Jan 2, 2006, 3:04:05 PM
33 (ref-time2 '(22574 61591)) ;Friday, Nov 18, 2016, 12:14:15 PM
34 (ref-time3 '(21377 23973 123456)) ;Sun, May 25, 2014, 03:04:05.123456am
35 (time-stamp-time-zone t)) ;use UTC
36 (cl-letf (((symbol-function 'time-stamp-conv-warn)
37 (lambda (old-format _new)
38 (ert-fail
39 (format "Unexpected format warning for '%s'" old-format))))
40 ((symbol-function 'system-name)
41 (lambda () "test-system-name.example.org")))
42 ;; Not all reference times are used in all tests;
43 ;; suppress the byte compiler's "unused" warning.
44 (list ref-time ref-time2 ref-time3)
45 ,@body)))
46(put 'with-time-stamp-test-env 'lisp-indent-hook 'defun)
27 47
28(ert-deftest test-time-stamp () 48(defmacro time-stamp-should-warn (form)
29 (let ((user-login-name "foo") 49 "Similar to `should' but verifies that a format warning is generated."
30 (time-stamp-time-zone t) 50 `(let ((warning-count 0))
31 (time '(23847 24475 657815 318000)) 51 (cl-letf (((symbol-function 'time-stamp-conv-warn)
32 (format "%:y-%02m-%02d %02H:%02M:%02S %u")) 52 (lambda (_old _new)
33 (should (equal (test-stamp format time) 53 (setq warning-count (1+ warning-count)))))
34 "2019-07-11 16:11:07 foo")))) 54 (should ,form)
55 (if (not (= warning-count 1))
56 (ert-fail (format "Should have warned about format: %S" ',form))))))
57
58;;; Tests:
59
60(ert-deftest time-stamp-test-day-of-week-documented ()
61 "Test documented time-stamp formats for day of week."
62 (with-time-stamp-test-env
63 (should (equal (time-stamp-string "%3a" ref-time) "Mon"))
64 (should (equal (time-stamp-string "%3A" ref-time) "MON"))
65 (should (equal (time-stamp-string "%:a" ref-time) "Monday"))
66 (should (equal (time-stamp-string "%#A" ref-time) "MONDAY"))
67 (should (equal (time-stamp-string "%3a" ref-time2) "Fri"))
68 (should (equal (time-stamp-string "%3A" ref-time2) "FRI"))
69 (should (equal (time-stamp-string "%:a" ref-time2) "Friday"))
70 (should (equal (time-stamp-string "%#A" ref-time2) "FRIDAY"))))
71
72(ert-deftest time-stamp-test-day-of-week-future ()
73 "Test implemented but as yet undocumented time-stamp formats for day of week."
74 (with-time-stamp-test-env
75 (should (equal (time-stamp-string "%#a" ref-time) "MON"))
76 (should (equal (time-stamp-string "%:A" ref-time) "Monday"))
77 (should (equal (time-stamp-string "%#a" ref-time2) "FRI"))
78 (should (equal (time-stamp-string "%:A" ref-time2) "Friday"))))
79
80(ert-deftest time-stamp-test-day-of-week-volatile-warns ()
81 "Test time-stamp formats for day of week that will change.
82Test that each generates a warning."
83 (with-time-stamp-test-env
84 (time-stamp-should-warn (equal
85 (time-stamp-string "%a" ref-time) "Monday"))
86 (time-stamp-should-warn (equal
87 (time-stamp-string "%A" ref-time) "MONDAY"))))
88
89(ert-deftest time-stamp-test-month-name-documented ()
90 "Test documented time-stamp formats for month name."
91 (with-time-stamp-test-env
92 (should (equal (time-stamp-string "%3b" ref-time) "Jan"))
93 (should (equal (time-stamp-string "%3B" ref-time) "JAN"))
94 (should (equal (time-stamp-string "%:b" ref-time) "January"))
95 (should (equal (time-stamp-string "%#B" ref-time) "JANUARY"))))
96
97(ert-deftest time-stamp-test-month-name-future ()
98 "Test implemented but as yet undocumented time-stamp formats for month name."
99 (with-time-stamp-test-env
100 (should (equal (time-stamp-string "%#b" ref-time) "JAN"))
101 (should (equal (time-stamp-string "%:B" ref-time) "January"))))
102
103(ert-deftest time-stamp-test-month-name-volatile-warns ()
104 "Test time-stamp formats for month name that will change.
105Test that each generates a warning."
106 (with-time-stamp-test-env
107 (time-stamp-should-warn (equal
108 (time-stamp-string "%b" ref-time) "January"))
109 (time-stamp-should-warn (equal
110 (time-stamp-string "%B" ref-time) "JANUARY"))))
111
112(ert-deftest time-stamp-test-day-of-month ()
113 "Test time-stamp formats for day of month."
114 (with-time-stamp-test-env
115 ;; documented 1-digit
116 (should (equal (time-stamp-string "%:d" ref-time) "2"))
117 (should (equal (time-stamp-string "%2d" ref-time) " 2"))
118 (should (equal (time-stamp-string "%02d" ref-time) "02"))
119 ;; documented 2-digit
120 (should (equal (time-stamp-string "%:d" ref-time2) "18"))
121 (should (equal (time-stamp-string "%2d" ref-time2) "18"))
122 (should (equal (time-stamp-string "%02d" ref-time2) "18"))
123 ;; undocumented future formats
124 (should (equal (time-stamp-string "%1d" ref-time) "2"))
125 (should (equal (time-stamp-string "%1d" ref-time2) "18"))
126 ;; changing formats
127 (time-stamp-should-warn (equal (time-stamp-string "%d" ref-time) "2"))
128 (time-stamp-should-warn (equal (time-stamp-string "%_d" ref-time) "2"))))
129
130(ert-deftest time-stamp-test-hours-24 ()
131 "Test time-stamp formats for hour on a 24-hour clock."
132 (with-time-stamp-test-env
133 ;; documented PM
134 (should (equal (time-stamp-string "%:H" ref-time) "15"))
135 (should (equal (time-stamp-string "%2H" ref-time) "15"))
136 (should (equal (time-stamp-string "%02H" ref-time) "15"))
137 ;; documented AM
138 (should (equal (time-stamp-string "%:H" ref-time2) "12"))
139 (should (equal (time-stamp-string "%2H" ref-time2) "12"))
140 (should (equal (time-stamp-string "%02H" ref-time2) "12"))
141 ;; documented 1-digit
142 (should (equal (time-stamp-string "%:H" ref-time3) "3"))
143 (should (equal (time-stamp-string "%2H" ref-time3) " 3"))
144 (should (equal (time-stamp-string "%02H" ref-time3) "03"))
145 ;; undocumented future formats
146 (should (equal (time-stamp-string "%1H" ref-time) "15"))
147 (should (equal (time-stamp-string "%1H" ref-time2) "12"))
148 (should (equal (time-stamp-string "%1H" ref-time3) "3"))
149 ;; changing formats
150 (time-stamp-should-warn (equal (time-stamp-string "%H" ref-time) "15"))
151 (time-stamp-should-warn (equal (time-stamp-string "%_H" ref-time) "15"))))
152
153(ert-deftest time-stamp-test-hours-12 ()
154 "Test time-stamp formats for hour on a 12-hour clock."
155 (with-time-stamp-test-env
156 ;; documented 1-digit, PM
157 (should (equal (time-stamp-string "%:I" ref-time) "3"))
158 (should (equal (time-stamp-string "%2I" ref-time) " 3"))
159 (should (equal (time-stamp-string "%02I" ref-time) "03"))
160 ;; documented 2-digit
161 (should (equal (time-stamp-string "%:I" ref-time2) "12"))
162 (should (equal (time-stamp-string "%2I" ref-time2) "12"))
163 (should (equal (time-stamp-string "%02I" ref-time2) "12"))
164 ;; undocumented future formats
165 (should (equal (time-stamp-string "%1I" ref-time) "3"))
166 (should (equal (time-stamp-string "%1I" ref-time2) "12"))
167 ;; changing formats
168 (time-stamp-should-warn (equal (time-stamp-string "%I" ref-time) "3"))
169 (time-stamp-should-warn (equal (time-stamp-string "%_I" ref-time) "3"))))
170
171(ert-deftest time-stamp-test-month-number ()
172 "Test time-stamp formats for month number."
173 (with-time-stamp-test-env
174 ;; documented 1-digit
175 (should (equal (time-stamp-string "%:m" ref-time) "1"))
176 (should (equal (time-stamp-string "%2m" ref-time) " 1"))
177 (should (equal (time-stamp-string "%02m" ref-time) "01"))
178 ;; documented 2-digit
179 (should (equal (time-stamp-string "%:m" ref-time2) "11"))
180 (should (equal (time-stamp-string "%2m" ref-time2) "11"))
181 (should (equal (time-stamp-string "%02m" ref-time2) "11"))
182 ;; undocumented future formats
183 (should (equal (time-stamp-string "%1m" ref-time) "1"))
184 (should (equal (time-stamp-string "%1m" ref-time2) "11"))
185 ;; changing formats
186 (time-stamp-should-warn (equal (time-stamp-string "%m" ref-time) "1"))
187 (time-stamp-should-warn (equal (time-stamp-string "%_m" ref-time) "1"))))
188
189(ert-deftest time-stamp-test-minute ()
190 "Test time-stamp formats for minute."
191 (with-time-stamp-test-env
192 ;; documented 1-digit
193 (should (equal (time-stamp-string "%:M" ref-time) "4"))
194 (should (equal (time-stamp-string "%2M" ref-time) " 4"))
195 (should (equal (time-stamp-string "%02M" ref-time) "04"))
196 ;; documented 2-digit
197 (should (equal (time-stamp-string "%:M" ref-time2) "14"))
198 (should (equal (time-stamp-string "%2M" ref-time2) "14"))
199 (should (equal (time-stamp-string "%02M" ref-time2) "14"))
200 ;; undocumented future formats
201 (should (equal (time-stamp-string "%1M" ref-time) "4"))
202 (should (equal (time-stamp-string "%1M" ref-time2) "14"))
203 ;; changing formats
204 (time-stamp-should-warn (equal (time-stamp-string "%M" ref-time) "4"))
205 (time-stamp-should-warn (equal (time-stamp-string "%_M" ref-time) "4"))))
206
207(ert-deftest time-stamp-test-second ()
208 "Test time-stamp formats for second."
209 (with-time-stamp-test-env
210 ;; documented 1-digit
211 (should (equal (time-stamp-string "%:S" ref-time) "5"))
212 (should (equal (time-stamp-string "%2S" ref-time) " 5"))
213 (should (equal (time-stamp-string "%02S" ref-time) "05"))
214 ;; documented 2-digit
215 (should (equal (time-stamp-string "%:S" ref-time2) "15"))
216 (should (equal (time-stamp-string "%2S" ref-time2) "15"))
217 (should (equal (time-stamp-string "%02S" ref-time2) "15"))
218 ;; undocumented future formats
219 (should (equal (time-stamp-string "%1S" ref-time) "5"))
220 (should (equal (time-stamp-string "%1S" ref-time2) "15"))
221 ;; changing formats
222 (time-stamp-should-warn (equal (time-stamp-string "%S" ref-time) "5"))
223 (time-stamp-should-warn (equal (time-stamp-string "%_S" ref-time) "5"))))
224
225(ert-deftest time-stamp-test-am-pm ()
226 "Test time-stamp formats for AM and PM strings."
227 (with-time-stamp-test-env
228 ;; documented
229 (should (equal (time-stamp-string "%#p" ref-time) "pm"))
230 (should (equal (time-stamp-string "%P" ref-time) "PM"))
231 (should (equal (time-stamp-string "%#p" ref-time3) "am"))
232 (should (equal (time-stamp-string "%P" ref-time3) "AM"))
233 ;; changing
234 (time-stamp-should-warn (equal (time-stamp-string "%p" ref-time) "pm"))))
235
236(ert-deftest time-stamp-test-day-number-in-week ()
237 "Test time-stamp formats for day number in week."
238 (with-time-stamp-test-env
239 (should (equal (time-stamp-string "%w" ref-time) "1"))
240 (should (equal (time-stamp-string "%w" ref-time2) "5"))
241 (should (equal (time-stamp-string "%w" ref-time3) "0"))))
242
243(ert-deftest time-stamp-test-year ()
244 "Test time-stamp formats for year."
245 (with-time-stamp-test-env
246 ;; documented
247 (should (equal (time-stamp-string "%02y" ref-time) "06"))
248 (should (equal (time-stamp-string "%:y" ref-time) "2006"))
249 ;; undocumented future formats
250 (should (equal (time-stamp-string "%Y" ref-time) "2006"))
251 ;; changing
252 (time-stamp-should-warn (equal (time-stamp-string "%y" ref-time) "2006"))))
253
254(ert-deftest time-stamp-test-time-zone ()
255 "Test time-stamp formats for time zone."
256 (with-time-stamp-test-env
257 ;; documented
258 (should (equal (time-stamp-string "%z" ref-time) "gmt"))
259 (should (equal (time-stamp-string "%Z" ref-time) "GMT"))
260 ;; undocumented future formats
261 (should (equal (time-stamp-string "%#Z" ref-time) "gmt"))))
262
263(ert-deftest time-stamp-test-non-date-conversions ()
264 "Test time-stamp formats for non-date items."
265 (with-time-stamp-test-env
266 ;; documented
267 (should (equal (time-stamp-string "%%" ref-time) "%")) ;% last char
268 (should (equal (time-stamp-string "%%P" ref-time) "%P")) ;% not last char
269 (should (equal (time-stamp-string "%f" ref-time) "time-stamped-file"))
270 (should (equal
271 (time-stamp-string "%F" ref-time) "/emacs/test/time-stamped-file"))
272 (should
273 (equal (time-stamp-string "%s" ref-time) "test-system-name.example.org"))
274 (should (equal (time-stamp-string "%u" ref-time) "test-logname"))
275 (should (equal (time-stamp-string "%U" ref-time) "Time Stamp Tester"))
276 (should (equal (time-stamp-string "%h" ref-time) "test-mail-host-name"))
277 ;; undocumented
278 (should (equal (time-stamp-string "%L" ref-time) "Time Stamp Tester"))
279 (should (equal (time-stamp-string "%l" ref-time) "test-logname"))
280 (should (equal
281 (time-stamp-string "%Q" ref-time) "test-system-name.example.org"))
282 (should (equal
283 (time-stamp-string "%q" ref-time) "test-system-name"))))
284
285(ert-deftest time-stamp-test-ignored-modifiers ()
286 "Test additional args allowed (but ignored) to allow for future expansion."
287 (with-time-stamp-test-env
288 ;; allowed modifiers
289 (should (equal (time-stamp-string "%.,@-+_ ^(stuff)P" ref-time3) "AM"))
290 ;; not all punctuation is allowed
291 (should-not (equal (time-stamp-string "%&P" ref-time3) "AM"))))
292
293(ert-deftest time-stamp-test-non-conversions ()
294 "Test that without a %, the text is copied literally."
295 (with-time-stamp-test-env
296 (should (equal (time-stamp-string "No percent" ref-time) "No percent"))))
297
298(ert-deftest time-stamp-test-string-width ()
299 "Test time-stamp string width modifiers."
300 (with-time-stamp-test-env
301 ;; strings truncate on the right or are blank-padded on the left
302 (should (equal (time-stamp-string "%0P" ref-time3) ""))
303 (should (equal (time-stamp-string "%1P" ref-time3) "A"))
304 (should (equal (time-stamp-string "%2P" ref-time3) "AM"))
305 (should (equal (time-stamp-string "%3P" ref-time3) " AM"))
306 (should (equal (time-stamp-string "%0%" ref-time3) ""))
307 (should (equal (time-stamp-string "%1%" ref-time3) "%"))
308 (should (equal (time-stamp-string "%2%" ref-time3) " %"))
309 (should (equal (time-stamp-string "%#3a" ref-time3) "SUN"))))
35 310
36;;; time-stamp-tests.el ends here 311;;; time-stamp-tests.el ends here