Skip to content

Commit

Permalink
Allow StringProvider#regexify to take Regex as input
Browse files Browse the repository at this point in the history
Close #208
  • Loading branch information
serpro69 committed Feb 10, 2024
1 parent a5510ac commit d03195d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit d03195d

Please sign in to comment.