mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
fix: check if variable exist with $$ notation
This commit is contained in:
+22
-8
@@ -31,18 +31,32 @@ def parse_string_value(str_value):
|
|||||||
return str_value
|
return str_value
|
||||||
|
|
||||||
|
|
||||||
def is_variable_exist(content):
|
def is_var_or_func_exist(content):
|
||||||
|
""" check if variable or function exist
|
||||||
|
"""
|
||||||
if not isinstance(content, basestring):
|
if not isinstance(content, basestring):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True if variable_regex_compile.search(content) else False
|
try:
|
||||||
|
match_start_position = content.index("$", 0)
|
||||||
|
except ValueError:
|
||||||
def is_function_exist(content):
|
|
||||||
if not isinstance(content, basestring):
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True if function_regex_compile.search(content) else False
|
while match_start_position < len(content):
|
||||||
|
dollar_match = dolloar_regex_compile.match(content, match_start_position)
|
||||||
|
if dollar_match:
|
||||||
|
match_start_position = dollar_match.end()
|
||||||
|
continue
|
||||||
|
|
||||||
|
func_match = function_regex_compile.match(content, match_start_position)
|
||||||
|
if func_match:
|
||||||
|
return True
|
||||||
|
|
||||||
|
var_match = variable_regex_compile.match(content, match_start_position)
|
||||||
|
if var_match:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def regex_findall_variables(content):
|
def regex_findall_variables(content):
|
||||||
@@ -580,7 +594,7 @@ def prepare_lazy_data(content, functions_mapping=None, check_variables_set=None,
|
|||||||
|
|
||||||
elif isinstance(content, basestring):
|
elif isinstance(content, basestring):
|
||||||
# content is in string format here
|
# content is in string format here
|
||||||
if not (is_variable_exist(content) or is_function_exist(content)):
|
if not is_var_or_func_exist(content):
|
||||||
# content is neither variable nor function
|
# content is neither variable nor function
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|||||||
@@ -703,6 +703,37 @@ class TestParserBasic(unittest.TestCase):
|
|||||||
self.assertEqual(parsed_testcase["num2"], 6)
|
self.assertEqual(parsed_testcase["num2"], 6)
|
||||||
self.assertEqual(parsed_testcase["num1"], 3)
|
self.assertEqual(parsed_testcase["num1"], 3)
|
||||||
|
|
||||||
|
def test_is_var_or_func_exist(self):
|
||||||
|
self.assertTrue(parser.is_var_or_func_exist("$var"))
|
||||||
|
self.assertTrue(parser.is_var_or_func_exist("${var}"))
|
||||||
|
self.assertTrue(parser.is_var_or_func_exist("$var${var}"))
|
||||||
|
self.assertFalse(parser.is_var_or_func_exist("${var"))
|
||||||
|
self.assertFalse(parser.is_var_or_func_exist("$$var"))
|
||||||
|
self.assertFalse(parser.is_var_or_func_exist("var$$0"))
|
||||||
|
self.assertTrue(parser.is_var_or_func_exist("var$$$0"))
|
||||||
|
self.assertFalse(parser.is_var_or_func_exist("var$$$$0"))
|
||||||
|
self.assertTrue(parser.is_var_or_func_exist("${func()}"))
|
||||||
|
self.assertTrue(parser.is_var_or_func_exist("${func($a)}"))
|
||||||
|
self.assertTrue(parser.is_var_or_func_exist("${func($a)}$b"))
|
||||||
|
|
||||||
|
def test_parse_variables_mapping_dollar_notation(self):
|
||||||
|
variables = {
|
||||||
|
"varA": "123$varB",
|
||||||
|
"varB": "456$$0",
|
||||||
|
"varC": "${sum_two($a, $b)}",
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": "abc"
|
||||||
|
}
|
||||||
|
functions = {
|
||||||
|
"sum_two": sum_two
|
||||||
|
}
|
||||||
|
prepared_variables = parser.prepare_lazy_data(variables, functions, variables.keys())
|
||||||
|
parsed_testcase = parser.parse_variables_mapping(prepared_variables)
|
||||||
|
self.assertEqual(parsed_testcase["varB"], "456$$0")
|
||||||
|
self.assertEqual(parsed_testcase["varA"], "123456$$0")
|
||||||
|
self.assertEqual(parsed_testcase["varC"], 3)
|
||||||
|
|
||||||
def test_prepare_lazy_data(self):
|
def test_prepare_lazy_data(self):
|
||||||
variables = {
|
variables = {
|
||||||
"host": "https://httprunner.org",
|
"host": "https://httprunner.org",
|
||||||
|
|||||||
Reference in New Issue
Block a user