mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
feat: call with python builtin functions
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import ast
|
||||
import builtins
|
||||
import re
|
||||
from typing import Any, Set, Text, Callable, Tuple, List, Dict, Union
|
||||
|
||||
from httprunner import loader, utils
|
||||
from httprunner.v3 import exceptions
|
||||
from httprunner.v3.exceptions import VariableNotFound, FunctionNotFound
|
||||
|
||||
@@ -180,6 +182,51 @@ def parse_function_params(params):
|
||||
return function_meta
|
||||
|
||||
|
||||
def get_mapping_function(function_name: Text, functions_mapping: Dict[Text, Callable]) -> Callable:
|
||||
""" get function from functions_mapping,
|
||||
if not found, then try to check if builtin function.
|
||||
|
||||
Args:
|
||||
function_name (str): function name
|
||||
functions_mapping (dict): functions mapping
|
||||
|
||||
Returns:
|
||||
mapping function object.
|
||||
|
||||
Raises:
|
||||
exceptions.FunctionNotFound: function is neither defined in debugtalk.py nor builtin.
|
||||
|
||||
"""
|
||||
if function_name in functions_mapping:
|
||||
return functions_mapping[function_name]
|
||||
|
||||
elif function_name in ["parameterize", "P"]:
|
||||
return loader.load_csv_file
|
||||
|
||||
elif function_name in ["environ", "ENV"]:
|
||||
return utils.get_os_environ
|
||||
|
||||
elif function_name in ["multipart_encoder", "multipart_content_type"]:
|
||||
# extension for upload test
|
||||
from httprunner.ext import uploader
|
||||
return getattr(uploader, function_name)
|
||||
|
||||
try:
|
||||
# check if HttpRunner builtin functions
|
||||
built_in_functions = loader.load_builtin_functions()
|
||||
return built_in_functions[function_name]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
try:
|
||||
# check if Python builtin functions
|
||||
return getattr(builtins, function_name)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
raise exceptions.FunctionNotFound(f"{function_name} is not found.")
|
||||
|
||||
|
||||
def parse_string(
|
||||
raw_string: Text,
|
||||
variables_mapping: Dict[Text, Any],
|
||||
@@ -225,10 +272,7 @@ def parse_string(
|
||||
func_match = function_regex_compile.match(raw_string, match_start_position)
|
||||
if func_match:
|
||||
func_name = func_match.group(1)
|
||||
try:
|
||||
func = functions_mapping[func_name]
|
||||
except KeyError:
|
||||
raise FunctionNotFound(f"{func_name} not found in {functions_mapping}")
|
||||
func = get_mapping_function(func_name, functions_mapping)
|
||||
|
||||
func_params_str = func_match.group(2)
|
||||
function_meta = parse_function_params(func_params_str)
|
||||
|
||||
@@ -364,9 +364,8 @@ class TestParserBasic(unittest.TestCase):
|
||||
value = parser.parse_data("ABC$var_5${func1($var_1, $var_3)}", variables_mapping, functions_mapping)
|
||||
self.assertEqual(value, "ABCTrueabc123")
|
||||
|
||||
# TODO: Python builtin functions
|
||||
# value = parser.parse_data("ABC${ord(a)}DEF${len(abcd)}", variables_mapping, functions_mapping)
|
||||
# self.assertEqual(value, "ABC97DEF4")
|
||||
value = parser.parse_data("ABC${ord(a)}DEF${len(abcd)}", variables_mapping, functions_mapping)
|
||||
self.assertEqual(value, "ABC97DEF4")
|
||||
|
||||
def test_parse_data_func_var_duplicate(self):
|
||||
variables_mapping = {
|
||||
|
||||
Reference in New Issue
Block a user