aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorBob Rogers2022-02-25 13:03:20 +0100
committerLars Ingebrigtsen2022-02-25 13:04:10 +0100
commitca3858563c7ba8ee3caa82fbd2b7c386ea60c0d3 (patch)
treee2285b3c2234c25415659956a19cdfc1def34812 /test
parent2b8bb05383ea1589027786795c9efaba4c718cce (diff)
downloademacs-ca3858563c7ba8ee3caa82fbd2b7c386ea60c0d3.tar.gz
emacs-ca3858563c7ba8ee3caa82fbd2b7c386ea60c0d3.zip
Add new file ietf-drums-date.el
* lisp/mail/ietf-drums-date.el: parse-time-string replacement which is compatible but can be made stricter if desired. * test/lisp/mail/ietf-drums-date-tests.el (added): Add tests for ietf-drums-parse-date-string. * lisp/mail/ietf-drums.el (ietf-drums-parse-date): Use ietf-drums-parse-date-string.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/mail/ietf-drums-date-tests.el190
1 files changed, 190 insertions, 0 deletions
diff --git a/test/lisp/mail/ietf-drums-date-tests.el b/test/lisp/mail/ietf-drums-date-tests.el
new file mode 100644
index 00000000000..5b798077ff9
--- /dev/null
+++ b/test/lisp/mail/ietf-drums-date-tests.el
@@ -0,0 +1,190 @@
1;;; ietf-drums-date-tests.el --- Test suite for ietf-drums-date.el -*- lexical-binding:t -*-
2
3;; Copyright (C) 2022 Free Software Foundation, Inc.
4
5;; Author: Bob Rogers <rogers@rgrjr.com>
6
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software: you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
21
22;;; Commentary:
23
24;;; Code:
25
26(require 'ert)
27(require 'ietf-drums)
28(require 'ietf-drums-date)
29
30(ert-deftest ietf-drums-date-tests ()
31 "Test basic ietf-drums-parse-date-string functionality."
32
33 ;; Test tokenization.
34 (should (equal (ietf-drums-date--tokenize-string " ") '()))
35 (should (equal (ietf-drums-date--tokenize-string " a b") '("a" "b")))
36 (should (equal (ietf-drums-date--tokenize-string "a bbc dde")
37 '("a" "bbc" "dde")))
38 (should (equal (ietf-drums-date--tokenize-string " , a 27 b,, c 14:32 ")
39 '("a" 27 "b" "c" "14:32")))
40 ;; Some folding whitespace tests.
41 (should (equal (ietf-drums-date--tokenize-string " a b (end) c" t)
42 '("a" "b")))
43 (should (equal (ietf-drums-date--tokenize-string "(quux)a (foo (bar)) b(baz)")
44 '("a" "b")))
45 (should (equal (ietf-drums-date--tokenize-string "a b\\cde")
46 ;; Strictly incorrect, but strictly unnecessary syntax.
47 '("a" "b\\cde")))
48 (should (equal (ietf-drums-date--tokenize-string "a b\\ de")
49 '("a" "b\\ de")))
50 (should (equal (ietf-drums-date--tokenize-string "a \\de \\(f")
51 '("a" "\\de" "\\(f")))
52
53 ;; Start with some compatible RFC822 dates.
54 (dolist (case '(("Mon, 22 Feb 2016 19:35:42 +0100"
55 (42 35 19 22 2 2016 1 -1 3600)
56 (22219 21758))
57 ("22 Feb 2016 19:35:42 +0100"
58 (42 35 19 22 2 2016 nil -1 3600)
59 (22219 21758))
60 ("Mon, 22 February 2016 19:35:42 +0100"
61 (42 35 19 22 2 2016 1 -1 3600)
62 (22219 21758))
63 ("Mon, 22 feb 2016 19:35:42 +0100"
64 (42 35 19 22 2 2016 1 -1 3600)
65 (22219 21758))
66 ("Monday, 22 february 2016 19:35:42 +0100"
67 (42 35 19 22 2 2016 1 -1 3600)
68 (22219 21758))
69 ("Monday, 22 february 2016 19:35:42 PST"
70 (42 35 19 22 2 2016 1 nil -28800)
71 (22219 54158))
72 ("Friday, 21 Sep 2018 13:47:58 PDT"
73 (58 47 13 21 9 2018 5 t -25200)
74 (23461 22782))
75 ("Friday, 21 Sep 2018 13:47:58 EDT"
76 (58 47 13 21 9 2018 5 t -14400)
77 (23461 11982))))
78 (let* ((input (car case))
79 (parsed (cadr case))
80 (encoded (caddr case)))
81 ;; The input should parse the same without RFC822.
82 (should (equal (ietf-drums-parse-date-string input) parsed))
83 (should (equal (ietf-drums-parse-date-string input nil t) parsed))
84 ;; Check the encoded date (the official output, though the
85 ;; decoded-time is easier to debug).
86 (should (equal (ietf-drums-parse-date input) encoded))))
87
88 ;; Test a few without timezones.
89 (dolist (case '(("Mon, 22 Feb 2016 19:35:42"
90 (42 35 19 22 2 2016 1 -1 nil))
91 ("Friday, 21 Sep 2018 13:47:58"
92 (58 47 13 21 9 2018 5 -1 nil))))
93 (let* ((input (car case))
94 (parsed (cadr case)))
95 ;; The input should parse the same without RFC822.
96 (should (equal (ietf-drums-parse-date-string input) parsed))
97 (should (equal (ietf-drums-parse-date-string input nil t) parsed))
98 ;; We can't check the encoded date here because it will differ
99 ;; depending on the TZ of the test environment.
100 ))
101
102 ;; Two-digit years are not allowed by the "modern" format.
103 (should (equal (ietf-drums-parse-date-string "22 Feb 16 19:35:42 +0100")
104 '(42 35 19 22 2 2016 nil -1 3600)))
105 (should (equal (ietf-drums-parse-date-string "22 Feb 16 19:35:42 +0100" nil t)
106 '(nil nil nil 22 2 nil nil -1 nil)))
107 (should (equal (should-error (ietf-drums-parse-date-string
108 "22 Feb 16 19:35:42 +0100" t t))
109 '(date-parse-error "Four-digit years are required" 16)))
110 (should (equal (ietf-drums-parse-date-string "22 Feb 96 19:35:42 +0100")
111 '(42 35 19 22 2 1996 nil -1 3600)))
112 (should (equal (ietf-drums-parse-date-string "22 Feb 96 19:35:42 +0100" nil t)
113 '(nil nil nil 22 2 nil nil -1 nil)))
114 (should (equal (should-error (ietf-drums-parse-date-string
115 "22 Feb 96 19:35:42 +0100" t t))
116 '(date-parse-error "Four-digit years are required" 96)))
117
118 ;; Try some dates with comments.
119 (should (equal (ietf-drums-parse-date-string
120 "22 Feb (today) 16 19:35:42 +0100")
121 '(42 35 19 22 2 2016 nil -1 3600)))
122 (should (equal (ietf-drums-parse-date-string
123 "22 Feb (today) 16 19:35:42 +0100" nil t)
124 '(nil nil nil 22 2 nil nil -1 nil)))
125 (should (equal (should-error (ietf-drums-parse-date-string
126 "22 Feb (today) 16 19:35:42 +0100" t t))
127 '(date-parse-error "Expected a year" nil)))
128 (should (equal (ietf-drums-parse-date-string
129 "22 Feb 96 (long ago) 19:35:42 +0100")
130 '(42 35 19 22 2 1996 nil -1 3600)))
131 (should (equal (ietf-drums-parse-date-string
132 "Friday, 21 Sep(comment \\) with \\( parens)18 19:35:42")
133 '(42 35 19 21 9 2018 5 -1 nil)))
134 (should (equal (ietf-drums-parse-date-string
135 "Friday, 21 Sep 18 19:35:42 (unterminated comment")
136 '(42 35 19 21 9 2018 5 -1 nil)))
137
138 ;; Test some RFC822 error cases
139 (dolist (test '(("33 1 2022" ("Slot out of range" day 33 1 31))
140 ("0 1 2022" ("Slot out of range" day 0 1 31))
141 ("1 1 2020 2021" ("Expected an alphabetic month" 1))
142 ("1 Jan 2020 2021" ("Expected a time" 2021))
143 ("1 Jan 2020 20:21 2000" ("Expected a timezone" 2000))
144 ("1 Jan 2020 20:21 +0200 33" ("Extra token(s)" 33))))
145 (should (equal (should-error (ietf-drums-parse-date-string (car test) t))
146 (cons 'date-parse-error (cadr test)))))
147
148 (dolist (test '(("22 Feb 196" nil ;; bad year
149 ("Four-digit years are required" 196))
150 ("22 Feb 16 19:35:24" t ;; two-digit year
151 ("Four-digit years are required" 16))
152 ("22 Feb 96 19:35:42" t ;; two-digit year
153 ("Four-digit years are required" 96))
154 ("2 Feb 2021 1996" nil
155 ("Expected a time" 1996))
156 ("22 Fub 1996" nil
157 ("Expected an alphabetic month" "fub"))
158 ("1 Jan 2020 30" nil
159 ("Expected a time" 30))
160 ("1 Jan 2020 16:47 15:15" nil
161 ("Expected a timezone" "15:15"))
162 ("1 Jan 2020 16:47 +0800 -0800" t
163 ("Extra token(s)" "-0800"))
164 ;; Range tests
165 ("32 Dec 2021" nil
166 ("Slot out of range" day 32 1 31))
167 ("0 Dec 2021" nil
168 ("Slot out of range" day 0 1 31))
169 ("3 13 2021" nil
170 ("Expected an alphabetic month" 13))
171 ("3 Dec 0000" t
172 ("Four-digit years are required" 0))
173 ("3 Dec 20021" nil
174 ("Slot out of range" year 20021 1 9999))
175 ("1 Jan 2020 24:21:14" nil
176 ("Slot out of range" hour "24:21:14" 0 23))
177 ("1 Jan 2020 14:60:21" nil
178 ("Slot out of range" minute "14:60:21" 0 59))
179 ("1 Jan 2020 14:21:61" nil
180 ("Slot out of range" second "14:21:61" 0 60))))
181 (should (equal (should-error
182 (ietf-drums-parse-date-string (car test) t (cadr test)))
183 (cons 'date-parse-error (caddr test)))))
184 (should (equal (ietf-drums-parse-date-string
185 "1 Jan 2020 14:21:60") ;; a leap second!
186 '(60 21 14 1 1 2020 nil -1 nil))))
187
188(provide 'ietf-drums-date-tests)
189
190;;; ietf-drums-date-tests.el ends here