summaryrefslogtreecommitdiff
path: root/tests/Hakyll/Core/Store/Tests.hs
blob: 3188c302e9cd289576d57fb67c7937ee4af2d6ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
module Hakyll.Core.Store.Tests
    ( tests
    ) where


--------------------------------------------------------------------------------
import           Data.Typeable                        (typeOf)
import           Test.Framework
import           Test.Framework.Providers.HUnit
import           Test.Framework.Providers.QuickCheck2
import qualified Test.HUnit                           as H
import           Test.QuickCheck
import           Test.QuickCheck.Monadic


--------------------------------------------------------------------------------
import qualified Hakyll.Core.Store                    as Store
import           TestSuite.Util


--------------------------------------------------------------------------------
tests :: [Test]
tests =
    [ testProperty "simple get . set"     simpleSetGet
    , testProperty "persistent get . set" persistentSetGet
    , testCase     "WrongType get . set"  wrongType
    ]


--------------------------------------------------------------------------------
simpleSetGet :: Property
simpleSetGet = monadicIO $ do
    key   <- pick arbitrary
    value <- pick arbitrary
    store <- run $ makeStoreTest
    run $ Store.set store key (value :: String)
    value' <- run $ Store.get store key
    assert $ Store.Found value == value'


--------------------------------------------------------------------------------
persistentSetGet :: Property
persistentSetGet = monadicIO $ do
    key    <- pick arbitrary
    value  <- pick arbitrary
    store1 <- run $ makeStoreTest
    run $ Store.set store1 key (value :: String)
    -- Now Create another store from the same dir to test persistence
    store2 <- run $ makeStoreTest
    value' <- run $ Store.get store2 key
    assert $ Store.Found value == value'


--------------------------------------------------------------------------------
wrongType :: H.Assertion
wrongType = do
    store <- makeStoreTest
    -- Store a string and try to fetch an int
    Store.set store ["foo", "bar"] ("qux" :: String)
    value <- Store.get store ["foo", "bar"] :: IO (Store.Result Int)
    print value
    H.assert $ case value of
        Store.WrongType e t ->
            e == typeOf (undefined :: Int) &&
            t == typeOf (undefined :: String)
        _                   -> False