diff --git a/codec/codec_test.go b/codec/codec_test.go index 4b9ee9c5..2bd9dc5c 100644 --- a/codec/codec_test.go +++ b/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) { @@ -2924,6 +2944,26 @@ func TestSimpleScalars(t *testing.T) { 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) { doTestIntfMapping(t, "json", testJsonH) } diff --git a/codec/z_all_test.go b/codec/z_all_test.go index 58a3aa06..cc9c8432 100644 --- a/codec/z_all_test.go +++ b/codec/z_all_test.go @@ -229,6 +229,11 @@ func testCodecGroup(t *testing.T) { t.Run("TestMsgpackScalars", TestMsgpackScalars) t.Run("TestBincScalars", TestBincScalars) 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("TestCborIntfMapping", TestCborIntfMapping) t.Run("TestMsgpackIntfMapping", TestMsgpackIntfMapping) @@ -265,6 +270,7 @@ func testJsonGroup(t *testing.T) { t.Run("TestJsonUintToInt", TestJsonUintToInt) t.Run("TestJsonDifferentMapOrSliceType", TestJsonDifferentMapOrSliceType) t.Run("TestJsonScalars", TestJsonScalars) + t.Run("TestJsonOmitempty", TestJsonOmitempty) t.Run("TestJsonIntfMapping", TestJsonIntfMapping) } @@ -289,6 +295,8 @@ func testBincGroup(t *testing.T) { t.Run("TestBincUintToInt", TestBincUintToInt) t.Run("TestBincDifferentMapOrSliceType", TestBincDifferentMapOrSliceType) t.Run("TestBincScalars", TestBincScalars) + t.Run("TestBincOmitempty", TestBincOmitempty) + t.Run("TestBincIntfMapping", TestBincIntfMapping) } func testCborGroup(t *testing.T) { @@ -313,7 +321,8 @@ func testCborGroup(t *testing.T) { t.Run("TestCborUintToInt", TestCborUintToInt) t.Run("TestCborDifferentMapOrSliceType", TestCborDifferentMapOrSliceType) t.Run("TestCborScalars", TestCborScalars) - + t.Run("TestCborOmitempty", TestCborOmitempty) + t.Run("TestCborIntfMapping", TestCborIntfMapping) t.Run("TestCborHalfFloat", TestCborHalfFloat) } @@ -337,6 +346,8 @@ func testMsgpackGroup(t *testing.T) { t.Run("TestMsgpackUintToInt", TestMsgpackUintToInt) t.Run("TestMsgpackDifferentMapOrSliceType", TestMsgpackDifferentMapOrSliceType) t.Run("TestMsgpackScalars", TestMsgpackScalars) + t.Run("TestMsgpackOmitempty", TestMsgpackOmitempty) + t.Run("TestMsgpackIntfMapping", TestMsgpackIntfMapping) } func testSimpleGroup(t *testing.T) { @@ -358,6 +369,8 @@ func testSimpleGroup(t *testing.T) { t.Run("TestSimpleUintToInt", TestSimpleUintToInt) t.Run("TestSimpleDifferentMapOrSliceType", TestSimpleDifferentMapOrSliceType) t.Run("TestSimpleScalars", TestSimpleScalars) + t.Run("TestSimpleOmitempty", TestSimpleOmitempty) + t.Run("TestSimpleIntfMapping", TestSimpleIntfMapping) } func testSimpleMammothGroup(t *testing.T) {