forked from francoispqt/gojay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_slice_test.go
187 lines (167 loc) · 3.39 KB
/
encode_slice_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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package gojay
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func (s *slicesTestObject) MarshalJSONObject(enc *Encoder) {
enc.AddSliceStringKey("sliceString", s.sliceString)
enc.AddSliceIntKey("sliceInt", s.sliceInt)
enc.AddSliceFloat64Key("sliceFloat64", s.sliceFloat64)
enc.AddSliceBoolKey("sliceBool", s.sliceBool)
}
func (s *slicesTestObject) IsNil() bool {
return s == nil
}
func TestEncodeSlices(t *testing.T) {
testCases := []struct {
name string
json string
obj slicesTestObject
}{
{
name: "basic slice string",
json: `{
"sliceString": ["foo","bar"],
"sliceInt": [],
"sliceFloat64": [],
"sliceBool": []
}`,
obj: slicesTestObject{
sliceString: []string{"foo", "bar"},
},
},
{
name: "basic slice bool",
json: `{
"sliceString": [],
"sliceInt": [],
"sliceFloat64": [],
"sliceBool": [true,false]
}`,
obj: slicesTestObject{
sliceBool: []bool{true, false},
},
},
{
name: "basic slice int",
json: `{
"sliceString": [],
"sliceFloat64": [],
"sliceInt": [1,2,3],
"sliceBool": []
}`,
obj: slicesTestObject{
sliceInt: []int{1, 2, 3},
},
},
{
name: "basic slice float64",
json: `{
"sliceString": [],
"sliceFloat64": [1.3,2.4,3.1],
"sliceInt": [],
"sliceBool": []
}`,
obj: slicesTestObject{
sliceFloat64: []float64{1.3, 2.4, 3.1},
},
},
}
for _, testCase := range testCases {
t.Run(
testCase.name,
func(t *testing.T) {
b := strings.Builder{}
enc := BorrowEncoder(&b)
err := enc.Encode(&testCase.obj)
require.Nil(t, err, "err should be nil")
require.JSONEq(t, testCase.json, b.String())
},
)
}
}
type testSliceSliceString [][]string
func (t testSliceSliceString) MarshalJSONArray(enc *Encoder) {
for _, s := range t {
enc.AddSliceString(s)
}
}
func (t testSliceSliceString) IsNil() bool {
return t == nil
}
type testSliceSliceBool [][]bool
func (t testSliceSliceBool) MarshalJSONArray(enc *Encoder) {
for _, s := range t {
enc.AddSliceBool(s)
}
}
func (t testSliceSliceBool) IsNil() bool {
return t == nil
}
type testSliceSliceInt [][]int
func (t testSliceSliceInt) MarshalJSONArray(enc *Encoder) {
for _, s := range t {
enc.AddSliceInt(s)
}
}
func (t testSliceSliceInt) IsNil() bool {
return t == nil
}
type testSliceSliceFloat64 [][]float64
func (t testSliceSliceFloat64) MarshalJSONArray(enc *Encoder) {
for _, s := range t {
enc.AddSliceFloat64(s)
}
}
func (t testSliceSliceFloat64) IsNil() bool {
return t == nil
}
func TestEncodeSliceSlices(t *testing.T) {
testCases := []struct {
name string
s MarshalerJSONArray
json string
}{
{
name: "slice of strings",
s: testSliceSliceString{
[]string{"foo", "bar"},
},
json: `[["foo","bar"]]`,
},
{
name: "slice of ints",
s: testSliceSliceInt{
[]int{1, 2},
},
json: `[[1,2]]`,
},
{
name: "slice of float",
s: testSliceSliceFloat64{
[]float64{1.1, 1.2},
},
json: `[[1.1,1.2]]`,
},
{
name: "slice of bool",
s: testSliceSliceBool{
[]bool{true, false},
},
json: `[[true,false]]`,
},
}
for _, testCase := range testCases {
t.Run(
testCase.name,
func(t *testing.T) {
b := strings.Builder{}
enc := BorrowEncoder(&b)
err := enc.Encode(testCase.s)
require.Nil(t, err, "err should be nil")
require.JSONEq(t, testCase.json, b.String())
},
)
}
}