| 1 |
{-# LANGUAGE QuasiQuotes #-} |
| 2 |
{-# LANGUAGE UndecidableInstances #-} |
| 3 |
{-# LANGUAGE FlexibleContexts #-} |
| 4 |
{-# LANGUAGE FlexibleInstances #-} |
| 5 |
{-# LANGUAGE RankNTypes #-} |
| 6 |
{-# LANGUAGE CPP #-} |
| 7 |
{-# LANGUAGE TemplateHaskell #-} |
| 8 |
|
| 9 |
module Data.Acid.TemplateHaskellSpec where |
| 10 |
|
| 11 |
import Test.Hspec hiding (context) |
| 12 |
|
| 13 |
import Data.SafeCopy (SafeCopy) |
| 14 |
import Control.DeepSeq (force) |
| 15 |
import Control.Exception (evaluate) |
| 16 |
import Language.Haskell.TH |
| 17 |
import Language.Haskell.TH.Quote |
| 18 |
import Control.Monad.Reader |
| 19 |
import Control.Monad.State |
| 20 |
|
| 21 |
import Data.Acid |
| 22 |
import Data.Acid.TemplateHaskell |
| 23 |
|
| 24 |
spec :: Spec |
| 25 |
spec = do |
| 26 |
let name = mkName "foo" |
| 27 |
nameT = ConT name |
| 28 |
upperName = mkName "Foo" |
| 29 |
upperNameT = ConT upperName |
| 30 |
|
| 31 |
describe "makeEventInstance" $ do |
| 32 |
it "works with monomorphic types" $ do |
| 33 |
eventType <- runQ [t| Int -> Query Char () |] |
| 34 |
makeEventInstance name eventType |
| 35 |
`quoteShouldBe` |
| 36 |
[d| instance QueryEvent $(return upperNameT) |] |
| 37 |
|
| 38 |
it "requires instances on polymorphic types" $ do |
| 39 |
let a = VarT (mkName "a") |
| 40 |
a' = return a |
| 41 |
eventType <- runQ [t| (Ord $(a')) => $(a') -> Update Char $(a') |] |
| 42 |
|
| 43 |
makeEventInstance name eventType |
| 44 |
`quoteShouldBe` |
| 45 |
[d| instance (Ord $(a')) => UpdateEvent $(return upperNameT) |
| 46 |
|] |
| 47 |
|
| 48 |
|
| 49 |
describe "analyseType" $ do |
| 50 |
it "can work with the Query type" $ do |
| 51 |
typ <- runQ [t| Int -> Query String Char |] |
| 52 |
|
| 53 |
analyseType name typ |
| 54 |
`shouldBe` TypeAnalysis |
| 55 |
{ tyvars = [] |
| 56 |
, context = [] |
| 57 |
, argumentTypes = [ConT ''Int] |
| 58 |
, stateType = ConT ''String |
| 59 |
, resultType = ConT ''Char |
| 60 |
, isUpdate = False |
| 61 |
} |
| 62 |
|
| 63 |
it "can work with the Update type" $ do |
| 64 |
typ <- runQ [t| Int -> Update String Char |] |
| 65 |
|
| 66 |
analyseType name typ |
| 67 |
`shouldBe` TypeAnalysis |
| 68 |
{ tyvars = [] |
| 69 |
, context = [] |
| 70 |
, argumentTypes = [ConT ''Int] |
| 71 |
, stateType = ConT ''String |
| 72 |
, resultType = ConT ''Char |
| 73 |
, isUpdate = True |
| 74 |
} |
| 75 |
|
| 76 |
it "can work with MonadReader" $ do |
| 77 |
typ <- runQ [t| forall m. (MonadReader Int m) => Int -> m () |] |
| 78 |
analyseType name typ |
| 79 |
`shouldBe` TypeAnalysis |
| 80 |
{ tyvars = [] |
| 81 |
, context = [] |
| 82 |
, argumentTypes = [ConT ''Int] |
| 83 |
, stateType = ConT ''Int |
| 84 |
, resultType = TupleT 0 |
| 85 |
, isUpdate = False |
| 86 |
} |
| 87 |
|
| 88 |
it "can work with MonadState" $ do |
| 89 |
typ <- runQ [t| forall m. (MonadState Int m) => Int -> m () |] |
| 90 |
analyseType name typ |
| 91 |
`shouldBe` TypeAnalysis |
| 92 |
{ tyvars = [] |
| 93 |
, context = [] |
| 94 |
, argumentTypes = [ConT ''Int] |
| 95 |
, stateType = ConT ''Int |
| 96 |
, resultType = TupleT 0 |
| 97 |
, isUpdate = True |
| 98 |
} |
| 99 |
|
| 100 |
it "can work with many type variables (note that eventCxts later rejects this)" $ do |
| 101 |
let m = mkName "m" |
| 102 |
typ <- runQ [t| (MonadReader Int $(varT m)) => Int -> Query Int ($(varT m) ()) |] |
| 103 |
analyseType name typ |
| 104 |
`shouldBe` TypeAnalysis |
| 105 |
{ tyvars = [] |
| 106 |
, context = |
| 107 |
[ ConT ''MonadReader |
| 108 |
`AppT` ConT ''Int |
| 109 |
`AppT` VarT m |
| 110 |
] |
| 111 |
, argumentTypes = [ConT ''Int] |
| 112 |
, stateType = ConT ''Int |
| 113 |
, resultType = VarT m `AppT` TupleT 0 |
| 114 |
, isUpdate = False |
| 115 |
} |
| 116 |
|
| 117 |
describe "eventCxts" $ do |
| 118 |
let binders = [] |
| 119 |
stateType = ConT ''Char |
| 120 |
it "rejects types with constrainted type variables unknown to state" $ do |
| 121 |
let predicate eventType = |
| 122 |
evaluate |
| 123 |
. force |
| 124 |
. map show |
| 125 |
$ eventCxts stateType binders name eventType |
| 126 |
eventType <- runQ [t| forall a. (Ord a) => Int -> Query Char a |] |
| 127 |
|
| 128 |
predicate eventType |
| 129 |
`shouldThrow` |
| 130 |
anyErrorCall |
| 131 |
|
| 132 |
it "accepts types with unconstrained type variables" $ do |
| 133 |
eventType <- runQ [t| forall a. Int -> Query Char a |] |
| 134 |
|
| 135 |
eventCxts stateType binders name eventType |
| 136 |
`shouldBe` |
| 137 |
[] |
| 138 |
let x = mkName "x" |
| 139 |
|
| 140 |
it "accepts constrained type variables in the state" $ do |
| 141 |
let binders :: [TyVarBndrUnit] |
| 142 |
#if MIN_VERSION_template_haskell(2,17,0) |
| 143 |
binders = [PlainTV (mkName "x") ()] |
| 144 |
#else |
| 145 |
binders = [PlainTV (mkName "x")] |
| 146 |
#endif |
| 147 |
stateType = ConT ''Maybe `AppT` VarT x |
| 148 |
eventType <- runQ [t| forall a. (Ord a) => Int -> Query (Maybe a) Int|] |
| 149 |
|
| 150 |
eventCxts stateType binders name eventType |
| 151 |
`shouldBe` |
| 152 |
[ConT ''Ord `AppT` VarT x] |
| 153 |
|
| 154 |
it "can rename a polymorphic state" $ do |
| 155 |
eventType <- runQ [t| forall r m. (MonadReader r m, Ord r) => Int -> m Char |] |
| 156 |
eventCxts stateType binders name eventType |
| 157 |
`shouldBe` |
| 158 |
[ConT ''Ord `AppT` ConT ''Char] |
| 159 |
|
| 160 |
|
| 161 |
quoteShouldBe :: (Eq a, Show a) => Q a -> Q [a] -> Expectation |
| 162 |
quoteShouldBe qa qb = do |
| 163 |
actual <- runQ qa |
| 164 |
[expected] <- runQ qb |
| 165 |
actual `shouldBe` expected |