Skip to main content

Gens

Gens provides foundational character and string generators.

Import​

import hedgehog.*
import hedgehog.extra.Gens

Non-whitespace character generator​

val nonWhitespaceCharGen: Gen[Char] =
Gens.genNonWhitespaceChar
// nonWhitespaceCharGen: GenT[Char] = GenT(
// hedgehog.core.GenT$$Lambda$11547/0x00007fa1b9e8b7f0@680013d9
// )
import hedgehog.*
import hedgehog.runner.*

object NonWhitespaceExampleSpec extends Properties {
override def tests: List[Test] = List(
property(
"Example for genNonWhitespaceChar",
testExampleForGenNonWhitespaceChar
).withTests(5) // only for documentation to show fewer logs
)

def testExampleForGenNonWhitespaceChar: Property =
for {
c <- nonWhitespaceCharGen.log("c")
} yield {
println(s"c: $c") // only for documentation to show the value
c.isWhitespace ==== false
}
}

// This is only for doc. Your test code should be run by sbt (or maybe another build tool)
NonWhitespaceExampleSpec.main(Array.empty)
// Using random seed: 79422079559
// c: 
// c: 碩
// c: ?
// c: 龱
// c: 蘀
// + repl.MdocSession$MdocApp$NonWhitespaceExampleSpec$.Example for genNonWhitespaceChar: OK, passed 5 tests

Character generator from custom ranges​

val alphaNumericLikeCharGen: Gen[Char] =
Gens.genCharByRange(
List(
48 -> 57,
65 -> 90,
97 -> 122
)
)
// alphaNumericLikeCharGen: GenT[Char] = GenT(
// hedgehog.core.GenT$$Lambda$11547/0x00007fa1b9e8b7f0@496839e3
// )
import hedgehog.*
import hedgehog.runner.*

object AlphaNumericLikeCharGenSpec extends Properties {
override def tests: List[Test] = List(
property(
"Example for alphaNumericLikeCharGen",
testExampleForAlphaNumericLikeCharGen
).withTests(5) // only for documentation to show fewer logs
)

def testExampleForAlphaNumericLikeCharGen: Property =
for {
c <- alphaNumericLikeCharGen.log("c")
} yield {
println(s"c: $c") // only for documentation to show the value
Result.assert(c.isLetterOrDigit).log("It should be alphanumeric char")
}
}

// This is only for doc. Your test code should be run by sbt (or maybe another build tool)
AlphaNumericLikeCharGenSpec.main(Array.empty)
// Using random seed: 79458510119
// c: 9
// c: Q
// c: i
// c: S
// c: y
// + repl.MdocSession$MdocApp$AlphaNumericLikeCharGenSpec$.Example for alphaNumericLikeCharGen: OK, passed 5 tests

Non-whitespace string generators​

val tokenGen: Gen[String] =
Gens.genUnsafeNonWhitespaceString(16)
// tokenGen: GenT[String] = GenT(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@5ab3d87c
// )

val boundedTokenGen: Gen[String] =
Gens.genUnsafeNonWhitespaceStringMinMax(4, 12)
// boundedTokenGen: GenT[String] = GenT(
// hedgehog.core.GenT$$Lambda$11543/0x00007fa1b9e8a6e0@42572abd
// )
import hedgehog.*
import hedgehog.runner.*

object NonWhitespaceStringExampleSpec extends Properties {
override def tests: List[Test] = List(
property(
"Example for tokenGen",
testExampleForTokenGen
).withTests(5), // only for documentation to show fewer logs
property(
"Example for boundedTokenGen",
testExampleForBoundedTokenGen
).withTests(5) // only for documentation to show fewer logs
)

def testExampleForTokenGen: Property =
for {
s <- tokenGen.log("s")
} yield {
println(s"s: $s") // only for documentation to show the value
Result.all(
s.map(_.isWhitespace ==== false).toList
)
}

def testExampleForBoundedTokenGen: Property =
for {
s2 <- boundedTokenGen.log("s2")
} yield {
println(s"s2: $s2") // only for documentation to show the value
Result.all(
s2.map(_.isWhitespace ==== false).toList
)
}

}

// This is only for doc. Your test code should be run by sbt (or maybe another build tool)
NonWhitespaceStringExampleSpec.main(Array.empty)
// Using random seed: 79465303979
// s: ኦ
// s: ♾ᜐ燋촼
// s: 늋ᙉ㐬
// s: 鳀ᘯ뼆▍ᢁ㍳떊
// s: 扼
// + repl.MdocSession$MdocApp$NonWhitespaceStringExampleSpec$.Example for tokenGen: OK, passed 5 tests
// s2: ኦ鈶?픥秂
// s2: ?✖絞⇽鳀
// s2: 蛴ᢋ竫ᥱ꾘
// s2: 멠䬩览
// s2: ꛛ廇嚖ᙇ峣嵇㶫
// + repl.MdocSession$MdocApp$NonWhitespaceStringExampleSpec$.Example for boundedTokenGen: OK, passed 5 tests

Constraints​

These are intentionally unsafe APIs and throw IllegalArgumentException for invalid bounds.

Gens.genUnsafeNonWhitespaceString(0)
// java.lang.IllegalArgumentException: maxLength for genUnsafeNonWhitespaceString should be a positive Int (> 0). [maxLength: 0]
// at hedgehog.extra.Gens.genUnsafeNonWhitespaceString(Gens.scala:30)
// at hedgehog.extra.Gens.genUnsafeNonWhitespaceString$(Gens.scala:8)
// at hedgehog.extra.Gens$.genUnsafeNonWhitespaceString(Gens.scala:53)
// at repl.MdocSession$MdocApp.$init$$$anonfun$1(gens.md:158)
Gens.genUnsafeNonWhitespaceStringMinMax(0, 10)
// java.lang.IllegalArgumentException: minLength for genUnsafeNonWhitespaceStringMinMax should be a positive Int (> 0). [minLength: 0]
// at hedgehog.extra.Gens.genUnsafeNonWhitespaceStringMinMax(Gens.scala:49)
// at hedgehog.extra.Gens.genUnsafeNonWhitespaceStringMinMax$(Gens.scala:8)
// at hedgehog.extra.Gens$.genUnsafeNonWhitespaceStringMinMax(Gens.scala:53)
// at repl.MdocSession$MdocApp.$init$$$anonfun$2(gens.md:167)
Gens.genUnsafeNonWhitespaceStringMinMax(10, 5)
// java.lang.IllegalArgumentException: maxLength for genUnsafeNonWhitespaceStringMinMax is less than minLength. maxLength for genUnsafeNonWhitespaceStringMinMax should be greater than or equal to minLength (minLength <= maxLength). [minLength: 10, maxLength: 5]
// at hedgehog.extra.Gens.genUnsafeNonWhitespaceStringMinMax(Gens.scala:45)
// at hedgehog.extra.Gens.genUnsafeNonWhitespaceStringMinMax$(Gens.scala:8)
// at hedgehog.extra.Gens$.genUnsafeNonWhitespaceStringMinMax(Gens.scala:53)
// at repl.MdocSession$MdocApp.$init$$$anonfun$3(gens.md:176)