@@ -4,19 +4,58 @@ module Page
44
55import qualified Data.Map as M
66
7+import Control.Monad.Reader (runReaderT)
78import Data.Binary
89import Test.Framework (testGroup)
910import Test.Framework.Providers.HUnit
1011import Test.Framework.Providers.QuickCheck2
1112import Test.HUnit
13+import System.Directory (getTemporaryDirectory, removeFile)
14+import System.FilePath ((</>))
1215
13−import Text.Hakyll.Page
16+import Text.Hakyll.Internal.Page
17+import Text.Hakyll.Context
18+import Text.Hakyll.HakyllAction
19+import Text.Hakyll
1420
1521-- Page test group.
1622pageGroup = testGroup "Page"
17− [ testProperty "prop_page_encode_id" prop_page_encode_id
23+ [ testCase "test_readPage_1" test_readPage_1
24+ , testCase "test_readPage_2" test_readPage_2
1825 ]
1926
20−-- Test encoding/decoding of pages.
21−prop_page_encode_id :: Page -> Bool
22−prop_page_encode_id page = decode (encode page) == page
27+-- | An abstract function to test page reading.
28+test_readPage :: FilePath -- ^ Filename to give to the temporary file.
29+ -> String -- ^ Content to put in the file.
30+ -> (Context -> Bool) -- ^ Assertion to run on the result Context.
31+ -> IO Bool -- ^ Result of the assertion.
32+test_readPage fileName content assertion = do
33+ temporaryDir <- getTemporaryDirectory
34+ let temporaryFile = temporaryDir </> fileName
35+ writeFile temporaryFile content
36+ page <- runReaderT (readPage temporaryFile) defaultHakyllConfiguration
37+ removeFile temporaryFile
38+ return $ assertion page
39+
40+-- | readPage test case 1.
41+test_readPage_1 = test_readPage fileName content assertion @? "test_readPage_1"
42+ where
43+ fileName = "test_readPage_1.markdown"
44+ content = unlines [ "---"
45+ , "author: Eric Cartman"
46+ , "---"
47+ , "This is a simple test."
48+ ]
49+ assertion page = M.lookup "author" page == Just "Eric Cartman"
50+
51+-- | readPage test case 2.
52+test_readPage_2 = test_readPage fileName content assertion @? "test_readPage_2"
53+ where
54+ fileName = "test_readPage_2.txt"
55+ content = unlines [ "--- someSection"
56+ , "This is a section."
57+ , "---"
58+ , "This is the body."
59+ ]
60+ assertion page = M.lookup "someSection" page == Just "This is a section.\n"
61+ && M.lookup "body" page == Just "This is the body.\n"