diff --git a/core/src/integration/kotlin/io/github/serpro69/kfaker/provider/StringIT.kt b/core/src/integration/kotlin/io/github/serpro69/kfaker/provider/StringIT.kt index 45d716389..06de55a83 100644 --- a/core/src/integration/kotlin/io/github/serpro69/kfaker/provider/StringIT.kt +++ b/core/src/integration/kotlin/io/github/serpro69/kfaker/provider/StringIT.kt @@ -74,9 +74,12 @@ class StringIT : DescribeSpec() { } } context("regexify") { - it("should resolve the regex to a string") { + it("should resolve the regex template to a string") { faker.string.regexify("""\d{6}""").all { it.isDigit() } shouldBe true } + it("should resolve the regex to a string") { + faker.string.regexify(Regex("""\d{6}""")).all { it.isDigit() } shouldBe true + } it("can have duplicates") { val list = List(1000) { faker.string.regexify("""\d\w""") } list.distinct().size shouldBeLessThan 1000 diff --git a/core/src/main/kotlin/io/github/serpro69/kfaker/provider/misc/StringProvider.kt b/core/src/main/kotlin/io/github/serpro69/kfaker/provider/misc/StringProvider.kt index 2f6551b93..0bd8cb76c 100644 --- a/core/src/main/kotlin/io/github/serpro69/kfaker/provider/misc/StringProvider.kt +++ b/core/src/main/kotlin/io/github/serpro69/kfaker/provider/misc/StringProvider.kt @@ -49,4 +49,12 @@ class StringProvider internal constructor( fun regexify(template: String) = with(fakerService) { resolveUniqueValue(template.generexify, "regexify") } + + /** + * Returns a string of generated values based on the [regex], + * for example `regexify(Regex("""\d{3}"""))` will return a string consisting of 3 random digits. + */ + fun regexify(regex: Regex) = with(fakerService) { + resolveUniqueValue(regex.toString().generexify, "regexify") + } }