-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjson_test.go
41 lines (36 loc) · 827 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
package tinydate
import (
"encoding/json"
"testing"
)
func TestMarshalJSON(t *testing.T) {
dat, err := TinyDate{year: 2020, month: 1, day: 3}.MarshalJSON()
assertNil(t, err)
assertEqual(t, `"2020-02-04"`, string(dat))
s := struct {
TD TinyDate
}{
TD: TinyDate{year: 2020, month: 1, day: 3},
}
dat, err = json.Marshal(s)
assertNil(t, err)
assertEqual(t, `{"TD":"2020-02-04"}`, string(dat))
}
func TestUnmarshalJSON(t *testing.T) {
dat := []byte(`"2020-02-04"`)
td := TinyDate{}
err := td.UnmarshalJSON(dat)
assertNil(t, err)
assertEqual(t, TinyDate{year: 2020, month: 1, day: 3}, td)
dat = []byte(`{"TD":"2020-02-04"}`)
s := struct {
TD TinyDate
}{}
err = json.Unmarshal(dat, &s)
assertNil(t, err)
assertEqual(t, struct {
TD TinyDate
}{
TD: TinyDate{year: 2020, month: 1, day: 3},
}, s)
}