Browse Source

codec: add omitempty tests

Updates #237
Ugorji Nwoke 7 years ago
parent
commit
6a0f936d95
2 changed files with 54 additions and 1 deletions
  1. 40 0
      codec/codec_test.go
  2. 14 1
      codec/z_all_test.go

+ 40 - 0
codec/codec_test.go

@@ -2287,6 +2287,26 @@ func doTestIntfMapping(t *testing.T, name string, h Handle) {
 	}
 	}
 }
 }
 
 
+func doTestOmitempty(t *testing.T, name string, h Handle) {
+	testOnce.Do(testInitAll)
+	if h.getBasicHandle().StructToArray {
+		t.Skipf("Skipping OmitEmpty test when StructToArray=true")
+	}
+	type T1 struct {
+		A int  `codec:"a"`
+		B *int `codec:"b,omitempty"`
+		C int  `codec:"c,omitempty"`
+	}
+	type T2 struct {
+		A int `codec:"a"`
+	}
+	var v1 T1
+	var v2 T2
+	b1 := testMarshalErr(v1, h, t, name+"-omitempty")
+	b2 := testMarshalErr(v2, h, t, name+"-no-omitempty-trunc")
+	testDeepEqualErr(b1, b2, t, name+"-omitempty-cmp")
+}
+
 // -----------------
 // -----------------
 
 
 func TestJsonDecodeNonStringScalarInStringContext(t *testing.T) {
 func TestJsonDecodeNonStringScalarInStringContext(t *testing.T) {
@@ -2924,6 +2944,26 @@ func TestSimpleScalars(t *testing.T) {
 	doTestScalars(t, "simple", testSimpleH)
 	doTestScalars(t, "simple", testSimpleH)
 }
 }
 
 
+func TestJsonOmitempty(t *testing.T) {
+	doTestOmitempty(t, "json", testJsonH)
+}
+
+func TestCborOmitempty(t *testing.T) {
+	doTestOmitempty(t, "cbor", testCborH)
+}
+
+func TestMsgpackOmitempty(t *testing.T) {
+	doTestOmitempty(t, "msgpack", testMsgpackH)
+}
+
+func TestBincOmitempty(t *testing.T) {
+	doTestOmitempty(t, "binc", testBincH)
+}
+
+func TestSimpleOmitempty(t *testing.T) {
+	doTestOmitempty(t, "simple", testSimpleH)
+}
+
 func TestJsonIntfMapping(t *testing.T) {
 func TestJsonIntfMapping(t *testing.T) {
 	doTestIntfMapping(t, "json", testJsonH)
 	doTestIntfMapping(t, "json", testJsonH)
 }
 }

+ 14 - 1
codec/z_all_test.go

@@ -229,6 +229,11 @@ func testCodecGroup(t *testing.T) {
 	t.Run("TestMsgpackScalars", TestMsgpackScalars)
 	t.Run("TestMsgpackScalars", TestMsgpackScalars)
 	t.Run("TestBincScalars", TestBincScalars)
 	t.Run("TestBincScalars", TestBincScalars)
 	t.Run("TestSimpleScalars", TestSimpleScalars)
 	t.Run("TestSimpleScalars", TestSimpleScalars)
+	t.Run("TestJsonOmitempty", TestJsonOmitempty)
+	t.Run("TestCborOmitempty", TestCborOmitempty)
+	t.Run("TestMsgpackOmitempty", TestMsgpackOmitempty)
+	t.Run("TestBincOmitempty", TestBincOmitempty)
+	t.Run("TestSimpleOmitempty", TestSimpleOmitempty)
 	t.Run("TestJsonIntfMapping", TestJsonIntfMapping)
 	t.Run("TestJsonIntfMapping", TestJsonIntfMapping)
 	t.Run("TestCborIntfMapping", TestCborIntfMapping)
 	t.Run("TestCborIntfMapping", TestCborIntfMapping)
 	t.Run("TestMsgpackIntfMapping", TestMsgpackIntfMapping)
 	t.Run("TestMsgpackIntfMapping", TestMsgpackIntfMapping)
@@ -265,6 +270,7 @@ func testJsonGroup(t *testing.T) {
 	t.Run("TestJsonUintToInt", TestJsonUintToInt)
 	t.Run("TestJsonUintToInt", TestJsonUintToInt)
 	t.Run("TestJsonDifferentMapOrSliceType", TestJsonDifferentMapOrSliceType)
 	t.Run("TestJsonDifferentMapOrSliceType", TestJsonDifferentMapOrSliceType)
 	t.Run("TestJsonScalars", TestJsonScalars)
 	t.Run("TestJsonScalars", TestJsonScalars)
+	t.Run("TestJsonOmitempty", TestJsonOmitempty)
 	t.Run("TestJsonIntfMapping", TestJsonIntfMapping)
 	t.Run("TestJsonIntfMapping", TestJsonIntfMapping)
 }
 }
 
 
@@ -289,6 +295,8 @@ func testBincGroup(t *testing.T) {
 	t.Run("TestBincUintToInt", TestBincUintToInt)
 	t.Run("TestBincUintToInt", TestBincUintToInt)
 	t.Run("TestBincDifferentMapOrSliceType", TestBincDifferentMapOrSliceType)
 	t.Run("TestBincDifferentMapOrSliceType", TestBincDifferentMapOrSliceType)
 	t.Run("TestBincScalars", TestBincScalars)
 	t.Run("TestBincScalars", TestBincScalars)
+	t.Run("TestBincOmitempty", TestBincOmitempty)
+	t.Run("TestBincIntfMapping", TestBincIntfMapping)
 }
 }
 
 
 func testCborGroup(t *testing.T) {
 func testCborGroup(t *testing.T) {
@@ -313,7 +321,8 @@ func testCborGroup(t *testing.T) {
 	t.Run("TestCborUintToInt", TestCborUintToInt)
 	t.Run("TestCborUintToInt", TestCborUintToInt)
 	t.Run("TestCborDifferentMapOrSliceType", TestCborDifferentMapOrSliceType)
 	t.Run("TestCborDifferentMapOrSliceType", TestCborDifferentMapOrSliceType)
 	t.Run("TestCborScalars", TestCborScalars)
 	t.Run("TestCborScalars", TestCborScalars)
-
+	t.Run("TestCborOmitempty", TestCborOmitempty)
+	t.Run("TestCborIntfMapping", TestCborIntfMapping)
 	t.Run("TestCborHalfFloat", TestCborHalfFloat)
 	t.Run("TestCborHalfFloat", TestCborHalfFloat)
 }
 }
 
 
@@ -337,6 +346,8 @@ func testMsgpackGroup(t *testing.T) {
 	t.Run("TestMsgpackUintToInt", TestMsgpackUintToInt)
 	t.Run("TestMsgpackUintToInt", TestMsgpackUintToInt)
 	t.Run("TestMsgpackDifferentMapOrSliceType", TestMsgpackDifferentMapOrSliceType)
 	t.Run("TestMsgpackDifferentMapOrSliceType", TestMsgpackDifferentMapOrSliceType)
 	t.Run("TestMsgpackScalars", TestMsgpackScalars)
 	t.Run("TestMsgpackScalars", TestMsgpackScalars)
+	t.Run("TestMsgpackOmitempty", TestMsgpackOmitempty)
+	t.Run("TestMsgpackIntfMapping", TestMsgpackIntfMapping)
 }
 }
 
 
 func testSimpleGroup(t *testing.T) {
 func testSimpleGroup(t *testing.T) {
@@ -358,6 +369,8 @@ func testSimpleGroup(t *testing.T) {
 	t.Run("TestSimpleUintToInt", TestSimpleUintToInt)
 	t.Run("TestSimpleUintToInt", TestSimpleUintToInt)
 	t.Run("TestSimpleDifferentMapOrSliceType", TestSimpleDifferentMapOrSliceType)
 	t.Run("TestSimpleDifferentMapOrSliceType", TestSimpleDifferentMapOrSliceType)
 	t.Run("TestSimpleScalars", TestSimpleScalars)
 	t.Run("TestSimpleScalars", TestSimpleScalars)
+	t.Run("TestSimpleOmitempty", TestSimpleOmitempty)
+	t.Run("TestSimpleIntfMapping", TestSimpleIntfMapping)
 }
 }
 
 
 func testSimpleMammothGroup(t *testing.T) {
 func testSimpleMammothGroup(t *testing.T) {