我正在使用pholser的端口。我必须生成匹配给定模式的字符串,如\[a-zA-Z0-9\\.\\-\\\\;\\:\\_\\@\\[\\]\\^/\\|\\}\\{]* Length 40
。
我将Generator类扩展为:
public class InputGenerator extends Generator<TestData> {...}
它会多载函数:
publicTestData generate(SourceOfRandomness random, GenerationStatus status) {...}
现在,random有类似**nextDouble(),nextInt()**的函数,但是没有字符串的函数!我如何生成符合上述模式的随机字符串?
3条答案
按热度按时间mlnl4t2r1#
下面是一个自定义生成器的代码段,它实现了
generate(..)
方法,以返回与您发布的模式匹配的随机字符串。edit一个简单的单元测试,用于演示
MyCharacterGenerator
类的用法。样本输出由
shouldHold
生成pdtvr36n2#
没有
random.nextString()
,但是在junit-quickcheck-generators库中有一种生成随机字符串的方法。你可以在使用gen().type(String.class)
创建新的生成器时访问它。但是,我们似乎没有太多的控制权。下面是一个简单的
StringBuilder
生成器示例,用于演示如何使用String
生成器:r9f1avp53#
我刚刚创建了一个库,它假定以一种通用方式完成您想要的任务:https://github.com/SimY4/coregex
简单用法示例: