-
Notifications
You must be signed in to change notification settings - Fork 4
/
BasicScalaCheckPropertiesTest.scala
55 lines (45 loc) · 1.6 KB
/
BasicScalaCheckPropertiesTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package showcase.testing.scalacheck
import com.oldratlee.scala.hello.Person
import org.scalacheck.Prop.propBoolean
import org.scalacheck.{Gen, Prop, Properties}
object BasicScalaCheckPropertiesTest extends Properties("Hello") {
// simple properties
property("Sums is associative") = Prop.forAll {
(x: Int, y: Int) => x + y == y + x
}
// create Option[String] instance randomly
property("person name") = Prop.forAll {
(name: String, lastName: Option[String], age: Int) => {
val person = new Person(name, age, null)
println(s"name: $name, lastName: $lastName, age: $age")
name == person.name
}
}
// constraining properties
property("constraining person name") = Prop.forAll(Gen.alphaStr, Gen.oneOf(Gen.alphaStr.sample, None), Gen.chooseNum[Int](0, 99)) {
(name: String, lastName: Option[String], age: Int) => {
val person = new Person(name, age, null)
println(s"name: $name, lastName: $lastName, age: $age")
name == person.name
}
}
// constraining properties
property("age from 0 ~ 120") = Prop.forAll {
(name: String, age: Int) => {
// set constraint by ==>
(age >= 0) ==> {
val person = new Person(name, age, null)
println(s"age: $age")
name == person.name
}
}
}
// Grouping Properties
val stringOnly = Prop.forAll(Gen.alphaStr) {
x: String => (x != "") ==> { x.size >= 0 }
}
val positiveNumOnly = Prop.forAll(Gen.posNum[Int]) { _ > 0 }
val alwaysPass = Prop.forAll { _: Int => true }
property("And") = stringOnly && positiveNumOnly
property("Or") = stringOnly && alwaysPass
}