-
Notifications
You must be signed in to change notification settings - Fork 14
/
encoder_test.go
187 lines (141 loc) · 4.12 KB
/
encoder_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
package goics_test
import (
"bytes"
"fmt"
"testing"
"time"
goics "github.com/jordic/goics"
)
func TestComponentCreation(t *testing.T) {
c := goics.NewComponent()
c.SetType("VCALENDAR")
c.AddProperty("CALSCAL", "GREGORIAN")
c.AddProperty("PRODID", "-//tmpo.io/src/goics")
if c.Properties["CALSCAL"][0] != "GREGORIAN" {
t.Error("Error adding property")
}
m := goics.NewComponent()
m.SetType("VEVENT")
m.AddProperty("UID", "testing1")
m.AddProperty("UID", "testing2") // Not that you'd ever _want_ to have multiple UIDs but for testing this is fine.
c.AddComponent(m)
if len(c.Elements) != 1 {
t.Error("Error adding a component")
}
ins := &EventTest{
component: c,
}
w := &bytes.Buffer{}
enc := goics.NewICalEncode(w)
enc.Encode(ins)
want := "BEGIN:VCALENDAR\r\nCALSCAL:GREGORIAN\r\nPRODID:-//tmpo.io/src/goics\r\nBEGIN:VEVENT\r\nUID:testing1\r\nUID:testing2\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"
got := w.String()
if got != want {
t.Errorf("encoded value mismatch:\ngot:\n%s\n\nwant:\n%s", got, want)
}
}
type EventTest struct {
component goics.Componenter
}
func (evt *EventTest) EmitICal() goics.Componenter {
return evt.component
}
func TestWritingSimple(t *testing.T) {
c := goics.NewComponent()
c.SetType("VCALENDAR")
c.AddProperty("CALSCAL", "GREGORIAN")
ins := &EventTest{
component: c,
}
w := &bytes.Buffer{}
enc := goics.NewICalEncode(w)
enc.Encode(ins)
result := &bytes.Buffer{}
fmt.Fprintf(result, "BEGIN:VCALENDAR"+goics.CRLF)
fmt.Fprintf(result, "CALSCAL:GREGORIAN"+goics.CRLF)
fmt.Fprintf(result, "END:VCALENDAR"+goics.CRLF)
res := bytes.Compare(w.Bytes(), result.Bytes())
if res != 0 {
t.Errorf("%s!=%s %d", w, result, res)
}
}
func TestFormatDateFieldFormat(t *testing.T) {
rkey := "DTEND;VALUE=DATE"
rval := "20140406"
ti := time.Date(2014, time.April, 06, 0, 0, 0, 0, time.UTC)
key, val := goics.FormatDateField("DTEND", ti)
if rkey != key {
t.Error("Expected", rkey, "Result", key)
}
if rval != val {
t.Error("Expected", rval, "Result", val)
}
}
func TestFormatDateTimeFieldFormat(t *testing.T) {
rkey := "X-MYDATETIME;VALUE=DATE-TIME"
rval := "20120901T130000"
ti := time.Date(2012, time.September, 01, 13, 0, 0, 0, time.UTC)
key, val := goics.FormatDateTimeField("X-MYDATETIME", ti)
if rkey != key {
t.Error("Expected", rkey, "Result", key)
}
if rval != val {
t.Error("Expected", rval, "Result", val)
}
}
func TestDateTimeFormat(t *testing.T) {
rkey := "DTSTART"
rval := "19980119T070000Z"
ti := time.Date(1998, time.January, 19, 07, 0, 0, 0, time.UTC)
key, val := goics.FormatDateTime("DTSTART", ti)
if rkey != key {
t.Error("Expected", rkey, "Result", key)
}
if rval != val {
t.Error("Expected", rval, "Result", val)
}
}
var shortLine = `asdf defined is a test\n\r`
func TestLineWriter(t *testing.T) {
w := &bytes.Buffer{}
result := &bytes.Buffer{}
fmt.Fprintf(result, shortLine)
encoder := goics.NewICalEncode(w)
encoder.WriteLine(shortLine)
res := bytes.Compare(w.Bytes(), result.Bytes())
if res != 0 {
t.Errorf("%s!=%s", w, result)
}
}
var longLine = `As returned by NewWriter, a Writer writes records terminated by thisisat test that is expanded in multi lines` + goics.CRLF
func TestLineWriterLongLine(t *testing.T) {
w := &bytes.Buffer{}
result := &bytes.Buffer{}
fmt.Fprintf(result, "As returned by NewWriter, a Writer writes records terminated by thisisat ")
fmt.Fprintf(result, goics.CRLFSP)
fmt.Fprintf(result, "test that is expanded in multi lines")
fmt.Fprintf(result, goics.CRLF)
encoder := goics.NewICalEncode(w)
encoder.WriteLine(longLine)
res := bytes.Compare(w.Bytes(), result.Bytes())
if res != 0 {
t.Errorf("%s!=%s %d", w, result, res)
}
}
func Test2ongLineWriter(t *testing.T) {
goics.LineSize = 10
w := &bytes.Buffer{}
result := &bytes.Buffer{}
fmt.Fprintf(result, "12345678")
fmt.Fprintf(result, goics.CRLF)
fmt.Fprintf(result, " 2345678")
fmt.Fprintf(result, goics.CRLF)
fmt.Fprintf(result, " 2345678")
var str = `1234567823456782345678`
encoder := goics.NewICalEncode(w)
encoder.WriteLine(str)
res := bytes.Compare(w.Bytes(), result.Bytes())
if res != 0 {
t.Errorf("%s!=%s %d", w, result, res)
}
}