-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathcomponents_test.go
192 lines (169 loc) · 4.24 KB
/
components_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
package ics
import (
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSetDuration(t *testing.T) {
date, _ := time.Parse(time.RFC822, time.RFC822)
duration := time.Duration(float64(time.Hour) * 2)
testCases := []struct {
name string
start time.Time
end time.Time
output string
}{
{
name: "test set duration - start",
start: date,
output: `BEGIN:VEVENT
UID:test-duration
DTSTART:20060102T150400Z
DTEND:20060102T170400Z
END:VEVENT
`,
},
{
name: "test set duration - end",
end: date,
output: `BEGIN:VEVENT
UID:test-duration
DTEND:20060102T150400Z
DTSTART:20060102T130400Z
END:VEVENT
`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := NewEvent("test-duration")
if !tc.start.IsZero() {
e.SetStartAt(tc.start)
}
if !tc.end.IsZero() {
e.SetEndAt(tc.end)
}
err := e.SetDuration(duration)
// we're not testing for encoding here so lets make the actual output line breaks == expected line breaks
text := strings.ReplaceAll(e.Serialize(defaultSerializationOptions()), "\r\n", "\n")
assert.Equal(t, tc.output, text)
assert.Equal(t, nil, err)
})
}
}
func TestSetAllDay(t *testing.T) {
date, _ := time.Parse(time.RFC822, time.RFC822)
testCases := []struct {
name string
start time.Time
end time.Time
duration time.Duration
output string
}{
{
name: "test set all day - start",
start: date,
output: `BEGIN:VEVENT
UID:test-allday
DTSTART;VALUE=DATE:20060102
END:VEVENT
`,
},
{
name: "test set all day - end",
end: date,
output: `BEGIN:VEVENT
UID:test-allday
DTEND;VALUE=DATE:20060102
END:VEVENT
`,
},
{
name: "test set all day - duration",
start: date,
duration: time.Hour * 24,
output: `BEGIN:VEVENT
UID:test-allday
DTSTART;VALUE=DATE:20060102
DTEND;VALUE=DATE:20060103
END:VEVENT
`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := NewEvent("test-allday")
if !tc.start.IsZero() {
e.SetAllDayStartAt(tc.start)
}
if !tc.end.IsZero() {
e.SetAllDayEndAt(tc.end)
}
if tc.duration != 0 {
err := e.SetDuration(tc.duration)
assert.NoError(t, err)
}
text := strings.ReplaceAll(e.Serialize(defaultSerializationOptions()), "\r\n", "\n")
assert.Equal(t, tc.output, text)
})
}
}
func TestGetLastModifiedAt(t *testing.T) {
e := NewEvent("test-last-modified")
lastModified := time.Unix(123456789, 0)
e.SetLastModifiedAt(lastModified)
got, err := e.GetLastModifiedAt()
if err != nil {
t.Fatalf("e.GetLastModifiedAt: %v", err)
}
if !got.Equal(lastModified) {
t.Errorf("got last modified = %q, want %q", got, lastModified)
}
}
func TestSetMailtoPrefix(t *testing.T) {
e := NewEvent("test-set-organizer")
e.SetOrganizer("org1@provider.com")
if !strings.Contains(e.Serialize(defaultSerializationOptions()), "ORGANIZER:mailto:org1@provider.com") {
t.Errorf("expected single mailto: prefix for email org1")
}
e.SetOrganizer("mailto:org2@provider.com")
if !strings.Contains(e.Serialize(defaultSerializationOptions()), "ORGANIZER:mailto:org2@provider.com") {
t.Errorf("expected single mailto: prefix for email org2")
}
e.AddAttendee("att1@provider.com")
if !strings.Contains(e.Serialize(defaultSerializationOptions()), "ATTENDEE:mailto:att1@provider.com") {
t.Errorf("expected single mailto: prefix for email att1")
}
e.AddAttendee("mailto:att2@provider.com")
if !strings.Contains(e.Serialize(defaultSerializationOptions()), "ATTENDEE:mailto:att2@provider.com") {
t.Errorf("expected single mailto: prefix for email att2")
}
}
func TestRemoveProperty(t *testing.T) {
testCases := []struct {
name string
output string
}{
{
name: "test RemoveProperty - start",
output: `BEGIN:VTODO
UID:test-removeproperty
X-TEST:42
END:VTODO
`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := NewTodo("test-removeproperty")
e.AddProperty("X-TEST", "42")
e.AddProperty("X-TESTREMOVE", "FOO")
e.AddProperty("X-TESTREMOVE", "BAR")
e.RemoveProperty("X-TESTREMOVE")
// adjust to expected linebreaks, since we're not testing the encoding
text := strings.ReplaceAll(e.Serialize(defaultSerializationOptions()), "\r\n", "\n")
assert.Equal(t, tc.output, text)
})
}
}