Skip to content

Commit

Permalink
Document usage of new StringProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Apr 30, 2022
1 parent f1c82f3 commit e79b8f2
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import io.github.serpro69.kfaker.Faker
import io.github.serpro69.kfaker.provider.ConstructorFilterStrategy
import io.github.serpro69.kfaker.provider.FallbackStrategy
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import java.util.*
Expand Down Expand Up @@ -235,6 +237,20 @@ class Extras : DescribeSpec({
}

}

describe("Random Strings from Templates") {
it("should replace template chars with actual ones") {
// START extras_random_strings_from_templates_zero
faker.string.numerify("123###").all { it.isDigit() } shouldBe true
faker.string.letterify("foo???").all { it.isLetter() } shouldBe true
faker.string.letterify("???BAR", true).all { it.isUpperCase() } shouldBe true
faker.string.letterify("???bar", false).all { it.isLowerCase() } shouldBe true
faker.string.bothify("foo???bar###")
faker.string.regexify("""\d{42}""").all { it.isDigit() } shouldBe true
faker.string.regexify("""\d{42}""").length shouldBe 42
// END extras_random_strings_from_templates_zero
}
}
})

// START extras_random_everything_two
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,18 @@ class Homepage : DescribeSpec({
// START random_class_instance_one
class Foo(val a: String)
class Bar(val foo: Foo)

val foo: Foo = faker.randomProvider.randomClassInstance()
val bar: Bar = faker.randomProvider.randomClassInstance()
// END random_class_instance_one
}
it("should generate types by configuration") {
// START random_class_instance_two
class Baz(val id: Int, val uuid: UUID, val name: String)

val baz: Baz = faker.randomProvider.randomClassInstance {
typeGenerator<UUID> { UUID.fromString("00000000-0000-0000-0000-000000000000") }
typeGenerator<Int> { faker.random.nextInt(min = 0, max = 9) }
typeGenerator<Int> { faker.random.nextInt(min = 0, max = 9) }
typeGenerator<String> { faker.name.name() } // Random name generated by Faker
}
assertEquals(baz.uuid, UUID.fromString("00000000-0000-0000-0000-000000000000"))
Expand All @@ -156,6 +158,18 @@ class Homepage : DescribeSpec({
// END random_service_one
}
}

context("StringProvider") {
val faker = Faker()
it("should generate random stuff") {
// START string_provider_one
faker.string.numerify("foo###bar") // foo123bar
faker.string.letterify("foo???bar", true) // fooXYZbar
faker.string.bothify("foo?##bar", false) // foox42bar
faker.string.regexify("""\d{2}\w""") // 42a
// END string_provider_one
}
}
}
})

Expand Down
3 changes: 3 additions & 0 deletions docs/src/orchid/resources/data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ homepageSections:
- title: '…with pre-configured type generation'
snippets: ['random_class_instance_two']
lang: 'kotlin'
- title: 'String templates'
snippets: ['string_provider_one']
lang: 'kotlin'
- title: 'Random numbers, strings, enums, UUIDs, and more…'
snippets: ['random_service_one']
lang: 'kotlin'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
---

== `Faker().rajnikanth`
== `Faker().randomProvider`

Provides functionality for creating a random instance of any (almost any) class.

Expand Down
22 changes: 22 additions & 0 deletions docs/src/orchid/resources/pages/data-provider/string_provider.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
---

== `Faker().string`

Provides functionality for generating strings from templates.

.Available Functions
[%collapsible]
====
[source,kotlin]
----
Faker().string.numerify("foo###bar") // foo123bar
Faker().string.letterify("foo???bar", true) // fooXYZbar
Faker().string.bothify("foo?##bar", false) // foox42bar
Faker().string.regexify("""\d{2}\w""") // 42a
----
====
27 changes: 27 additions & 0 deletions docs/src/orchid/resources/wiki/extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [Deterministic constructor selection](#deterministic-constructor-selection)
* [Configuring the size of generated Collections](#configuring-the-size-of-generated-collections)
* [Random Everything](#random-everything)
* [Random Strings from Templates](#random-strings-from-templates)

<br>

Expand Down Expand Up @@ -344,3 +345,29 @@ Faker provides its wrapper functions around `java.util.Random` (with some additi
{% btc %}{% endbtc %}

<br>

## Random Strings from Templates

Faker's `StringProvider` allows for replacing certain user-defined parts of strings with randomly generated chars (letters and digits), as well as generating strings from regex expressions.
The following functions are available withing the `Faker().string`:

{% tabs %}

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

{% endtabs %}

{% btc %}{% endbtc %}

<br>





0 comments on commit e79b8f2

Please sign in to comment.