Skip to content

Commit 6bf421b

Browse files
authored
refactor(*) adding unit test cases, etc. (#57)
1 parent da5b384 commit 6bf421b

File tree

9 files changed

+163
-120
lines changed

9 files changed

+163
-120
lines changed

basic.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (e *basicEncoderImpl) encodeBasicBool(input interface{}) *PrimitivePacketEn
217217
// encodeBasicStringSlice encode reflect.Value of []string to NodePacketEncoder
218218
func (e *basicEncoderImpl) encodeBasicStringSlice(value reflect.Value) *NodePacketEncoder {
219219
var node = NewNodeSlicePacketEncoder(int(e.observe))
220-
if out, ok := utils.ToStringSliceArray(value.Interface()); ok {
220+
if out, ok := utils.ToStringSlice(value.Interface()); ok {
221221
for _, v := range out {
222222
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
223223
item.SetStringValue(fmt.Sprintf("%v", v))
@@ -230,7 +230,7 @@ func (e *basicEncoderImpl) encodeBasicStringSlice(value reflect.Value) *NodePack
230230
// encodeBasicInt32Slice encode reflect.Value of []int32 to NodePacketEncoder
231231
func (e *basicEncoderImpl) encodeBasicInt32Slice(value reflect.Value) *NodePacketEncoder {
232232
var node = NewNodeSlicePacketEncoder(int(e.observe))
233-
if out, ok := utils.ToInt64SliceArray(value.Interface()); ok {
233+
if out, ok := utils.ToInt64Slice(value.Interface()); ok {
234234
for _, v := range out {
235235
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
236236
item.SetInt32Value(int32(v.(int64)))
@@ -243,7 +243,7 @@ func (e *basicEncoderImpl) encodeBasicInt32Slice(value reflect.Value) *NodePacke
243243
// encodeBasicUint32Slice encode reflect.Value of []uint32 to NodePacketEncoder
244244
func (e *basicEncoderImpl) encodeBasicUint32Slice(value reflect.Value) *NodePacketEncoder {
245245
var node = NewNodeSlicePacketEncoder(int(e.observe))
246-
if out, ok := utils.ToUInt64SliceArray(value.Interface()); ok {
246+
if out, ok := utils.ToUInt64Slice(value.Interface()); ok {
247247
for _, v := range out {
248248
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
249249
item.SetUInt32Value(uint32(v.(uint64)))
@@ -256,7 +256,7 @@ func (e *basicEncoderImpl) encodeBasicUint32Slice(value reflect.Value) *NodePack
256256
// encodeBasicInt64Slice encode reflect.Value of []int64 to NodePacketEncoder
257257
func (e *basicEncoderImpl) encodeBasicInt64Slice(value reflect.Value) *NodePacketEncoder {
258258
var node = NewNodeSlicePacketEncoder(int(e.observe))
259-
if out, ok := utils.ToInt64SliceArray(value.Interface()); ok {
259+
if out, ok := utils.ToInt64Slice(value.Interface()); ok {
260260
for _, v := range out {
261261
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
262262
item.SetInt64Value(v.(int64))
@@ -269,7 +269,7 @@ func (e *basicEncoderImpl) encodeBasicInt64Slice(value reflect.Value) *NodePacke
269269
// encodeBasicUint64Slice encode reflect.Value of []uint64 to NodePacketEncoder
270270
func (e *basicEncoderImpl) encodeBasicUint64Slice(value reflect.Value) *NodePacketEncoder {
271271
var node = NewNodeSlicePacketEncoder(int(e.observe))
272-
if out, ok := utils.ToUInt64SliceArray(value.Interface()); ok {
272+
if out, ok := utils.ToUInt64Slice(value.Interface()); ok {
273273
for _, v := range out {
274274
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
275275
item.SetUInt64Value(v.(uint64))
@@ -282,7 +282,7 @@ func (e *basicEncoderImpl) encodeBasicUint64Slice(value reflect.Value) *NodePack
282282
// encodeBasicFloat32Slice encode reflect.Value of []float32 to NodePacketEncoder
283283
func (e *basicEncoderImpl) encodeBasicFloat32Slice(value reflect.Value) *NodePacketEncoder {
284284
var node = NewNodeSlicePacketEncoder(int(e.observe))
285-
if out, ok := utils.ToUFloat64SliceArray(value.Interface()); ok {
285+
if out, ok := utils.ToUFloat64Slice(value.Interface()); ok {
286286
for _, v := range out {
287287
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
288288
item.SetFloat32Value(float32(v.(float64)))
@@ -295,7 +295,7 @@ func (e *basicEncoderImpl) encodeBasicFloat32Slice(value reflect.Value) *NodePac
295295
// encodeBasicFloat64Slice encode reflect.Value of []float64 to NodePacketEncoder
296296
func (e *basicEncoderImpl) encodeBasicFloat64Slice(value reflect.Value) *NodePacketEncoder {
297297
var node = NewNodeSlicePacketEncoder(int(e.observe))
298-
if out, ok := utils.ToUFloat64SliceArray(value.Interface()); ok {
298+
if out, ok := utils.ToUFloat64Slice(value.Interface()); ok {
299299
for _, v := range out {
300300
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
301301
item.SetFloat64Value(v.(float64))
@@ -308,7 +308,7 @@ func (e *basicEncoderImpl) encodeBasicFloat64Slice(value reflect.Value) *NodePac
308308
// encodeBasicBoolSlice encode reflect.Value of []bool to NodePacketEncoder
309309
func (e *basicEncoderImpl) encodeBasicBoolSlice(value reflect.Value) *NodePacketEncoder {
310310
var node = NewNodeSlicePacketEncoder(int(e.observe))
311-
if out, ok := utils.ToBoolSliceArray(value.Interface()); ok {
311+
if out, ok := utils.ToBoolSlice(value.Interface()); ok {
312312
for _, v := range out {
313313
var item = NewPrimitivePacketEncoder(utils.KeyOfSliceItem)
314314
item.SetBoolValue(v.(bool))

internal/mark/tag_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package mark
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestIsNode(t *testing.T) {
11+
var expected byte = 0x81
12+
tag := NewTag(expected)
13+
assert.True(t, tag.IsNode(), "should be a node")
14+
assert.False(t, tag.IsSlice(), "It should not be a Slice")
15+
assert.Equal(t, expected, tag.Raw(), fmt.Sprintf("value does not match(%v): %v", expected, tag.Raw()))
16+
assert.Equal(t, byte(0x01), tag.SeqID(), fmt.Sprintf("value does not match(%v): %v", 0x01, tag.SeqID()))
17+
}

internal/pprof/pprof.go

Lines changed: 0 additions & 87 deletions
This file was deleted.

internal/tester/data_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package tester
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestBasicTestData(t *testing.T) {
11+
input := BasicTestData{
12+
Vstring: "foo",
13+
Vint32: int32(127),
14+
Vint64: int64(-1),
15+
Vuint32: uint32(130),
16+
Vuint64: uint64(18446744073709551615),
17+
Vfloat32: float32(0.25),
18+
Vfloat64: float64(23),
19+
Vbool: true,
20+
}
21+
assert.NotEmpty(t, input, "Should not equal empty")
22+
assert.Equal(t, "foo", input.Vstring, fmt.Sprintf("value does not match(%v): %v", "foo", input.Vstring))
23+
}

internal/utils/format_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestFormatBytes(t *testing.T) {
11+
expected := "0x81 0xa 0x2 0x1 0x61 0x3 0x1 0x62 0x10 0x2 0x83 0x48"
12+
data := []byte{0x81, 0xa, 0x2, 0x1, 0x61, 0x3, 0x1, 0x62, 0x10, 0x2, 0x83, 0x48}
13+
value := FormatBytes(data)
14+
assert.Equal(t, expected, value, fmt.Sprintf("value does not match(%v): %v", expected, value))
15+
}

internal/utils/key_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestKeyOf(t *testing.T) {
11+
value := KeyOf("0x01")
12+
assert.Equal(t, byte(0x01), value, fmt.Sprintf("value does not match(%v): %v", byte(0x01), value))
13+
14+
value = KeyOf("0X01")
15+
assert.Equal(t, byte(0x01), value, fmt.Sprintf("value does not match(%v): %v", byte(0x01), value))
16+
17+
value = KeyOf("01")
18+
assert.Equal(t, byte(0x01), value, fmt.Sprintf("value does not match(%v): %v", byte(0x01), value))
19+
}
20+
21+
func TestIsEmptyKey(t *testing.T) {
22+
assert.True(t, IsEmptyKey(0x00), "0x00 is a empty key")
23+
}
24+
25+
func TestForbiddenCustomizedKey(t *testing.T) {
26+
assert.True(t, ForbiddenCustomizedKey(0x01), "0x01 is disabled")
27+
assert.False(t, ForbiddenCustomizedKey(0x10), "0x10 is allowed")
28+
}
29+
30+
func TestAllowableSignalKey(t *testing.T) {
31+
assert.True(t, AllowableSignalKey(0x02), "0x01 is allowed")
32+
assert.False(t, AllowableSignalKey(0x01), "0x10 is disabled")
33+
}

internal/utils/slice.go

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,47 @@ import (
66
"strconv"
77
)
88

9-
// ToSliceArray converting interface to interface slice
10-
func ToSliceArray(arg interface{}) (arr []interface{}, ok bool) {
11-
return ToSliceArrayWith(arg, func(value reflect.Value) interface{} {
12-
return value.Interface()
13-
})
14-
}
15-
16-
// ToStringSliceArray converting interface to interface slice for string
17-
func ToStringSliceArray(arg interface{}) (out []interface{}, ok bool) {
18-
return ToSliceArrayWith(arg, func(value reflect.Value) interface{} {
9+
// ToStringSlice converting interface to interface slice for string
10+
func ToStringSlice(arg interface{}) (out []interface{}, ok bool) {
11+
return ToSliceWith(arg, func(value reflect.Value) interface{} {
1912
return fmt.Sprintf("%v", value)
2013
})
2114
}
2215

23-
// ToInt64SliceArray converting interface to interface slice for int64
24-
func ToInt64SliceArray(arg interface{}) (out []interface{}, ok bool) {
25-
return ToSliceArrayWith(arg, func(value reflect.Value) interface{} {
16+
// ToInt64Slice converting interface to interface slice for int64
17+
func ToInt64Slice(arg interface{}) (out []interface{}, ok bool) {
18+
return ToSliceWith(arg, func(value reflect.Value) interface{} {
2619
i64, _ := strconv.ParseInt(fmt.Sprintf("%v", value), 10, 64)
2720
return i64
2821
})
2922
}
3023

31-
// ToUInt64SliceArray converting interface to interface slice for uint64
32-
func ToUInt64SliceArray(arg interface{}) (out []interface{}, ok bool) {
33-
return ToSliceArrayWith(arg, func(value reflect.Value) interface{} {
24+
// ToUInt64Slice converting interface to interface slice for uint64
25+
func ToUInt64Slice(arg interface{}) (out []interface{}, ok bool) {
26+
return ToSliceWith(arg, func(value reflect.Value) interface{} {
3427
ui64, _ := strconv.ParseUint(fmt.Sprintf("%v", value), 10, 64)
3528
return ui64
3629
})
3730
}
3831

39-
// ToUFloat64SliceArray converting interface to interface slice for float64
40-
func ToUFloat64SliceArray(arg interface{}) (out []interface{}, ok bool) {
41-
return ToSliceArrayWith(arg, func(value reflect.Value) interface{} {
32+
// ToUFloat64Slice converting interface to interface slice for float64
33+
func ToUFloat64Slice(arg interface{}) (out []interface{}, ok bool) {
34+
return ToSliceWith(arg, func(value reflect.Value) interface{} {
4235
f64, _ := strconv.ParseFloat(fmt.Sprintf("%v", value), 64)
4336
return f64
4437
})
4538
}
4639

47-
// ToUFloat64SliceArray converting interface to interface slice for bool
48-
func ToBoolSliceArray(arg interface{}) (out []interface{}, ok bool) {
49-
return ToSliceArrayWith(arg, func(value reflect.Value) interface{} {
40+
// ToUFloat64Slice converting interface to interface slice for bool
41+
func ToBoolSlice(arg interface{}) (out []interface{}, ok bool) {
42+
return ToSliceWith(arg, func(value reflect.Value) interface{} {
5043
bl, _ := strconv.ParseBool(fmt.Sprintf("%v", value))
5144
return bl
5245
})
5346
}
5447

55-
// ToSliceArrayWith converting interface to interface slice, and using a custom handle
56-
func ToSliceArrayWith(arg interface{}, handle func(value reflect.Value) interface{}) (arr []interface{}, ok bool) {
48+
// ToSliceWith converting interface to interface slice, and using a custom handle
49+
func ToSliceWith(arg interface{}, handle func(value reflect.Value) interface{}) (arr []interface{}, ok bool) {
5750
argValue := reflect.ValueOf(arg)
5851
if argValue.Type().Kind() == reflect.Slice {
5952
length := argValue.Len()

internal/utils/slice_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestToStringSlice(t *testing.T) {
12+
value := reflect.ValueOf([]string{"a", "b"})
13+
out, ok := ToStringSlice(value.Interface())
14+
assert.True(t, ok, "must be successfully converted")
15+
assert.Equal(t, "a", out[0], fmt.Sprintf("value does not match(%v): %v", "a", out[0]))
16+
assert.Equal(t, "b", out[1], fmt.Sprintf("value does not match(%v): %v", "a", out[1]))
17+
}
18+
19+
func TestToInt64Slice(t *testing.T) {
20+
value := reflect.ValueOf([]int64{1, 2})
21+
out, ok := ToInt64Slice(value.Interface())
22+
assert.True(t, ok, "must be successfully converted")
23+
assert.Equal(t, int64(1), out[0], fmt.Sprintf("value does not match(%v): %v", int64(1), out[0]))
24+
assert.Equal(t, int64(2), out[1], fmt.Sprintf("value does not match(%v): %v", int64(2), out[1]))
25+
}
26+
27+
func TestToUInt64Slice(t *testing.T) {
28+
value := reflect.ValueOf([]uint64{1, 2})
29+
out, ok := ToUInt64Slice(value.Interface())
30+
assert.True(t, ok, "must be successfully converted")
31+
assert.Equal(t, uint64(1), out[0], fmt.Sprintf("value does not match(%v): %v", uint64(1), out[0]))
32+
assert.Equal(t, uint64(2), out[1], fmt.Sprintf("value does not match(%v): %v", uint64(2), out[1]))
33+
}
34+
35+
func TestToUFloat64Slice(t *testing.T) {
36+
value := reflect.ValueOf([]float64{1, 2})
37+
out, ok := ToUFloat64Slice(value.Interface())
38+
assert.True(t, ok, "must be successfully converted")
39+
assert.Equal(t, float64(1), out[0], fmt.Sprintf("value does not match(%v): %v", float64(1), out[0]))
40+
assert.Equal(t, float64(2), out[1], fmt.Sprintf("value does not match(%v): %v", float64(2), out[1]))
41+
}
42+
43+
func TestToBoolSlice(t *testing.T) {
44+
value := reflect.ValueOf([]bool{true, false})
45+
out, ok := ToBoolSlice(value.Interface())
46+
assert.True(t, ok, "must be successfully converted")
47+
assert.Equal(t, true, out[0], fmt.Sprintf("value does not match(%v): %v", true, out[0]))
48+
assert.Equal(t, false, out[1], fmt.Sprintf("value does not match(%v): %v", false, out[1]))
49+
}

tester.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func testDecoder(observe byte, buf []byte, callback func(v []byte) (interface{},
2727
newObservableTester(observe).
2828
Init(callback).
2929
Write(buf).
30-
CloseWith(100)
30+
CloseWith(150)
3131
}
3232

3333
// Init create a channel for testing

0 commit comments

Comments
 (0)