on master help sign in
Merge branch 'master' into type-safe-identifiers
commit0807e4c49aeb66fdbe9ea9a755b6865bfb075336 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateTue 24 May 2011, 23:17 (15 years ago) parentf6f7cde01d9898
3 files changed+139
tests/Hakyll/Core/Rules/Tests.hs+67 tests/Hakyll/Core/Store/Tests.hs+66 tests/TestSuite.hs+6
tests/Hakyll/Core/Rules/Tests.hsnew file+67
@@ -0,0 +1,67 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
3+module Hakyll.Core.Rules.Tests
4+ ( tests
5+ ) where
6+
7+import qualified Data.Map as M
8+import qualified Data.Set as S
9+
10+import Test.Framework
11+import Test.Framework.Providers.HUnit
12+import Test.HUnit hiding (Test)
13+
14+import Hakyll.Core.Rules
15+import Hakyll.Core.Rules.Internal
16+import Hakyll.Core.Identifier
17+import Hakyll.Core.Routes
18+import Hakyll.Core.Compiler
19+import Hakyll.Core.Resource.Provider
20+import Hakyll.Core.Resource.Provider.Dummy
21+import Hakyll.Web.Page
22+
23+tests :: [Test]
24+tests =
25+ [ testCase "runRules" rulesTest
26+ ]
27+
28+-- | Main test
29+--
30+rulesTest :: Assertion
31+rulesTest = do
32+ p <- provider
33+ let ruleSet = runRules rules p
34+ assert $ expected == S.fromList (map fst (rulesCompilers ruleSet))
35+ where
36+ expected = S.fromList
37+ [ Identifier Nothing "posts/a-post.markdown"
38+ , Identifier Nothing "posts/some-other-post.markdown"
39+ , Identifier (Just "raw") "posts/a-post.markdown"
40+ , Identifier (Just "raw") "posts/some-other-post.markdown"
41+ ]
42+
43+-- | Dummy resource provider
44+--
45+provider :: IO ResourceProvider
46+provider = dummyResourceProvider $ M.fromList $ map (flip (,) "No content")
47+ [ "posts/a-post.markdown"
48+ , "posts/some-other-post.markdown"
49+ ]
50+
51+-- | Example rules
52+--
53+rules :: Rules
54+rules = do
55+ -- Compile some posts
56+ match "posts/*" $ do
57+ route $ setExtension "html"
58+ compile pageCompiler
59+
60+ -- Compile them, raw
61+ group "raw" $ do
62+ route idRoute
63+ match "posts/*" $ do
64+ route $ setExtension "html"
65+ compile getResourceString
66+
67+ return ()
tests/Hakyll/Core/Store/Tests.hsnew file+66
@@ -0,0 +1,66 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Hakyll.Core.Store.Tests
3+ ( tests
4+ ) where
5+
6+import Control.Applicative ((<$>))
7+import Control.Monad (replicateM)
8+
9+import Test.Framework
10+import Test.Framework.Providers.QuickCheck2
11+import Test.Framework.Providers.HUnit
12+import Test.QuickCheck
13+import Test.QuickCheck.Monadic
14+import qualified Test.HUnit as H
15+
16+import Hakyll.Core.Identifier
17+import Hakyll.Core.Store
18+
19+tests :: [Test]
20+tests =
21+ [ testProperty "simple storeGet . storeSet" simpleSetGet
22+ , testProperty "persistent storeGet . storeSet" persistentSetGet
23+ , testCase "WrongType storeGet . storeSet" wrongType
24+ ]
25+
26+simpleSetGet :: Property
27+simpleSetGet = monadicIO $ do
28+ identifier <- parseIdentifier . unFileName <$> pick arbitrary
29+ FileName name <- pick arbitrary
30+ value <- pick arbitrary
31+ store <- run $ makeStore "_store"
32+ run $ storeSet store name identifier (value :: String)
33+ value' <- run $ storeGet store name identifier
34+ assert $ Found value == value'
35+
36+persistentSetGet :: Property
37+persistentSetGet = monadicIO $ do
38+ identifier <- parseIdentifier . unFileName <$> pick arbitrary
39+ FileName name <- pick arbitrary
40+ value <- pick arbitrary
41+ store1 <- run $ makeStore "_store"
42+ run $ storeSet store1 name identifier (value :: String)
43+ -- Now Create another store from the same dir to test persistence
44+ store2 <- run $ makeStore "_store"
45+ value' <- run $ storeGet store2 name identifier
46+ assert $ Found value == value'
47+
48+wrongType :: H.Assertion
49+wrongType = do
50+ store <- makeStore "_store"
51+ -- Store a string and try to fetch an int
52+ storeSet store "foo" "bar" ("qux" :: String)
53+ value <- storeGet store "foo" "bar" :: IO (StoreGet Int)
54+ H.assert $ case value of WrongType _ _ -> True
55+ _ -> False
56+
57+newtype FileName = FileName {unFileName :: String}
58+ deriving (Show)
59+
60+instance Arbitrary FileName where
61+ arbitrary = do
62+ length' <- choose (5, 100)
63+ str <- replicateM length' $ elements cs
64+ return $ FileName str
65+ where
66+ cs = ['a' .. 'z'] ++ ['A' .. 'Z'] ++ ['0' .. '9'] ++ ".- "
tests/TestSuite.hs+6
@@ -5,6 +5,8 @@ import Test.Framework (defaultMain, testGroup)
55import qualified Hakyll.Core.DependencyAnalyzer.Tests
66import qualified Hakyll.Core.Identifier.Tests
77import qualified Hakyll.Core.Routes.Tests
8+import qualified Hakyll.Core.Rules.Tests
9+import qualified Hakyll.Core.Store.Tests
810import qualified Hakyll.Web.Page.Tests
911import qualified Hakyll.Web.Page.Metadata.Tests
1012import qualified Hakyll.Web.RelativizeUrls.Tests
@@ -19,6 +21,10 @@ main = defaultMain
1921 Hakyll.Core.Identifier.Tests.tests
2022 , testGroup "Hakyll.Core.Routes.Tests"
2123 Hakyll.Core.Routes.Tests.tests
24+ , testGroup "Hakyll.Core.Rules.Tests"
25+ Hakyll.Core.Rules.Tests.tests
26+ , testGroup "Hakyll.Core.Store.Tests"
27+ Hakyll.Core.Store.Tests.tests
2228 , testGroup "Hakyll.Web.Page.Tests"
2329 Hakyll.Web.Page.Tests.tests
2430 , testGroup "Hakyll.Web.Page.Metadata.Tests"