| 1 |
-------------------------------------------------------------------------------- |
| 2 |
{-# LANGUAGE OverloadedStrings #-} |
| 3 |
{-# LANGUAGE TemplateHaskell #-} |
| 4 |
module Hakyll.Web.Template.Tests |
| 5 |
( tests |
| 6 |
) where |
| 7 |
|
| 8 |
|
| 9 |
-------------------------------------------------------------------------------- |
| 10 |
import Test.Tasty (TestTree, testGroup) |
| 11 |
import Test.Tasty.HUnit (Assertion, assertBool, testCase, |
| 12 |
(@=?), (@?=)) |
| 13 |
|
| 14 |
import Data.Either (isLeft) |
| 15 |
import System.IO (nativeNewline, Newline(..)) |
| 16 |
|
| 17 |
-------------------------------------------------------------------------------- |
| 18 |
import Hakyll.Core.Compiler |
| 19 |
import Hakyll.Core.Identifier |
| 20 |
import Hakyll.Core.Item |
| 21 |
import Hakyll.Core.Provider |
| 22 |
import Hakyll.Web.Template |
| 23 |
import Hakyll.Web.Template.Context |
| 24 |
import Hakyll.Web.Template.Internal |
| 25 |
import Hakyll.Web.Template.List |
| 26 |
import TestSuite.Util |
| 27 |
|
| 28 |
|
| 29 |
-------------------------------------------------------------------------------- |
| 30 |
tests :: TestTree |
| 31 |
tests = testGroup "Hakyll.Web.Template.Tests" $ concat |
| 32 |
[ [ testCase "case01" $ test ("template.html.out", "template.html", "example.md") |
| 33 |
, testCase "case02" $ test ("strip.html.out", "strip.html", "example.md") |
| 34 |
, testCase "case03" $ test ("just-meta.html.out", "just-meta.html", "example.md") |
| 35 |
, testCase "applyJoinTemplateList" testApplyJoinTemplateList |
| 36 |
] |
| 37 |
|
| 38 |
, fromAssertions "parseTemplate" |
| 39 |
[ Right [Chunk "Hello ", Expr (Call "guest" [])] |
| 40 |
@=? parse "Hello $guest()$" |
| 41 |
, Right [If (Call "a" [StringLiteral "bar"]) [Chunk "foo"] Nothing] |
| 42 |
@=? parse "$if(a(\"bar\"))$foo$endif$" |
| 43 |
-- 'If' trim check. |
| 44 |
, Right [ TrimL |
| 45 |
, If (Ident (TemplateKey "body")) |
| 46 |
[ TrimR |
| 47 |
, Chunk "\n" |
| 48 |
, Expr (Ident (TemplateKey "body")) |
| 49 |
, Chunk "\n" |
| 50 |
, TrimL |
| 51 |
] |
| 52 |
(Just [ TrimR |
| 53 |
, Chunk "\n" |
| 54 |
, Expr (Ident (TemplateKey "body")) |
| 55 |
, Chunk "\n" |
| 56 |
, TrimL |
| 57 |
]) |
| 58 |
, TrimR |
| 59 |
] |
| 60 |
@=? parse "$-if(body)-$\n$body$\n$-else-$\n$body$\n$-endif-$" |
| 61 |
-- 'For' trim check. |
| 62 |
, Right [ TrimL |
| 63 |
, For (Ident (TemplateKey "authors")) |
| 64 |
[TrimR, Chunk "\n body \n", TrimL] |
| 65 |
Nothing |
| 66 |
, TrimR |
| 67 |
] |
| 68 |
@=? parse "$-for(authors)-$\n body \n$-endfor-$" |
| 69 |
-- 'Partial' trim check. |
| 70 |
, Right [ TrimL |
| 71 |
, Partial (StringLiteral "path") |
| 72 |
, TrimR |
| 73 |
] |
| 74 |
@=? parse "$-partial(\"path\")-$" |
| 75 |
-- 'Expr' trim check. |
| 76 |
, Right [ TrimL |
| 77 |
, Expr (Ident (TemplateKey "foo")) |
| 78 |
, TrimR |
| 79 |
] |
| 80 |
@=? parse "$-foo-$" |
| 81 |
-- fail on incomplete template. |
| 82 |
, assertBool "did not yield error" $ isLeft $ |
| 83 |
parse "a$b" |
| 84 |
-- fail on mismatched template syntax. |
| 85 |
, assertBool "did not fail to parse" $ isLeft $ |
| 86 |
parse "$for(xs)$\n <p>foo</p>\n$endif$" |
| 87 |
] |
| 88 |
|
| 89 |
, [testCase "embeddedTemplate" testEmbeddedTemplate] |
| 90 |
] |
| 91 |
where |
| 92 |
parse = parseTemplateElemsFile "" |
| 93 |
|
| 94 |
|
| 95 |
-------------------------------------------------------------------------------- |
| 96 |
test :: (Identifier, Identifier, Identifier) -> Assertion |
| 97 |
test (outf, tplf, itemf) = do |
| 98 |
store <- newTestStore |
| 99 |
provider <- newTestProvider store |
| 100 |
|
| 101 |
out <- resourceString provider outf |
| 102 |
tpl <- testCompilerDone store provider tplf templateBodyCompiler |
| 103 |
item <- testCompilerDone store provider itemf $ |
| 104 |
getResourceBody |
| 105 |
>>= renderParagraphs |
| 106 |
>>= applyTemplate (itemBody tpl) testContext |
| 107 |
|
| 108 |
out @=? itemBody item |
| 109 |
cleanTestEnv |
| 110 |
|
| 111 |
|
| 112 |
-------------------------------------------------------------------------------- |
| 113 |
testContext :: Context String |
| 114 |
testContext = mconcat |
| 115 |
[ defaultContext |
| 116 |
, listField "authors" (bodyField "name") $ do |
| 117 |
n1 <- makeItem "Jan" |
| 118 |
n2 <- makeItem "Piet" |
| 119 |
return [n1, n2] |
| 120 |
, functionField "rev" $ \args _ -> return $ unwords $ map reverse args |
| 121 |
] |
| 122 |
|
| 123 |
|
| 124 |
-------------------------------------------------------------------------------- |
| 125 |
testApplyJoinTemplateList :: Assertion |
| 126 |
testApplyJoinTemplateList = do |
| 127 |
store <- newTestStore |
| 128 |
provider <- newTestProvider store |
| 129 |
tpl <- testCompilerDone store provider "tpl" $ |
| 130 |
compileTemplateItem (Item "tpl" "<b>$body$</b>") |
| 131 |
str <- testCompilerDone store provider "item3" $ |
| 132 |
applyJoinTemplateList ", " tpl defaultContext [i1, i2] |
| 133 |
|
| 134 |
str @?= "<b>Hello</b>, <b>World</b>" |
| 135 |
cleanTestEnv |
| 136 |
where |
| 137 |
i1 = Item "item1" "Hello" |
| 138 |
i2 = Item "item2" "World" |
| 139 |
|
| 140 |
|
| 141 |
-------------------------------------------------------------------------------- |
| 142 |
embeddedTemplate :: Template |
| 143 |
embeddedTemplate = $(embedTemplate "tests/data/embed.html") |
| 144 |
|
| 145 |
-------------------------------------------------------------------------------- |
| 146 |
testEmbeddedTemplate :: Assertion |
| 147 |
testEmbeddedTemplate = do |
| 148 |
store <- newTestStore |
| 149 |
provider <- newTestProvider store |
| 150 |
str <- testCompilerDone store provider "item3" $ |
| 151 |
applyTemplate embeddedTemplate defaultContext item |
| 152 |
|
| 153 |
itemBody str @?= ("<p>Hello, world</p>" ++ (newline nativeNewline)) |
| 154 |
cleanTestEnv |
| 155 |
where |
| 156 |
item = Item "item1" "Hello, world" |
| 157 |
|
| 158 |
newline LF = "\n" |
| 159 |
newline CRLF = "\r\n" |
| 160 |
|