2016-07-26 20:11:52 +03:00
|
|
|
{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
|
2016-07-26 10:56:52 +03:00
|
|
|
module Data.Mergeable.Spec where
|
2016-07-26 10:58:14 +03:00
|
|
|
|
2017-07-28 21:37:02 +03:00
|
|
|
import Control.Applicative (Alternative(..))
|
2016-07-26 11:02:52 +03:00
|
|
|
import Data.Functor.Identity
|
2017-01-08 08:10:14 +03:00
|
|
|
import Data.Functor.Listable
|
2017-07-28 21:37:02 +03:00
|
|
|
import Data.Maybe (catMaybes)
|
2016-07-26 11:02:52 +03:00
|
|
|
import Data.Mergeable
|
2016-07-26 10:58:14 +03:00
|
|
|
import Test.Hspec
|
2017-01-08 08:10:14 +03:00
|
|
|
import Test.Hspec.LeanCheck
|
|
|
|
import Test.LeanCheck
|
2016-07-26 10:58:14 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
2016-07-26 19:47:38 +03:00
|
|
|
spec = parallel $ do
|
2016-07-26 20:06:18 +03:00
|
|
|
describe "[]" $ do
|
2017-01-08 08:10:14 +03:00
|
|
|
withAlternativeInstances sequenceAltLaws (tiers :: [Tier String])
|
|
|
|
withAlternativeInstances mergeLaws (tiers :: [Tier String])
|
2016-07-26 20:16:46 +03:00
|
|
|
describe "Maybe" $ do
|
2017-01-08 08:10:14 +03:00
|
|
|
withAlternativeInstances sequenceAltLaws (tiers :: [Tier (Maybe Char)])
|
|
|
|
withAlternativeInstances mergeLaws (tiers :: [Tier (Maybe Char)])
|
2016-07-26 20:17:13 +03:00
|
|
|
describe "Identity" $ do
|
2017-01-08 08:10:14 +03:00
|
|
|
withAlternativeInstances sequenceAltLaws (Identity `mapT` tiers :: [Tier (Identity Char)])
|
|
|
|
withAlternativeInstances mergeLaws (Identity `mapT` tiers :: [Tier (Identity Char)])
|
2017-10-10 20:18:11 +03:00
|
|
|
describe "ListableSyntax" $ do
|
|
|
|
withAlternativeInstances sequenceAltLaws (tiers :: [Tier (ListableSyntax Char)])
|
|
|
|
withAlternativeInstances mergeLaws (tiers :: [Tier (ListableSyntax Char)])
|
2016-07-26 11:04:24 +03:00
|
|
|
|
2017-01-08 08:10:14 +03:00
|
|
|
prop "subsumes catMaybes/Just" $
|
2016-07-26 21:56:02 +03:00
|
|
|
\ a -> sequenceAlt a `shouldBe` pure (catMaybes (a :: [Maybe Char]))
|
|
|
|
|
2017-01-08 08:10:14 +03:00
|
|
|
mergeLaws :: forall f g a. (Mergeable f, Alternative g, Eq (g (f a)), Show (f a), Show (g (f a))) => [Tier (f a)] -> [Tier (Blind (a -> g a))] -> Spec
|
2016-07-26 20:03:13 +03:00
|
|
|
mergeLaws value function = describe "merge" $ do
|
2016-07-26 20:30:09 +03:00
|
|
|
prop "identity" . forAll value $
|
2016-07-26 22:27:16 +03:00
|
|
|
\ a -> merge pure a `shouldNotBe` (empty :: g (f a))
|
2016-07-26 20:30:09 +03:00
|
|
|
|
2017-01-08 08:10:14 +03:00
|
|
|
prop "relationship with sequenceAlt" . forAll (value >< function) $
|
2016-07-26 20:03:13 +03:00
|
|
|
\ (a, f) -> merge (getBlind f) a `shouldBe` sequenceAlt (fmap (getBlind f) a)
|
|
|
|
|
2017-01-08 08:10:14 +03:00
|
|
|
sequenceAltLaws :: forall f g a. (Mergeable f, Alternative g, Eq (g (f a)), Show (f a), Show (g (f a))) => [Tier (f a)] -> [Tier (Blind (a -> g a))] -> Spec
|
|
|
|
sequenceAltLaws value function = describe "sequenceAlt" $ do
|
|
|
|
prop "identity" . forAll value $
|
|
|
|
\ a -> sequenceAlt (pure <$> a) `shouldNotBe` (empty :: g (f a))
|
2016-07-26 11:15:56 +03:00
|
|
|
|
2017-01-08 08:10:14 +03:00
|
|
|
prop "relationship with merge" . forAll (productWith ((Blind .) . fmap . getBlind) function value :: [Tier (Blind (f (g a)))]) $
|
2017-07-28 21:37:02 +03:00
|
|
|
\ a -> sequenceAlt (getBlind a) `shouldBe` merge id (getBlind a)
|
2016-07-26 20:12:09 +03:00
|
|
|
|
|
|
|
|
2017-01-08 08:10:14 +03:00
|
|
|
withAlternativeInstances :: forall f a. (Listable a, Eq (f a), Show (f a)) => (forall g. (Alternative g, Eq (g (f a)), Show (g (f a))) => [Tier (f a)] -> [Tier (Blind (a -> g a))] -> Spec) -> [Tier (f a)] -> Spec
|
2016-07-26 20:12:09 +03:00
|
|
|
withAlternativeInstances laws gen = do
|
2017-01-08 08:10:14 +03:00
|
|
|
describe "[]" $ laws gen (fmap const `mapT` tiers :: [Tier (Blind (a -> [a]))])
|
|
|
|
describe "Maybe" $ laws gen (fmap const `mapT` tiers :: [Tier (Blind (a -> Maybe a))])
|
|
|
|
|
|
|
|
|
|
|
|
newtype Blind a = Blind { getBlind :: a }
|
|
|
|
deriving Functor
|
|
|
|
|
|
|
|
instance Listable a => Listable (Blind a) where
|
|
|
|
tiers = Blind `mapT` tiers
|
|
|
|
|
|
|
|
instance Show (Blind a) where
|
|
|
|
showsPrec _ _ = showString "*"
|