Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RandomService#randomString to replace nextString #102

Merged
merged 1 commit into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,17 @@ class Extras : DescribeSpec({

it("should generate random string of English chars") {
// START extras_random_everything_four
faker.random.randomAlphanumeric(
length = 10,
faker.random.randomString(
length = 42,
numericalChars = false
)
// END extras_random_everything_four
}

it("should generate random string in a given locale") {
// START extras_random_everything_five
faker.random.nextString(
length = 21,
faker.random.randomString(
length = 24,
locale = Locale.forLanguageTag("nb-NO"),
auxiliaryChars = true,
numericalChars = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Homepage : DescribeSpec({
// START random_service_one
faker.random.nextInt(intRange = 0..1000)
faker.random.nextLong(bound = 999L)
faker.random.nextString(length = 99)
faker.random.randomString(length = 99)
faker.random.nextEnum<TestEnum>()
faker.random.nextEnum(TestEnum::class.java) {
it != TestEnum.SOME // Exclude 'SOME' enum
Expand Down
50 changes: 40 additions & 10 deletions core/src/main/kotlin/io/github/serpro69/kfaker/RandomService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ class RandomService internal constructor(private val config: FakerConfig) {
fun nextInt(min: Int, max: Int) = random.nextInt(max - min + 1) + min

/**
* Returns the a pseudo-randomly selected value from the [list] of values.
* Returns a pseudo-randomly selected value from the [list] of values.
*/
fun <T> randomValue(list: List<T>) = list[nextInt(list.size)]

/**
* Returns the a pseudo-randomly selected value from the [array] of values.
* Returns a pseudo-randomly selected value from the [array] of values.
*/
fun <T> randomValue(array: Array<T>) = array[nextInt(array.size)]

Expand All @@ -80,10 +80,12 @@ class RandomService internal constructor(private val config: FakerConfig) {
* English alphabet letters and optional [numericalChars],
* or an empty string for a `length < 1`.
*
* @param length the length of the resulting string
* @param numericalChars add additional numerical chars from 0 to 9 to the resulting string
* @param length the length of the resulting string.
* Default: `24`
* @param numericalChars add additional numerical chars from 0 to 9 to the resulting string.
* Default: `true`
*/
fun randomAlphanumeric(length: Int = 10, numericalChars: Boolean = true): String {
fun randomString(length: Int = 24, numericalChars: Boolean = true): String {
if (length < 1) return ""
val charset = if (numericalChars) {
alphabeticLowerCharset + alphabeticUpperCharset + numericCharset
Expand Down Expand Up @@ -148,23 +150,51 @@ class RandomService internal constructor(private val config: FakerConfig) {
* consisting of pseudo-randomly generated characters
* in a given [locale] with optional [auxiliaryChars] and [numericalChars]
*
* This function is intended to be used when [randomAlphanumeric] is not sufficient,
* i.e. when a non-English [locale] or additional chars are needed in the resulting string.
*
* @param length the length of the resulting string
* @param locale locale to use to generate the charset. Defaults to `locale` config value set for the `faker` instance
* @param auxiliaryChars add additional auxiliary chars to the resulting string as defined in [Character_Elements](https://www.unicode.org/reports/tr35/tr35-general.html#Character_Elements)
* @param numericalChars add additional numerical chars from 0 to 9 to the resulting string
*/
@JvmOverloads
@Deprecated(
message = "This function is deprecated and will be removed in future releases.\n" +
"Note that default value for 'length' param has changed from '100' to '24' in the new 'randomString' function.",
replaceWith = ReplaceWith("randomString"),
level = DeprecationLevel.WARNING
)
fun nextString(
length: Int = 100,
locale: Locale = Locale.forLanguageTag(config.locale),
auxiliaryChars: Boolean = false,
numericalChars: Boolean = false
): String = randomString(length, locale, auxiliaryChars, numericalChars)

/**
* Returns [String] with the specified [length] (or an empty string for a `length < 1`)
* consisting of pseudo-randomly generated characters
* in a given [locale] with optional [auxiliaryChars] and [numericalChars]
*
* @param length the length of the resulting string.
* Default: `24`
* @param locale locale to use to generate the charset.
* Defaults to `locale` config value set for the `faker` instance.
* @param auxiliaryChars add additional auxiliary chars to the resulting string as defined in
* [Character_Elements](https://www.unicode.org/reports/tr35/tr35-general.html#Character_Elements).
* Default: `false`
* @param numericalChars add additional numerical chars from 0 to 9 to the resulting string
* Default: `false`
*/
@JvmOverloads
fun randomString(
length: Int = 24,
locale: Locale = Locale.forLanguageTag(config.locale),
auxiliaryChars: Boolean = false,
numericalChars: Boolean = false
): String {
if (length < 1) return "" // base case
if (locale in listOf(Locale.ENGLISH, Locale.US, Locale.UK, Locale.CANADA)) return randomAlphanumeric(length, numericalChars)
if (locale in listOf(Locale.ENGLISH, Locale.US, Locale.UK, Locale.CANADA)) return randomString(
length,
numericalChars
)

val localeData = LocaleData.getInstance(ULocale.forLocale(locale))
val mainChars = localeData.getExemplarSet(UnicodeSet.MIN_VALUE, LocaleData.ES_STANDARD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.FakerConfig
import io.github.serpro69.kfaker.RandomService
import java.util.*
import kotlin.Boolean
import kotlin.Char
import kotlin.Double
Expand Down Expand Up @@ -110,7 +109,7 @@ class RandomProvider internal constructor(fakerConfig: FakerConfig) {
Int::class -> randomService.nextInt()
Short::class -> randomService.nextInt().toShort()
Byte::class -> randomService.nextInt().toByte()
String::class -> randomService.nextString()
String::class -> randomService.randomString()
Char::class -> randomService.nextChar()
Boolean::class -> randomService.nextBoolean()
// TODO: 16.06.19 Arrays
Expand Down
24 changes: 12 additions & 12 deletions core/src/test/kotlin/io/github/serpro69/kfaker/RandomServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ internal class RandomServiceTest : DescribeSpec({
}
}

context("nextString() fun") {
it("default generated string is 100 char length") {
randomService.nextString().length shouldBe 100
context("randomString() fun") {
it("default generated string is 24 char length") {
randomService.randomString().length shouldBe 24
}
}

Expand Down Expand Up @@ -191,15 +191,15 @@ internal class RandomServiceTest : DescribeSpec({
context("randomAlphanumeric() fun") {
it("generates a string from the alphaNumeric source") {
val sourceGenerated = ('A'..'Z') + ('a'..'z') + ('0'..'9')
val listAlphanumeric = List(100) { randomService.randomAlphanumeric(100) }
val listAlphanumeric = List(100) { randomService.randomString(100) }
listAlphanumeric.forEach {
sourceGenerated shouldContainAll it.map { c -> c }
}
}

it("consecutive runs return different values") {
val one = randomService.randomAlphanumeric()
val two = randomService.randomAlphanumeric()
val one = randomService.randomString()
val two = randomService.randomString()
one shouldNotBe two
}

Expand All @@ -208,21 +208,21 @@ internal class RandomServiceTest : DescribeSpec({
val c2 = fakerConfig { random = Random(42) }
val r1 = RandomService(c1)
val r2 = RandomService(c2)
r1.randomAlphanumeric() shouldBe r2.randomAlphanumeric()
r1.randomString() shouldBe r2.randomString()
}

it("default generated string is 10 char length") {
val defaultLength = 10
randomService.randomAlphanumeric().length shouldBe defaultLength
it("default generated string is 24 char length") {
val defaultLength = 24
randomService.randomString().length shouldBe defaultLength
}

it("should return a specific length of the string") {
val expectedLength = 100
randomService.randomAlphanumeric(expectedLength).length shouldBe expectedLength
randomService.randomString(expectedLength).length shouldBe expectedLength
}

it("returns an empty string when length is 0") {
randomService.randomAlphanumeric(0) shouldBe ""
randomService.randomString(0) shouldBe ""
}
}

Expand Down