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

test function of Property type class is not available #39

Closed
marcellussiegburg opened this issue Aug 8, 2017 · 4 comments
Closed

test function of Property type class is not available #39

marcellussiegburg opened this issue Aug 8, 2017 · 4 comments

Comments

@marcellussiegburg
Copy link

I would like to run (all) my tests using QuickCheck and SmallCheck using Hspec. I Thought it might be helpful to run both test cases using the same Spec structure as when running test cases with either of them.

My idea was to create a library which handles the importing of QuickCheck and SmallCheck (and maybe other testing libraries) and reexports an unique interface for running both. The implementation is very simple.
That is why I present the basic code here (including a Spec example):

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
module Data.ListSpec where

import qualified Test.QuickCheck       as QC
import qualified Test.SmallCheck       as SC
import qualified Test.Hspec.SmallCheck as SC
import           Test.Hspec            hiding (it)
import qualified Test.Hspec            as Hspec

spec :: Spec
spec = do
  describe "length" $
    it "is higher for longer lists" $ property $ \ xs ->
      not (null xs) ==> length xs > length (tail (xs :: [Int]))

it descr p = do
  Hspec.it (descr ++ " (SC)") $ SC.property p
  Hspec.it (descr ++ " (QC)") $ QC.property p

infixr 0 ==>
(==>) :: (SC.Testable m prop, QC.Testable prop)
  => Bool -> prop -> (SC.Property m, QC.Property)
cond ==> p = (cond SC.==> p, cond QC.==> p)

property :: (SC.Testable IO prop, QC.Testable prop)
  => prop -> (SC.Property IO, QC.Property)
property p = (SC.property p, QC.property p)

instance QC.Testable (SC.Property m, QC.Property) where
  property (_, p) = QC.property p

instance (Monad m, m ~ n) => SC.Testable n (SC.Property m, QC.Property) where
  test (p, _) = SC.test p

Unfortunately it is not possible to implement the last Testable instance for SmallCheck as function test is not being exported. Thus, my suggestion is to export it and rerelease SmallCheck. I understand the Test.SmallCheck.Property module (and its function test) is internal. If it is required to stress this, I would suggest renaming this module to Test.SmallCheck.Internal.Property, exporting the test function and making it a visible module of the package.

Of course I am open to suggestions to achieve my goal (without code duplication) otherwise.

UnkindPartition added a commit that referenced this issue Aug 8, 2017
@UnkindPartition
Copy link
Collaborator

Does the above work for you? If so, I'll make a release.

@UnkindPartition
Copy link
Collaborator

Also, you are aware of hspec-smallcheck, right?

@marcellussiegburg
Copy link
Author

Yes, it does work. Thanks in advance for the release.
Yes, I am aware of hspec-smallcheck. That is why I use the library in the code above (import of Test.Hspec.SmallCheck).

UnkindPartition added a commit that referenced this issue Aug 8, 2017
@UnkindPartition
Copy link
Collaborator

UnkindPartition commented Aug 8, 2017

Got it — just wasn't sure what problem you were solving.

Released as v1.1.2.

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