Add 2010 to copyright years.
[bpt/emacs.git] / test / icalendar-testsuite.el
CommitLineData
7ec01532
GM
1;; icalendar-testsuite.el --- Test suite for icalendar.el
2
114f9c96 3;; Copyright (C) 2005, 2008, 2009, 2010 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)
f052351a 54 (icalendar-testsuite--test-diarytime-to-isotime)
aad81014 55 (icalendar-testsuite--test-calendar-style)
6fe539d2
UJ
56 (icalendar-testsuite--test-create-uid)
57 (icalendar-testsuite--test-parse-vtimezone))
7ec01532
GM
58
59(defun icalendar-testsuite--test-format-ical-event ()
a4766629 60 "Test `icalendar--format-ical-event'."
7ec01532
GM
61 (let ((icalendar-import-format "%s%d%l%o%t%u%c")
62 (icalendar-import-format-summary "SUM %s")
63 (icalendar-import-format-location " LOC %s")
64 (icalendar-import-format-description " DES %s")
65 (icalendar-import-format-organizer " ORG %s")
66 (icalendar-import-format-status " STA %s")
67 (icalendar-import-format-url " URL %s")
68 (icalendar-import-format-class " CLA %s")
7ec01532
GM
69 (event (icalendar-testsuite--get-ical-event "BEGIN:VEVENT
70DTSTAMP:20030509T043439Z
71DTSTART:20030509T103000
72SUMMARY:sum
73ORGANIZER:org
74LOCATION:loc
75DTEND:20030509T153000
76DESCRIPTION:des
77END:VEVENT
78")))
79 (assert (string= (icalendar--format-ical-event event)
80 "SUM sum DES des LOC loc ORG org") t)
81 (setq icalendar-import-format (lambda (&rest ignore)
82 "helloworld"))
83 (assert (string= (icalendar--format-ical-event event)
84 "helloworld") t)
85 (setq icalendar-import-format
86 (lambda (e)
87 (format "-%s-%s-%s-%s-%s-%s-%s-"
88 (icalendar--get-event-property event 'SUMMARY)
89 (icalendar--get-event-property event 'DESCRIPTION)
90 (icalendar--get-event-property event 'LOCATION)
91 (icalendar--get-event-property event 'ORGANIZER)
92 (icalendar--get-event-property event 'STATUS)
93 (icalendar--get-event-property event 'URL)
94 (icalendar--get-event-property event 'CLASS))))
95 (assert (string= (icalendar--format-ical-event event)
96 "-sum-des-loc-org-nil-nil-nil-") t)))
97
98(defun icalendar-testsuite--test-parse-summary-and-rest ()
a4766629 99 "Test `icalendar--parse-summary-and-rest'."
7ec01532
GM
100 (let ((icalendar-import-format "%s%d%l%o%t%u%c")
101 (icalendar-import-format-summary "SUM %s")
102 (icalendar-import-format-location " LOC %s")
103 (icalendar-import-format-description " DES %s")
104 (icalendar-import-format-organizer " ORG %s")
105 (icalendar-import-format-status " STA %s")
106 (icalendar-import-format-url " URL %s")
107 (icalendar-import-format-class " CLA %s")
7ec01532 108 (result))
f052351a 109 (setq result (icalendar--parse-summary-and-rest "SUM sum ORG org"))
7ec01532
GM
110 (assert (string= (cdr (assoc 'org result)) "org"))
111
112 (setq result (icalendar--parse-summary-and-rest
f052351a 113 "SUM sum DES des LOC loc ORG org STA sta URL url CLA cla"))
7ec01532
GM
114 (assert (string= (cdr (assoc 'des result)) "des"))
115 (assert (string= (cdr (assoc 'loc result)) "loc"))
116 (assert (string= (cdr (assoc 'org result)) "org"))
117 (assert (string= (cdr (assoc 'sta result)) "sta"))
118 (assert (string= (cdr (assoc 'cla result)) "cla"))
119
120 (setq icalendar-import-format (lambda () "Hello world"))
121 (setq result (icalendar--parse-summary-and-rest
122 "blah blah "))
123 (assert (not result))
124 ))
125
126(defun icalendar-testsuite--get-ical-event (ical-string)
a4766629
GM
127 "Helper function for testing `icalendar-testsuite--test-format-ical-event'.
128Return icalendar event for ICAL-STRING."
7ec01532
GM
129 (save-excursion
130 (with-temp-buffer
131 (insert ical-string)
132 (goto-char (point-min))
133 (car (icalendar--read-element nil nil)))))
134
135(defun icalendar-testsuite--test-import-format-sample ()
136 "Test method for `icalendar-import-format-sample'."
137 (assert (string= (icalendar-import-format-sample
138 (icalendar-testsuite--get-ical-event "BEGIN:VEVENT
139DTSTAMP:20030509T043439Z
140DTSTART:20030509T103000
141SUMMARY:a
142ORGANIZER:d
143LOCATION:c
144DTEND:20030509T153000
145DESCRIPTION:b
146END:VEVENT
147"))
148 (concat "SUMMARY=`a' DESCRIPTION=`b' LOCATION=`c' "
149 "ORGANIZER=`d' STATUS=`' URL=`' CLASS=`'"))))
150
151(defun icalendar-testsuite--test-first-weekday-of-year ()
a4766629 152 "Test method for `icalendar-first-weekday-of-year'."
7ec01532
GM
153 (assert (eq 1 (icalendar-first-weekday-of-year "TU" 2008)))
154 (assert (eq 3 (icalendar-first-weekday-of-year "WE" 2007)))
155 (assert (eq 5 (icalendar-first-weekday-of-year "TH" 2006)))
156 (assert (eq 7 (icalendar-first-weekday-of-year "FR" 2005)))
157 (assert (eq 3 (icalendar-first-weekday-of-year "SA" 2004)))
158 (assert (eq 5 (icalendar-first-weekday-of-year "SU" 2003)))
159 (assert (eq 7 (icalendar-first-weekday-of-year "MO" 2002)))
160 (assert (eq 3 (icalendar-first-weekday-of-year "MO" 2000)))
161 (assert (eq 1 (icalendar-first-weekday-of-year "TH" 1970))))
162
a4766629
GM
163(defun icalendar-testsuite--test-datestring-to-isodate ()
164 "Test method for `icalendar--datestring-to-isodate'."
165 (let ((calendar-date-style 'iso))
166 ;; numeric iso
167 (assert (string= (icalendar--datestring-to-isodate "2008 05 11")
168 "20080511"))
169 (assert (string= (icalendar--datestring-to-isodate "2008 05 31")
170 "20080531"))
171 (assert (string= (icalendar--datestring-to-isodate "2008 05 31" 2)
172 "20080602"))
173
174 ;; numeric european
175 (setq calendar-date-style 'european)
176 (assert (string= (icalendar--datestring-to-isodate "11 05 2008")
177 "20080511"))
178 (assert (string= (icalendar--datestring-to-isodate "31 05 2008")
179 "20080531"))
180 (assert (string= (icalendar--datestring-to-isodate "31 05 2008" 2)
181 "20080602"))
182
183 ;; numeric american
184 (setq calendar-date-style 'american)
185 (assert (string= (icalendar--datestring-to-isodate "11 05 2008")
186 "20081105"))
187 (assert (string= (icalendar--datestring-to-isodate "12 30 2008")
188 "20081230"))
189 (assert (string= (icalendar--datestring-to-isodate "12 30 2008" 2)
190 "20090101"))
191
192 ;; non-numeric
193 (setq calendar-date-style nil) ;not necessary for conversion
194 (assert (string= (icalendar--datestring-to-isodate "Nov 05 2008")
195 "20081105"))
196 (assert (string= (icalendar--datestring-to-isodate "05 Nov 2008")
197 "20081105"))
198 (assert (string= (icalendar--datestring-to-isodate "2008 Nov 05")
199 "20081105"))))
200
201(defun icalendar-testsuite--test-datetime-to-diary-date ()
202 "Test method for `icalendar--datetime-to-diary-date'."
203 (let* ((datetime '(59 59 23 31 12 2008))
204 (calendar-date-style 'iso))
205 (assert (string= (icalendar--datetime-to-diary-date datetime)
206 "2008 12 31"))
207 (setq calendar-date-style 'european)
208 (assert (string= (icalendar--datetime-to-diary-date datetime)
209 "31 12 2008"))
210 (setq calendar-date-style 'american)
211 (assert (string= (icalendar--datetime-to-diary-date datetime)
212 "12 31 2008"))))
213
f052351a
UJ
214(defun icalendar-testsuite--test-diarytime-to-isotime ()
215 "Test method for `icalendar--diarytime-to-isotime'."
216 (assert (string= (icalendar--diarytime-to-isotime "0100" "")
217 "T010000"))
218 (assert (string= (icalendar--diarytime-to-isotime "0100" "am")
219 "T010000"))
220 (assert (string= (icalendar--diarytime-to-isotime "0100" "pm")
221 "T130000"))
222 (assert (string= (icalendar--diarytime-to-isotime "1200" "")
223 "T120000"))
224 (assert (string= (icalendar--diarytime-to-isotime "17:17" "")
225 "T171700"))
226 (assert (string= (icalendar--diarytime-to-isotime "1200" "am")
227 "T000000"))
228 (assert (string= (icalendar--diarytime-to-isotime "1201" "am")
229 "T000100"))
230 (assert (string= (icalendar--diarytime-to-isotime "1259" "am")
231 "T005900"))
232 (assert (string= (icalendar--diarytime-to-isotime "1200" "pm")
233 "T120000"))
234 (assert (string= (icalendar--diarytime-to-isotime "1201" "pm")
235 "T120100"))
236 (assert (string= (icalendar--diarytime-to-isotime "1259" "pm")
237 "T125900")))
238
a4766629
GM
239(defun icalendar-testsuite--test-calendar-style ()
240 "Test method for `icalendar--date-style'."
241 (dolist (calendar-date-style '(iso american european))
242 (assert (eq (icalendar--date-style) calendar-date-style)))
243 (let ((cds calendar-date-style)
244 (european-calendar-style t))
245 (makunbound 'calendar-date-style)
246 (assert (eq (icalendar--date-style) 'european))
247 (with-no-warnings (setq european-calendar-style nil)) ;still get warning!?! FIXME
248 (assert (eq (icalendar--date-style) 'american))
249 (setq calendar-date-style cds)))
250
aad81014
UJ
251(defun icalendar-testsuite--test-create-uid ()
252 "Test method for `icalendar--create-uid'."
f052351a
UJ
253 (let* ((icalendar-uid-format "xxx-%t-%c-%h-%u-%s")
254 t-ct
255 (icalendar--uid-count 77)
256 (entry-full "30.06.1964 07:01 blahblah")
257 (hash (format "%d" (abs (sxhash entry-full))))
258 (contents "DTSTART:19640630T070100\nblahblah")
259 (username (or user-login-name "UNKNOWN_USER"))
260 )
aad81014
UJ
261 ;; FIXME! If a test fails 'current-time is screwed. FIXME!
262 (fset 't-ct (symbol-function 'current-time))
263 (fset 'current-time (lambda () '(1 2 3)))
264 (assert (= 77 icalendar--uid-count))
f052351a
UJ
265 (assert (string= (concat "xxx-123-77-" hash "-" username "-19640630")
266 (icalendar--create-uid entry-full contents)))
aad81014
UJ
267 (assert (= 78 icalendar--uid-count))
268 (fset 'current-time (symbol-function 't-ct))
f052351a
UJ
269
270 (setq contents "blahblah")
271 (setq icalendar-uid-format "yyy%syyy")
272 (assert (string= (concat "yyyDTSTARTyyy")
273 (icalendar--create-uid entry-full contents)))
aad81014
UJ
274 ))
275
6fe539d2
UJ
276(defun icalendar-testsuite--test-parse-vtimezone ()
277 (let (vtimezone result)
278 (setq vtimezone (icalendar-testsuite--get-ical-event "BEGIN:VTIMEZONE
279TZID:thename
280BEGIN:STANDARD
281DTSTART:16010101T040000
282TZOFFSETFROM:+0300
283TZOFFSETTO:+0200
284RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10
285END:STANDARD
286BEGIN:DAYLIGHT
287DTSTART:16010101T030000
288TZOFFSETFROM:+0200
289TZOFFSETTO:+0300
290RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=3
291END:DAYLIGHT
292END:VTIMEZONE
293"))
294 (setq result (icalendar--parse-vtimezone vtimezone))
295 (assert (string= "thename" (car result)))
296 (message (cdr result))
297 (assert (string= "STD-02:00DST-03:00,M3.5.0/03:00:00,M10.5.0/04:00:00" (cdr result)))
298 (setq vtimezone (icalendar-testsuite--get-ical-event "BEGIN:VTIMEZONE
299TZID:anothername
300BEGIN:STANDARD
301DTSTART:16010101T040000
302TZOFFSETFROM:+0300
303TZOFFSETTO:+0200
304RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2MO;BYMONTH=10
305END:STANDARD
306BEGIN:DAYLIGHT
307DTSTART:16010101T030000
308TZOFFSETFROM:+0200
309TZOFFSETTO:+0300
310RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2MO;BYMONTH=3
311END:DAYLIGHT
312END:VTIMEZONE
313"))
314 (setq result (icalendar--parse-vtimezone vtimezone))
315 (assert (string= "anothername" (car result)))
316 (message (cdr result))
317 (assert (string= "STD-02:00DST-03:00,M3.2.1/03:00:00,M10.2.1/04:00:00" (cdr result)))))
f052351a 318
7ec01532
GM
319;; ======================================================================
320;; Test methods for exporting from diary to icalendar
321;; ======================================================================
322
a4766629
GM
323(defun icalendar-testsuite--test-export (input-iso input-european input-american
324 expected-output)
7ec01532 325 "Perform an export test.
a4766629 326Argument INPUT-ISO iso style diary string.
7ec01532
GM
327Argument INPUT-EUROPEAN european style diary string.
328Argument INPUT-AMERICAN american style diary string.
a4766629
GM
329Argument EXPECTED-OUTPUT expected icalendar result string.
330
331European style input data must use german month names. American
332and ISO style input data must use english month names."
7ec01532 333 (message "--- icalendar-testsuite--test-export ---")
a4766629 334 (let ((calendar-date-style 'iso)
7ec01532
GM
335 (icalendar-recurring-start-year 2000))
336 (set-time-zone-rule "CET") ;;FIXME: reset timezone!
a4766629
GM
337 (when input-iso
338 (let ((calendar-month-name-array
339 ["January" "February" "March" "April" "May" "June" "July" "August"
340 "September" "October" "November" "December"])
341 (calendar-day-name-array
342 ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday"
343 "Saturday"]))
344 (setq calendar-date-style 'iso)
345 (icalendar-testsuite--do-test-export input-iso expected-output)))
7ec01532
GM
346 (when input-european
347 (let ((calendar-month-name-array
348