Commit | Line | Data |
---|---|---|
7ec01532 GM |
1 | ;; icalendar-testsuite.el --- Test suite for icalendar.el |
2 | ||
21b6f58f | 3 | ;; Copyright (C) 2005, 2008, 2009 Free Software Foundation, Inc. |
7ec01532 GM |
4 | |
5 | ;; Author: Ulf Jasper <ulf.jasper@web.de> | |
6 | ;; Created: March 2005 | |
7 | ;; Keywords: calendar | |
8 | ;; Human-Keywords: calendar, diary, iCalendar, vCalendar | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
4f43e937 | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
7ec01532 | 13 | ;; it under the terms of the GNU General Public License as published by |
4f43e937 GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
7ec01532 GM |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
4f43e937 | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
7ec01532 GM |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; TODO: | |
28 | ;; - Add more unit tests for functions, timezone etc. | |
29 | ||
a4766629 GM |
30 | ;; Note: Watch the trailing blank that is added on import. |
31 | ||
7ec01532 GM |
32 | ;;; Code: |
33 | (defun icalendar-testsuite-run () | |
34 | "Run icalendar test suite." | |
35 | (interactive) | |
36 | (icalendar-testsuite--run-function-tests) | |
37 | (icalendar-testsuite--run-import-tests) | |
38 | (icalendar-testsuite--run-export-tests) | |
39 | (icalendar-testsuite--run-cycle-tests) | |
40 | (icalendar-testsuite--run-real-world-tests) | |
41 | (message "All icalendar tests finished successfully.")) | |
42 | ||
43 | ;; ====================================================================== | |
44 | ;; Test methods for functions | |
45 | ;; ====================================================================== | |
46 | (defun icalendar-testsuite--run-function-tests () | |
47 | "Perform tests for single icalendar functions." | |
48 | (icalendar-testsuite--test-parse-summary-and-rest) | |
49 | (icalendar-testsuite--test-format-ical-event) | |
50 | (icalendar-testsuite--test-import-format-sample) | |
a4766629 GM |
51 | (icalendar-testsuite--test-first-weekday-of-year) |
52 | (icalendar-testsuite--test-datestring-to-isodate) | |
53 | (icalendar-testsuite--test-datetime-to-diary-date) | |
aad81014 UJ |
54 | (icalendar-testsuite--test-calendar-style) |
55 | (icalendar-testsuite--test-create-uid)) | |
7ec01532 GM |
56 | |
57 | (defun icalendar-testsuite--test-format-ical-event () | |
a4766629 | 58 | "Test `icalendar--format-ical-event'." |
7ec01532 GM |
59 | (let ((icalendar-import-format "%s%d%l%o%t%u%c") |
60 | (icalendar-import-format-summary "SUM %s") | |
61 | (icalendar-import-format-location " LOC %s") | |
62 | (icalendar-import-format-description " DES %s") | |
63 | (icalendar-import-format-organizer " ORG %s") | |
64 | (icalendar-import-format-status " STA %s") | |
65 | (icalendar-import-format-url " URL %s") | |
66 | (icalendar-import-format-class " CLA %s") | |
7ec01532 GM |
67 | (event (icalendar-testsuite--get-ical-event "BEGIN:VEVENT |
68 | DTSTAMP:20030509T043439Z | |
69 | DTSTART:20030509T103000 | |
70 | SUMMARY:sum | |
71 | ORGANIZER:org | |
72 | LOCATION:loc | |
73 | DTEND:20030509T153000 | |
74 | DESCRIPTION:des | |
75 | END:VEVENT | |
76 | "))) | |
77 | (assert (string= (icalendar--format-ical-event event) | |
78 | "SUM sum DES des LOC loc ORG org") t) | |
79 | (setq icalendar-import-format (lambda (&rest ignore) | |
80 | "helloworld")) | |
81 | (assert (string= (icalendar--format-ical-event event) | |
82 | "helloworld") t) | |
83 | (setq icalendar-import-format | |
84 | (lambda (e) | |
85 | (format "-%s-%s-%s-%s-%s-%s-%s-" | |
86 | (icalendar--get-event-property event 'SUMMARY) | |
87 | (icalendar--get-event-property event 'DESCRIPTION) | |
88 | (icalendar--get-event-property event 'LOCATION) | |
89 | (icalendar--get-event-property event 'ORGANIZER) | |
90 | (icalendar--get-event-property event 'STATUS) | |
91 | (icalendar--get-event-property event 'URL) | |
92 | (icalendar--get-event-property event 'CLASS)))) | |
93 | (assert (string= (icalendar--format-ical-event event) | |
94 | "-sum-des-loc-org-nil-nil-nil-") t))) | |
95 | ||
96 | (defun icalendar-testsuite--test-parse-summary-and-rest () | |
a4766629 | 97 | "Test `icalendar--parse-summary-and-rest'." |
7ec01532 GM |
98 | (let ((icalendar-import-format "%s%d%l%o%t%u%c") |
99 | (icalendar-import-format-summary "SUM %s") | |
100 | (icalendar-import-format-location " LOC %s") | |
101 | (icalendar-import-format-description " DES %s") | |
102 | (icalendar-import-format-organizer " ORG %s") | |
103 | (icalendar-import-format-status " STA %s") | |
104 | (icalendar-import-format-url " URL %s") | |
105 | (icalendar-import-format-class " CLA %s") | |
7ec01532 GM |
106 | (result)) |
107 | ;; FIXME: need a trailing blank char! | |
108 | (setq result (icalendar--parse-summary-and-rest "SUM sum ORG org ")) | |
109 | (assert (string= (cdr (assoc 'org result)) "org")) | |
110 | ||
111 | (setq result (icalendar--parse-summary-and-rest | |
112 | "SUM sum DES des LOC loc ORG org STA sta URL url CLA cla ")) | |
113 | (assert (string= (cdr (assoc 'des result)) "des")) | |
114 | (assert (string= (cdr (assoc 'loc result)) "loc")) | |
115 | (assert (string= (cdr (assoc 'org result)) "org")) | |
116 | (assert (string= (cdr (assoc 'sta result)) "sta")) | |
117 | (assert (string= (cdr (assoc 'cla result)) "cla")) | |
118 | ||
119 | (setq icalendar-import-format (lambda () "Hello world")) | |
120 | (setq result (icalendar--parse-summary-and-rest | |
121 | "blah blah ")) | |
122 | (assert (not result)) | |
123 | )) | |
124 | ||
125 | (defun icalendar-testsuite--get-ical-event (ical-string) | |
a4766629 GM |
126 | "Helper function for testing `icalendar-testsuite--test-format-ical-event'. |
127 | Return icalendar event for ICAL-STRING." | |
7ec01532 GM |
128 | (save-excursion |
129 | (with-temp-buffer | |
130 | (insert ical-string) | |
131 | (goto-char (point-min)) | |
132 | (car (icalendar--read-element nil nil))))) | |
133 | ||
134 | (defun icalendar-testsuite--test-import-format-sample () | |
135 | "Test method for `icalendar-import-format-sample'." | |
136 | (assert (string= (icalendar-import-format-sample | |
137 | (icalendar-testsuite--get-ical-event "BEGIN:VEVENT | |
138 | DTSTAMP:20030509T043439Z | |
139 | DTSTART:20030509T103000 | |
140 | SUMMARY:a | |
141 | ORGANIZER:d | |
142 | LOCATION:c | |
143 | DTEND:20030509T153000 | |
144 | DESCRIPTION:b | |
145 | END:VEVENT | |
146 | ")) | |
147 | (concat "SUMMARY=`a' DESCRIPTION=`b' LOCATION=`c' " | |
148 | "ORGANIZER=`d' STATUS=`' URL=`' CLASS=`'")))) | |
149 | ||
150 | (defun icalendar-testsuite--test-first-weekday-of-year () | |
a4766629 | 151 | "Test method for `icalendar-first-weekday-of-year'." |
7ec01532 GM |
152 | (assert (eq 1 (icalendar-first-weekday-of-year "TU" 2008))) |
153 | (assert (eq 3 (icalendar-first-weekday-of-year "WE" 2007))) | |
154 | (assert (eq 5 (icalendar-first-weekday-of-year "TH" 2006))) | |
155 | (assert (eq 7 (icalendar-first-weekday-of-year "FR" 2005))) | |
156 | (assert (eq 3 (icalendar-first-weekday-of-year "SA" 2004))) | |
157 | (assert (eq 5 (icalendar-first-weekday-of-year "SU" 2003))) | |
158 | (assert (eq 7 (icalendar-first-weekday-of-year "MO" 2002))) | |
159 | (assert (eq 3 (icalendar-first-weekday-of-year "MO" 2000))) | |
160 | (assert (eq 1 (icalendar-first-weekday-of-year "TH" 1970)))) | |
161 | ||
a4766629 GM |
162 | (defun icalendar-testsuite--test-datestring-to-isodate () |
163 | "Test method for `icalendar--datestring-to-isodate'." | |
164 | (let ((calendar-date-style 'iso)) | |
165 | ;; numeric iso | |
166 | (assert (string= (icalendar--datestring-to-isodate "2008 05 11") | |
167 | "20080511")) | |
168 | (assert (string= (icalendar--datestring-to-isodate "2008 05 31") | |
169 | "20080531")) | |
170 | (assert (string= (icalendar--datestring-to-isodate "2008 05 31" 2) | |
171 | "20080602")) | |
172 | ||
173 | ;; numeric european | |
174 | (setq calendar-date-style 'european) | |
175 | (assert (string= (icalendar--datestring-to-isodate "11 05 2008") | |
176 | "20080511")) | |
177 | (assert (string= (icalendar--datestring-to-isodate "31 05 2008") | |
178 | "20080531")) | |
179 | (assert (string= (icalendar--datestring-to-isodate "31 05 2008" 2) | |
180 | "20080602")) | |
181 | ||
182 | ;; numeric american | |
183 | (setq calendar-date-style 'american) | |
184 | (assert (string= (icalendar--datestring-to-isodate "11 05 2008") | |
185 | "20081105")) | |
186 | (assert (string= (icalendar--datestring-to-isodate "12 30 2008") | |
187 | "20081230")) | |
188 | (assert (string= (icalendar--datestring-to-isodate "12 30 2008" 2) | |
189 | "20090101")) | |
190 | ||
191 | ;; non-numeric | |
192 | (setq calendar-date-style nil) ;not necessary for conversion | |
193 | (assert (string= (icalendar--datestring-to-isodate "Nov 05 2008") | |
194 | "20081105")) | |
195 | (assert (string= (icalendar--datestring-to-isodate "05 Nov 2008") | |
196 | "20081105")) | |
197 | (assert (string= (icalendar--datestring-to-isodate "2008 Nov 05") | |
198 | "20081105")))) | |
199 | ||
200 | (defun icalendar-testsuite--test-datetime-to-diary-date () | |
201 | "Test method for `icalendar--datetime-to-diary-date'." | |
202 | (let* ((datetime '(59 59 23 31 12 2008)) | |
203 | (calendar-date-style 'iso)) | |
204 | (assert (string= (icalendar--datetime-to-diary-date datetime) | |
205 | "2008 12 31")) | |
206 | (setq calendar-date-style 'european) | |
207 | (assert (string= (icalendar--datetime-to-diary-date datetime) | |
208 | "31 12 2008")) | |
209 | (setq calendar-date-style 'american) | |
210 | (assert (string= (icalendar--datetime-to-diary-date datetime) | |
211 | "12 31 2008")))) | |
212 | ||
213 | (defun icalendar-testsuite--test-calendar-style () | |
214 | "Test method for `icalendar--date-style'." | |
215 | (dolist (calendar-date-style '(iso american european)) | |
216 | (assert (eq (icalendar--date-style) calendar-date-style))) | |
217 | (let ((cds calendar-date-style) | |
218 | (european-calendar-style t)) | |
219 | (makunbound 'calendar-date-style) | |
220 | (assert (eq (icalendar--date-style) 'european)) | |
221 | (with-no-warnings (setq european-calendar-style nil)) ;still get warning!?! FIXME | |
222 | (assert (eq (icalendar--date-style) 'american)) | |
223 | (setq calendar-date-style cds))) | |
224 | ||
aad81014 UJ |
225 | (defun icalendar-testsuite--test-create-uid () |
226 | "Test method for `icalendar--create-uid'." | |
227 | (let (t-ct | |
228 | (icalendar--uid-count 77)) | |
229 | ;; FIXME! If a test fails 'current-time is screwed. FIXME! | |
230 | (fset 't-ct (symbol-function 'current-time)) | |
231 | (fset 'current-time (lambda () '(1 2 3))) | |
232 | (assert (= 77 icalendar--uid-count)) | |
233 | (assert (string= "emacs12378" (icalendar--create-uid))) | |
234 | (assert (= 78 icalendar--uid-count)) | |
235 | (fset 'current-time (symbol-function 't-ct)) | |
236 | )) | |
237 | ||
7ec01532 GM |
238 | ;; ====================================================================== |
239 | ;; Test methods for exporting from diary to icalendar | |
240 | ;; ====================================================================== | |
241 | ||
a4766629 GM |
242 | (defun icalendar-testsuite--test-export (input-iso input-european input-american |
243 | expected-output) | |
7ec01532 | 244 | "Perform an export test. |
a4766629 | 245 | Argument INPUT-ISO iso style diary string. |
7ec01532 GM |
246 | Argument INPUT-EUROPEAN european style diary string. |
247 | Argument INPUT-AMERICAN american style diary string. | |
a4766629 GM |
248 | Argument EXPECTED-OUTPUT expected icalendar result string. |
249 | ||
250 | European style input data must use german month names. American | |
251 | and ISO style input data must use english month names." | |
7ec01532 | 252 | (message "--- icalendar-testsuite--test-export ---") |
a4766629 | 253 | (let ((calendar-date-style 'iso) |
7ec01532 GM |
254 | (icalendar-recurring-start-year 2000)) |
255 | (set-time-zone-rule "CET") ;;FIXME: reset timezone! | |
a4766629 GM |
256 | (when input-iso |
257 | (let ((calendar-month-name-array | |
258 | ["January" "February" "March" "April" "May" "June" "July" "August" | |
259 | "September" "October" "November" "December"]) | |
260 | (calendar-day-name-array | |
261 | ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" | |
262 | "Saturday"])) | |
263 | (setq calendar-date-style 'iso) | |
264 | (icalendar-testsuite--do-test-export input-iso expected-output))) | |
7ec01532 GM |
265 | (when input-european |
266 | (let ((calendar-month-name-array | |
267 |