From aac919ba537db031b34d585359e3d2f6851afd26 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 17 Apr 2019 19:01:11 +0800 Subject: [PATCH] fix: check if variable exist with $$ notation --- httprunner/parser.py | 30 ++++++++++++++++++++++-------- tests/test_parser.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/httprunner/parser.py b/httprunner/parser.py index f80af2d0..7485127d 100644 --- a/httprunner/parser.py +++ b/httprunner/parser.py @@ -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 diff --git a/tests/test_parser.py b/tests/test_parser.py index 57945547..3dfb7847 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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",