add test function

This commit is contained in:
guonaihong 2019-07-15 22:58:16 +08:00
parent 15e9387ca0
commit 36f6c493d4
3 changed files with 17 additions and 2 deletions

View File

@ -1177,6 +1177,20 @@ func testBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, bad
assert.Error(t, err) assert.Error(t, err)
} }
type Identifier struct {
Assigner *Reference `json:"assigner,omitempty"`
}
type Reference struct {
Identifier *Identifier `json:"identifier,omitempty"`
}
func TestLoopReference(t *testing.T) {
r := Reference{}
err := mapForm(&r, map[string][]string{})
assert.Error(t, err)
}
func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) { func testProtoBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
assert.Equal(t, name, b.Name()) assert.Equal(t, name, b.Name())

View File

@ -18,6 +18,7 @@ import (
var LimitMappingCallNumber = 10000 var LimitMappingCallNumber = 10000
var errUnknownType = errors.New("Unknown type") var errUnknownType = errors.New("Unknown type")
var ErrMaybeCircularReference = errors.New("Maybe entering a circular reference")
func mapUri(ptr interface{}, m map[string][]string) error { func mapUri(ptr interface{}, m map[string][]string) error {
return mapFormByTag(ptr, m, "uri") return mapFormByTag(ptr, m, "uri")
@ -73,7 +74,7 @@ func mapping(value reflect.Value, field reflect.StructField, setter setter, tag
} }
if callNumber > LimitMappingCallNumber { if callNumber > LimitMappingCallNumber {
return false, errors.New("Maybe entering a circular reference") return false, ErrMaybeCircularReference
} }
if vKind != reflect.Struct || !field.Anonymous { if vKind != reflect.Struct || !field.Anonymous {

View File

@ -52,7 +52,7 @@ func TestMappingBaseTypes(t *testing.T) {
field := val.Elem().Type().Field(0) field := val.Elem().Type().Field(0)
_, err := mapping(val, emptyField, formSource{field.Name: {tt.form}}, "form") _, err := mapping(val, emptyField, formSource{field.Name: {tt.form}}, "form", 0)
assert.NoError(t, err, testName) assert.NoError(t, err, testName)
actual := val.Elem().Field(0).Interface() actual := val.Elem().Field(0).Interface()