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

123 lines
4.1 KiB
Haskell
Raw Normal View History

2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
module Hakyll.Web.Template.Tests
( tests
) where
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
import Test.Framework (Test, testGroup)
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (Assertion, (@=?), (@?=))
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
2013-05-06 21:32:25 +00:00
import Hakyll.Core.Compiler
import Hakyll.Core.Identifier
2012-11-19 13:59:55 +00:00
import Hakyll.Core.Item
import Hakyll.Core.Provider
2012-12-15 17:02:47 +00:00
import Hakyll.Web.Pandoc
2012-11-19 13:59:55 +00:00
import Hakyll.Web.Template
import Hakyll.Web.Template.Context
import Hakyll.Web.Template.Internal
import Hakyll.Web.Template.List
2012-11-19 13:59:55 +00:00
import TestSuite.Util
--------------------------------------------------------------------------------
tests :: Test
2014-10-27 12:19:56 +00:00
tests = testGroup "Hakyll.Core.Template.Tests" $ concat
[ [ testCase "case01" $ test ("template.html.out", "template.html", "example.md")
, testCase "case02" $ test ("strip.html.out", "strip.html", "example.md")
2014-10-27 12:19:56 +00:00
, testCase "applyJoinTemplateList" testApplyJoinTemplateList
]
, fromAssertions "readTemplate"
2016-07-24 15:30:47 +00:00
[ [Chunk "Hello ", Expr (Call "guest" [])]
@=? readTemplateElems "Hello $guest()$"
, [If (Call "a" [StringLiteral "bar"]) [Chunk "foo"] Nothing]
@=? readTemplateElems "$if(a(\"bar\"))$foo$endif$"
2016-07-23 10:19:27 +00:00
-- 'If' trim check.
2016-07-24 15:30:47 +00:00
, [ TrimL
, If (Ident (TemplateKey "body"))
[ TrimR
, Chunk "\n"
, Expr (Ident (TemplateKey "body"))
, Chunk "\n"
, TrimL
]
(Just [ TrimR
, Chunk "\n"
, Expr (Ident (TemplateKey "body"))
, Chunk "\n"
, TrimL
])
, TrimR
]
@=? readTemplateElems "$-if(body)-$\n$body$\n$-else-$\n$body$\n$-endif-$"
2016-07-23 10:41:41 +00:00
-- 'For' trim check.
2016-07-24 15:30:47 +00:00
, [ TrimL
, For (Ident (TemplateKey "authors"))
[TrimR, Chunk "\n body \n", TrimL]
Nothing
, TrimR
]
@=? readTemplateElems "$-for(authors)-$\n body \n$-endfor-$"
2016-07-23 10:52:55 +00:00
-- 'Partial' trim check.
2016-07-24 15:30:47 +00:00
, [ TrimL
, Partial (StringLiteral "path")
, TrimR
]
@=? readTemplateElems "$-partial(\"path\")-$"
2016-07-23 12:46:15 +00:00
-- 'Expr' trim check.
2016-07-24 15:30:47 +00:00
, [ TrimL
, Expr (Ident (TemplateKey "foo"))
, TrimR
]
@=? readTemplateElems "$-foo-$"
2014-10-27 12:19:56 +00:00
]
]
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
test :: (Identifier, Identifier, Identifier) -> Assertion
test (outf, tplf, itemf) = do
2013-01-06 17:33:00 +00:00
store <- newTestStore
2012-11-19 13:59:55 +00:00
provider <- newTestProvider store
out <- resourceString provider outf
tpl <- testCompilerDone store provider tplf templateBodyCompiler
item <- testCompilerDone store provider itemf $
2012-12-15 17:02:47 +00:00
pandocCompiler >>= applyTemplate (itemBody tpl) testContext
2012-11-19 13:59:55 +00:00
out @=? itemBody item
2013-02-09 14:11:40 +00:00
cleanTestEnv
2012-11-19 14:12:53 +00:00
--------------------------------------------------------------------------------
testContext :: Context String
testContext = mconcat
2013-05-06 21:32:25 +00:00
[ defaultContext
, listField "authors" (bodyField "name") $ do
n1 <- makeItem "Jan"
n2 <- makeItem "Piet"
return [n1, n2]
2013-08-10 14:18:59 +00:00
, functionField "rev" $ \args _ -> return $ unwords $ map reverse args
2012-11-19 14:12:53 +00:00
]
--------------------------------------------------------------------------------
testApplyJoinTemplateList :: Assertion
testApplyJoinTemplateList = do
store <- newTestStore
provider <- newTestProvider store
str <- testCompilerDone store provider "item3" $
applyJoinTemplateList ", " tpl defaultContext [i1, i2]
str @?= "<b>Hello</b>, <b>World</b>"
2013-02-09 14:11:40 +00:00
cleanTestEnv
where
i1 = Item "item1" "Hello"
i2 = Item "item2" "World"
2016-07-24 15:30:47 +00:00
tpl = readTemplate "<b>$body$</b>"