-
Notifications
You must be signed in to change notification settings - Fork 3
/
json_test.go
43 lines (37 loc) · 866 Bytes
/
json_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
package tinytime
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMarshalJSON(t *testing.T) {
dat, err := TinyTime{unix: 1585750374}.MarshalJSON()
assert.Nil(t, err)
assert.Equal(t, `"2020-04-01T14:12:54Z"`, string(dat))
s := struct {
TD TinyTime
}{
TD: TinyTime{unix: 1585750374},
}
dat, err = json.Marshal(s)
assert.Nil(t, err)
assert.Equal(t, `{"TD":"2020-04-01T14:12:54Z"}`, string(dat))
}
func TestUnmarshalJSON(t *testing.T) {
dat := []byte(`"2020-04-01T14:12:54Z"`)
tt := TinyTime{}
err := tt.UnmarshalJSON(dat)
assert.Nil(t, err)
assert.Equal(t, TinyTime{unix: 1585750374}, tt)
dat = []byte(`{"TD":"2020-04-01T14:12:54Z"}`)
s := struct {
TD TinyTime
}{}
err = json.Unmarshal(dat, &s)
assert.Nil(t, err)
assert.Equal(t, struct {
TD TinyTime
}{
TD: TinyTime{unix: 1585750374},
}, s)
}