refined4s StringGens
Import
import hedgehog.*
import hedgehog.extra.refined4s.gens.StringGens
import refined4s.types.all.*
Non-empty and non-blank string generators
val genNonWhitespaceString: Gen[NonEmptyString] =
StringGens.genNonWhitespaceString(PosInt(24))
// genNonWhitespaceString: GenT[NonEmptyString] = GenT(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@1dfbb252
// )
val genNonWhitespaceStringMinMax: Gen[NonEmptyString] =
StringGens.genNonWhitespaceStringMinMax(PosInt(4), PosInt(12))
// genNonWhitespaceStringMinMax: GenT[NonEmptyString] = GenT(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@145971d3
// )
val genNonBlankString: Gen[NonBlankString] =
StringGens.genNonBlankString(PosInt(24))
// genNonBlankString: GenT[NonBlankString] = GenT(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@51d286ec
// )
val genNonBlankStringMinMax: Gen[NonBlankString] =
StringGens.genNonBlankStringMinMax(PosInt(3), PosInt(12))
// genNonBlankStringMinMax: GenT[NonBlankString] = GenT(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@7f0aaf2f
// )
import hedgehog.*
import hedgehog.runner.*
import hedgehog.extra.refined4s.gens.StringGens
import refined4s.types.all.*
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
property(
"Example for genNonBlankString",
testExampleForGenNonBlankString
).withTests(5), // only for documentation to show fewer logs
property(
"Example for genNonBlankStringMinMax",
testExampleForGenNonBlankStringMinMax
).withTests(5), // only for documentation to show fewer logs
)
def testExampleForGenNonWhitespaceString: Property =
for {
s <- StringGens.genNonWhitespaceString(PosInt(24)).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(4), PosInt(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 >= 4", s.value.length, 4)(_ >= _),
Result.diffNamed(s"The length of [$s] must be <= 12", s.value.length, 12)(_ <= _),
))
}
def testExampleForGenNonBlankString: Property =
for {
s <- StringGens.genNonBlankString(PosInt(24)).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 testExampleForGenNonBlankStringMinMax: Property =
for {
s <- StringGens.genNonBlankStringMinMax(PosInt(3), PosInt(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: 89257351653
// s: ᑴꛌ찕ј
// s: 냩闖⡮鬒㜉
// s: 蕅찲膔靠潚ᡡ댎♲禎ᢿ긾
// s: ꔲ㲁杶ꤍ
// s: 矍靮ﴏꍟ䶃
// [32m+[0m repl.MdocSession$MdocApp0$StringGensExampleSpec$.Example for genNonWhitespaceString: OK, passed 5 tests
// s: ᑴꛌ찕ј
// s: 냩闖⡮鬒
// s: ꍋ蕅찲膔靠
// s: 縋⦜谚騤㤔偰
// s: 긾퐮⟻쇫
// [32m+[0m repl.MdocSession$MdocApp0$StringGensExampleSpec$.Example for genNonWhitespaceStringMinMax: OK, passed 5 tests
// s: ᑴꛌ찕ј
// s: 냩闖⡮鬒㜉
// s: 蕅찲膔靠潚ᡡ댎♲禎ᢿ긾
// s: ꔲ㲁杶ꤍ
// s: 矍靮ﴏꍟ䶃
// [32m+[0m repl.MdocSession$MdocApp0$StringGensExampleSpec$.Example for genNonBlankString: OK, passed 5 tests
// s: ᑴꛌ찕
// s: 凿냩闖
// s: 툗ഒ䱅
// s: ꍋ蕅찲膔靠
// s: ﰻ縋⦜谚騤㤔
// [32m+[0m repl.MdocSession$MdocApp0$StringGensExampleSpec$.Example for genNonBlankStringMinMax: OK, passed 5 tests
Custom character generator variants
val genNonEmptyStringAlphaNum: Gen[NonEmptyString] =
StringGens.genNonEmptyString(Gen.alphaNum, PosInt(16))
// genNonEmptyStringAlphaNum: GenT[NonEmptyString] = GenT(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@997ad03
// )
val genNonEmptyStringUnicode: Gen[NonEmptyString] =
StringGens.genNonEmptyStringMinMax(Gen.unicodeAll, PosInt(2), PosInt(10))
// genNonEmptyStringUnicode: GenT[NonEmptyString] = GenT(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@5924985d
// )
import hedgehog.*
import hedgehog.runner.*
import hedgehog.extra.refined4s.gens.StringGens
import refined4s.types.all.*
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(16)).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(2), PosInt(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: 89275565965
// s: 9
// s: R
// s: u23X9t5r
// s: zuhL
// s: 377t1s62l25Dj2f1
// [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
UUID generator
val genUuid: Gen[Uuid] =
StringGens.genUuid
// genUuid: GenT[Uuid] = GenT(
// hedgehog.core.GenT$$Lambda$11547/0x00007fa1b9e8b7f0@39ec794f
// )
import hedgehog.*
import hedgehog.runner.*
import hedgehog.extra.refined4s.gens.StringGens
object StringGensUuidExampleSpec extends Properties {
override def tests: List[Test] = List(
property(
"Example for genUuid",
testExampleForGenUuid
).withTests(5), // only for documentation to show fewer logs
)
def testExampleForGenUuid: Property =
for {
uuid <- StringGens.genUuid.log("uuid")
} yield {
println(s"uuid: $uuid") // only for documentation to show the value
val expected = java.util.UUID.fromString(uuid.value)
val actual = uuid.toUUID
actual ==== expected
}
}
// This is only for doc. Your test code should be run by sbt (or maybe another build tool)
StringGensUuidExampleSpec.main(Array.empty)
// Using random seed: 89281337057
// uuid: a4fea6f5-99ce-40b1-ad01-eec8e008878e
// uuid: dfc13fff-dbff-4de2-8f28-66914f3e3426
// uuid: e1c16249-66da-4a00-9fe5-a2c4be260d13
// uuid: e28564ce-2a86-4826-9d50-0ec0a38b649a
// uuid: 081b3351-c01a-442f-8d28-0a9bb81cc865
// [32m+[0m repl.MdocSession$MdocApp4$StringGensUuidExampleSpec$.Example for genUuid: OK, passed 5 tests