From 1dff099486f5baaf7bebd74eb70247647ab342f0 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Tue, 24 May 2011 14:40:45 +0200 Subject: Rules tests should look like that --- tests/Hakyll/Core/Rules/Tests.hs | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/Hakyll/Core/Rules/Tests.hs diff --git a/tests/Hakyll/Core/Rules/Tests.hs b/tests/Hakyll/Core/Rules/Tests.hs new file mode 100644 index 0000000..42b95fc --- /dev/null +++ b/tests/Hakyll/Core/Rules/Tests.hs @@ -0,0 +1,41 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS_GHC -fno-warn-unused-do-bind #-} +module Hakyll.Core.Rules.Tests + where + +import qualified Data.Map as M + +import Test.Framework + +import Hakyll.Core.Rules +import Hakyll.Core.Identifier +import Hakyll.Core.Routes +import Hakyll.Core.Compiler +import Hakyll.Core.Resource.Provider +import Hakyll.Core.Resource.Provider.Dummy +import Hakyll.Web.Page + +-- | Dummy resource provider +-- +provider :: IO ResourceProvider +provider = dummyResourceProvider $ M.fromList $ map (flip (,) "No content") + [ "posts/a-post.markdown" + , "posts/some-other-post.markdown" + ] + +-- | Example rules +-- +rules :: Rules +rules = do + -- Compile some posts + match "posts/*" $ do + route $ setExtension "html" + compile pageCompiler + + -- Compile them, raw + group "raw" $ do + match "posts/*" $ do + route $ setExtension "html" + compile getResourceString + + return () -- cgit v1.2.3 From 6025e2fab8eae692b6af8ad8f2a6e3f27ebeafc4 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Tue, 24 May 2011 22:29:47 +0200 Subject: Finish rules test --- tests/Hakyll/Core/Rules/Tests.hs | 25 +++++++++++++++++++++++++ tests/TestSuite.hs | 3 +++ 2 files changed, 28 insertions(+) diff --git a/tests/Hakyll/Core/Rules/Tests.hs b/tests/Hakyll/Core/Rules/Tests.hs index 42b95fc..1b388e8 100644 --- a/tests/Hakyll/Core/Rules/Tests.hs +++ b/tests/Hakyll/Core/Rules/Tests.hs @@ -4,10 +4,14 @@ module Hakyll.Core.Rules.Tests where import qualified Data.Map as M +import qualified Data.Set as S import Test.Framework +import Test.Framework.Providers.HUnit +import Test.HUnit hiding (Test) import Hakyll.Core.Rules +import Hakyll.Core.Rules.Internal import Hakyll.Core.Identifier import Hakyll.Core.Routes import Hakyll.Core.Compiler @@ -15,6 +19,26 @@ import Hakyll.Core.Resource.Provider import Hakyll.Core.Resource.Provider.Dummy import Hakyll.Web.Page +tests :: [Test] +tests = + [ testCase "runRules" rulesTest + ] + +-- | Main test +-- +rulesTest :: Assertion +rulesTest = do + p <- provider + let ruleSet = runRules rules p + assert $ expected == S.fromList (map fst (rulesCompilers ruleSet)) + where + expected = S.fromList + [ Identifier Nothing "posts/a-post.markdown" + , Identifier Nothing "posts/some-other-post.markdown" + , Identifier (Just "raw") "posts/a-post.markdown" + , Identifier (Just "raw") "posts/some-other-post.markdown" + ] + -- | Dummy resource provider -- provider :: IO ResourceProvider @@ -34,6 +58,7 @@ rules = do -- Compile them, raw group "raw" $ do + route idRoute match "posts/*" $ do route $ setExtension "html" compile getResourceString diff --git a/tests/TestSuite.hs b/tests/TestSuite.hs index 4d2c95d..37a9803 100644 --- a/tests/TestSuite.hs +++ b/tests/TestSuite.hs @@ -5,6 +5,7 @@ import Test.Framework (defaultMain, testGroup) import qualified Hakyll.Core.DependencyAnalyzer.Tests import qualified Hakyll.Core.Identifier.Tests import qualified Hakyll.Core.Routes.Tests +import qualified Hakyll.Core.Rules.Tests import qualified Hakyll.Web.Page.Tests import qualified Hakyll.Web.Page.Metadata.Tests import qualified Hakyll.Web.RelativizeUrls.Tests @@ -19,6 +20,8 @@ main = defaultMain Hakyll.Core.Identifier.Tests.tests , testGroup "Hakyll.Core.Routes.Tests" Hakyll.Core.Routes.Tests.tests + , testGroup "Hakyll.Core.Rules.Tests" + Hakyll.Core.Rules.Tests.tests , testGroup "Hakyll.Web.Page.Tests" Hakyll.Web.Page.Tests.tests , testGroup "Hakyll.Web.Page.Metadata.Tests" -- cgit v1.2.3 From 01d989894cbd5724240b07a577de2c7654c22300 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Tue, 24 May 2011 23:17:32 +0200 Subject: Add some store tests --- tests/Hakyll/Core/Rules/Tests.hs | 3 +- tests/Hakyll/Core/Store/Tests.hs | 66 ++++++++++++++++++++++++++++++++++++++++ tests/TestSuite.hs | 3 ++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tests/Hakyll/Core/Store/Tests.hs diff --git a/tests/Hakyll/Core/Rules/Tests.hs b/tests/Hakyll/Core/Rules/Tests.hs index 1b388e8..ac2126c 100644 --- a/tests/Hakyll/Core/Rules/Tests.hs +++ b/tests/Hakyll/Core/Rules/Tests.hs @@ -1,7 +1,8 @@ {-# LANGUAGE OverloadedStrings #-} {-# OPTIONS_GHC -fno-warn-unused-do-bind #-} module Hakyll.Core.Rules.Tests - where + ( tests + ) where import qualified Data.Map as M import qualified Data.Set as S diff --git a/tests/Hakyll/Core/Store/Tests.hs b/tests/Hakyll/Core/Store/Tests.hs new file mode 100644 index 0000000..4f35abd --- /dev/null +++ b/tests/Hakyll/Core/Store/Tests.hs @@ -0,0 +1,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'] ++ ".- " diff --git a/tests/TestSuite.hs b/tests/TestSuite.hs index 37a9803..e459529 100644 --- a/tests/TestSuite.hs +++ b/tests/TestSuite.hs @@ -6,6 +6,7 @@ import qualified Hakyll.Core.DependencyAnalyzer.Tests import qualified Hakyll.Core.Identifier.Tests import qualified Hakyll.Core.Routes.Tests import qualified Hakyll.Core.Rules.Tests +import qualified Hakyll.Core.Store.Tests import qualified Hakyll.Web.Page.Tests import qualified Hakyll.Web.Page.Metadata.Tests import qualified Hakyll.Web.RelativizeUrls.Tests @@ -22,6 +23,8 @@ main = defaultMain Hakyll.Core.Routes.Tests.tests , testGroup "Hakyll.Core.Rules.Tests" Hakyll.Core.Rules.Tests.tests + , testGroup "Hakyll.Core.Store.Tests" + Hakyll.Core.Store.Tests.tests , testGroup "Hakyll.Web.Page.Tests" Hakyll.Web.Page.Tests.tests , testGroup "Hakyll.Web.Page.Metadata.Tests" -- cgit v1.2.3