Просмотр исходного кода

codec: test: add test for checking if TestStruc encoded == decoded

Note: we do not use TestStrucFlex because it is omitempty.
Ugorji Nwoke 6 лет назад
Родитель
Сommit
f06fded8c4
3 измененных файлов с 53 добавлено и 5 удалено
  1. 46 3
      codec/codec_test.go
  2. 2 2
      codec/values_test.go
  3. 5 0
      codec/z_all_test.go

+ 46 - 3
codec/codec_test.go

@@ -530,7 +530,7 @@ ugorji
 	table = append(table, testMbsT(primitives))
 	table = append(table, maps...)
 	table = append(table, newTestStrucFlex(0, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly))
-
+	// table = append(table, newTestStrucFlex(0, testNumRepeatString, true, !testSkipIntf, testMapStringKeyOnly))
 }
 
 func testTableVerify(f testVerifyFlag, h Handle) (av []interface{}) {
@@ -734,7 +734,7 @@ func testDeepEqualErr(v1, v2 interface{}, t *testing.T, name string) {
 	if err := deepEqual(v1, v2); err == nil {
 		logTv(t, "%s: values equal", name)
 	} else {
-		failT(t, "%s: values not equal: %v. 1: %v, 2: %v", name, err, v1, v2)
+		failT(t, "%s: values not equal: %v. 1: %#v, 2: %#v", name, err, v1, v2)
 	}
 }
 
@@ -2486,6 +2486,27 @@ func doTestBytesEncodedAsArray(t *testing.T, name string, h Handle) {
 	testDeepEqualErr(un, out, t, name)
 }
 
+func doTestStrucEncDec(t *testing.T, name string, h Handle) {
+	testOnce.Do(testInitAll)
+	{
+		var ts1 = newTestStruc(2, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
+		var ts2 TestStruc
+		bs := testMarshalErr(ts1, h, t, name)
+		testUnmarshalErr(&ts2, bs, h, t, name)
+		testDeepEqualErr(ts1, &ts2, t, name)
+	}
+	// Note: We cannot use TestStrucFlex because it has omitempty values,
+	// Meaning that sometimes, encoded and decoded will not be same.
+	// {
+	// 	var ts1 = newTestStrucFlex(2, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
+	// 	var ts2 TestStrucFlex
+	// 	bs := testMarshalErr(ts1, h, t, name)
+	// 	fmt.Printf("\n\n%s\n\n", bs)
+	// 	testUnmarshalErr(&ts2, bs, h, t, name)
+	// 	testDeepEqualErr(ts1, &ts2, t, name)
+	// }
+}
+
 // -----------------
 
 func TestJsonDecodeNonStringScalarInStringContext(t *testing.T) {
@@ -2838,7 +2859,7 @@ func TestMapRangeIndex(t *testing.T) {
 		&t2: t2,
 	}
 	var m2c = make(map[*T]*T)
-	for k, _ := range m2 {
+	for k := range m2 {
 		m2c[k] = k
 	}
 
@@ -3407,6 +3428,28 @@ func TestSimpleBytesEncodedAsArray(t *testing.T) {
 	doTestBytesEncodedAsArray(t, "simple", testSimpleH)
 }
 
+func TestJsonStrucEncDec(t *testing.T) {
+	doTestStrucEncDec(t, "json", testJsonH)
+}
+
+func TestCborStrucEncDec(t *testing.T) {
+	doTestStrucEncDec(t, "cbor", testCborH)
+}
+
+func TestMsgpackStrucEncDec(t *testing.T) {
+	doTestStrucEncDec(t, "msgpack", testMsgpackH)
+}
+
+func TestBincStrucEncDec(t *testing.T) {
+	doTestStrucEncDec(t, "binc", testBincH)
+}
+
+func TestSimpleStrucEncDec(t *testing.T) {
+	doTestStrucEncDec(t, "simple", testSimpleH)
+}
+
+// --------
+
 func TestMultipleEncDec(t *testing.T) {
 	doTestMultipleEncDec(t, "json", testJsonH)
 }

+ 2 - 2
codec/values_test.go

@@ -60,7 +60,7 @@ type AnonInTestStruc struct {
 
 	// use these to test 0-len or nil slices/maps/arrays
 	AI64arr0    [0]int64
-	A164slice0  []int64
+	AI64slice0  []int64
 	AUi64sliceN []uint64
 	AMSU16N     map[string]uint16
 	AMSU16E     map[string]uint16
@@ -245,7 +245,7 @@ func populateTestStrucCommon(ts *TestStrucCommon, n int, bench, useInterface, us
 			math.MaxFloat32, math.SmallestNonzeroFloat32,
 		},
 
-		A164slice0:  []int64{},
+		AI64slice0:  []int64{},
 		AUi64sliceN: nil,
 		AMSU16N:     nil,
 		AMSU16E:     map[string]uint16{},

+ 5 - 0
codec/z_all_test.go

@@ -176,6 +176,7 @@ func testJsonGroup(t *testing.T) {
 	t.Run("TestJsonMaxDepth", TestJsonMaxDepth)
 	t.Run("TestJsonSelfExt", TestJsonSelfExt)
 	t.Run("TestJsonBytesEncodedAsArray", TestJsonBytesEncodedAsArray)
+	t.Run("TestJsonStrucEncDec", TestJsonStrucEncDec)
 
 	t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
 }
@@ -207,6 +208,7 @@ func testBincGroup(t *testing.T) {
 	t.Run("TestBincMaxDepth", TestBincMaxDepth)
 	t.Run("TestBincSelfExt", TestBincSelfExt)
 	t.Run("TestBincBytesEncodedAsArray", TestBincBytesEncodedAsArray)
+	t.Run("TestBincStrucEncDec", TestBincStrucEncDec)
 }
 
 func testCborGroup(t *testing.T) {
@@ -237,6 +239,7 @@ func testCborGroup(t *testing.T) {
 	t.Run("TestCborMaxDepth", TestCborMaxDepth)
 	t.Run("TestCborSelfExt", TestCborSelfExt)
 	t.Run("TestCborBytesEncodedAsArray", TestCborBytesEncodedAsArray)
+	t.Run("TestCborStrucEncDec", TestCborStrucEncDec)
 
 	t.Run("TestCborHalfFloat", TestCborHalfFloat)
 	t.Run("TestCborSkipTags", TestCborSkipTags)
@@ -268,6 +271,7 @@ func testMsgpackGroup(t *testing.T) {
 	t.Run("TestMsgpackMaxDepth", TestMsgpackMaxDepth)
 	t.Run("TestMsgpackSelfExt", TestMsgpackSelfExt)
 	t.Run("TestMsgpackBytesEncodedAsArray", TestMsgpackBytesEncodedAsArray)
+	t.Run("TestCborStrucEncDec", TestCborStrucEncDec)
 
 	t.Run("TestMsgpackDecodeMapAndExtSizeMismatch", TestMsgpackDecodeMapAndExtSizeMismatch)
 }
@@ -297,6 +301,7 @@ func testSimpleGroup(t *testing.T) {
 	t.Run("TestSimpleMaxDepth", TestSimpleMaxDepth)
 	t.Run("TestSimpleSelfExt", TestSimpleSelfExt)
 	t.Run("TestSimpleBytesEncodedAsArray", TestSimpleBytesEncodedAsArray)
+	t.Run("TestSimpleStrucEncDec", TestSimpleStrucEncDec)
 }
 
 func testSimpleMammothGroup(t *testing.T) {