Merge pull request #1241 from xucong053/bugfix-failed-to-assert

fix: failed to assert when int is compared with int64 #1232
This commit is contained in:
debugtalk
2022-04-11 19:59:01 +08:00
committed by GitHub
4 changed files with 40 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"reflect"
"strconv" "strconv"
"strings" "strings"
@@ -198,6 +199,39 @@ func Interface2Float64(i interface{}) (float64, error) {
return 0, errors.New("failed to convert interface to float64") return 0, errors.New("failed to convert interface to float64")
} }
func TypeNormalization(raw interface{}) interface{} {
rawValue := reflect.ValueOf(raw)
switch rawValue.Kind() {
case reflect.Int:
return rawValue.Int()
case reflect.Int8:
return rawValue.Int()
case reflect.Int16:
return rawValue.Int()
case reflect.Int32:
return rawValue.Int()
case reflect.Float32:
return rawValue.Float()
case reflect.Uint:
return rawValue.Uint()
case reflect.Uint8:
return rawValue.Uint()
case reflect.Uint16:
return rawValue.Uint()
case reflect.Uint32:
return rawValue.Uint()
default:
return raw
}
}
func InterfaceType(raw interface{}) string {
if raw == nil {
return ""
}
return reflect.TypeOf(raw).String()
}
var ErrUnsupportedFileExt = fmt.Errorf("unsupported file extension") var ErrUnsupportedFileExt = fmt.Errorf("unsupported file extension")
// LoadFile loads file content with file extension and assigns to structObj // LoadFile loads file content with file extension and assigns to structObj

View File

@@ -117,7 +117,7 @@ func (p *Parser) Parse(raw interface{}, variablesMapping map[string]interface{})
return parsedMap, nil return parsedMap, nil
default: default:
// other types, e.g. nil, int, float, bool // other types, e.g. nil, int, float, bool
return raw, nil return builtin.TypeNormalization(raw), nil
} }
} }

View File

@@ -632,7 +632,7 @@ func TestParseVariables(t *testing.T) {
}{ }{
{ {
map[string]interface{}{"varA": "$varB", "varB": "$varC", "varC": "123", "a": 1, "b": 2}, map[string]interface{}{"varA": "$varB", "varB": "$varC", "varC": "123", "a": 1, "b": 2},
map[string]interface{}{"varA": "123", "varB": "123", "varC": "123", "a": 1, "b": 2}, map[string]interface{}{"varA": "123", "varB": "123", "varC": "123", "a": int64(1), "b": int64(2)},
}, },
{ {
map[string]interface{}{"n": 34.5, "a": 12.3, "b": "$n", "varFoo2": "${max($a, $b)}"}, map[string]interface{}{"n": 34.5, "a": 12.3, "b": "$n", "varFoo2": "${max($a, $b)}"},

View File

@@ -170,7 +170,9 @@ func (v *responseObject) Validate(iValidators []interface{}, variablesMapping ma
Str("checkExpr", validator.Check). Str("checkExpr", validator.Check).
Str("assertMethod", assertMethod). Str("assertMethod", assertMethod).
Interface("expectValue", expectValue). Interface("expectValue", expectValue).
Str("expectValueType", builtin.InterfaceType(expectValue)).
Interface("checkValue", checkValue). Interface("checkValue", checkValue).
Str("checkValueType", builtin.InterfaceType(checkValue)).
Bool("result", result). Bool("result", result).
Msgf("validate %s", checkItem) Msgf("validate %s", checkItem)
if !result { if !result {
@@ -179,7 +181,9 @@ func (v *responseObject) Validate(iValidators []interface{}, variablesMapping ma
Str("checkExpr", validator.Check). Str("checkExpr", validator.Check).
Str("assertMethod", assertMethod). Str("assertMethod", assertMethod).
Interface("checkValue", checkValue). Interface("checkValue", checkValue).
Str("checkValueType", builtin.InterfaceType(checkValue)).
Interface("expectValue", expectValue). Interface("expectValue", expectValue).
Str("expectValueType", builtin.InterfaceType(expectValue)).
Msg("assert failed") Msg("assert failed")
return errors.New("step validation failed") return errors.New("step validation failed")
} }