forked from emersion/go-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_test.go
95 lines (86 loc) · 2.62 KB
/
date_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
package imap
import (
"testing"
"time"
)
var expectedDateTime = time.Date(2009, time.November, 2, 23, 0, 0, 0, time.FixedZone("", -6*60*60))
var expectedDate = time.Date(2009, time.November, 2, 0, 0, 0, 0, time.FixedZone("", 0))
func TestParseMessageDateTime(t *testing.T) {
tests := []struct {
in string
out time.Time
ok bool
}{
// some permutations
{"2 Nov 2009 23:00 -0600", expectedDateTime, true},
{"Tue, 2 Nov 2009 23:00:00 -0600", expectedDateTime, true},
{"Tue, 2 Nov 2009 23:00:00 -0600 (MST)", expectedDateTime, true},
// whitespace
{" 2 Nov 2009 23:00 -0600", expectedDateTime, true},
{"Tue, 2 Nov 2009 23:00:00 -0600", expectedDateTime, true},
{"Tue, 2 Nov 2009 23:00:00 -0600 (MST)", expectedDateTime, true},
// invalid
{"abc10 Nov 2009 23:00 -0600123", expectedDateTime, false},
{"10.Nov.2009 11:00:00 -9900", expectedDateTime, false},
}
for _, test := range tests {
out, err := parseMessageDateTime(test.in)
if !test.ok {
if err == nil {
t.Errorf("ParseMessageDateTime(%q) expected error; got %q", test.in, out)
}
} else if err != nil {
t.Errorf("ParseMessageDateTime(%q) expected %q; got %v", test.in, test.out, err)
} else if !out.Equal(test.out) {
t.Errorf("ParseMessageDateTime(%q) expected %q; got %q", test.in, test.out, out)
}
}
}
func TestParseDateTime(t *testing.T) {
tests := []struct {
in string
out time.Time
ok bool
}{
{"2-Nov-2009 23:00:00 -0600", expectedDateTime, true},
// whitespace
{" 2-Nov-2009 23:00:00 -0600", expectedDateTime, true},
// invalid or incorrect
{"10-Nov-2009", time.Time{}, false},
{"abc10-Nov-2009 23:00:00 -0600123", time.Time{}, false},
}
for _, test := range tests {
out, err := time.Parse(DateTimeLayout, test.in)
if !test.ok {
if err == nil {
t.Errorf("ParseDateTime(%q) expected error; got %q", test.in, out)
}
} else if err != nil {
t.Errorf("ParseDateTime(%q) expected %q; got %v", test.in, test.out, err)
} else if !out.Equal(test.out) {
t.Errorf("ParseDateTime(%q) expected %q; got %q", test.in, test.out, out)
}
}
}
func TestParseDate(t *testing.T) {
tests := []struct {
in string
out time.Time
ok bool
}{
{"2-Nov-2009", expectedDate, true},
{" 2-Nov-2009", expectedDate, true},
}
for _, test := range tests {
out, err := time.Parse(DateLayout, test.in)
if !test.ok {
if err == nil {
t.Errorf("ParseDate(%q) expected error; got %q", test.in, out)
}
} else if err != nil {
t.Errorf("ParseDate(%q) expected %q; got %v", test.in, test.out, err)
} else if !out.Equal(test.out) {
t.Errorf("ParseDate(%q) expected %q; got %q", test.in, test.out, out)
}
}
}