1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Applicative ((<$>), (<*>))
import Data.Either
import Data.Function (on)
import Data.Monoid
import qualified Data.Text as T
import System.IO.Unsafe (unsafePerformIO)
import Test.Hspec
import Text.Mustache
import Text.Mustache.Compile
import Text.Mustache.Parser
import Text.Mustache.Types
escaped :: Bool
escaped = True
unescaped :: Bool
unescaped = False
parserSpec :: Spec
parserSpec =
describe "mustacheParser" $ do
let lparse = parse "testsuite"
let returnedOne = return . return
let text = "test12356p0--=-34{}jnv,\n"
it "parses text" $
lparse text `shouldBe` returnedOne (TextBlock text)
it "parses a variable" $
lparse "{{name}}" `shouldBe` returnedOne (Variable escaped (NamedData ["name"]))
it "parses a variable with whitespace" $
lparse "{{ name }}" `shouldBe` returnedOne (Variable escaped (NamedData ["name"]))
it "allows '-' in variable names" $
lparse "{{ name-name }}" `shouldBe`
returnedOne (Variable True (NamedData ["name-name"]))
it "allows '_' in variable names" $
lparse "{{ name_name }}" `shouldBe`
returnedOne (Variable True (NamedData ["name_name"]))
it "parses a variable unescaped with {{{}}}" $
lparse "{{{name}}}" `shouldBe` returnedOne (Variable unescaped (NamedData ["name"]))
it "parses a variable unescaped with {{{}}} with whitespace" $
lparse "{{{ name }}}" `shouldBe`
returnedOne (Variable False (NamedData ["name"]))
it "parses a variable unescaped with &" $
lparse "{{&name}}" `shouldBe` returnedOne (Variable unescaped (NamedData ["name"]))
it "parses a variable unescaped with & with whitespace" $
lparse "{{& name }}" `shouldBe`
returnedOne (Variable False (NamedData ["name"]))
it "parses a partial" $
lparse "{{>myPartial}}" `shouldBe`
returnedOne (Partial (Just "") "myPartial")
it "parses a partial with whitespace" $
lparse "{{> myPartial }}" `shouldBe`
returnedOne (Partial (Just "") "myPartial")
it "parses the an empty section" $
lparse "{{#section}}{{/section}}" `shouldBe`
returnedOne (Section (NamedData ["section"]) mempty)
it "parses the an empty section with whitespace" $
lparse "{{# section }}{{/ section }}" `shouldBe`
returnedOne (Section (NamedData ["section"]) mempty)
it "parses a delimiter change" $
lparse "{{=<< >>=}}<<var>>{{var}}" `shouldBe`
return [Variable True (NamedData ["var"]), TextBlock "{{var}}"]
it "parses a delimiter change with whitespace" $
lparse "{{=<< >>=}}<< var >>{{var}}" `shouldBe`
return [Variable True (NamedData ["var"]), TextBlock "{{var}}"]
it "parses two subsequent delimiter changes" $
lparse "{{=(( ))=}}(( var ))((=-- $-=))--#section$---/section$-" `shouldBe`
return [Variable True (NamedData ["var"]), Section (NamedData ["section"]) []]
it "propagates a delimiter change from a nested scope" $
lparse "{{#section}}{{=<< >>=}}<</section>><<var>>" `shouldBe`
return [Section (NamedData ["section"]) [], Variable escaped (NamedData ["var"])]
it "fails if the tag contains illegal characters" $
lparse "{{#&}}" `shouldSatisfy` isLeft
it "parses a nested variable" $
lparse "{{ name.val }}" `shouldBe` returnedOne (Variable escaped (NamedData ["name", "val"]))
it "parses a variable containing whitespace" $
lparse "{{ val space }}" `shouldBe` returnedOne (Variable escaped (NamedData ["val space"]))
substituteSpec :: Spec
substituteSpec =
describe "substitute" $ do
let toTemplate ast' = Template "testsuite" ast' mempty
it "substitutes a html escaped value for a variable" $
substitute
(toTemplate [Variable escaped (NamedData ["name"])])
(object ["name" ~> ("\" ' < > &" :: T.Text)])
`shouldBe` "" ' < > &"
it "substitutes raw value for an unescaped variable" $
substitute
(toTemplate [Variable unescaped (NamedData ["name"])])
(object ["name" ~> ("\" ' < > &" :: T.Text)])
`shouldBe` "\" ' < > &"
it "substitutes a section when the key is present (and an empty object)" $
substitute
(toTemplate [Section (NamedData ["section"]) [TextBlock "t"]])
(object ["section" ~> object []])
`shouldBe` "t"
it "substitutes a section when the key is present (and 'true')" $
substitute
(toTemplate [Section (NamedData ["section"]) [TextBlock "t"]])
(object ["section" ~> True])
`shouldBe` "t"
it "substitutes a section once when the key is present and a singleton list" $
substitute
(toTemplate [Section (NamedData ["section"]) [TextBlock "t"]])
(object ["section" ~> ["True" :: T.Text]])
`shouldBe` "t"
it "substitutes a section twice when the key is present and a list with two items" $
substitute
(toTemplate [Section (NamedData ["section"]) [TextBlock "t"]])
(object ["section" ~> (["True", "False"] :: [T.Text])])
`shouldBe` "tt"
it "substitutes a section twice when the key is present and a list with two\
\ objects, changing the scope to each object" $
substitute
(toTemplate [Section (NamedData ["section"]) [Variable escaped (NamedData ["t"])]])
(object
[ "section" ~>
[ object ["t" ~> ("var1" :: T.Text)]
, object ["t" ~> ("var2" :: T.Text)]
]
])
`shouldBe` "var1var2"
it "substitutes an inverse section when the key is present (and null)" $
substitute
(toTemplate [InvertedSection (NamedData ["section"]) [TextBlock "t"]])
(object ["section" ~> Null])
`shouldBe` "t"
it "does not substitute a section when the key is not present" $
substitute
(toTemplate [Section (NamedData ["section"]) [TextBlock "t"]])
(object [])
`shouldBe` ""
it "does not substitute a section when the key is present (and 'false')" $
substitute
(toTemplate [Section (NamedData ["section"]) [TextBlock "t"]])
(object ["section" ~> False])
`shouldBe` ""
it "does not substitute a section when the key is present (and null)" $
substitute
(toTemplate [Section (NamedData ["section"]) [TextBlock "t"]])
(object ["section" ~> Null])
`shouldBe` ""
it "does not substitute a section when the key is present (and empty list)" $
substitute
(toTemplate [Section (NamedData ["section"]) [TextBlock "t"]])
(object ["section" ~> ([] :: [T.Text])])
`shouldBe` ""
it "substitutes a lambda by applying lambda to contained text" $
substitute
(toTemplate [Section (NamedData ["lambda"]) [TextBlock "t"]])
(object ["lambda" ~> (overText T.toUpper)])
`shouldBe` "T"
it "substitutes a lambda by applying lambda to the nested substitution results" $
substitute
(toTemplate [Section (NamedData ["lambda"]) [TextBlock "t", Variable escaped (NamedData ["inner"])]])
(object [ "lambda" ~> (overText T.toUpper)
, "inner" ~> ("var" :: T.Text)
])
`shouldBe` "TVAR"
it "substitutes a lambda used directly as if applied to empty block" $
substitute
(toTemplate [Variable escaped (NamedData ["lambda"])])
(object ["lambda" ~> (Lambda $ \[] -> return [TextBlock "T"])])
`shouldBe` "T"
it "substitutes a nested section" $
substitute
(toTemplate [Variable escaped (NamedData ["outer", "inner"])])
(object
[ "outer" ~> object ["inner" ~> ("success" :: T.Text)]
, "inner" ~> ("error" :: T.Text)
]
)
`shouldBe` "success"
converterSpec :: Spec
converterSpec =
describe "toMustache" $
it "converts a String" $
toMustache ("My String" :: String) `shouldSatisfy` \case (String "My String") -> True; _ -> False
-- This is a one-off instance to define how we want the Spec to compare templates
instance Eq Template where
(==) = (==) `on` ast
compileTimeSpec :: Spec
compileTimeSpec =
describe "compileTimeCompiling" $ do
it "creates compiled templates from a QuasiQuoter" $
Right [mustache|This {{ template }} was injected at compile time with a quasiquoter|] `shouldBe`
compileTemplate "Template Name" "This {{ template }} was injected at compile time with a quasiquoter"
it "creates compiled templates from an embedded file" $
Right $(embedTemplate ["test/unit/examples"] "test-template.txt.mustache") `shouldBe`
compileTemplate "Template Name" "This {{ template }} was injected at compile time with an embedded file\n"
it "creates compiled templates from a single embedded file" $
Right $(embedSingleTemplate "test/unit/examples/test-template.txt.mustache") `shouldBe`
compileTemplate "Template Name" "This {{ template }} was injected at compile time with an embedded file\n"
it "creates compiled templates from an embedded file containing partials" $
Right $(embedTemplate ["test/unit/examples", "test/unit/examples/partials"] "test-template-partials.txt.mustache") `shouldBe`
unsafePerformIO (automaticCompile ["test/unit/examples", "test/unit/examples/partials"] "test-template-partials.txt.mustache")
main :: IO ()
main = hspec $ do
parserSpec
substituteSpec
converterSpec
compileTimeSpec
|