Skip to main content

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(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@5119987c
// )

val genNonWhitespaceStringMinMax: Gen[NonEmptyString] =
StringGens.genNonWhitespaceStringMinMax(PosInt(4), PosInt(12))
// genNonWhitespaceStringMinMax: GenT[NonEmptyString] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@25e90908
// )

val genNonBlankString: Gen[NonBlankString] =
StringGens.genNonBlankString(PosInt(24))
// genNonBlankString: GenT[NonBlankString] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@15ab65e9
// )

val genNonBlankStringMinMax: Gen[NonBlankString] =
StringGens.genNonBlankStringMinMax(PosInt(3), PosInt(12))
// genNonBlankStringMinMax: GenT[NonBlankString] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@74537558
// )
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: 144147937962
// s: 
// s: 펼篵�㉡
// s: 赎韸ꀫ㵵
// s: ?뾭鱿쮛㯐꛲ᓩ䑫ྦ鿿䔠퉽
// s: 爎ꅀ診峉ഺ뚔埗㽱됵삅四榶갉樞?뎎瘯쏓ᨀ篳
// + repl.MdocSession$MdocApp0$StringGensExampleSpec$.Example for genNonWhitespaceString: OK, passed 5 tests
// s: ⑺䏳꨾
// s: ㉡苧볢စ?
// s: Mэ詻?
// s: 䑫ྦ鿿䔠
// s: 뽌࣏肙ᗭ쁆✪禢⺃ଁꞎ歚輢
// + repl.MdocSession$MdocApp0$StringGensExampleSpec$.Example for genNonWhitespaceStringMinMax: OK, passed 5 tests
// s: 
// s: 펼篵�㉡
// s: 赎韸ꀫ㵵
// s: ?뾭鱿쮛㯐꛲ᓩ䑫ྦ鿿䔠퉽
// s: 爎ꅀ診峉ഺ뚔埗㽱됵삅四榶갉樞?뎎瘯쏓ᨀ篳
// + repl.MdocSession$MdocApp0$StringGensExampleSpec$.Example for genNonBlankString: OK, passed 5 tests
// s: ⑺䏳
// s: 僡썽赎
// s: 볢စ
// s: ɧMэ詻?먦
// s: 뵝?袛
// + 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(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@6994d9d6
// )

val genNonEmptyStringUnicode: Gen[NonEmptyString] =
StringGens.genNonEmptyStringMinMax(Gen.unicodeAll, PosInt(2), PosInt(10))
// genNonEmptyStringUnicode: GenT[NonEmptyString] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@4d83ca5
// )
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: 144174753790
// s: 865K
// s: k2
// s: Y
// s: 128QJ9y
// s: s060o57586
// + repl.MdocSession$MdocApp2$StringGensCustomCharExampleSpec$.Example for genNonEmptyStringAlphaNum: OK, passed 5 tests
// s: 簹煐࿎
// s: 귓褱Ⲁ峂
// s: ᳪꍔ錀
// s: 鉶ự᝸
// s: 믈鮶?꒯ᕅ后
// + repl.MdocSession$MdocApp2$StringGensCustomCharExampleSpec$.Example for genNonEmptyStringUnicode: OK, passed 5 tests

UUID generator

val genUuid: Gen[Uuid] =
StringGens.genUuid
// genUuid: GenT[Uuid] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@4a9285fa
// )
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: 144180025990
// uuid: f2081382-e3fc-42b6-98b6-0038afbdd495
// uuid: e0624d3f-796e-418e-a528-7a1716a5bd89
// uuid: 26097340-f65b-46bb-8c25-e49a97984a61
// uuid: a051e3ba-eb39-404c-b51f-f18697402922
// uuid: cd1c1525-2268-4901-a971-bcc94daa0fae
// + repl.MdocSession$MdocApp4$StringGensUuidExampleSpec$.Example for genUuid: OK, passed 5 tests