/
map_test.go
77 lines (62 loc) · 1.41 KB
/
map_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
package slices
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Map_UnmarshalText(t *testing.T) {
r := require.New(t)
m := Map{}
err := m.UnmarshalText([]byte(`{"a":"b"}`))
r.NoError(err)
r.Equal("b", m["a"])
}
func Test_Map_MarshalJSON(t *testing.T) {
r := require.New(t)
m := Map{"a": "b"}
b, err := json.Marshal(m)
r.NoError(err)
r.Equal([]byte(`{"a":"b"}`), b)
}
func Test_Map_UnmarshalJSON(t *testing.T) {
r := require.New(t)
m := Map{}
err := json.Unmarshal([]byte(`{"a":"b"}`), &m)
r.NoError(err)
r.Equal("b", m["a"])
}
// for the next test case
type Ship struct {
Name string
Crew Map `json:"crew"`
}
func Test_Map_UnmarshalJSON_Nested(t *testing.T) {
r := require.New(t)
p := &Ship{}
err := json.Unmarshal([]byte(`{"crew":{"captain":"Han", "navigator":"Chewie"}}`), p)
r.NoError(err)
r.Equal("Han", p.Crew["captain"])
r.Equal("Chewie", p.Crew["navigator"])
}
func Test_Map_UnMarshalJSON_uninitialized_map_does_not_panic(t *testing.T) {
r := require.New(t)
maps := make([]Map, 0)
r.NotPanics(func() {
err := json.Unmarshal([]byte(`[{"a": "b"}]`), &maps)
r.NoError(err)
r.Len(maps, 1)
})
}
func Test_Map_Scan(t *testing.T) {
r := require.New(t)
in := []byte(`{"a":"b"}`)
m := Map{}
r.NoError(m.Scan(in))
r.Equal("b", m["a"])
}
func Test_Map_Null_Scan(t *testing.T) {
r := require.New(t)
m := Map{}
r.NoError(m.Scan(nil))
r.Equal(Map{}, m)
}