Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Minimal example of using `purescript-quickcheck-laws` to verify a type class instance on a custom type
module Pair where
import Effect
import Prelude
import Data.Eq (class Eq1)
import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary)
import Test.QuickCheck.Laws (checkLaws)
import Test.QuickCheck.Laws.Data.Functor (checkFunctor)
data Pair a = Pair a a
derive instance eq1Pair :: Eq1 Pair
derive instance eqPair :: Eq a => Eq (Pair a)
instance arbPair :: Arbitrary a => Arbitrary (Pair a) where
arbitrary = Pair <$> arbitrary <*> arbitrary
instance functorPair :: Functor Pair where
map f (Pair x y) = Pair (f x) (f y)
prxPair :: Proxy2 Pair
prxPair = Proxy2
main :: Effect Unit
main = do checkLaws "Pair" (checkFunctor prxPair)
{-
Or, in the repl (if you don't want to define `main`):
> import Prelude
> import Test.QuickCheck.Laws
> import Test.QuickCheck.Laws.Data.Functor
> import Pair
> checkLaws "Pair" (checkFunctor prxPair)
Checking laws of Pair instances...
Checking 'Identity' law for Functor
1000/1000 test(s) passed.
Checking 'Composition' law for Functor
1000/1000 test(s) passed.
unit
> -- Also works (without checkLaws):
> checkFunctor prxPair
Checking 'Identity' law for Functor
1000/1000 test(s) passed.
Checking 'Composition' law for Functor
1000/1000 test(s) passed.
unit
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.