Skip to content

Commit

Permalink
test: check pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Jan 26, 2018
1 parent c15d118 commit 89f92fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
34 changes: 19 additions & 15 deletions filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type DInner struct {
}

type bInner struct {
D dInner `json:"d"`
D *dInner `json:"d"`
B []gInner `json:"b"`
K int `json:"k"`
}
Expand All @@ -50,7 +50,7 @@ type cInner struct {

type testStruct struct {
A int `json:"a"`
N string `json:"n"`
N *string `json:"n"`
B []bInner `json:"b"`
C int `json:"c"`
G string `json:"g"`
Expand All @@ -64,7 +64,7 @@ func (s *FilterSuite) TestFilterSimpleObject() {

s.NotNil(mask)

res, err := filter(testStruct{A: 11, N: "nnn", C: 44, G: "ggg"}, mask)
res, err := filter(testStruct{A: 11, N: getStringPointer("nnn"), C: 44, G: "ggg"}, mask)
s.Nil(err)

j, err := json.Marshal(res)
Expand All @@ -90,14 +90,14 @@ func (s *FilterSuite) TestFilterComplexObject() {

obj := testStruct{
A: 11,
N: "nn",
N: getStringPointer("nn"),
C: 44,
G: "gg",
B: []bInner{
bInner{
K: 99,
B: []gInner{gInner{Z: 33}},
D: dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
D: &dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
},
},
}
Expand Down Expand Up @@ -128,27 +128,27 @@ func (s *FilterSuite) TestFilterComplexObjectArray() {

obj := []testStruct{testStruct{
A: 11,
N: "nn",
N: getStringPointer("nn"),
C: 44,
G: "gg",
B: []bInner{
bInner{
K: 99,
B: []gInner{gInner{Z: 33}},
D: dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
D: &dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
},
},
},
testStruct{
A: 11,
N: "nn",
N: getStringPointer("nn"),
C: 44,
G: "gg",
B: []bInner{
bInner{
K: 99,
B: []gInner{gInner{Z: 33}},
D: dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
D: &dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
},
},
}}
Expand All @@ -168,40 +168,40 @@ func (s *FilterSuite) TestFilterMap() {
obj := map[string]interface{}{
"a": testStruct{
A: 11,
N: "nn",
N: getStringPointer("nn"),
C: 44,
G: "gg",
B: []bInner{
bInner{
K: 99,
B: []gInner{gInner{Z: 33}},
D: dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
D: &dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
},
},
},
"b": testStruct{
A: 11,
N: "nn",
N: getStringPointer("nn"),
C: 44,
G: "gg",
B: []bInner{
bInner{
K: 99,
B: []gInner{gInner{Z: 33}},
D: dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
D: &dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
},
},
},
"c": testStruct{
A: 11,
N: "nn",
N: getStringPointer("nn"),
C: 44,
G: "gg",
B: []bInner{
bInner{
K: 99,
B: []gInner{gInner{Z: 33}},
D: dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
D: &dInner{G: gInner{Z: 22}, B: 34, C: cInner{A: 32}},
},
},
},
Expand All @@ -218,3 +218,7 @@ func (s *FilterSuite) TestFilterMap() {
func TestFilter(t *testing.T) {
suite.Run(t, new(FilterSuite))
}

func getStringPointer(s string) *string {
return &s
}
2 changes: 1 addition & 1 deletion util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type UtilSuite struct {
}

func (s *UtilSuite) TestgetFiledNamesByJSONKeys() {
fieldNames, err := getFiledNamesByJSONKeys(testStruct{A: 11, N: "nnn", C: 44, G: "ggg"}, []string{"a", "n", "fff"})
fieldNames, err := getFiledNamesByJSONKeys(testStruct{A: 11, N: getStringPointer("nnn"), C: 44, G: "ggg"}, []string{"a", "n", "fff"})

s.Nil(err)
s.Len(fieldNames, 2)
Expand Down

0 comments on commit 89f92fa

Please sign in to comment.