Skip to content

Commit

Permalink
codec: add omitempty tests
Browse files Browse the repository at this point in the history
Updates #237
  • Loading branch information
ugorji committed Apr 6, 2018
1 parent 0332d21 commit 6a0f936
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
40 changes: 40 additions & 0 deletions codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
15 changes: 14 additions & 1 deletion codec/z_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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) {
Expand All @@ -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)
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 6a0f936

Please sign in to comment.