summaryrefslogtreecommitdiff
path: root/tests/Hakyll/Core/Store/Tests.hs
blob: 4f35abdad7e9c6b707cf5c4d2b882f70e2c8e871 (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
{-# LANGUAGE OverloadedStrings #-}
module Hakyll.Core.Store.Tests
    ( tests
    ) where

import Control.Applicative ((<$>))
import Control.Monad (replicateM)

import Test.Framework
import Test.Framework.Providers.QuickCheck2
import Test.Framework.Providers.HUnit
import Test.QuickCheck
import Test.QuickCheck.Monadic
import qualified Test.HUnit as H

import Hakyll.Core.Identifier
import Hakyll.Core.Store

tests :: [Test]
tests =
    [ testProperty "simple storeGet . storeSet" simpleSetGet
    , testProperty "persistent storeGet . storeSet" persistentSetGet
    , testCase     "WrongType storeGet . storeSet" wrongType
    ]

simpleSetGet :: Property
simpleSetGet = monadicIO $ do
    identifier <- parseIdentifier . unFileName <$> pick arbitrary
    FileName name <- pick arbitrary
    value <- pick arbitrary
    store <- run $ makeStore "_store"
    run $ storeSet store name identifier (value :: String)
    value' <- run $ storeGet store name identifier
    assert $ Found value == value'

persistentSetGet :: Property
persistentSetGet = monadicIO $ do
    identifier <- parseIdentifier . unFileName <$> pick arbitrary
    FileName name <- pick arbitrary
    value <- pick arbitrary
    store1 <- run $ makeStore "_store"
    run $ storeSet store1 name identifier (value :: String)
    -- Now Create another store from the same dir to test persistence
    store2 <- run $ makeStore "_store"
    value' <- run $ storeGet store2 name identifier
    assert $ Found value == value'

wrongType :: H.Assertion
wrongType = do
    store <- makeStore "_store"
    -- Store a string and try to fetch an int
    storeSet store "foo" "bar" ("qux" :: String)
    value <- storeGet store "foo" "bar" :: IO (StoreGet Int)
    H.assert $ case value of WrongType _ _ -> True
                             _             -> False

newtype FileName = FileName {unFileName :: String}
                 deriving (Show)

instance Arbitrary FileName where
    arbitrary = do
        length' <- choose (5, 100)
        str <- replicateM length' $ elements cs
        return $ FileName str
      where
        cs = ['a' .. 'z'] ++ ['A' .. 'Z'] ++ ['0' .. '9'] ++ ".- "