7 Star 38 Fork 19

YoMo / y3-codec-golang

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
structure_decoder_test.go 17.16 KB
一键复制 编辑 原始数据 按行查看 历史
chenjunbiao 提交于 2021-02-25 19:04 . expose generic parsing methods
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
package y3
import (
"reflect"
"testing"
"github.com/yomorun/y3-codec-golang/internal/utils"
"github.com/yomorun/y3-codec-golang/internal/tester"
)
func newBasic() tester.BasicTestData {
return tester.BasicTestData{
Vstring: "foo",
Vint32: int32(127),
Vint64: int64(-1),
Vuint32: uint32(130),
Vuint64: uint64(18446744073709551615),
Vfloat32: float32(0.25),
Vfloat64: float64(23),
Vbool: true,
}
}
func TestBasic_Struct(t *testing.T) {
t.Parallel()
input := newBasic()
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result tester.BasicTestData
runDecode(t, inputBuf, &result)
testBasicStruct(t, result, input)
}
func runDecode(t *testing.T, inputBuf []byte, output interface{}) {
_, err := newStructDecoder(output, structDecoderOptionConfig(&structDecoderConfig{
ZeroFields: true,
TagName: "y3",
})).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
}
func TestDecode_Embedded(t *testing.T) {
t.Parallel()
input := tester.EmbeddedTestData{
BasicTestData: newBasic(),
Vaction: "drink",
}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result tester.EmbeddedTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
if result.Vaction != "drink" {
t.Errorf("vstring value should be 'drink': %#v", result.Vaction)
}
testBasicStruct(t, result.BasicTestData, input.BasicTestData)
}
func TestDecode_EmbeddedMore(t *testing.T) {
t.Parallel()
input := tester.EmbeddedMoreTestData{EmbeddedTestData: tester.EmbeddedTestData{BasicTestData: newBasic(), Vaction: "drink"}, Vanimal: "bird"}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result tester.EmbeddedMoreTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
if result.Vanimal != "bird" {
t.Errorf("vstring value should be 'bird': %#v", result.Vanimal)
}
if result.Vaction != "drink" {
t.Errorf("vstring value should be 'drink': %#v", result.Vaction)
}
testBasicStruct(t, result.BasicTestData, input.BasicTestData)
}
func TestDecoder_Named(t *testing.T) {
t.Parallel()
input := tester.NamedTestData{Base: newBasic(), Vaction: "drink"}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result tester.NamedTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
if result.Vaction != "drink" {
t.Errorf("vstring value should be 'drink': %#v", result.Vaction)
}
testBasicStruct(t, result.Base, input.Base)
}
func TestDecoder_NamedMore(t *testing.T) {
t.Parallel()
input := tester.NamedMoreTestData{MyNest: tester.NamedTestData{Base: newBasic(), Vaction: "drink"}, Vanimal: "bird"}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result tester.NamedMoreTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
if result.Vanimal != "bird" {
t.Errorf("vstring value should be 'bird': %#v", result.Vanimal)
}
if result.MyNest.Vaction != "drink" {
t.Errorf("vstring value should be 'drink': %#v", result.MyNest.Vaction)
}
testBasicStruct(t, result.MyNest.Base, input.MyNest.Base)
}
func TestArray(t *testing.T) {
t.Parallel()
input := tester.ArrayTestData{
"foo",
[2]string{"foo", "bar"},
[2]int32{1, 2},
[2]int64{1, 2},
[2]uint32{1, 2},
[2]uint64{1, 2},
[2]float32{1, 2},
[2]float64{1, 2},
}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result tester.ArrayTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
testArrayString(t, result.Vbar, input.Vbar)
testArrayInt32(t, result.Vint32Array, input.Vint32Array)
testArrayInt64(t, result.Vint64Array, input.Vint64Array)
testArrayUint32(t, result.Vuint32Array, input.Vuint32Array)
testArrayUint64(t, result.Vuint64Array, input.Vuint64Array)
testArrayFloat32(t, result.Vfloat32Array, input.Vfloat32Array)
testArrayFloat64(t, result.Vfloat64Array, input.Vfloat64Array)
}
func TestSlice(t *testing.T) {
t.Parallel()
input := tester.SliceTestData{
"foo",
[]string{"foo", "bar"},
[]int32{1, 2},
[]int64{1, 2},
[]uint32{1, 2},
[]uint64{1, 2},
[]float32{1, 2},
[]float64{1, 2},
}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result tester.SliceTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
testSliceString(t, result.Vbar, input.Vbar)
testSliceInt32(t, result.Vint32Slice, input.Vint32Slice)
testSliceInt64(t, result.Vint64Slice, input.Vint64Slice)
testSliceUint32(t, result.Vuint32Slice, input.Vuint32Slice)
testSliceUint64(t, result.Vuint64Slice, input.Vuint64Slice)
testSliceFloat32(t, result.Vfloat32Slice, input.Vfloat32Slice)
testSliceFloat64(t, result.Vfloat64Slice, input.Vfloat64Slice)
}
func TestSliceStruct(t *testing.T) {
t.Parallel()
input := tester.SliceStructTestData{
Vstring: "foo",
BaseList: []tester.BasicTestData{newBasic(), newBasic()},
NamedMoreList: []tester.NamedMoreTestData{
{MyNest: tester.NamedTestData{Base: newBasic(), Vaction: "drink"}, Vanimal: "bird"},
{MyNest: tester.NamedTestData{Base: newBasic(), Vaction: "drink"}, Vanimal: "bird"}},
EmbeddedMoreList: []tester.EmbeddedMoreTestData{
{EmbeddedTestData: tester.EmbeddedTestData{BasicTestData: newBasic(), Vaction: "drink"}, Vanimal: "bird"},
{EmbeddedTestData: tester.EmbeddedTestData{BasicTestData: newBasic(), Vaction: "drink"}, Vanimal: "bird"}},
}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result tester.SliceStructTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
testValue(t, result.Vstring, input.Vstring)
testValueWith(t, len(result.BaseList), len(input.BaseList), "BaseList len")
for i, v := range result.BaseList {
testBasicStruct(t, v, input.BaseList[i])
}
testValueWith(t, len(result.NamedMoreList), len(input.NamedMoreList), "NamedMoreList len")
for i, v := range result.NamedMoreList {
testValue(t, v.Vanimal, input.NamedMoreList[i].Vanimal)
testValue(t, v.MyNest.Vaction, input.NamedMoreList[i].MyNest.Vaction)
testBasicStruct(t, v.MyNest.Base, input.NamedMoreList[i].MyNest.Base)
}
testValueWith(t, len(result.EmbeddedMoreList), len(input.EmbeddedMoreList), "EmbeddedMoreList len")
for i, v := range result.EmbeddedMoreList {
testValue(t, v.Vanimal, input.EmbeddedMoreList[i].Vanimal)
testValue(t, v.Vaction, input.EmbeddedMoreList[i].Vaction)
testBasicStruct(t, v.BasicTestData, input.EmbeddedMoreList[i].BasicTestData)
}
}
func TestArrayStruct(t *testing.T) {
t.Parallel()
input := tester.ArrayStructTestData{
Vstring: "foo",
BaseList: [2]tester.BasicTestData{newBasic(), newBasic()},
NamedMoreList: [2]tester.NamedMoreTestData{
{MyNest: tester.NamedTestData{Base: newBasic(), Vaction: "drink"}, Vanimal: "bird"},
{MyNest: tester.NamedTestData{Base: newBasic(), Vaction: "drink"}, Vanimal: "bird"}},
EmbeddedMoreList: [2]tester.EmbeddedMoreTestData{
{EmbeddedTestData: tester.EmbeddedTestData{BasicTestData: newBasic(), Vaction: "drink"}, Vanimal: "bird"},
{EmbeddedTestData: tester.EmbeddedTestData{BasicTestData: newBasic(), Vaction: "drink"}, Vanimal: "bird"}},
}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result tester.ArrayStructTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
testValue(t, result.Vstring, input.Vstring)
testValueWith(t, len(result.BaseList), len(input.BaseList), "BaseList len")
for i, v := range result.BaseList {
testBasicStruct(t, v, input.BaseList[i])
}
testValueWith(t, len(result.NamedMoreList), len(input.NamedMoreList), "NamedMoreList len")
for i, v := range result.NamedMoreList {
testValue(t, v.Vanimal, input.NamedMoreList[i].Vanimal)
testValue(t, v.MyNest.Vaction, input.NamedMoreList[i].MyNest.Vaction)
testBasicStruct(t, v.MyNest.Base, input.NamedMoreList[i].MyNest.Base)
}
testValueWith(t, len(result.EmbeddedMoreList), len(input.EmbeddedMoreList), "EmbeddedMoreList len")
for i, v := range result.EmbeddedMoreList {
testValue(t, v.Vanimal, input.EmbeddedMoreList[i].Vanimal)
testValue(t, v.Vaction, input.EmbeddedMoreList[i].Vaction)
testBasicStruct(t, v.BasicTestData, input.EmbeddedMoreList[i].BasicTestData)
}
}
func TestRootSliceWithBasicStruct(t *testing.T) {
t.Parallel()
input := []tester.BasicTestData{newBasic(), newBasic()}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result []tester.BasicTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
testValueWith(t, len(result), len(input), "[]BasicTestData len")
for i, v := range result {
testBasicStruct(t, v, input[i])
}
}
func TestRootSliceWithSliceStruct(t *testing.T) {
t.Parallel()
input1 := tester.SliceStructTestData{
Vstring: "foo",
BaseList: []tester.BasicTestData{newBasic(), newBasic()},
NamedMoreList: []tester.NamedMoreTestData{
{MyNest: tester.NamedTestData{Base: newBasic(), Vaction: "drink"}, Vanimal: "bird"},
{MyNest: tester.NamedTestData{Base: newBasic(), Vaction: "drink"}, Vanimal: "bird"}},
EmbeddedMoreList: []tester.EmbeddedMoreTestData{
{EmbeddedTestData: tester.EmbeddedTestData{BasicTestData: newBasic(), Vaction: "drink"}, Vanimal: "bird"},
{EmbeddedTestData: tester.EmbeddedTestData{BasicTestData: newBasic(), Vaction: "drink"}, Vanimal: "bird"}},
}
input := []tester.SliceStructTestData{input1, input1}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result []tester.SliceStructTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
testValueWith(t, len(result), len(input), "[]SliceStructTestData len")
for i, v := range result {
testValue(t, v.Vstring, input[i].Vstring)
testValueWith(t, len(v.BaseList), len(input[i].BaseList), "BaseList len")
for i, v := range v.BaseList {
testBasicStruct(t, v, input[i].BaseList[i])
}
testValueWith(t, len(v.NamedMoreList), len(input[i].NamedMoreList), "NamedMoreList len")
for i, vv := range v.NamedMoreList {
testValue(t, vv.Vanimal, input[i].NamedMoreList[i].Vanimal)
testValue(t, vv.MyNest.Vaction, input[i].NamedMoreList[i].MyNest.Vaction)
testBasicStruct(t, vv.MyNest.Base, input[i].NamedMoreList[i].MyNest.Base)
}
testValueWith(t, len(v.EmbeddedMoreList), len(input[i].EmbeddedMoreList), "EmbeddedMoreList len")
for i, vv := range v.EmbeddedMoreList {
testValue(t, vv.Vanimal, input[i].EmbeddedMoreList[i].Vanimal)
testValue(t, vv.Vaction, input[i].EmbeddedMoreList[i].Vaction)
testBasicStruct(t, vv.BasicTestData, input[i].EmbeddedMoreList[i].BasicTestData)
}
}
}
func TestNested(t *testing.T) {
t.Parallel()
input := tester.NestedTestData{tester.Sub1NestedTestData{tester.Sub2NestedTestData{tester.Sub3NestedTestData{
BasicList: []tester.BasicTestData{newBasic(), newBasic()},
}}}}
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
var result tester.NestedTestData
_, err := newStructDecoder(&result).Decode(inputBuf)
if err != nil {
t.Errorf("got an err: %s", err.Error())
t.FailNow()
}
for i, v := range result.SubNested.SubNested.SubNested.BasicList {
testBasicStruct(t, v, input.SubNested.SubNested.SubNested.BasicList[i])
}
}
func testBasicStruct(t *testing.T, result tester.BasicTestData, expected tester.BasicTestData) {
if result.Vstring != expected.Vstring {
t.Errorf("Vstring value should be: %v", expected.Vstring)
}
if result.Vint32 != expected.Vint32 {
t.Errorf("Vint32 value should be: %v", expected.Vint32)
}
if result.Vint64 != expected.Vint64 {
t.Errorf("Vint64 value should be: %#v", expected.Vint64)
}
if result.Vuint32 != expected.Vuint32 {
t.Errorf("Vuint32 value should be: %v", expected.Vuint32)
}
if result.Vuint64 != expected.Vuint64 {
t.Errorf("Vuint64 value should be: %v", expected.Vuint64)
}
if result.Vfloat32 != expected.Vfloat32 {
t.Errorf("Vfloat32 value should be: %v", expected.Vfloat32)
}
if result.Vfloat64 != expected.Vfloat64 {
t.Errorf("Vfloat64 value should be: %v", expected.Vfloat64)
}
if result.Vbool != expected.Vbool {
t.Errorf("Vbool value should be: %v", expected.Vbool)
}
}
func testValue(t *testing.T, result interface{}, expected interface{}) {
testValueWith(t, result, expected, "")
}
func testValueWith(t *testing.T, result interface{}, expected interface{}, prefix string) {
getPrefix := func(prefix string, def string) string {
if prefix != "" && len(prefix) > 0 {
return prefix
}
return def
}
switch reflect.ValueOf(expected).Kind() {
case reflect.String:
if result.(string) != expected.(string) {
t.Errorf("%v value should be: %v", getPrefix(prefix, "string"), expected)
}
case reflect.Int32:
if result.(int32) != expected.(int32) {
t.Errorf("%v value should be: %v", getPrefix(prefix, "int32"), expected)
}
case reflect.Int64:
if result.(int64) != expected.(int64) {
t.Errorf("%v value should be: %v", getPrefix(prefix, "int64"), expected)
}
case reflect.Uint32:
if result.(uint32) != expected.(uint32) {
t.Errorf("%v value should be: %v", getPrefix(prefix, "uint32"), expected)
}
case reflect.Uint64:
if result.(uint64) != expected.(uint64) {
t.Errorf("%v value should be: %v", getPrefix(prefix, "uint64"), expected)
}
case reflect.Float32:
if result.(float32) != expected.(float32) {
t.Errorf("%v value should be: %v", getPrefix(prefix, "float32"), expected)
}
case reflect.Float64:
if result.(float64) != expected.(float64) {
t.Errorf("%v value should be: %v", getPrefix(prefix, "float64"), expected)
}
case reflect.Bool:
if result.(bool) != expected.(bool) {
t.Errorf("%v value should be: %v", getPrefix(prefix, "bool"), expected)
}
}
}
func testArrayString(t *testing.T, result [2]string, expected [2]string) {
for i, v := range result {
if v != expected[i] {
t.Errorf(
"[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testArrayInt32(t *testing.T, result [2]int32, expected [2]int32) {
for i, v := range result {
if v != expected[i] {
t.Errorf(
"[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testArrayInt64(t *testing.T, result [2]int64, expected [2]int64) {
for i, v := range result {
if v != expected[i] {
t.Errorf("[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testArrayUint32(t *testing.T, result [2]uint32, expected [2]uint32) {
for i, v := range result {
if v != expected[i] {
t.Errorf("[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testArrayUint64(t *testing.T, result [2]uint64, expected [2]uint64) {
for i, v := range result {
if v != expected[i] {
t.Errorf("[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testArrayFloat32(t *testing.T, result [2]float32, expected [2]float32) {
for i, v := range result {
if v != expected[i] {
t.Errorf("[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testArrayFloat64(t *testing.T, result [2]float64, expected [2]float64) {
for i, v := range result {
if v != expected[i] {
t.Errorf("[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testSliceString(t *testing.T, result []string, expected []string) {
for i, v := range result {
if v != expected[i] {
t.Errorf(
"[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testSliceInt32(t *testing.T, result []int32, expected []int32) {
for i, v := range result {
if v != expected[i] {
t.Errorf(
"[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testSliceInt64(t *testing.T, result []int64, expected []int64) {
for i, v := range result {
if v != expected[i] {
t.Errorf("[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testSliceUint32(t *testing.T, result []uint32, expected []uint32) {
for i, v := range result {
if v != expected[i] {
t.Errorf("[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testSliceUint64(t *testing.T, result []uint64, expected []uint64) {
for i, v := range result {
if v != expected[i] {
t.Errorf("[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testSliceFloat32(t *testing.T, result []float32, expected []float32) {
for i, v := range result {
if v != expected[i] {
t.Errorf("[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
func testSliceFloat64(t *testing.T, result []float64, expected []float64) {
for i, v := range result {
if v != expected[i] {
t.Errorf("[%d] should be '%#v', got '%#v'", i, expected[i], v)
}
}
}
Go
1
https://gitee.com/yomorun/y3-codec-golang.git
git@gitee.com:yomorun/y3-codec-golang.git
yomorun
y3-codec-golang
y3-codec-golang
master

搜索帮助