我试着写一个快速的身份检查测试
f $ y = f y
我最初的计划是编写一个任意的生成器,它返回具有签名的函数&integer
Gen (Int -> Int, Int)
在
prop_DollerDoesNothing
使用/不使用
$
得到同样的结果。
这是我的密码:
prop_DollarDoesNothing :: Property
prop_DollarDoesNothing =
forAll arbitraryFuncInt (\(f, y) -> (f $ y) == (f y))
arbitraryFuncInt :: Gen (Int -> Int, Int)
arbitraryFuncInt = do
f <- elements [(\x -> x*2), (\x -> x+3), (\x -> x-2)]
y <- arbitrary :: Gen Int
return (f, y)
它生成了以下有用的错误消息:
* No instance for (Show (Int -> Int))
arising from a use of `forAll'
(maybe you haven't applied a function to enough arguments?)
* In the expression:
forAll arbitraryFuncInt (\ (f, y) -> (f $ y) == (f y))
In an equation for `prop_DollarDoesNothing':
prop_DollarDoesNothing
= forAll arbitraryFuncInt (\ (f, y) -> (f $ y) == (f y))
所以,我修正了错误,通过应用任意函数并返回
arbitraryFuncInt
prop_DollarDoesNothing :: Property
prop_DollarDoesNothing =
forAll arbitraryFuncInt (\(x, y) -> x == y)
arbitraryFuncInt :: Gen (Int, Int)
arbitraryFuncInt = do
f <- elements [(\x -> x*2), (\x -> x+3), (\x -> x-2)]
y <- arbitrary :: Gen Int
return (f $ y, f y)
我的问题是:
-
是否由于没有
Show
?
-
我能为
Show (Int -> Int)
使
# 1
可能吗?
-
可以在给定类型签名的情况下快速检查生成任意函数,在这种情况下,我将测试所有函数(给定类型)的真实身份。上面,我手动指定了3个测试函数,我希望以某种方式实现自动化,理想情况下是这样
f <- arbitrary :: Gen (Int -> Int)