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(
// run = hedgehog.core.GenT$$Lambda$11272/0x00007f0539e262d8@3274b66d
// )
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: 134468760146
// 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(
// run = hedgehog.core.GenT$$Lambda$11272/0x00007f0539e262d8@6d0a54f0
// )
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: 134489148963
// c: G
// c: x
// c: v
// c: n
// c: H
// + 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(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@74f5d4ee
// )

val boundedTokenGen: Gen[String] =
Gens.genUnsafeNonWhitespaceStringMinMax(4, 12)
// boundedTokenGen: GenT[String] = GenT(
// run = hedgehog.core.GenT$$Lambda$11268/0x00007f0539e251c8@143b6c4e
// )
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: 134493255486
// 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)