2010-01-25 14:12:20 +00:00
|
|
|
module Page
|
|
|
|
( pageGroup
|
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2010-03-26 13:10:10 +00:00
|
|
|
import Control.Monad.Reader (runReaderT)
|
2010-01-25 14:12:20 +00:00
|
|
|
import Data.Binary
|
|
|
|
import Test.Framework (testGroup)
|
|
|
|
import Test.Framework.Providers.HUnit
|
|
|
|
import Test.Framework.Providers.QuickCheck2
|
|
|
|
import Test.HUnit
|
2010-03-26 13:10:10 +00:00
|
|
|
import System.Directory (getTemporaryDirectory, removeFile)
|
|
|
|
import System.FilePath ((</>))
|
2010-01-25 14:12:20 +00:00
|
|
|
|
2010-03-26 13:10:10 +00:00
|
|
|
import Text.Hakyll.Internal.Page
|
|
|
|
import Text.Hakyll.Context
|
|
|
|
import Text.Hakyll.HakyllAction
|
|
|
|
import Text.Hakyll
|
2010-01-25 14:12:20 +00:00
|
|
|
|
|
|
|
-- Page test group.
|
|
|
|
pageGroup = testGroup "Page"
|
2010-03-26 13:10:10 +00:00
|
|
|
[ testCase "test_readPage_1" test_readPage_1
|
|
|
|
, testCase "test_readPage_2" test_readPage_2
|
2010-04-01 10:26:31 +00:00
|
|
|
, testCase "test_readPage_3" test_readPage_3
|
2010-04-04 13:08:59 +00:00
|
|
|
, testCase "test_readPage_4" test_readPage_4
|
2010-01-25 14:12:20 +00:00
|
|
|
]
|
|
|
|
|
2010-03-26 13:10:10 +00:00
|
|
|
-- | An abstract function to test page reading.
|
|
|
|
test_readPage :: FilePath -- ^ Filename to give to the temporary file.
|
|
|
|
-> String -- ^ Content to put in the file.
|
|
|
|
-> (Context -> Bool) -- ^ Assertion to run on the result Context.
|
|
|
|
-> IO Bool -- ^ Result of the assertion.
|
|
|
|
test_readPage fileName content assertion = do
|
|
|
|
temporaryDir <- getTemporaryDirectory
|
|
|
|
let temporaryFile = temporaryDir </> fileName
|
|
|
|
writeFile temporaryFile content
|
2010-08-02 10:59:22 +00:00
|
|
|
page <- runDefaultHakyll (readPage temporaryFile)
|
2010-03-26 13:10:10 +00:00
|
|
|
removeFile temporaryFile
|
|
|
|
return $ assertion page
|
|
|
|
|
|
|
|
-- | readPage test case 1.
|
|
|
|
test_readPage_1 = test_readPage fileName content assertion @? "test_readPage_1"
|
|
|
|
where
|
|
|
|
fileName = "test_readPage_1.markdown"
|
|
|
|
content = unlines [ "---"
|
|
|
|
, "author: Eric Cartman"
|
|
|
|
, "---"
|
|
|
|
, "This is a simple test."
|
|
|
|
]
|
2010-06-20 08:45:17 +00:00
|
|
|
assertion page = M.lookup "author" (unContext page) == Just "Eric Cartman"
|
2010-03-26 13:10:10 +00:00
|
|
|
|
|
|
|
-- | readPage test case 2.
|
|
|
|
test_readPage_2 = test_readPage fileName content assertion @? "test_readPage_2"
|
|
|
|
where
|
|
|
|
fileName = "test_readPage_2.txt"
|
|
|
|
content = unlines [ "--- someSection"
|
|
|
|
, "This is a section."
|
|
|
|
, "---"
|
|
|
|
, "This is the body."
|
|
|
|
]
|
2010-06-20 08:45:17 +00:00
|
|
|
assertion page =
|
|
|
|
let m = unContext page
|
|
|
|
in M.lookup "someSection" m == Just "This is a section.\n"
|
|
|
|
&& M.lookup "body" m == Just "This is the body.\n"
|
2010-04-01 10:26:31 +00:00
|
|
|
|
|
|
|
-- | readPage test case 3.
|
|
|
|
test_readPage_3 = test_readPage fileName content assertion @? "test_readPage_3"
|
|
|
|
where
|
|
|
|
fileName = "test_readPage_3.txt"
|
|
|
|
content = unlines [ "No metadata here, sorry."
|
|
|
|
]
|
2010-06-20 08:45:17 +00:00
|
|
|
assertion page =
|
|
|
|
M.lookup "body" (unContext page) == Just "No metadata here, sorry.\n"
|
2010-04-04 13:08:59 +00:00
|
|
|
|
|
|
|
-- | readPage test case 4.
|
|
|
|
test_readPage_4 = test_readPage fileName content assertion @? "test_readPage_4"
|
|
|
|
where
|
|
|
|
fileName = "test_readPage_4.txt"
|
|
|
|
content = unlines [ "--- section"
|
|
|
|
, "This is a section."
|
|
|
|
, "---"
|
|
|
|
, "Header"
|
|
|
|
, "------"
|
|
|
|
, "The header is not a separate section."
|
|
|
|
]
|
2010-06-20 08:45:17 +00:00
|
|
|
assertion page = M.lookup "body" (unContext page) == Just body
|
2010-04-04 13:08:59 +00:00
|
|
|
body = unlines [ "Header"
|
|
|
|
, "------"
|
|
|
|
, "The header is not a separate section."
|
|
|
|
]
|