From 1aafdd840184cd732d4b2f0f940f1cfefd678ed9 Mon Sep 17 00:00:00 2001 From: xucong053 Date: Mon, 11 Apr 2022 16:23:18 +0800 Subject: [PATCH] fix: failed to assert when int is compared with int64 #1232 --- hrp/internal/builtin/utils.go | 34 ++++++++++++++++++++++++++++++++++ hrp/parser.go | 2 +- hrp/parser_test.go | 2 +- hrp/response.go | 4 ++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/hrp/internal/builtin/utils.go b/hrp/internal/builtin/utils.go index 2e70f906..47654912 100644 --- a/hrp/internal/builtin/utils.go +++ b/hrp/internal/builtin/utils.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "path/filepath" + "reflect" "strconv" "strings" @@ -198,6 +199,39 @@ func Interface2Float64(i interface{}) (float64, error) { 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") // LoadFile loads file content with file extension and assigns to structObj diff --git a/hrp/parser.go b/hrp/parser.go index 1e5c6b07..46ab0f47 100644 --- a/hrp/parser.go +++ b/hrp/parser.go @@ -117,7 +117,7 @@ func (p *Parser) Parse(raw interface{}, variablesMapping map[string]interface{}) return parsedMap, nil default: // other types, e.g. nil, int, float, bool - return raw, nil + return builtin.TypeNormalization(raw), nil } } diff --git a/hrp/parser_test.go b/hrp/parser_test.go index 41b376b8..a0311a71 100644 --- a/hrp/parser_test.go +++ b/hrp/parser_test.go @@ -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": "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)}"}, diff --git a/hrp/response.go b/hrp/response.go index 1396f2e2..af726837 100644 --- a/hrp/response.go +++ b/hrp/response.go @@ -170,7 +170,9 @@ func (v *responseObject) Validate(iValidators []interface{}, variablesMapping ma Str("checkExpr", validator.Check). Str("assertMethod", assertMethod). Interface("expectValue", expectValue). + Str("expectValueType", builtin.InterfaceType(expectValue)). Interface("checkValue", checkValue). + Str("checkValueType", builtin.InterfaceType(checkValue)). Bool("result", result). Msgf("validate %s", checkItem) if !result { @@ -179,7 +181,9 @@ func (v *responseObject) Validate(iValidators []interface{}, variablesMapping ma Str("checkExpr", validator.Check). Str("assertMethod", assertMethod). Interface("checkValue", checkValue). + Str("checkValueType", builtin.InterfaceType(checkValue)). Interface("expectValue", expectValue). + Str("expectValueType", builtin.InterfaceType(expectValue)). Msg("assert failed") return errors.New("step validation failed") }