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

Avoid singleton random generators #312

Merged
merged 2 commits into from
May 9, 2019
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 @@ -30,10 +30,6 @@

package com.salesforce.op.testkit

import com.salesforce.op.features.types.FeatureType

import scala.util.Random

/**
* This trait allows the value to be empty
*/
Expand Down
22 changes: 11 additions & 11 deletions testkit/src/main/scala/com/salesforce/op/testkit/RandomText.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,35 +213,35 @@ object RandomText {
*
* @return a random comboboxes generator
*/
val randomComboBoxes: RandomText[ComboBox] = justText[ComboBox](1, 20)
def randomComboBoxes: RandomText[ComboBox] = justText[ComboBox](1, 20)

/**
* Produces random country names from a list that we keep in our resource file
*
* @return a random country names generator
*/
val countries: RandomText[Country] = selectRandom[Country](Countries)
def countries: RandomText[Country] = selectRandom[Country](Countries)

/**
* Produces random US state names from a list that we keep in our resource file
*
* @return a random state names generator
*/
val states: RandomText[State] = selectRandom[State](States)
def states: RandomText[State] = selectRandom[State](States)

/**
* Produces random California city names from a list that we keep in our resource file
*
* @return a random city names generator
*/
val cities: RandomText[City] = selectRandom(CitiesOfCalifornia)
def cities: RandomText[City] = selectRandom(CitiesOfCalifornia)

/**
* Produces random San Jose street names from a list that we keep in our resource file
*
* @return a random street names generator
*/
val streets: RandomText[Street] = selectRandom[Street](StreetsOfSanJose)
def streets: RandomText[Street] = selectRandom[Street](StreetsOfSanJose)

/**
* Produces a random Base64 strings generator
Expand All @@ -263,10 +263,10 @@ object RandomText {
RandomText[Base64]((rng: Random) => new String(b64.encode(randomBytes(rng))))
}

private val goodUsPhones = (rng: Random) =>
private def goodUsPhones = (rng: Random) =>
RandomAreaCode(rng) + (5550000 + rng.nextInt(10000))

private val badUsPhones = (rng: Random) => {
private def badUsPhones = (rng: Random) => {
val raw = 10000000000L + (rng.nextInt(199) * 10000000L + rng.nextInt(10000000)) toString

raw.substring(rng.nextInt(2), 6 + rng.nextInt(raw.length - 6))
Expand All @@ -275,7 +275,7 @@ object RandomText {
/**
* A generator of random US phone numbers
*/
val phones: RandomText[Phone] = RandomText[Phone](goodUsPhones)
def phones: RandomText[Phone] = RandomText[Phone](goodUsPhones)

/**
* A generator of random US phone numbers with some errors
Expand All @@ -299,7 +299,7 @@ object RandomText {
/**
* A generator of random IDs, length from 1 to 41 char. An id consists of alphanumerics and '_'
*/
val ids: RandomText[ID] = {
def ids: RandomText[ID] = {
val charsOfID = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
RandomText[ID](randomStringOfAlphabet(charsOfID, 1, 41))
}
Expand All @@ -308,7 +308,7 @@ object RandomText {
* A generator of random unique IDs, length at least 3, no upper limit (they are unique!).
* A unique id consists of alphanumerics and '_', followed by an '_' and a unique serial number
*/
val uniqueIds: RandomText[ID] = {
def uniqueIds: RandomText[ID] = {
val charsOfID = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
val randoms = new RandomStream(randomStringOfAlphabet(charsOfID, 1, 20))
val numbers = RandomStream.incrementing(1, 1, 1)
Expand All @@ -319,7 +319,7 @@ object RandomText {
/**
* A generator of random URLs
*/
val urls: RandomText[URL] = {
def urls: RandomText[URL] = {
val lowerCaseAlphaNum = "abcdefghijklmnopqrstuvwxyz0123456789"
val urlChars = lowerCaseAlphaNum + "-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,18 @@ class RandomTextTest extends FlatSpec with TestCommon with Assertions {
})
}

"ID generator" should "generate IDs" in {
"id generator" should "generate IDs" in {
val g = RandomText.ids withProbabilityOfEmpty 0.001
check(g,
(s: String) => s.length > 0 && s.length < 60 && (s matches "\\w+"))
}

"UniqueID generator" should "generate IDs" in {
"id generator" should "not produce empty IDs by default" in {
val g = RandomText.ids.limit(10000)
g.forall(id => !id.value.isEmpty && !id.value.contains("")) shouldBe true
}

"unique id generator" should "generate IDs" in {
val g = RandomText.uniqueIds
check(g,
(s: String) => s.length > 0 && s.length < 60 && (s matches "\\w+_[0-9a-f]+"),
Expand All @@ -213,6 +218,11 @@ class RandomTextTest extends FlatSpec with TestCommon with Assertions {
)
}

"unique id generator" should "not produce empty IDs by default" in {
val g = RandomText.uniqueIds.limit(10000)
g.forall(id => !id.value.isEmpty && !id.value.contains("")) shouldBe true
}

"emails generator" should "generate emails" in {
val g = RandomText.emails("whitehouse.gov") withProbabilityOfEmpty 0.1
check(g, sampleSize = 10,
Expand Down