diff --git a/src/main/java/com/auxiliary/datadriven/Functions.java b/src/main/java/com/auxiliary/datadriven/Functions.java index 78600ba..c05d061 100644 --- a/src/main/java/com/auxiliary/datadriven/Functions.java +++ b/src/main/java/com/auxiliary/datadriven/Functions.java @@ -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 { } }); } + + /** + * 定义根据预设词语随机返回函数,该函数需要预先设定切分词语的标识符 + *

+ * 用法:${word(词语(,词语))} + *

+ *

+ * 参数解释: + *

+ *

+ *

+ * 示例: + *

+ *

+ *

+ * 注意: + *

    + *
  1. 括号中至少要存在一个词语,否则不会匹配
  2. + *
  3. 词语间分隔符为英文的逗号,在词语中请勿使用该符号
  4. + *
+ *

+ * + * @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 word = new RandomObject<>(); + word.addObject(words); + + //返回词语,若词语为空,则返回空串 + return word.toObject().orElse(""); + }); + } }