-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_test.go
More file actions
128 lines (112 loc) · 3.04 KB
/
data_test.go
File metadata and controls
128 lines (112 loc) · 3.04 KB
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
package xmldatetime
import (
"bytes"
"encoding/xml"
"testing"
"time"
)
type ParseFunc func(string) (time.Time, error)
func cutOut(s string, i int) string {
if i == 0 {
return s[1:]
} else if i == len(s)-1 {
return s[:i]
}
return s[:i] + s[i+1:]
}
func TestParseIncorrect(t *testing.T) {
fullS := "2017-08-16T13:07:00.1+02:00"
for _, f := range []ParseFunc{Parse, ParseRe, ParseRe2} {
for i := 0; i < len(fullS); i++ {
v := cutOut(fullS, i)
_, err := f(v)
if err == nil {
t.Errorf("want error, got nil: %s", v)
t.FailNow()
}
}
}
}
func TestParse(t *testing.T) {
for _, f := range []ParseFunc{Parse, ParseRe, ParseRe2} {
for _, v := range []string{
"2017-08-16T13:07:00.09251+02:00",
"2017-08-16T11:07:00.09251Z",
"2017-08-16T11:07:00.09251",
} {
tm, err := f(v)
if err != nil {
t.Errorf("error: %s", err)
t.FailNow()
}
ex := time.Date(2017, time.August, 16, 11, 07, 0, 92510000, time.UTC)
if !tm.UTC().Equal(ex.UTC()) {
t.Errorf("want: %v, got: %s", ex, tm)
}
}
}
// error "2017-08-16T11:07:00.092510",
}
func TestStringify(t *testing.T) {
ex := time.Date(2017, time.August, 16, 11, 07, 0, 92510000, time.UTC)
if s := stringify(ex); s != "2017-08-16T11:07:00.09251" {
t.Errorf("want: 2017-08-16T11:07:00.09251, got: %s", s)
t.FailNow()
}
ex = time.Date(2017, time.August, 16, 11, 07, 0, 92510000, time.FixedZone("-02:00", -2*60*60))
if s := stringify(ex); s != "2017-08-16T11:07:00.09251-02:00" {
t.Errorf("want: 2017-08-16T11:07:00.09251-02:00, got: %s", s)
t.FailNow()
}
ex = time.Date(2017, time.August, 16, 11, 07, 0, 0, time.FixedZone("-02:00", -2*60*60))
if s := stringify(ex); s != "2017-08-16T11:07:00-02:00" {
t.Errorf("want: 2017-08-16T11:07:00-02:00, got: %s", s)
t.FailNow()
}
}
func TestCustomTime_MarshalXML(t *testing.T) {
ex := time.Date(2017, time.August, 16, 13, 07, 0, 92510000, time.FixedZone("+02:00", 2*60*60))
c := CustomTime{ex}
got, err := xml.Marshal(c)
if err != nil {
t.Errorf("marshaling: %s", err)
t.FailNow()
}
want := `<CustomTime>2017-08-16T13:07:00.09251+02:00</CustomTime>`
if !bytes.Equal([]byte(want), got) {
t.Errorf("want: %v, got: %s", want, got)
t.FailNow()
}
}
func TestCustomTime_UnmarshalXML(t *testing.T) {
xmlS := `<someTime>2017-08-16T13:07:00.09251+02:00</someTime>`
c := CustomTime{}
err := xml.Unmarshal([]byte(xmlS), &c)
if err != nil {
t.Errorf("problem with unmarshal: %s", err)
}
ex := time.Date(2017, time.August, 16, 13, 07, 0, 92510000, time.FixedZone("+02:00", 2*60*60))
if !c.Time.Equal(ex) {
t.Errorf("want: %s, got: %s", ex, c.Time)
t.FailNow()
}
if !c.Time.UTC().Equal(ex.UTC()) {
t.Errorf("want: %s, got: %s", ex.UTC(), c.Time.UTC())
t.FailNow()
}
}
func BenchmarkParse(b *testing.B) {
for i := 0; i < b.N; i++ {
Parse("2017-08-16T13:07:00.09251+02:00")
}
}
func BenchmarkParseRe(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseRe("2017-08-16T13:07:00.09251+02:00")
}
}
func BenchmarkParseRe2(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseRe2("2017-08-16T13:07:00.09251+02:00")
}
}