Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function
testLaws
for testing type class laws.
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
1 parent
0e99312
commit cc1c30b
Showing
5 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters