A handy utility for generating strings that match given regular expression criteria.
You can use this library directly by compiling an instance of coregex from any given regular expression pattern:
varpattern = Pattern.compile("[a-zA-Z]{3}");
varcoregex = Coregex.from(pattern);Having an instance of coregex, you can ask it to generate a string matching original regular expression. At any given time this should be true:
varseed = ThreadLocalRandom.current().nextLong();
assertpattern.matcher(coregex.generate(seed)).matches();Given the library is primarily intended to be used in property based testing, it comes with a set of integrations for popular property based testing frameworks.
Include the following dependency into your project:
testImplementation "com.github.simy4.coregex:coregex-functionaljava-quickcheck"Use the provided CoregexArbirary class to generate a string that would match the regular expression predicate:
@RunWith(PropertyTestRunner.class)
publicclassMyTest {
privatestaticfinalPatternPATTERN = Pattern.compile("[a-zA-Z]{3}");
publicPropertymyProperty() {
returnproperty(CoregexArbitrary.gen(PATTERN), CoregexArbitrary.shrink(PATTERN), str -> prop(3 == str.length()));
}
}Include the following dependency into your project:
libraryDependencies ++=Seq("com.github.simy4.coregex"%%"coregex-hedgehog"%Test)Use the provided CoregexGen class to generate a string that would match the regular expression predicate:
objectMySpecextendsProperties {
deftests:List[Test] =List(
property("my property", myProperty),
)
defmyProperty:Property=for {
str <-CoregexGen.fromRegex("[a-zA-Z]{3}".r).forAll
} yieldResult.assert(str.length ====3)
}Include the following dependency into your project:
testImplementation "com.github.simy4.coregex:coregex-jetCheck"Use the provided CoregexGenerator class to generate a string that would match the regular expression predicate:
PropertyChecker.forAll(CoregexGenerator.of("[a-zA-Z]{3}"), str -> 3 == str.length());Include the following dependency into your project:
testImplementation "com.github.simy4.coregex:coregex-jqwik"Use the provided Regex annotation to generate a string that would match the regular expression predicate:
classMyTest {
@PropertyvoidmyProperty(@ForAll@Regex("[a-zA-Z]{3}") Stringstr) {
assertThat(str).hasLength(3);
}
}Include the following dependency into your project:
testImplementation "com.github.simy4.coregex:coregex-junit-quickcheck"Use the provided Regex annotation to generate a string that would match the regular expression predicate:
@RunWith(JUnitQuickcheck.class)
publicclassMyTest {
@PropertypublicvoidmyProperty(@Regex("[a-zA-Z]{3}") Stringstr) {
assertThat(str).hasLength(3);
}
}Include the following dependency into your project:
testImplementation "com.github.simy4.coregex:coregex-kotest"Use the provided CoregexArbirary class to generate a string that would match the regular expression predicate:
classMyTest : DescribeSpec({
describe("my property") {
it("should hold") {
checkAll(CoregexArbitrary.of("[a-zA-Z]{3}")) { str ->
str.length shouldBe 3
}
}
}
})Include the following dependency into your project:
libraryDependencies ++=Seq("com.github.simy4.coregex"%%"coregex-scalacheck"%Test)Use the provided CoregexInstances trait to constrain string arbitraries:
objectMySpecificationextendsProperties("MySpecification") withCoregexInstances {
property("my property") = forAll { (str: StringMatching["[a-zA-Z]{3}"]) =>3== str.length }
}Include the following dependency into your project:
testImplementation "com.github.simy4.coregex:coregex-vavr-test"Use the provided CoregexArbirary class to generate a string that would match the regular expression predicate:
Property.def("my property")
.forAll(CoregexArbitrary.of("[a-zA-Z]{3}"))
.suchThat(str -> 3 == str.length())
.check();Include the following dependency into your project:
libraryDependencies ++=Seq("com.github.simy4.coregex"%%"coregex-zio-test"%Test)Use the provided CoregexGen class to generate a string that would match the regular expression predicate:
objectMySpecextendsZIOSpecDefault {
defspec= suite("my spec")(
test("my property") {
check(CoregexGen.from(Pattern.compile("[a-zA-Z]{3}"))) { str => assertTrue(str.length ==3)
}
}
)
}