Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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 @@ -9,34 +9,26 @@ import zio.test.magnolia.DeriveGen
object char extends CharInstances

trait CharInstances {
private val alphaCharGen: Gen[Random, Char] =
Gen.weighted(Gen.char(65, 90) -> 26, Gen.char(97, 122) -> 26)

private val numericCharGen: Gen[Random, Char] =
Gen.weighted(Gen.char(48, 57) -> 10)

private val whitespaceChars: Seq[Char] =
(Char.MinValue to Char.MaxValue).filter(_.isWhitespace)

implicit def digitArbitrary: DeriveGen[Refined[Char, Digit]] =
DeriveGen.instance(numericCharGen.map(value => Refined.unsafeApply(value)))
DeriveGen.instance(Gen.numericChar.map(value => Refined.unsafeApply(value)))

implicit def letterDeriveGen: DeriveGen[Refined[Char, Letter]] =
DeriveGen.instance(alphaCharGen.map(value => Refined.unsafeApply(value)))
DeriveGen.instance(Gen.alphaChar.map(value => Refined.unsafeApply(value)))

implicit def lowerCaseDeriveGen: DeriveGen[Refined[Char, LowerCase]] =
DeriveGen.instance(
alphaCharGen.map(value => Refined.unsafeApply(value.toLower))
Gen.alphaChar.map(value => Refined.unsafeApply(value.toLower))
)

implicit def upperCaseDeriveGen: DeriveGen[Refined[Char, UpperCase]] =
DeriveGen.instance(
alphaCharGen.map(value => Refined.unsafeApply(value.toUpper))
Gen.alphaChar.map(value => Refined.unsafeApply(value.toUpper))
)

implicit def whitespaceDeriveGen: DeriveGen[Refined[Char, Whitespace]] = {
val whiteSpaceGens: Seq[Gen[Random, Char]] =
whitespaceChars.map(Gen.const(_))
Gen.whitespaceChars.map(Gen.const(_))

DeriveGen.instance(
Gen
Expand Down
18 changes: 18 additions & 0 deletions test/shared/src/main/scala/zio/test/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ final case class Gen[-R, +A](sample: ZStream[R, Nothing, Sample[R, A]]) { self =

object Gen extends GenZIO with FunctionVariants with TimeVariants {

/**
* A generator of alpha characters.
*/
val alphaChar: Gen[Random, Char] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we alphabetize please? Otherwise looks great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, didn't get what you mean.
Alphabetize random char generator?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry. Just put the constructors in alphabetical order with all the existing constructors. So like right now whiteSpaceChars is near the top of the file above the existing alphaNumericString generator but it should be towards the end of the file after the weighted constructor. It doesn't actually change the logic of your implementation but just makes it easier for users and contributors to find things in the file, especially when it is a large one like this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think now they are alphabetized:)

weighted(char(65, 90) -> 26, char(97, 122) -> 26)

/**
* A generator of alphanumeric characters. Shrinks toward '0'.
*/
Expand Down Expand Up @@ -594,6 +600,12 @@ object Gen extends GenZIO with FunctionVariants with TimeVariants {
val none: Gen[Any, Option[Nothing]] =
Gen.const(None)

/**
* A generator of numeric characters. Shrinks toward '0'.
*/
val numericChar: Gen[Random, Char] =
weighted(char(48, 57) -> 10)

/**
* A generator of optional values. Shrinks toward `None`.
*/
Expand Down Expand Up @@ -781,6 +793,12 @@ object Gen extends GenZIO with FunctionVariants with TimeVariants {
uniform.flatMap(n => map.rangeImpl(Some(n), None).head._2)
}

/**
* A generator of whitespace characters.
*/
val whitespaceChars: Seq[Char] =
(Char.MinValue to Char.MaxValue).filter(_.isWhitespace)

/**
* Zips the specified generators together pairwise. The new generator will
* generate elements as long as any generator is generating elements, running
Expand Down