Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add examples of use with test frameworks like tasty, hspec, etc. #23

Open
chessai opened this issue Jul 10, 2018 · 2 comments
Open

add examples of use with test frameworks like tasty, hspec, etc. #23

chessai opened this issue Jul 10, 2018 · 2 comments

Comments

@chessai
Copy link
Collaborator

chessai commented Jul 10, 2018

These examples shouldn't need to be large but will be useful in showing people what something like 'lawsToTest' (found in the test suite for primitive) looks like

@andrewthad
Copy link
Owner

Agreed. It would be good to provide example of how to use this with test frameworks.

@chessai chessai changed the title add examples of use with things like tasty, hspec, etc. add examples of use with test frameworks like tasty, hspec, etc. Jul 10, 2018
@chessai
Copy link
Collaborator Author

chessai commented Nov 12, 2018

i haven't used this with hspec, but i typically use the following with tasty:

{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeApplications #-}

import Data.Monoid (Sum)
import Data.Map (Map)
import Test.QuickCheck.Classes (Laws(Laws))
import Test.QuickCheck (Property)
import qualified Test.Tasty.QuickCheck as TQC
import Test.Tasty (TestName, TestTree, defaultMain, testGroup)

import qualified Test.QuickCheck.Classes as QCC

main = do
  let pInt = Proxy @Int
  let pSumInt = Proxy @(Sum Int)
  let pMapInt = Proxy @(Map Int)
  let pMapIntInt = Proxy @(Map Int Int)
  defaultMain $ testGroup "properties"
    [ lawsToTest "Int" (mkLaws pInt [QCC.eqLaws, QCC.ordLaws])
    , lawsToTest "Sum Int" (mkLaws pSumInt [QCC.monoidLaws])
    , lawsToTest "Map Int" (mkLaws pMapInt [QCC.functorLaws])
    , lawsToTest "Map Int Int" (mkLaws pMapIntInt [QCC.monoidLaws, QCC.isListLaws])
    ]

mkLaws :: Proxy (a :: k) -> [Proxy a -> Laws] -> [Laws]
mkLaws p fs = map ($ p) fs

pairsToTestTree :: [(String, Property)] -> [TestTree]
pairsToTestTree pairs = map (uncurry TQC.testProperty) pairs

lawsToTest :: TestName -> [Laws] -> TestTree
lawsToTest testName laws = testGroup testName (map lawToTest laws)

lawToTest :: Laws -> TestTree
lawToTest (Laws name pairs) = testGroup name (pairsToTestTree pairs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants