fix: json unmarshal int

This commit is contained in:
debugtalk
2021-10-09 23:17:36 +08:00
parent fbc557ecd9
commit bfc1742e3b
4 changed files with 66 additions and 13 deletions

View File

@@ -49,8 +49,37 @@ func EndsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...int
}
func EqualLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
if !assert.IsType(t, 129, expected, fmt.Sprintf("expected type is not int, got %#v", expected)) {
return false
length, err := convertInt(expected)
if err != nil {
return assert.Fail(t, fmt.Sprintf("expected type is not int, got %#v", expected), msgAndArgs...)
}
return assert.Len(t, actual, length, msgAndArgs...)
}
func convertInt(value interface{}) (int, error) {
switch v := value.(type) {
case int:
return v, nil
case int8:
return int(v), nil
case int16:
return int(v), nil
case int32:
return int(v), nil
case int64:
return int(v), nil
case uint:
return int(v), nil
case uint8:
return int(v), nil
case uint16:
return int(v), nil
case uint32:
return int(v), nil
case uint64:
return int(v), nil
default:
return 0, fmt.Errorf("unsupported int convertion for %v(%T)", v, v)
}
return assert.Len(t, actual, expected.(int), msgAndArgs...)
}