在 Mockito 中,如何模拟将函数接口作为方法参数的方法?例如:
String foo(String m, Function<Double, Options> r) {}
我有许多以下类型的记录,其属性值在字符串中
{
"City": "Pune",
"Temperature": "32",
"Unit": "C",
"Date": "22-11-2012"
}
Run Code Online (Sandbox Code Playgroud)
以及定义数据类型和其他属性的相关记录描述符
{
"City": {
"datatype": "string"
},
"Temperature": {
"datatype": "double"
},
"Unit": {
"datatype": "string"
},
"Date": {
"datatype": "datetime",
"dateformat": "%d-%m-%Y",
"timezone": "UTC"
}
}
Run Code Online (Sandbox Code Playgroud)
我需要将记录属性值从字符串转换为记录描述符中提到的适当数据类型
我有一个函数调度字典
{
"int" : string_to_int,
"double": string_to_double,
"bool": string_to_bool,
"datetime": string_to_datetime
}
def string_to_int(value):
<<convert to integer>>
def string_to_double(value):
<<convert to double>>
def string_to_bool(value):
<<convert to bool>>
def string_to_datetime(value, date_format, time_zone):
<<convert to datetime>>
Run Code Online (Sandbox Code Playgroud)
通过循环遍历每个属性,如何在 python 中执行函数调度以将属性值转换为适当的数据类型?在循环中不使用任何 if..else 逻辑的情况下,为数据时间转换传递额外参数的正确方法是什么?