From 83923affa871b28ea7b9ef66b4a2f1f60a324f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E8=81=AA?= Date: Wed, 29 Dec 2021 15:02:12 +0800 Subject: [PATCH] feat: data-driven. --- convert.go | 2 +- internal/builtin/function.go | 8 +++----- parser.go | 20 ++++--------------- parser_test.go | 38 +++++++++++++++++++++++++++--------- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/convert.go b/convert.go index ba3b8d15..3d9880eb 100644 --- a/convert.go +++ b/convert.go @@ -96,7 +96,7 @@ func loadFromYAML(path string) (*TCase, error) { return tc, err } -func loadFromCSV(path string) []map[string]string { +func LoadFromCSV(path string) []map[string]string { path, err := filepath.Abs(path) if err != nil { log.Error().Str("path", path).Err(err).Msg("convert absolute path failed") diff --git a/internal/builtin/function.go b/internal/builtin/function.go index 2384d654..be7c129e 100644 --- a/internal/builtin/function.go +++ b/internal/builtin/function.go @@ -3,6 +3,7 @@ package builtin import ( "crypto/md5" "encoding/hex" + "github.com/httprunner/hrp" "math" "math/rand" "time" @@ -14,7 +15,8 @@ var Functions = map[string]interface{}{ "gen_random_string": genRandomString, // call with one argument "max": math.Max, // call with two arguments "md5": MD5, - "getAppVersion": getAppVersion, // test + "parameterize": hrp.LoadFromCSV, + "P": hrp.LoadFromCSV, } func init() { @@ -45,7 +47,3 @@ func MD5(str string) string { hasher.Write([]byte(str)) return hex.EncodeToString(hasher.Sum(nil)) } - -func getAppVersion() []float64 { - return []float64{3.1, 3.3} -} diff --git a/parser.go b/parser.go index e95946c8..1c836f36 100644 --- a/parser.go +++ b/parser.go @@ -259,25 +259,13 @@ func contains(s []string, e string) bool { return false } -func getMappingFunction(funcName string) (interface{}, error) { - if function, ok := builtin.Functions[funcName]; ok { - // function is builtin - return function, nil - } else if contains([]string{"parameterize", "P"}, funcName) { - // parameterize function - return loadFromCSV, nil - } else { - // function not found - return nil, fmt.Errorf("function %s is not found", funcName) - } -} - // callFunc call function with arguments // only support return at most one result value func callFunc(funcName string, arguments ...interface{}) (interface{}, error) { - function, err := getMappingFunction(funcName) - if err != nil { - return nil, err + function, ok := builtin.Functions[funcName] + if !ok { + // function not found + return nil, fmt.Errorf("function %s is not found", funcName) } funcValue := reflect.ValueOf(function) if funcValue.Kind() != reflect.Func { diff --git a/parser_test.go b/parser_test.go index 194f70f1..cb1bcd8f 100644 --- a/parser_test.go +++ b/parser_test.go @@ -625,8 +625,11 @@ func TestParseParameters(t *testing.T) { expectVars []map[string]interface{} }{ { - map[string]interface{}{"username-password": "${parameterize(examples/account.csv)}", "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}}, - []map[string]interface{}{{"username": "test1", "password": "111111", "user_agent": "IOS/10.1"}, + map[string]interface{}{ + "username-password": "${parameterize(examples/account.csv)}", + "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}}, + []map[string]interface{}{ + {"username": "test1", "password": "111111", "user_agent": "IOS/10.1"}, {"username": "test1", "password": "111111", "user_agent": "IOS/10.2"}, {"username": "test2", "password": "222222", "user_agent": "IOS/10.1"}, {"username": "test2", "password": "222222", "user_agent": "IOS/10.2"}, @@ -634,12 +637,23 @@ func TestParseParameters(t *testing.T) { {"username": "test3", "password": "333333", "user_agent": "IOS/10.2"}}, }, { - map[string]interface{}{}, - nil, + map[string]interface{}{ + "username-password": [][]interface{}{{"test1", "111111"}, {"test2", "222222"}, {"test3", "333333"}}, + "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}, + "app_version": []interface{}{0.3}}, + []map[string]interface{}{ + {"username": "test1", "password": "111111", "user_agent": "IOS/10.1", "app_version": 0.3}, + {"username": "test1", "password": "111111", "user_agent": "IOS/10.2", "app_version": 0.3}, + {"username": "test2", "password": "222222", "user_agent": "IOS/10.1", "app_version": 0.3}, + {"username": "test2", "password": "222222", "user_agent": "IOS/10.2", "app_version": 0.3}, + {"username": "test3", "password": "333333", "user_agent": "IOS/10.1", "app_version": 0.3}, + {"username": "test3", "password": "333333", "user_agent": "IOS/10.2", "app_version": 0.3}}, }, { - nil, - nil, + map[string]interface{}{}, nil, + }, + { + nil, nil, }, } for _, data := range testData { @@ -655,13 +669,19 @@ func TestParseParametersError(t *testing.T) { rawVars map[string]interface{} }{ { - map[string]interface{}{"username_password": "${parameterize(examples/account.csv)}", "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}}, + map[string]interface{}{ + "username_password": "${parameterize(examples/account.csv)}", + "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}}, }, { - map[string]interface{}{"username-password": "${parameterize(examples/account.csv)}", "user-agent": []interface{}{"IOS/10.1", "IOS/10.2"}}, + map[string]interface{}{ + "username-password": "${parameterize(examples/account.csv)}", + "user-agent": []interface{}{"IOS/10.1", "IOS/10.2"}}, }, { - map[string]interface{}{"username-password": "${param(examples/account.csv)}", "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}}, + map[string]interface{}{ + "username-password": "${param(examples/account.csv)}", + "user_agent": []interface{}{"IOS/10.1", "IOS/10.2"}}, }, } for _, data := range testData {