Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Feb 11, 2024
1 parent 6a89556 commit a31586c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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") {
Expand Down
22 changes: 22 additions & 0 deletions docs/src/orchid/resources/wiki/extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

<br>
Expand Down

0 comments on commit a31586c

Please sign in to comment.