forked from mirror/go-json
add func Unmarshal tests
This commit is contained in:
parent
585ce46b31
commit
d34d79600a
|
@ -152,6 +152,7 @@ func Test_Decoder(t *testing.T) {
|
||||||
B string `json:"str"`
|
B string `json:"str"`
|
||||||
C bool
|
C bool
|
||||||
D *T
|
D *T
|
||||||
|
E func()
|
||||||
}
|
}
|
||||||
content := []byte(`
|
content := []byte(`
|
||||||
{
|
{
|
||||||
|
@ -162,7 +163,8 @@ func Test_Decoder(t *testing.T) {
|
||||||
"aa": 2,
|
"aa": 2,
|
||||||
"bb": "world",
|
"bb": "world",
|
||||||
"cc": true
|
"cc": true
|
||||||
}
|
},
|
||||||
|
"e" : null
|
||||||
}`)
|
}`)
|
||||||
assertErr(t, json.Unmarshal(content, &v))
|
assertErr(t, json.Unmarshal(content, &v))
|
||||||
assertEq(t, "struct.A", 123, v.A)
|
assertEq(t, "struct.A", 123, v.A)
|
||||||
|
@ -171,6 +173,7 @@ func Test_Decoder(t *testing.T) {
|
||||||
assertEq(t, "struct.D.AA", 2, v.D.AA)
|
assertEq(t, "struct.D.AA", 2, v.D.AA)
|
||||||
assertEq(t, "struct.D.BB", "world", v.D.BB)
|
assertEq(t, "struct.D.BB", "world", v.D.BB)
|
||||||
assertEq(t, "struct.D.CC", true, v.D.CC)
|
assertEq(t, "struct.D.CC", true, v.D.CC)
|
||||||
|
assertEq(t, "struct.E", nil, v.E)
|
||||||
t.Run("struct.field null", func(t *testing.T) {
|
t.Run("struct.field null", func(t *testing.T) {
|
||||||
var v struct {
|
var v struct {
|
||||||
A string
|
A string
|
||||||
|
@ -179,8 +182,9 @@ func Test_Decoder(t *testing.T) {
|
||||||
D map[string]interface{}
|
D map[string]interface{}
|
||||||
E [2]string
|
E [2]string
|
||||||
F interface{}
|
F interface{}
|
||||||
|
G func()
|
||||||
}
|
}
|
||||||
assertErr(t, json.Unmarshal([]byte(`{"a":null,"b":null,"c":null,"d":null,"e":null,"f":null}`), &v))
|
assertErr(t, json.Unmarshal([]byte(`{"a":null,"b":null,"c":null,"d":null,"e":null,"f":null,"g":null}`), &v))
|
||||||
assertEq(t, "string", v.A, "")
|
assertEq(t, "string", v.A, "")
|
||||||
assertNeq(t, "[]string", v.B, nil)
|
assertNeq(t, "[]string", v.B, nil)
|
||||||
assertEq(t, "[]string", len(v.B), 0)
|
assertEq(t, "[]string", len(v.B), 0)
|
||||||
|
@ -191,6 +195,7 @@ func Test_Decoder(t *testing.T) {
|
||||||
assertNeq(t, "array", v.E, nil)
|
assertNeq(t, "array", v.E, nil)
|
||||||
assertEq(t, "array", len(v.E), 2)
|
assertEq(t, "array", len(v.E), 2)
|
||||||
assertEq(t, "interface{}", v.F, nil)
|
assertEq(t, "interface{}", v.F, nil)
|
||||||
|
assertEq(t, "func", v.G, nil)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
t.Run("interface", func(t *testing.T) {
|
t.Run("interface", func(t *testing.T) {
|
||||||
|
@ -239,6 +244,11 @@ func Test_Decoder(t *testing.T) {
|
||||||
assertEq(t, "interface", nil, v)
|
assertEq(t, "interface", nil, v)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
t.Run("func", func(t *testing.T) {
|
||||||
|
var v func()
|
||||||
|
assertErr(t, json.Unmarshal([]byte(`null`), &v))
|
||||||
|
assertEq(t, "func", nil, v)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIssue98(t *testing.T) {
|
func TestIssue98(t *testing.T) {
|
||||||
|
|
|
@ -11,7 +11,7 @@ func assertErr(t *testing.T, err error) {
|
||||||
|
|
||||||
func assertEq(t *testing.T, msg string, exp interface{}, act interface{}) {
|
func assertEq(t *testing.T, msg string, exp interface{}, act interface{}) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if exp != act {
|
if exp != act && exp != nil && act != nil {
|
||||||
t.Fatalf("failed to test for %s. exp=[%v] but act=[%v]", msg, exp, act)
|
t.Fatalf("failed to test for %s. exp=[%v] but act=[%v]", msg, exp, act)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue