scala - ScalaCheck:生成任意类型的任意函数
问题描述
我已经实现了以下功能:
/**
* Returns a function h , which is the composition of the functions f and g.
*/
def compose[A, B, C](g: B => C, f: A => B): A => C = f.andThen(g)
我正在尝试使用ScalaCheck对其进行测试。我可以生成以下测试,这些测试可以编译并通过:
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}
class ComposeSpec extends FlatSpec with Matchers with PropertyChecks {
"Compose" should "return a function h , which is the composition of the
functions f and g" in {
forAll { (a: Int, g: Int => Int, f: Int => Int) =>
compose(g, f)(a) should be(g(f(a)))
}
forAll { (a: String, g: Double => Int, f: String => Double) =>
compose(g, f)(a) should be(g(f(a)))
}
}
}
但是,如您所见,我正在生成具有已定义类型的任意函数,并且还将参数a
的类型与函数的输入类型相匹配f
。我想做的是这样的:
forAll { (a: A, g: B => C, f: A => B) =>
compose(g, f)(a) should be(g(f(a)))
}
但我不知道它的语法,也不知道它是否可能。你可以帮帮我吗?
解决方案
scalatest网站有这样的说法forAll
:
需要提供一个隐式
Arbitrary
生成器和对象,该方法会将每一行数据传递给每个参数类型。ScalaCheck在其 伴生对象中为常见类型(如、、等)提供了许多隐式生成器。只要您使用 ScalaCheck 已经为其提供隐式生成器的类型,您就不必担心它们。对象也一样,由 ScalaCheck 的伴生对象提供。大多数情况下,您可以简单地将属性函数传递给,编译器将获取 ScalaCheck 提供的隐式值。Shrink
forAll
Arbitrary
Int
String
List[Float]
org.scalacheck.Arbitrary
Arbitrary
Shrink
org.scalacheck.Shrink
forAll
所以不幸的是,你不能用它forAll
来检查每一种可能的类型,因为每一种可能的类型都没有隐式Arbitrary
和Shrink
对象。生成任何类型的任意对象似乎几乎是不可能的。
你能做的最好的事情是:
def checkComposeForAll[A : Arbitrary : Shrink, B : Arbitrary : Shrink, C : Arbitrary : Shrink]() = {
forAll { (a: A, g: B => C, f: A => B) =>
compose(g, f)(a) should be(g(f(a)))
}
}
checkComposeForAll[Int, Int, Int]()
checkComposeForAll[String, String, String]()
checkComposeForAll[List[Int], Double, Int]()
// ... etc, check a bunch of types ...
推荐阅读
- c# - 连接到 SharePoint 时出现 Powers Shell 问题
- django - 密码未使用 rest api 在自定义用户模型上进行散列
- java - 如何知道springboot休息服务需要多少休息时间?
- c# - CS 1001(需要标识符) - 控制器从 ASP.NET 视图接收列表模型数据
- linux - 从 Linux 中的特定列中获取值
- azure-devops - 从没有继承的 Azure DevOps Git 存储库权限中删除组和用户
- java - 与 JMeter 相比,Blazemeter 结果是否正确?
- microsoft-graph-api - 如何获取 MS Teams 私人频道的 SharePoint 网站
- javascript - 如果我将输入类型数字的值存储在另一个变量中,它最后会丢失一个数字
- angular - DropDown 与边距重叠