-
Notifications
You must be signed in to change notification settings - Fork 14
/
decoder_test.go
373 lines (316 loc) · 8.2 KB
/
decoder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package goics_test
import (
"os"
"strings"
"testing"
"time"
goics "github.com/jordic/goics"
)
func TestTesting(t *testing.T) {
if 1 != 1 {
t.Error("Error setting up testing")
}
}
type Calendar struct {
Data map[string]string
}
func (e *Calendar) ConsumeICal(c *goics.Calendar, err error) error {
for k, v := range c.Data {
e.Data[k] = v.Val
}
return err
}
func NewCal() *Calendar {
return &Calendar{
Data: make(map[string]string),
}
}
var source = "asdf\nasdf\nasdf"
func TestEndOfFile(t *testing.T) {
a := goics.NewDecoder(strings.NewReader(source))
err := a.Decode(&Calendar{})
if err != goics.ErrCalendarNotFound {
t.Errorf("Decode filed, decode raised %s", err)
}
if a.Lines() != 3 {
t.Errorf("Decode should advance to %d", a.Lines())
}
}
var sourceEOFMultiline = `BEGIN:VCALENDAR
VERSION:2.0
FOO
BAR
BAR
`
func TestEndOfFileMultiline(t *testing.T) {
a := goics.NewDecoder(strings.NewReader(sourceEOFMultiline))
a.Decode(&Calendar{})
if expected := 5; a.Lines() != expected {
t.Errorf("Decode should advance to %d but %d is returned", expected, a.Lines())
}
}
var test2 = `BEGIN:VCALENDAR
PRODID;X-RICAL-TZSOURCE=TZINFO:-//test//EN
CALSCALE:GREGORIAN
VERSION:2.0
END:VCALENDAR
`
func TestInsideCalendar(t *testing.T) {
a := goics.NewDecoder(strings.NewReader(test2))
consumer := NewCal()
err := a.Decode(consumer)
if err != nil {
t.Errorf("Failed %s", err)
}
if consumer.Data["CALSCALE"] != "GREGORIAN" {
t.Error("No extra keys for calendar decoded")
}
if consumer.Data["VERSION"] != "2.0" {
t.Error("No extra keys for calendar decoded")
}
}
var test3 = `BEGIN:VCALENDAR
PRODID;X-RICAL-TZSOURCE=TZINFO:-//test//EN
CALSCALE:GREGORIAN
VERSION:2.`
func TestDetectIncompleteCalendar(t *testing.T) {
a := goics.NewDecoder(strings.NewReader(test3))
err := a.Decode(&Calendar{})
if err != goics.ErrParseEndCalendar {
t.Error("Test failed")
}
}
var testlonglines = `BEGIN:VCALENDAR
PRODID;X-RICAL-TZSOURCE=TZINFO:-//test//EN
CALSCALE:GREGORIANGREGORIANGREGORIANGREGORIANGREGORIANGREGORIANGREGORIAN
GREGORIANGREGORIAN
VERSION:2.0
END:VCALENDAR
`
func TestParseLongLines(t *testing.T) {
a := goics.NewDecoder(strings.NewReader(testlonglines))
cons := NewCal()
_ = a.Decode(cons)
str := cons.Data["CALSCALE"]
if len(str) != 81 {
t.Errorf("Multiline test failed %d", len(cons.Data["CALSCALE"]))
}
if strings.Contains("str", " ") {
t.Error("Not handling correct begining of line")
}
}
var testlonglinestab = `BEGIN:VCALENDAR
PRODID;X-RICAL-TZSOURCE=TZINFO:-//test//EN
CALSCALE:GREGORIANGREGORIANGREGORIANGREGORIANGREGORIANGREGORIANGREGORIAN
GREGORIANGREGORIAN
VERSION:2.0
END:VCALENDAR
`
func TestParseLongLinesTab(t *testing.T) {
a := goics.NewDecoder(strings.NewReader(testlonglinestab))
cons := NewCal()
_ = a.Decode(cons)
str := cons.Data["CALSCALE"]
if len(str) != 81 {
t.Errorf("Multiline tab field test failed %d", len(str))
}
if strings.Contains("str", "\t") {
t.Error("Not handling correct begining of line")
}
}
var testlonglinestab3 = `BEGIN:VCALENDAR
PRODID;X-RICAL-TZSOURCE=TZINFO:-//test//EN
CALSCALE:GREGORIANGREGORIANGREGORIANGREGORIANGREGORIANGREGORIANGREGORIAN
GREGORIANGREGORIANGREGORIANGREGORIANGREGORIANGREGORIANGREGORIANGREGORIANGG
GRESESERSERSER
VERSION:2.0
END:VCALENDAR
`
func TestParseLongLinesMultilinethree(t *testing.T) {
a := goics.NewDecoder(strings.NewReader(testlonglinestab3))
cons := NewCal()
_ = a.Decode(cons)
str := cons.Data["CALSCALE"]
if len(str) != 151 {
t.Errorf("Multiline (3lines) tab field test failed %d", len(str))
}
if strings.Contains("str", "\t") {
t.Error("Not handling correct begining of line")
}
}
var valarmCt = `BEGIN:VCALENDAR
BEGIN:VEVENT
STATUS:CONFIRMED
CREATED:20131205T115046Z
DTEND;VALUE=DATE:20140111
TRANSP:OPAQUE
SUMMARY:PASTILLA Cu cs
DTSTART;VALUE=DATE:20140110
DTSTAMP:20131205T115046Z
LAST-MODIFIED:20131205T115046Z
SEQUENCE:0
DESCRIPTION:
BEGIN:VALARM
X-WR-ALARMUID:E283310A-82B3-47CF-A598-FD36634B21F3
UID:E283310A-82B3-47CF-A598-FD36634B21F3
TRIGGER:-PT15H
X-APPLE-DEFAULT-ALARM:TRUE
ATTACH;VALUE=URI:Basso
ACTION:AUDIO
END:VALARM
END:VEVENT
END:VCALENDAR`
func TestNotParsingValarm(t *testing.T) {
a := goics.NewDecoder(strings.NewReader(valarmCt))
cons := NewCal()
err := a.Decode(cons)
if err != nil {
t.Errorf("Error decoding %s", err)
}
}
var testEscapeValue = `BEGIN:VCALENDAR
X-TEST:one\, two\, three\n\Nfour\;five\\six
END:VCALENDAR`
func TestUnescapeValue(t *testing.T) {
a := goics.NewDecoder(strings.NewReader(testEscapeValue))
cons := NewCal()
err := a.Decode(cons)
if err != nil {
t.Errorf("Error decoding %s", err)
}
expected := "one, two, three\n\nfour;five\\six"
if actual := cons.Data["X-TEST"]; expected != actual {
t.Errorf("unexpected summary: %q", actual)
}
}
func TestReadingRealFile(t *testing.T) {
file, err := os.Open("fixtures/test.ics")
if err != nil {
t.Error("Can't read file")
}
defer file.Close()
cal := goics.NewDecoder(file)
cons := NewCal()
err = cal.Decode(cons)
if err != nil {
t.Error("Cant decode a complete file")
}
if len(cal.Calendar.Events) != 28 {
t.Errorf("Wrong number of events detected %d", len(cal.Calendar.Events))
}
}
// Multiple keys with same name...
// From libical tests
// https://github.com/libical/libical/blob/master/test-data/incoming.ics
var dataMultipleAtendee = `BEGIN:VCALENDAR
PRODID:-//ACME/DesktopCalendar//EN
METHOD:REQUEST
X-LIC-NOTE:#I3. Updates C1
X-LIC-EXPECT:REQUEST-UPDATE
VERSION:2.0
BEGIN:VEVENT
ORGANIZER:Mailto:[email protected]
ATTENDEE;ROLE=CHAIR;PARTSTAT=ACCEPTED;CN=BIG A:Mailto:[email protected]
ATTENDEE;RSVP=TRUE;CUTYPE=INDIVIDUAL;CN=B:Mailto:[email protected]
ATTENDEE;RSVP=TRUE;CUTYPE=INDIVIDUAL;CN=C:Mailto:[email protected]
ATTENDEE;RSVP=TRUE;CUTYPE=INDIVIDUAL;CN=Hal:Mailto:[email protected]
ATTENDEE;RSVP=FALSE;CUTYPE=ROOM:[email protected]
ATTENDEE;ROLE=NON-PARTICIPANT;RSVP=FALSE:Mailto:[email protected]
DTSTAMP:19970611T193000Z
DTSTART:19970701T190000Z
DTEND:19970701T193000Z
SUMMARY: Pool party
SEQUENCE:2
STATUS:CONFIRMED
END:VEVENT
END:VCALENDAR`
type EventA struct {
Start, End time.Time
ID, Summary string
Attendees []string
}
type EventsA []EventA
func (e *EventsA) ConsumeICal(c *goics.Calendar, err error) error {
for _, el := range c.Events {
node := el.Data
dtstart, err := node["DTSTART"].DateDecode()
if err != nil {
return err
}
dtend, err := node["DTEND"].DateDecode()
if err != nil {
return err
}
d := EventA{
Start: dtstart,
End: dtend,
ID: node["UID"].Val,
Summary: node["SUMMARY"].Val,
}
// Get Atendees
if val, ok := el.List["ATTENDEE"]; ok {
d.Attendees = make([]string, 0)
for _, n := range val {
d.Attendees = append(d.Attendees, n.Val)
}
}
*e = append(*e, d)
}
return nil
}
func TestDataMultipleAtendee(t *testing.T) {
d := goics.NewDecoder(strings.NewReader(dataMultipleAtendee))
consumer := EventsA{}
err := d.Decode(&consumer)
if err != nil {
t.Error("Error decoding events")
}
if len(consumer) != 1 {
t.Error("Wrong size of consumer list..")
}
if len(consumer[0].Attendees) != 6 {
t.Errorf("Wrong list of atendees detectet %d", len(consumer[0].Attendees))
}
att := consumer[0].Attendees[0]
if att != "Mailto:[email protected]" {
t.Errorf("Atendee list should be %s", att)
}
}
var dataMultiline = `BEGIN:VCALENDAR
BEGIN:VEVENT
ORGANIZER:Mailto:[email protected]
ATTENDEE;
ROLE=CHAIR;
PARTSTAT
=ACCEPTED;CN=
BIG A
:Mailto:[email protected]
ATTENDEE;RSVP=TRUE;CUTYPE=INDIVIDUAL;
CN=B:Mailto:[email protected]
ATTENDEE;RSVP=TRUE;CUTYPE=INDIVIDUAL;CN=C:Mailto:[email protected]
DTSTAMP:19970611T193000Z
DTSTART:19970701T190000Z
DTEND:19970701T193000Z
SUMMARY: Pool party
END:VEVENT
END:VCALENDAR`
func TestMulitine(t *testing.T) {
d := goics.NewDecoder(strings.NewReader(dataMultiline))
consumer := EventsA{}
err := d.Decode(&consumer)
if err != nil {
t.Errorf("Error decoding events %+v", err)
}
if len(consumer) != 1 {
t.Error("Wrong size of consumer list..")
}
if size := len(consumer[0].Attendees); size != 3 {
t.Errorf("Wrong size of attendees detected %d", size)
}
if att := consumer[0].Attendees[0]; att != "Mailto:[email protected]" {
t.Errorf("Attendees list should be %s", att)
}
}