저는 hspec 및 QuickCheck를 사용하여 Functor 인스턴스에 대한 펑터 법칙의 유효성을 검사합니다. 나는이 기능표현식에 대한 인수로 형식 생성자
functorIdentity :: (Functor f, Eq (f a)) => f a -> Bool
및
functorComposition :: (Functor f, Eq (f c)) => (Fun a b) -> (Fun b c) -> f a -> Bool
나는 다음과 같은 코드의 두 사용하여 블록을 테스트하고 있습니다 :
testListFunctorness :: IO()
testListFunctorness =
hspec $ do
describe "list" $ do
it "should obey functor identity" $ do
property (functorIdentity :: [Int] -> Bool)
it "should obey functor composition" $ do
property
(functorComposition :: (Fun Int String) -> (Fun String Int) -> [Int] -> Bool)
, A의 동일한 속성을 테스트하는 것은 다른 Functor 인스턴스, [Int]
을 제외한 모든 것을 복사해야합니다.
testMaybeFunctorness :: IO()
testMaybeFunctorness =
hspec $ do
describe "maybe" $ do
it "should obey functor identity" $ do
property (functorIdentity :: Maybe Int -> Bool)
it "should obey functor composition" $ do
property
(functorComposition :: (Fun Int String) -> (Fun String Int) -> Maybe Int -> Bool)
다른 Functor
인스턴스를 통해 어떤 식 으로든 다형성이있는 식을 작성할 수 있어야하지만, 시작하는 방법을 생각조차 할 수없는 것처럼 느껴집니다.
Functor
여러 개의 테스트 로직 블록을 편리하게 재사용 할 수 있습니까?
저는 여기 AB 문제가 있다고 확신하지만 B가 무엇인지 알 수 없습니다. – N3dst4