You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
849 B
27 lines
849 B
package green.thisfieldwas.embracingnondeterminism.stdlib
|
|
|
|
import org.scalatest.matchers.must.Matchers
|
|
import org.scalatest.propspec.AnyPropSpec
|
|
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
|
|
import org.scalacheck.Arbitrary.arbitrary
|
|
|
|
/** Proves that Scala's Option is a functor by definition.
|
|
*/
|
|
class OptionSpec extends AnyPropSpec with ScalaCheckPropertyChecks with Matchers {
|
|
|
|
property("Option.map() preserves identity functions") {
|
|
forAll(arbitrary[Option[Int]]) { fa =>
|
|
fa.map(identity) mustBe identity(fa)
|
|
}
|
|
}
|
|
|
|
property("Option.map() preserves function composition") {
|
|
forAll(for {
|
|
fa <- arbitrary[Option[Double]]
|
|
f <- arbitrary[Double => String]
|
|
g <- arbitrary[String => Int]
|
|
} yield (fa, f, g)) { case (fa, f, g) =>
|
|
fa.map(g compose f) mustBe fa.map(f).map(g)
|
|
}
|
|
}
|
|
}
|
|
|