hakyll/tests/Hakyll/Web/Template/Tests.hs

55 lines
1.6 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
module Hakyll.Web.Template.Tests
( tests
) where
import Test.Framework
import Test.HUnit hiding (Test)
import qualified Data.Map as M
import Hakyll.Web.Page
import Hakyll.Web.Template
2011-02-10 11:30:58 +00:00
import Hakyll.Web.Template.Read
import TestSuite.Util
tests :: [Test]
tests = fromAssertions "applyTemplate"
-- Hakyll templates
2011-11-23 14:24:20 +00:00
[ applyTemplateAssertion readTemplate applyTemplate
"bar" "$foo$" [("foo", "bar")]
2011-11-23 14:24:20 +00:00
, applyTemplateAssertion readTemplate applyTemplate
"$ barqux" "$$ $foo$$bar$" [("foo", "bar"), ("bar", "qux")]
2011-11-23 14:24:20 +00:00
, applyTemplateAssertion readTemplate applyTemplate
"$foo$" "$foo$" []
-- Hamlet templates
2011-11-23 14:24:20 +00:00
, applyTemplateAssertion readHamletTemplate applyTemplate
"<head><title>notice</title></head><body>A paragraph</body>"
"<head\n\
\ <title>#{title}\n\
\<body\n\
\ A paragraph\n"
[("title", "notice")]
2011-11-23 14:24:20 +00:00
-- Missing keys
, let missing "foo" = "bar"
missing "bar" = "qux"
missing x = reverse x
in applyTemplateAssertion readTemplate (applyTemplateWith missing)
"bar foo ver" "$foo$ $bar$ $rev$" [("bar", "foo")]
]
-- | Utility function to create quick template tests
--
2011-11-23 14:24:20 +00:00
applyTemplateAssertion :: (String -> Template)
-> (Template -> Page String -> Page String)
-> String
-> String
-> [(String, String)]
-> Assertion
applyTemplateAssertion parser apply expected template page =
expected @=? pageBody (apply (parser template) (fromMap $ M.fromList page))