diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 35bcb46bd..3b03e0ae1 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -20,6 +20,7 @@ [discrete] === Fixed +* https://github.com/serpro69/kotlin-faker/pull/205[#205] [core] Fix `Person.birthDate` range error during leap year * https://github.com/serpro69/kotlin-faker/issues/204[#204] [core] Fix RandomClassProvider handling "constructor-less" types in collections [discrete] diff --git a/core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Extras.kt b/core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Extras.kt index c9e95c564..a4d9c6109 100644 --- a/core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Extras.kt +++ b/core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Extras.kt @@ -4,6 +4,7 @@ import io.github.serpro69.kfaker.Faker import io.github.serpro69.kfaker.fakerConfig import io.github.serpro69.kfaker.provider.misc.ConstructorFilterStrategy import io.github.serpro69.kfaker.provider.misc.FallbackStrategy +import io.github.serpro69.kfaker.provider.misc.RandomProvider import io.kotest.core.spec.style.DescribeSpec import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Assertions.assertEquals @@ -25,7 +26,7 @@ class Extras : DescribeSpec({ context("configurable constructor arg type generation") { it("should generate pre-configured constructor params") { - fun randomString() = "X3a8s813dcb"; + fun randomString() = "X3a8s813dcb" // START extras_random_instance_two class Baz(val id: Int, val uuid: UUID, val relatedUuid: UUID, val user: String) @@ -351,6 +352,34 @@ class Extras : DescribeSpec({ // END extras_random_everything_nine } + it("should generate unique integers via local-unique-provider") { + // START extras_random_everything_ten + val ints = List(21) { + faker.random.unique.nextInt(42) + } + assert(ints.distinct().size == 21) + // cleanup of unique values via enum key for nextInt function + faker.random.unique.clear(RandomProvider.Key.NEXT_INT) + // END extras_random_everything_ten + } + + it("should generate unique integers via global-unique-provider") { + // START extras_random_everything_eleven + faker.unique.configuration { enable(faker::random) } + val uniqueInts = List(21) { + faker.random.nextInt(42) + } + assert(uniqueInts.distinct().size == 21) + // cleanup global unique values for Random provider + faker.unique.clear(faker::random) + // disable global unique values for Random provider + faker.unique.configuration { disable(faker::random) } + val ints = List(21) { + faker.random.nextInt(42) + } + assert(ints.distinct().size < 21) + // END extras_random_everything_eleven + } } describe("Random Strings from Templates") { diff --git a/docs/src/orchid/resources/wiki/extras.md b/docs/src/orchid/resources/wiki/extras.md index fdfd9b22f..0e64c318c 100644 --- a/docs/src/orchid/resources/wiki/extras.md +++ b/docs/src/orchid/resources/wiki/extras.md @@ -551,6 +551,28 @@ Faker provides its wrapper functions around `java.util.Random` (with some additi {% endtabs %} +### Unique Random Values + +Just like most data providers, `Faker#random` supports generation of unique values. See {{ anchor(title='Generator of Unique Values', collectionType='wiki', collectionId='', itemId='Generator of Unique Values') }} page for usage details. + +Both "local" (provider level) and "global" (faker level) generation of unique values are supported for `RandomProvider`: + +{% tabs %} + +{% kotlin "Kotlin" %} +{% filter compileAs('md') %} +```kotlin +{% snippet 'extras_random_everything_ten' %} +``` + +```kotlin +{% snippet 'extras_random_everything_eleven' %} +``` +{% endfilter %} +{% endkotlin %} + +{% endtabs %} + {% btc %}{% endbtc %}