Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Jun 29, 2024
1 parent 1def197 commit 25e73a8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Extras : DescribeSpec({
// generate random string elements for parameters of List<String> type
randomListString()
} else {
// generate random string elements for parameters of other Collection types (Set, Map)
// generate random string elements for parameters of Set type
randomString()
}
}
Expand All @@ -72,6 +72,21 @@ class Extras : DescribeSpec({
assertEquals(baz.set.all { it == "string" }, true)
}

it("should generate pre-configured map key/value pairs for constructor params") {
fun randomKey() = "key"
fun randomValue() = "value"
// START extras_random_instance_seventeen
class Baz(val map: Map<String, String>)

val baz: Baz = faker.randomClass.randomClassInstance {
mapEntryKeyTypeGenerator { randomKey() }
mapEntryValueTypeGenerator { randomValue() }
}
// END extras_random_instance_seventeen

assertEquals(baz.map.keys.all { it == "key" }, true)
assertEquals(baz.map.values.all { it == "value" }, true)
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion docs/src/orchid/resources/wiki/extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ A random class instance will be generated using the following precedence rules:

#### Predefined collection element types

It may be desirable to define how elements in a collection parameter are generated, for this `collectionElementTypeGenerator` function can be used:
It may be desirable to define how elements of a `Collection` (currently supports `List`s and `Set`s) constructor parameter type are generated, for this `collectionElementTypeGenerator` function can be used:

{% tabs %}

Expand Down Expand Up @@ -285,6 +285,20 @@ Explore other properties of `it`, which exposes more details about the [paramete
{% endfilter %}
{% endinfo %}

There are also two similar methods for map entries: `mapEntryKeyTypeGenerator` and `mapEntryValueTypeGenerator`, which can be used to configure how keys and values are generated in `Map` constructor parameter types:

{% tabs %}

{% kotlin "Kotlin" %}
{% filter compileAs('md') %}
```kotlin
{% snippet 'extras_random_instance_seventeen' %}
```
{% endfilter %}
{% endkotlin %}

{% endtabs %}

<br>

### Deterministic constructor selection
Expand Down

0 comments on commit 25e73a8

Please sign in to comment.