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