refined StringGens
Import
import hedgehog.*
import hedgehog.extra.refined.StringGens
import eu.timepit.refined.types.numeric.PosInt
import eu.timepit.refined.types.string.NonEmptyString
Non-whitespace string generators
val genNonWhitespaceString: Gen[NonEmptyString] =
StringGens.genNonWhitespaceString(PosInt.unsafeFrom(20))
// genNonWhitespaceString: GenT[NonEmptyString] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@607f6cf7
// )
val genNonWhitespaceStringMinMax: Gen[NonEmptyString] =
StringGens.genNonWhitespaceStringMinMax(PosInt.unsafeFrom(3), PosInt.unsafeFrom(12))
// genNonWhitespaceStringMinMax: GenT[NonEmptyString] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@66723c61
// )
import hedgehog.*
import hedgehog.runner.*
import hedgehog.extra.refined.StringGens
import eu.timepit.refined.types.numeric.PosInt
import eu.timepit.refined.types.string.NonEmptyString
object StringGensExampleSpec extends Properties {
override def tests: List[Test] = List(
property(
"Example for genNonWhitespaceString",
testExampleForGenNonWhitespaceString
).withTests(5), // only for documentation to show fewer logs
property(
"Example for genNonWhitespaceStringMinMax",
testExampleForGenNonWhitespaceStringMinMax
).withTests(5), // only for documentation to show fewer logs
)
def testExampleForGenNonWhitespaceString: Property =
for {
s <- StringGens.genNonWhitespaceString(PosInt.unsafeFrom(20)).log("s")
} yield {
println(s"s: $s") // only for documentation to show the value
Result.all(List(
Result.assert(s.value.nonEmpty).log(s"The value [$s] must be non-empty"),
Result.assert(!s.value.exists(_.isWhitespace)).log(s"The value [$s] must not contain whitespace"),
))
}
def testExampleForGenNonWhitespaceStringMinMax: Property =
for {
s <- StringGens.genNonWhitespaceStringMinMax(PosInt.unsafeFrom(3), PosInt.unsafeFrom(12)).log("s")
} yield {
println(s"s: $s") // only for documentation to show the value
Result.all(List(
Result.assert(s.value.nonEmpty).log(s"The value [$s] must be non-empty"),
Result.assert(!s.value.exists(_.isWhitespace)).log(s"The value [$s] must not contain whitespace"),
Result.diffNamed(s"The length of [$s] must be >= 3", s.value.length, 3)(_ >= _),
Result.diffNamed(s"The length of [$s] must be <= 12", s.value.length, 12)(_ <= _),
))
}
}
// This is only for doc. Your test code should be run by sbt (or maybe another build tool)
StringGensExampleSpec.main(Array.empty)
// Using random seed: 140463781008
// s: 붟ঁ뒷韴
// s: 牛祌ﺯ錴溈㫡㠯
// s: 鉀ლ
// s: 㻡鈸疇뷇刄ᇚᣥ狞ą㜃妈
// s: Ὁ츑职먐ﱅ맠㯨똹탘འ즛熿▄
// [32m+[0m repl.MdocSession$MdocApp0$StringGensExampleSpec$.Example for genNonWhitespaceString: OK, passed 5 tests
// s: 붟ঁ뒷
// s: 郠괧买迄퉾䙨
// s: 㫡㠯귬㫃֚
// s: 럝몦㡢펚┤⤘蠙
// s: ą㜃妈칯k语३㿵
// [32m+[0m repl.MdocSession$MdocApp0$StringGensExampleSpec$.Example for genNonWhitespaceStringMinMax: OK, passed 5 tests
Custom character generator variants
val genNonEmptyStringAlphaNum: Gen[NonEmptyString] =
StringGens.genNonEmptyString(Gen.alphaNum, PosInt.unsafeFrom(20))
// genNonEmptyStringAlphaNum: GenT[NonEmptyString] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@31ad6394
// )
val genNonEmptyStringUnicode: Gen[NonEmptyString] =
StringGens.genNonEmptyStringMinMax(Gen.unicodeAll, PosInt.unsafeFrom(2), PosInt.unsafeFrom(10))
// genNonEmptyStringUnicode: GenT[NonEmptyString] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@5ddc6b1a
// )
import hedgehog.*
import hedgehog.runner.*
import hedgehog.extra.refined.StringGens
import eu.timepit.refined.types.numeric.PosInt
import eu.timepit.refined.types.string.NonEmptyString
object StringGensCustomCharExampleSpec extends Properties {
override def tests: List[Test] = List(
property(
"Example for genNonEmptyStringAlphaNum",
testExampleForGenNonEmptyStringAlphaNum
).withTests(5), // only for documentation to show fewer logs
property(
"Example for genNonEmptyStringUnicode",
testExampleForGenNonEmptyStringUnicode
).withTests(5), // only for documentation to show fewer logs
)
def testExampleForGenNonEmptyStringAlphaNum: Property =
for {
s <- StringGens.genNonEmptyString(Gen.alphaNum, PosInt.unsafeFrom(20)).log("s")
} yield {
println(s"s: $s") // only for documentation to show the value
Result.all(List(
Result.assert(s.value.nonEmpty).log(s"The value [$s] must be non-empty"),
Result
.assert(s.value.forall(c => c.isDigit || ('a' to 'z').contains(c) || ('A' to 'Z').contains(c)))
.log(s"The value [$s] must contain only alphabet or digit"),
))
}
def testExampleForGenNonEmptyStringUnicode: Property =
for {
s <- StringGens.genNonEmptyStringMinMax(Gen.unicodeAll, PosInt.unsafeFrom(2), PosInt.unsafeFrom(10)).log("s")
} yield {
println(s"s: $s") // only for documentation to show the value
Result.all(List(
Result.assert(s.value.nonEmpty).log(s"The value [$s] must be non-empty"),
Result.diffNamed(s"The length of [$s] must be >= 2", s.value.length, 2)(_ >= _),
Result.diffNamed(s"The length of [$s] must be <= 10", s.value.length, 10)(_ <= _),
))
}
}
// This is only for doc. Your test code should be run by sbt (or maybe another build tool)
StringGensCustomCharExampleSpec.main(Array.empty)
// Using random seed: 140474460284
// s: 8
// s: Ymz5
// s: ydZO8sb629
// s: 4
// s: 8t
// [32m+[0m repl.MdocSession$MdocApp2$StringGensCustomCharExampleSpec$.Example for genNonEmptyStringAlphaNum: OK, passed 5 tests
// s: ず᯽眿
// s: ﰿ
// s: ᇜ龔
// s: 昨樉Ԗ胄ꍭ
// s: 괷栦겪⏾ۅნ公盌誌
// [32m+[0m repl.MdocSession$MdocApp2$StringGensCustomCharExampleSpec$.Example for genNonEmptyStringUnicode: OK, passed 5 tests