数据驱动函数中添加随机词语返回函数

This commit is contained in:
彭宇琦 2021-01-15 19:25:58 +08:00
parent 970b97104a
commit c2d8cacd76
1 changed files with 44 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package com.auxiliary.datadriven;
import com.auxiliary.tool.data.CarLicecenType;
import com.auxiliary.tool.data.MobleNumberType;
import com.auxiliary.tool.data.PresetString;
import com.auxiliary.tool.data.RandomObject;
import com.auxiliary.tool.data.RandomString;
import com.auxiliary.tool.data.StringMode;
import com.auxiliary.tool.date.Time;
@ -347,4 +348,47 @@ public class Functions {
}
});
}
/**
* 定义根据预设词语随机返回函数该函数需要预先设定切分词语的标识符
* <p>
* 用法${word(词语(,词语))}
* </p>
* <p>
* <b>参数解释</b>
* <ul>
* <li>词语预设的词语范围用于设定随机返回词语的范围</li>
* </ul>
* <p>
* <p>
* <b>示例</b>
* <ul>
* <li>${word(in,out,stop)}表示从词语inout或者stop中随机返回一个词语</li>
* </ul>
* </p>
* <p>
* <b>注意</b>
* <ol>
* <li>括号中至少要存在一个词语否则不会匹配</li>
* <li>词语间分隔符为英文的逗号在词语中请勿使用该符号</li>
* </ol>
* </p>
*
* @param splitText 分隔符必须按照正则表达式的形式传入
* @return {@link DataDriverFunction}类对象
*/
public static DataDriverFunction randomWord() {
String regex = "word\\([^,]+(,[^,]+)*\\)";
return new DataDriverFunction(regex, text -> {
//获取括号中的内容并按照传入的规则对内容进行截取
String[] words = text.substring(text.indexOf("(") + 1, text.indexOf(")")).split(",");
//将词语添加至随机对象中
RandomObject<String> word = new RandomObject<>();
word.addObject(words);
//返回词语若词语为空则返回空串
return word.toObject().orElse("");
});
}
}