fix: check if variable exist with $$ notation

This commit is contained in:
debugtalk
2019-04-17 19:01:11 +08:00
parent eda555f1b6
commit aac919ba53
2 changed files with 53 additions and 8 deletions

View File

@@ -31,18 +31,32 @@ def parse_string_value(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):
return False
return True if variable_regex_compile.search(content) else False
def is_function_exist(content):
if not isinstance(content, basestring):
try:
match_start_position = content.index("$", 0)
except ValueError:
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):
@@ -580,7 +594,7 @@ def prepare_lazy_data(content, functions_mapping=None, check_variables_set=None,
elif isinstance(content, basestring):
# 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
return content

View File

@@ -703,6 +703,37 @@ class TestParserBasic(unittest.TestCase):
self.assertEqual(parsed_testcase["num2"], 6)
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):
variables = {
"host": "https://httprunner.org",