Browse Source

codec: add tests for MissingFielder (missing fields)

Updates #258
Ugorji Nwoke 7 years ago
parent
commit
449f40126c
3 changed files with 81 additions and 0 deletions
  1. 41 0
      codec/codec_test.go
  2. 30 0
      codec/values_test.go
  3. 10 0
      codec/z_all_test.go

+ 41 - 0
codec/codec_test.go

@@ -2310,6 +2310,27 @@ func doTestOmitempty(t *testing.T, name string, h Handle) {
 	testDeepEqualErr(b1, b2, t, name+"-omitempty-cmp")
 }
 
+func doTestMissingFields(t *testing.T, name string, h Handle) {
+	testOnce.Do(testInitAll)
+	if codecgen {
+		t.Skipf("Skipping Missing Fields tests as it is not honored by codecgen")
+	}
+	if h.getBasicHandle().StructToArray {
+		t.Skipf("Skipping Missing Fields test when StructToArray=true")
+	}
+	// encode missingFielderT2, decode into missingFielderT1, encode it out again, decode into new missingFielderT2, compare
+	v1 := missingFielderT2{S: "true seven eight", B: true, F: 777.0, I: -888}
+	b1 := testMarshalErr(v1, h, t, name+"-missing-enc-2")
+	// xdebugf("b1: %s", b1)
+	var v2 missingFielderT1
+	testUnmarshalErr(&v2, b1, h, t, name+"-missing-dec-1")
+	// xdebugf("unmarshal worked")
+	b2 := testMarshalErr(&v2, h, t, name+"-missing-enc-1")
+	var v3 missingFielderT2
+	testUnmarshalErr(&v3, b2, h, t, name+"-missing-dec-2")
+	testDeepEqualErr(v1, v3, t, name+"-missing-cmp-2")
+}
+
 // -----------------
 
 func TestJsonDecodeNonStringScalarInStringContext(t *testing.T) {
@@ -2987,6 +3008,26 @@ func TestSimpleIntfMapping(t *testing.T) {
 	doTestIntfMapping(t, "simple", testSimpleH)
 }
 
+func TestJsonMissingFields(t *testing.T) {
+	doTestMissingFields(t, "json", testJsonH)
+}
+
+func TestCborMissingFields(t *testing.T) {
+	doTestMissingFields(t, "cbor", testCborH)
+}
+
+func TestMsgpackMissingFields(t *testing.T) {
+	doTestMissingFields(t, "msgpack", testMsgpackH)
+}
+
+func TestBincMissingFields(t *testing.T) {
+	doTestMissingFields(t, "binc", testBincH)
+}
+
+func TestSimpleMissingFields(t *testing.T) {
+	doTestMissingFields(t, "simple", testSimpleH)
+}
+
 // TODO:
 //
 // Add Tests for:

+ 30 - 0
codec/values_test.go

@@ -38,6 +38,36 @@ type stringUint64T struct {
 	U uint64
 }
 
+type missingFielderT1 struct {
+	S string
+	B bool
+	f float64
+	i int64
+}
+
+func (t *missingFielderT1) CodecMissingField(field []byte, value interface{}) bool {
+	switch string(field) {
+	case "F":
+		t.f = value.(float64)
+	case "I":
+		t.i = value.(int64)
+	default:
+		return false
+	}
+	return true
+}
+
+func (t *missingFielderT1) CodecMissingFields() []MissingFieldPair {
+	return []MissingFieldPair{{"F", t.f}, {"I", t.i}}
+}
+
+type missingFielderT2 struct {
+	S string
+	B bool
+	F float64
+	I int64
+}
+
 type AnonInTestStruc struct {
 	AS         string
 	AI64       int64

+ 10 - 0
codec/z_all_test.go

@@ -239,6 +239,11 @@ func testCodecGroup(t *testing.T) {
 	t.Run("TestMsgpackIntfMapping", TestMsgpackIntfMapping)
 	t.Run("TestBincIntfMapping", TestBincIntfMapping)
 	t.Run("TestSimpleIntfMapping", TestSimpleIntfMapping)
+	t.Run("TestJsonMissingFields", TestJsonMissingFields)
+	t.Run("TestCborMissingFields", TestCborMissingFields)
+	t.Run("TestMsgpackMissingFields", TestMsgpackMissingFields)
+	t.Run("TestBincMissingFields", TestBincMissingFields)
+	t.Run("TestSimpleMissingFields", TestSimpleMissingFields)
 
 	t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
 	t.Run("TestCborHalfFloat", TestCborHalfFloat)
@@ -272,6 +277,7 @@ func testJsonGroup(t *testing.T) {
 	t.Run("TestJsonScalars", TestJsonScalars)
 	t.Run("TestJsonOmitempty", TestJsonOmitempty)
 	t.Run("TestJsonIntfMapping", TestJsonIntfMapping)
+	t.Run("TestJsonMissingFields", TestJsonMissingFields)
 }
 
 func testBincGroup(t *testing.T) {
@@ -297,6 +303,7 @@ func testBincGroup(t *testing.T) {
 	t.Run("TestBincScalars", TestBincScalars)
 	t.Run("TestBincOmitempty", TestBincOmitempty)
 	t.Run("TestBincIntfMapping", TestBincIntfMapping)
+	t.Run("TestBincMissingFields", TestBincMissingFields)
 }
 
 func testCborGroup(t *testing.T) {
@@ -323,6 +330,7 @@ func testCborGroup(t *testing.T) {
 	t.Run("TestCborScalars", TestCborScalars)
 	t.Run("TestCborOmitempty", TestCborOmitempty)
 	t.Run("TestCborIntfMapping", TestCborIntfMapping)
+	t.Run("TestCborMissingFields", TestCborMissingFields)
 	t.Run("TestCborHalfFloat", TestCborHalfFloat)
 }
 
@@ -348,6 +356,7 @@ func testMsgpackGroup(t *testing.T) {
 	t.Run("TestMsgpackScalars", TestMsgpackScalars)
 	t.Run("TestMsgpackOmitempty", TestMsgpackOmitempty)
 	t.Run("TestMsgpackIntfMapping", TestMsgpackIntfMapping)
+	t.Run("TestMsgpackMissingFields", TestMsgpackMissingFields)
 }
 
 func testSimpleGroup(t *testing.T) {
@@ -371,6 +380,7 @@ func testSimpleGroup(t *testing.T) {
 	t.Run("TestSimpleScalars", TestSimpleScalars)
 	t.Run("TestSimpleOmitempty", TestSimpleOmitempty)
 	t.Run("TestSimpleIntfMapping", TestSimpleIntfMapping)
+	t.Run("TestSimpleMissingFields", TestSimpleMissingFields)
 }
 
 func testSimpleMammothGroup(t *testing.T) {