From 4f5b89ee031f8efa24ca30decad6aea73570aee0 Mon Sep 17 00:00:00 2001 From: Sergio Pro Date: Thu, 4 Nov 2021 15:17:51 +0100 Subject: [PATCH] Add RandomService#randomString to replace nextString --- .../io/github/serpro69/kfaker/docs/Extras.kt | 8 +-- .../github/serpro69/kfaker/docs/Homepage.kt | 2 +- .../github/serpro69/kfaker/RandomService.kt | 50 +++++++++++++++---- .../kfaker/provider/RandomProvider.kt | 3 +- .../serpro69/kfaker/RandomServiceTest.kt | 24 ++++----- 5 files changed, 58 insertions(+), 29 deletions(-) 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 690b0a84e..719403f03 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 @@ -116,8 +116,8 @@ 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 @@ -125,8 +125,8 @@ class Extras : DescribeSpec({ 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 diff --git a/core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Homepage.kt b/core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Homepage.kt index 9288974d3..ec2e07b84 100644 --- a/core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Homepage.kt +++ b/core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Homepage.kt @@ -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() faker.random.nextEnum(TestEnum::class.java) { it != TestEnum.SOME // Exclude 'SOME' enum diff --git a/core/src/main/kotlin/io/github/serpro69/kfaker/RandomService.kt b/core/src/main/kotlin/io/github/serpro69/kfaker/RandomService.kt index 91ff397db..3ef01cf72 100644 --- a/core/src/main/kotlin/io/github/serpro69/kfaker/RandomService.kt +++ b/core/src/main/kotlin/io/github/serpro69/kfaker/RandomService.kt @@ -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 randomValue(list: List) = 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 randomValue(array: Array) = array[nextInt(array.size)] @@ -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 @@ -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) diff --git a/core/src/main/kotlin/io/github/serpro69/kfaker/provider/RandomProvider.kt b/core/src/main/kotlin/io/github/serpro69/kfaker/provider/RandomProvider.kt index 1e238989c..32fbdefa1 100644 --- a/core/src/main/kotlin/io/github/serpro69/kfaker/provider/RandomProvider.kt +++ b/core/src/main/kotlin/io/github/serpro69/kfaker/provider/RandomProvider.kt @@ -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 @@ -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 diff --git a/core/src/test/kotlin/io/github/serpro69/kfaker/RandomServiceTest.kt b/core/src/test/kotlin/io/github/serpro69/kfaker/RandomServiceTest.kt index 635f0f2b1..284634f60 100644 --- a/core/src/test/kotlin/io/github/serpro69/kfaker/RandomServiceTest.kt +++ b/core/src/test/kotlin/io/github/serpro69/kfaker/RandomServiceTest.kt @@ -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 } } @@ -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 } @@ -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 "" } }