Skip to content

Commit a98ecab

Browse files
committed
test: add test for empty slice
1 parent bd4e27d commit a98ecab

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

codec_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -70,48 +70,96 @@ func TestMarshalInt32Slice(t *testing.T) {
7070
})
7171
}
7272

73+
func TestMarshalInt32SliceNil(t *testing.T) {
74+
testMarshalBasicSlice(t, []int32{}, func(v []byte) (interface{}, error) {
75+
return ToInt32Slice(v)
76+
})
77+
}
78+
7379
func TestMarshalUInt32Slice(t *testing.T) {
7480
testMarshalBasicSlice(t, []uint32{11, 22}, func(v []byte) (interface{}, error) {
7581
return ToUInt32Slice(v)
7682
})
7783
}
7884

85+
func TestMarshalUInt32SliceNil(t *testing.T) {
86+
testMarshalBasicSlice(t, []uint32{}, func(v []byte) (interface{}, error) {
87+
return ToUInt32Slice(v)
88+
})
89+
}
90+
7991
func TestMarshalInt64Slice(t *testing.T) {
8092
testMarshalBasicSlice(t, []int64{-4097, 255}, func(v []byte) (interface{}, error) {
8193
return ToInt64Slice(v)
8294
})
8395
}
8496

97+
func TestMarshalInt64SliceNil(t *testing.T) {
98+
testMarshalBasicSlice(t, []int64{}, func(v []byte) (interface{}, error) {
99+
return ToInt64Slice(v)
100+
})
101+
}
102+
85103
func TestMarshalUInt64Slice(t *testing.T) {
86104
testMarshalBasicSlice(t, []uint64{0, 1}, func(v []byte) (interface{}, error) {
87105
return ToUInt64Slice(v)
88106
})
89107
}
90108

109+
func TestMarshalUInt64SliceNil(t *testing.T) {
110+
testMarshalBasicSlice(t, []uint64{}, func(v []byte) (interface{}, error) {
111+
return ToUInt64Slice(v)
112+
})
113+
}
114+
91115
func TestMarshalFloat32Slice(t *testing.T) {
92116
testMarshalBasicSlice(t, []float32{0.25, 0.375}, func(v []byte) (interface{}, error) {
93117
return ToFloat32Slice(v)
94118
})
95119
}
96120

121+
func TestMarshalFloat32SliceNil(t *testing.T) {
122+
testMarshalBasicSlice(t, []float32{}, func(v []byte) (interface{}, error) {
123+
return ToFloat32Slice(v)
124+
})
125+
}
126+
97127
func TestMarshalFloat64Slice(t *testing.T) {
98128
testMarshalBasicSlice(t, []float64{0.12, 0.45}, func(v []byte) (interface{}, error) {
99129
return ToFloat64Slice(v)
100130
})
101131
}
102132

133+
func TestMarshalFloat64SliceNil(t *testing.T) {
134+
testMarshalBasicSlice(t, []float64{}, func(v []byte) (interface{}, error) {
135+
return ToFloat64Slice(v)
136+
})
137+
}
138+
103139
func TestMarshalBoolSlice(t *testing.T) {
104140
testMarshalBasicSlice(t, []bool{true, false}, func(v []byte) (interface{}, error) {
105141
return ToBoolSlice(v)
106142
})
107143
}
108144

145+
func TestMarshalBoolSliceNil(t *testing.T) {
146+
testMarshalBasicSlice(t, []bool{}, func(v []byte) (interface{}, error) {
147+
return ToBoolSlice(v)
148+
})
149+
}
150+
109151
func TestMarshalUTF8StringSlice(t *testing.T) {
110152
testMarshalBasicSlice(t, []string{"a", "b"}, func(v []byte) (interface{}, error) {
111153
return ToUTF8StringSlice(v)
112154
})
113155
}
114156

157+
func TestMarshalUTF8StringSliceNil(t *testing.T) {
158+
testMarshalBasicSlice(t, []string{}, func(v []byte) (interface{}, error) {
159+
return ToUTF8StringSlice(v)
160+
})
161+
}
162+
115163
func testMarshalBasicSlice(t *testing.T, expected interface{}, converter func(v []byte) (interface{}, error)) {
116164
flag := false
117165

@@ -263,6 +311,32 @@ func TestMarshalObjectSlice(t *testing.T) {
263311
}
264312
}
265313

314+
func TestMarshalObjectSliceNil(t *testing.T) {
315+
flag := false
316+
input := exampleSliceNil{
317+
Names: []string{},
318+
}
319+
320+
codec := NewCodec(0x30)
321+
inputBuf, _ := codec.Marshal(input)
322+
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
323+
324+
testDecoder(0x10, inputBuf, func(v []byte) (interface{}, error) {
325+
flag = true
326+
testPrintf("v=%#x\n", v)
327+
var mold []string
328+
err := ToObject(v, &mold)
329+
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
330+
testPrintf("mold=%v\n", mold)
331+
assert.Equal(t, 0, len(mold), fmt.Sprintf("value len does not match(%v): %v", 0, len(mold)))
332+
return mold, err
333+
})
334+
335+
if !flag {
336+
t.Errorf("Observable does not listen to values")
337+
}
338+
}
339+
266340
type exampleData struct {
267341
Name string `y3:"0x10"`
268342
Noise float32 `y3:"0x11"`
@@ -277,3 +351,7 @@ type thermometer struct {
277351
type exampleSlice struct {
278352
Therms []thermometer `y3:"0x12"`
279353
}
354+
355+
type exampleSliceNil struct {
356+
Names []string `y3:"0x10"`
357+
}

0 commit comments

Comments
 (0)