From 9d68340eaa520f7e32c839302e59878bd05a6ee4 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 4 Jul 2017 15:10:18 +0800 Subject: [PATCH] parse functions: add support for variable argument --- ate/utils.py | 11 ++++++++++- test/test_utils.py | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ate/utils.py b/ate/utils.py index c8a0768f..cc721445 100644 --- a/ate/utils.py +++ b/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. diff --git a/test/test_utils.py b/test/test_utils.py index 7af7e033..c20da35d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -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()}"