def test_is_functon(self): content = "${func()}" self.assertTrue(utils.is_functon(content)) content = "${func(5)}" 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" self.assertFalse(utils.is_functon(content)) content = "abc" self.assertFalse(utils.is_functon(content))
def get_eval_value(self, data): """ evaluate data recursively, each variable in data will be evaluated. """ if isinstance(data, (list, tuple)): return [self.get_eval_value(item) for item in data] if isinstance(data, dict): evaluated_data = {} for key, value in data.items(): evaluated_data[key] = self.get_eval_value(value) return evaluated_data if isinstance(data, (int, float)): return data # data is in string format here data = "" if data is None else data.strip() if utils.is_variable(data): # variable marker: $var variable_name = utils.parse_variable(data) value = self.testcase_variables_mapping.get(variable_name) if value is None: raise exception.ParamsError( "%s is not defined in bind variables!" % variable_name) return value elif utils.is_functon(data): # function marker: ${func(1, 2, a=3, b=4)} fuction_meta = utils.parse_function(data) func_name = fuction_meta['func_name'] args = fuction_meta.get('args', []) kwargs = fuction_meta.get('kwargs', {}) args = self.get_eval_value(args) kwargs = self.get_eval_value(kwargs) return self.testcase_config["functions"][func_name](*args, **kwargs) else: return data
def get_eval_value(self, data): """ evaluate data recursively, each variable in data will be evaluated. """ if isinstance(data, (list, tuple)): return [self.get_eval_value(item) for item in data] if isinstance(data, dict): evaluated_data = {} for key, value in data.items(): evaluated_data[key] = self.get_eval_value(value) return evaluated_data if isinstance(data, (int, float)): return data # data is in string format here data = "" if data is None else data.strip() if utils.is_functon(data): # function marker: ${func(1, 2, a=3, b=4)} fuction_meta = utils.parse_function(data) func_name = fuction_meta['func_name'] args = fuction_meta.get('args', []) kwargs = fuction_meta.get('kwargs', {}) args = self.get_eval_value(args) kwargs = self.get_eval_value(kwargs) return self.testcase_functions_config[func_name](*args, **kwargs) elif utils.get_contain_variables(data): parsed_data = utils.parse_variables( data, self.testcase_variables_mapping) return parsed_data else: return data