-
Notifications
You must be signed in to change notification settings - Fork 10
/
parse_test.go
149 lines (142 loc) · 3.04 KB
/
parse_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
package ical
import (
"fmt"
"os"
"reflect"
"testing"
"time"
)
var calendarList = []string{"fixtures/example.ics", "fixtures/with-alarm.ics", "fixtures/facebookbirthday.ics", "fixtures/malformed-date.ics"}
func TestParse(t *testing.T) {
for _, filename := range calendarList {
file, _ := os.Open(filename)
_, err := Parse(file, nil)
file.Close()
if err != nil {
t.Error(fmt.Errorf("%v on '%s'", err, filename))
}
}
}
func BenchmarkParse(b *testing.B) {
for _, filename := range calendarList {
file, _ := os.Open(filename)
info, _ := file.Stat()
b.Run(filename, func(b *testing.B) {
b.SetBytes(info.Size())
for n := 0; n < b.N; n++ {
_, _ = Parse(file, nil)
}
})
}
}
func Test_parseDate(t *testing.T) {
loc, _ := time.LoadLocation("America/New_York")
type args struct {
prop *Property
l *time.Location
}
tests := []struct {
name string
args args
want time.Time
}{
{
name: "Property with only date layout",
args: args{
prop: &Property{
Name: "DTSTART",
Params: map[string]*Param{
"VALUE": {
Values: []string{"DATE"},
},
},
Value: "19980119",
},
l: time.Local,
},
want: time.Date(1998, time.January, 19, 0, 0, 0, 0, time.Local),
},
{
name: "Floating (local) date-time (no time zone)",
args: args{
prop: &Property{
Name: "DTSTART",
Params: map[string]*Param{
"TZID": {
Values: []string{"America/New_York"},
},
},
Value: "19980119T020000",
},
l: time.Local,
},
want: time.Date(1998, time.January, 19, 2, 0, 0, 0, loc),
},
{
name: "Date with no layout indication",
args: args{
prop: &Property{
Name: "DSTART",
Value: "19980119T020000",
},
l: time.Local,
},
want: time.Date(1998, time.January, 19, 2, 0, 0, 0, time.Local),
},
{
name: "Date with no layout and in UTC",
args: args{
prop: &Property{
Name: "DSTART",
Value: "19980119T070000Z",
},
l: time.Local,
},
want: time.Date(1998, time.January, 19, 7, 0, 0, 0, time.UTC),
},
{
name: "Property with date-time layout",
args: args{
prop: &Property{
Name: "DSTART",
Params: map[string]*Param{
"VALUE": {
Values: []string{"DATE-TIME"},
},
},
Value: "19980119T070000",
},
l: time.Local,
},
want: time.Date(1998, time.January, 19, 7, 0, 0, 0, time.Local),
},
{
name: "Datetime with bad timezone",
args: args{
prop: &Property{
Name: "DTSTART",
Params: map[string]*Param{
"TZID": {
Values: []string{"Z"},
},
},
Value: "19980119T020000",
},
l: time.Local,
},
want: time.Date(1998, time.January, 19, 2, 0, 0, 0, time.UTC),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseDate(tt.args.prop, tt.args.l)
if (err != nil) != false {
t.Errorf("parseDate() error = %v, wantErr %v", err, false)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseDate() = %v, want %v", got, tt.want)
}
})
}
}