-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.time_test.go
95 lines (89 loc) · 3.14 KB
/
encode.time_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 msgpack
import (
"bytes"
"fmt"
"testing"
"time"
"github.com/blugnu/test"
)
func TestTimeEncoders(t *testing.T) {
// ARRANGE
ept := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
buf := &bytes.Buffer{}
enc := Encoder{out: buf}
// ARRANGE
testcases := []struct {
name string
t time.Time
ts string
sut func(time.Time) error
test func(error)
}{
{name: "EncodeRFC3339", t: ept, sut: func(tt time.Time) error { return enc.EncodeRFC3339(tt) }, test: func(error) {
test.BytesEqual(t, append([]byte{maskFixString | 20}, []byte("1970-01-01T00:00:00Z")...), buf.Bytes())
}},
{name: "EncodeRFC3339Micro", t: ept, sut: func(tt time.Time) error { return enc.EncodeRFC3339Micro(tt) }, test: func(error) {
test.BytesEqual(t, append([]byte{maskFixString | 27}, []byte("1970-01-01T00:00:00.000000Z")...), buf.Bytes(), test.BytesString)
}},
{name: "EncodeRFC3339",
t: time.Date(2106, 02, 07, 06, 28, 16, 0, time.UTC),
sut: func(tt time.Time) error { return enc.EncodeRFC3339(tt) }, test: func(error) {
test.BytesEqual(t, append([]byte{maskFixString | 20}, []byte("2106-02-07T06:28:16Z")...), buf.Bytes())
}},
{name: "encodeTimestamp (max ts32)",
t: time.Unix(2<<31-1, 0).UTC(),
sut: func(tt time.Time) error {
test.Equal(t, "2106-02-07T06:28:15Z", tt.Format(time.RFC3339))
return encodeTimestamp(&enc, tt)
},
test: func(error) {
test.BytesEqual(t, []byte{typeFixExt4, 0xff, 0xff, 0xff, 0xff, 0xff}, buf.Bytes())
}},
{name: "encodeTimestamp (min ts64)",
t: time.Unix(2<<31, 0).UTC(),
sut: func(tt time.Time) error {
test.Equal(t, "2106-02-07T06:28:16Z", tt.Format(time.RFC3339))
return encodeTimestamp(&enc, tt)
},
test: func(error) {
test.BytesEqual(t, []byte{typeFixExt8, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, buf.Bytes())
}},
{name: "encodeTimestamp (max ts64)",
t: time.Unix(2<<33-1, 0).UTC(),
sut: func(tt time.Time) error {
test.Equal(t, "2514-05-30T01:53:03Z", tt.Format(time.RFC3339))
return encodeTimestamp(&enc, tt)
},
test: func(error) {
test.BytesEqual(t, []byte{typeFixExt8, 0xff, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff}, buf.Bytes())
}},
{name: "encodeTimestamp (min pos ts96)",
t: time.Unix(2<<33, 0).UTC(),
sut: func(tt time.Time) error {
test.Equal(t, "2514-05-30T01:53:04Z", tt.Format(time.RFC3339))
return encodeTimestamp(&enc, tt)
},
test: func(error) {
test.BytesEqual(t, []byte{typeExt8, 12, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00}, buf.Bytes())
}},
{name: "encodeTimestamp (max ts96)",
t: time.Unix(2<<62-1, 999999999).UTC(),
sut: func(tt time.Time) error {
test.Equal(t, "292277026596-12-04T15:30:07.999999999Z", tt.Format(time.RFC3339Nano))
return encodeTimestamp(&enc, tt)
},
test: func(error) {
test.BytesEqual(t, []byte{typeExt8, 12, 0xff, 0x2a, 0x4a, 0xe5, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, buf.Bytes())
}},
}
for _, tc := range testcases {
t.Run(fmt.Sprintf("%s(%s)", tc.name, tc.t), func(t *testing.T) {
// ARRANGE
buf.Reset()
// ACT
err := tc.sut(tc.t)
// ASSERT
tc.test(err)
})
}
}