mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
parse functions: add support for variable argument
This commit is contained in:
11
ate/utils.py
11
ate/utils.py
@@ -17,7 +17,7 @@ except NameError:
|
||||
PYTHON_VERSION = 3
|
||||
|
||||
variable_regexp = re.compile(r"^\$(\w+)$")
|
||||
function_regexp = re.compile(r"^\$\{(\w+)\(([\w =,]*)\)\}$")
|
||||
function_regexp = re.compile(r"^\$\{(\w+)\(([\$\w =,]*)\)\}$")
|
||||
|
||||
def gen_random_string(str_len):
|
||||
return ''.join(
|
||||
@@ -168,10 +168,19 @@ def is_functon(content):
|
||||
return True if matched else False
|
||||
|
||||
def parse_string_value(str_value):
|
||||
""" parse string to number if possible
|
||||
e.g. "123" => 123
|
||||
"12.2" => 12.3
|
||||
"abc" => "abc"
|
||||
"$var" => "$var"
|
||||
"""
|
||||
try:
|
||||
return ast.literal_eval(str_value)
|
||||
except ValueError:
|
||||
return str_value
|
||||
except SyntaxError:
|
||||
# e.g. $var, ${func}
|
||||
return str_value
|
||||
|
||||
def parse_function(content):
|
||||
""" parse function name and args from string content.
|
||||
|
||||
@@ -170,10 +170,14 @@ class TestUtils(ApiServerUnittest):
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func(1, 2)}"
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func($a, $b)}"
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func(a=1, b=2)}"
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func(1, 2, a=3, b=4)}"
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func(1, $b, c=$x, d=4)}"
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func}"
|
||||
self.assertFalse(utils.is_functon(content))
|
||||
content = "$abc"
|
||||
@@ -188,6 +192,10 @@ class TestUtils(ApiServerUnittest):
|
||||
self.assertEqual(utils.parse_string_value(str_value), 12.3)
|
||||
str_value = "a123"
|
||||
self.assertEqual(utils.parse_string_value(str_value), "a123")
|
||||
str_value = "$var"
|
||||
self.assertEqual(utils.parse_string_value(str_value), "$var")
|
||||
str_value = "${func}"
|
||||
self.assertEqual(utils.parse_string_value(str_value), "${func}")
|
||||
|
||||
def test_parse_functon(self):
|
||||
content = "${func()}"
|
||||
|
||||
Reference in New Issue
Block a user