1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 06:41:45 +03:00
semantic/test/Data/Mergeable/Spec.hs

49 lines
2.3 KiB
Haskell
Raw Normal View History

{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
module Data.Mergeable.Spec where
2016-07-26 10:58:14 +03:00
import Data.Functor.Identity
import Data.Mergeable
2016-07-26 10:58:14 +03:00
import Prologue
2016-07-26 12:39:02 +03:00
import Syntax
2016-07-26 10:58:14 +03:00
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck
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
withAlternativeInstances sequenceAltLaws (arbitrary :: Gen [Char])
withAlternativeInstances mergeLaws (arbitrary :: Gen [Char])
2016-07-26 20:16:46 +03:00
describe "Maybe" $ do
withAlternativeInstances sequenceAltLaws (arbitrary :: Gen (Maybe Char))
withAlternativeInstances mergeLaws (arbitrary :: Gen (Maybe Char))
2016-07-26 20:17:13 +03:00
describe "Identity" $ do
withAlternativeInstances sequenceAltLaws (Identity <$> arbitrary :: Gen (Identity Char))
withAlternativeInstances mergeLaws (Identity <$> arbitrary :: Gen (Identity Char))
2016-07-26 20:17:33 +03:00
describe "Syntax" $ do
withAlternativeInstances sequenceAltLaws (sized (syntaxOfSize (const arbitrary)) :: Gen (Syntax Char Char))
withAlternativeInstances mergeLaws (sized (syntaxOfSize (const arbitrary)) :: Gen (Syntax Char Char))
mergeLaws :: forall f g a. (Mergeable f, Alternative g, Eq (g (f a)), Show (f a), Show (g (f a))) => Gen (f a) -> Gen (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 $
\ a -> merge pure a `shouldBe` (pure a :: g (f a))
let pair = (,) <$> value <*> function
prop "relationship with sequenceAlt" . forAll pair $
2016-07-26 20:03:13 +03:00
\ (a, f) -> merge (getBlind f) a `shouldBe` sequenceAlt (fmap (getBlind f) a)
2016-07-26 20:13:45 +03:00
sequenceAltLaws :: forall f g a. (Mergeable f, Alternative g, Eq (g (f a)), Show (f a), Show (g (f a))) => Gen (f a) -> Gen (Blind (a -> g a)) -> Spec
sequenceAltLaws value function = do
describe "sequenceAlt" $ do
prop "identity" . forAll value $
2016-07-26 11:25:45 +03:00
\ a -> sequenceAlt (pure <$> a) `shouldBe` (pure a :: g (f a))
2016-07-26 20:25:16 +03:00
prop "relationship with merge" . forAll (Blind <$> (fmap . getBlind <$> function <*> value) :: Gen (Blind (f (g a)))) $
\ a -> sequenceAlt (getBlind a) `shouldBe` merge identity (getBlind a)
2016-07-26 20:12:32 +03:00
withAlternativeInstances :: forall f a. (Arbitrary a, CoArbitrary a, Eq (f a), Show (f a)) => (forall g. (Alternative g, Eq (g (f a)), Show (g (f a))) => Gen (f a) -> Gen (Blind (a -> g a)) -> Spec) -> Gen (f a) -> Spec
withAlternativeInstances laws gen = do
describe "Maybe" $ laws gen (arbitrary :: Gen (Blind (a -> Maybe a)))