Skip to content

Commit

Permalink
Add function testLaws for testing type class laws.
Browse files Browse the repository at this point in the history
Add function `testLaws`, which can be used to test that a given instance
of a type class obeys the laws associated with that type class.
  • Loading branch information
jonathanknowles committed Dec 2, 2020
1 parent 0e99312 commit cc1c30b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/test-utils/cardano-wallet-test-utils.cabal
Expand Up @@ -44,6 +44,7 @@ library
, iohk-monitoring
, process
, QuickCheck
, quickcheck-classes
, stm
, template-haskell
, time
Expand All @@ -55,6 +56,7 @@ library
Test.Hspec.Extra
Test.QuickCheck.Extra
Test.Utils.FilePath
Test.Utils.Laws
Test.Utils.Paths
Test.Utils.Roundtrip
Test.Utils.StaticServer
Expand Down
52 changes: 52 additions & 0 deletions lib/test-utils/src/Test/Utils/Laws.hs
@@ -0,0 +1,52 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Utils.Laws
( testLaws
, testLawsMany
) where

import Prelude

import Control.Monad
( forM_ )
import Data.Proxy
( Proxy (..) )
import Data.Typeable
( Typeable, typeRep )
import Test.Hspec
( Spec, describe, it )
import Test.QuickCheck
( Arbitrary (..) )
import Test.QuickCheck.Classes
( Laws (..) )

-- | Constructs a test to check that the given type class instance obeys the
-- given set of laws.
--
-- Example usage:
--
-- >>> testLaws @Natural ordLaws
--
testLaws
:: forall a. (Arbitrary a, Eq a, Show a, Typeable a)
=> (Proxy a -> Laws)
-> Spec
testLaws getLaws =
describe description $
forM_ (lawsProperties laws) $ uncurry it
where
description = mconcat
[ "Testing "
, lawsTypeclass laws
, " laws for type "
, show (typeRep $ Proxy @a)
]
laws = getLaws $ Proxy @a

testLawsMany
:: forall a. (Arbitrary a, Eq a, Show a, Typeable a)
=> [Proxy a -> Laws]
-> Spec
testLawsMany getLawsMany =
testLaws @a `mapM_` getLawsMany
1 change: 1 addition & 0 deletions nix/.stack.nix/cardano-wallet-test-utils.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions nix/.stack.nix/default.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions stack.yaml
Expand Up @@ -14,6 +14,9 @@ extra-deps:
- OddWord-1.0.2.0
- command-0.1.1
- wai-extra-3.0.29.1
- primitive-addr-0.1.0.2
- quickcheck-classes-0.6.4.0
- quickcheck-classes-base-0.6.1.0

# Needed because network > 3.1 is needed
- servant-0.17
Expand Down

0 comments on commit cc1c30b

Please sign in to comment.