mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 15:37:35 +08:00
fix: json unmarshal int
This commit is contained in:
+32
-3
@@ -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 {
|
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)) {
|
length, err := convertInt(expected)
|
||||||
return false
|
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...)
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -80,7 +80,9 @@ func loadFromJSON(path string) (*TCase, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tc := &TCase{}
|
tc := &TCase{}
|
||||||
err = json.Unmarshal(file, tc)
|
decoder := json.NewDecoder(bytes.NewReader(file))
|
||||||
|
decoder.UseNumber()
|
||||||
|
err = decoder.Decode(tc)
|
||||||
return tc, err
|
return tc, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-4
@@ -83,10 +83,7 @@ func TestDumpAndLoadYAML(t *testing.T) {
|
|||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, tc.Config.Name, demoTestCase.Config.Name) {
|
if !assert.Equal(t, tc.Config, demoTestCase.Config) {
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
if !assert.Equal(t, tc.Config.BaseURL, demoTestCase.Config.BaseURL) {
|
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, tc.TestSteps[1].Name, demoTestCase.TestSteps[1].Name()) {
|
if !assert.Equal(t, tc.TestSteps[1].Name, demoTestCase.TestSteps[1].Name()) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package httpboomer
|
package httpboomer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -61,6 +62,11 @@ func parseData(raw interface{}, variablesMapping map[string]interface{}) (interf
|
|||||||
rawValue := reflect.ValueOf(raw)
|
rawValue := reflect.ValueOf(raw)
|
||||||
switch rawValue.Kind() {
|
switch rawValue.Kind() {
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
|
// json.Number
|
||||||
|
if rawValue, ok := raw.(json.Number); ok {
|
||||||
|
return parseJSONNumber(rawValue)
|
||||||
|
}
|
||||||
|
// other string
|
||||||
value := rawValue.String()
|
value := rawValue.String()
|
||||||
value = strings.TrimSpace(value)
|
value = strings.TrimSpace(value)
|
||||||
return parseString(value, variablesMapping)
|
return parseString(value, variablesMapping)
|
||||||
@@ -97,6 +103,16 @@ func parseData(raw interface{}, variablesMapping map[string]interface{}) (interf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseJSONNumber(raw json.Number) (interface{}, error) {
|
||||||
|
if strings.Contains(raw.String(), ".") {
|
||||||
|
// float64
|
||||||
|
return raw.Float64()
|
||||||
|
} else {
|
||||||
|
// int64
|
||||||
|
return raw.Int64()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
regexVariable = `[a-zA-Z_]\w*` // variable name should start with a letter or underscore
|
regexVariable = `[a-zA-Z_]\w*` // variable name should start with a letter or underscore
|
||||||
regexFunctionName = `[a-zA-Z_]\w*` // function name should start with a letter or underscore
|
regexFunctionName = `[a-zA-Z_]\w*` // function name should start with a letter or underscore
|
||||||
@@ -252,17 +268,26 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
|||||||
|
|
||||||
argumentsValue := make([]reflect.Value, len(arguments))
|
argumentsValue := make([]reflect.Value, len(arguments))
|
||||||
for index, argument := range arguments {
|
for index, argument := range arguments {
|
||||||
// ensure each argument type match
|
argumentValue := reflect.ValueOf(argument)
|
||||||
expectArgumentType := funcValue.Type().In(index)
|
expectArgumentType := funcValue.Type().In(index)
|
||||||
actualArgumentType := reflect.TypeOf(argument)
|
actualArgumentType := reflect.TypeOf(argument)
|
||||||
if expectArgumentType != actualArgumentType {
|
|
||||||
// function argument type not match
|
// type match
|
||||||
err := fmt.Errorf("function %s argument %d type not match, expect %v, actual %v",
|
if expectArgumentType == actualArgumentType {
|
||||||
|
argumentsValue[index] = argumentValue
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// type not match, check if convertible
|
||||||
|
if !actualArgumentType.ConvertibleTo(expectArgumentType) {
|
||||||
|
// function argument type not match and not convertible
|
||||||
|
err := fmt.Errorf("function %s argument %d type is neither match nor convertible, expect %v, actual %v",
|
||||||
funcName, index, expectArgumentType, actualArgumentType)
|
funcName, index, expectArgumentType, actualArgumentType)
|
||||||
log.Printf("[callFunction] error: %s", err.Error())
|
log.Printf("[callFunction] error: %s", err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
argumentsValue[index] = reflect.ValueOf(argument)
|
// convert argument to expect type
|
||||||
|
argumentsValue[index] = argumentValue.Convert(expectArgumentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[callFunction] func: %v, input arguments: %v", funcName, arguments)
|
log.Printf("[callFunction] func: %v, input arguments: %v", funcName, arguments)
|
||||||
|
|||||||
Reference in New Issue
Block a user