forked from snapframework/heist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tests.hs
574 lines (450 loc) · 18.6 KB
/
Tests.hs
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Text.Templating.Heist.Tests
( tests
, quickRender
) where
------------------------------------------------------------------------------
import Control.Monad.State
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as L
import qualified Data.Map as Map
import Data.Maybe
import Data.Monoid
import System.IO.Unsafe
import Test.Framework (Test)
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
import qualified Test.HUnit as H
import Test.QuickCheck
import Test.QuickCheck.Monadic
------------------------------------------------------------------------------
import Text.Templating.Heist
import Text.Templating.Heist.Internal
import Text.Templating.Heist.Types
import Text.Templating.Heist.Splices.Apply
import Text.Templating.Heist.Splices.Ignore
import Text.Templating.Heist.Splices.Markdown
import Text.XML.Expat.Cursor
import Text.XML.Expat.Format
import qualified Text.XML.Expat.Tree as X
------------------------------------------------------------------------------
tests :: [Test]
tests = [ testProperty "heist/simpleBind" simpleBindTest
, testProperty "heist/simpleApply" simpleApplyTest
, testCase "heist/stateMonoid" monoidTest
, testCase "heist/templateAdd" addTest
, testCase "heist/hasTemplate" hasTemplateTest
, testCase "heist/getDoc" getDocTest
, testCase "heist/load" loadTest
, testCase "heist/fsLoad" fsLoadTest
, testCase "heist/renderNoName" renderNoNameTest
, testCase "heist/doctype" doctypeTest
, testCase "heist/attributeSubstitution" attrSubstTest
, testCase "heist/bindAttribute" bindAttrTest
, testCase "heist/markdown" markdownTest
, testCase "heist/markdownText" markdownTextTest
, testCase "heist/apply" applyTest
, testCase "heist/ignore" ignoreTest
]
------------------------------------------------------------------------------
simpleBindTest :: Property
simpleBindTest = monadicIO $ forAllM arbitrary prop
where
prop :: Bind -> PropertyM IO ()
prop bind = do
let template = buildBindTemplate bind
let result = buildResult bind
spliceResult <- run $ evalTemplateMonad (runNodeList template)
(X.Text "")
emptyTemplateState
assert $ result == spliceResult
------------------------------------------------------------------------------
simpleApplyTest :: Property
simpleApplyTest = monadicIO $ forAllM arbitrary prop
where
prop :: Apply -> PropertyM IO ()
prop apply = do
let correct = calcCorrect apply
result <- run $ calcResult apply
assert $ correct == result
------------------------------------------------------------------------------
monoidTest :: IO ()
monoidTest = do
H.assertBool "left monoid identity" $ mempty `mappend` es == es
H.assertBool "right monoid identity" $ es `mappend` mempty == es
where es = emptyTemplateState :: TemplateState IO
------------------------------------------------------------------------------
addTest :: IO ()
addTest = do
H.assertEqual "lookup test" (Just []) $
fmap (_itNodes . fst) $ lookupTemplate "aoeu" ts
H.assertEqual "splice touched" 0 $ Map.size (_spliceMap ts)
where
ts = addTemplate "aoeu" [] (mempty::TemplateState IO)
------------------------------------------------------------------------------
hasTemplateTest :: H.Assertion
hasTemplateTest = do
ets <- loadT "templates"
let tm = either (error "Error loading templates") _templateMap ets
let ts = setTemplates tm emptyTemplateState :: TemplateState IO
H.assertBool "hasTemplate ts" (hasTemplate "index" ts)
------------------------------------------------------------------------------
getDocTest :: H.Assertion
getDocTest = do
d <- getDoc "bkteoar"
H.assertBool "non-existent doc" $ isLeft d
f <- getDoc "templates/index.tpl"
H.assertBool "index doc" $ not $ isLeft f
------------------------------------------------------------------------------
loadTest :: H.Assertion
loadTest = do
ets <- loadT "templates"
either (error "Error loading templates")
(\ts -> do let tm = _templateMap ts
H.assertBool "loadTest size" $ Map.size tm == 17
) ets
------------------------------------------------------------------------------
fsLoadTest :: H.Assertion
fsLoadTest = do
ets <- loadT "templates"
let tm = either (error "Error loading templates") _templateMap ets
let ts = setTemplates tm emptyTemplateState :: TemplateState IO
let f = g ts
f isNothing "abc/def/xyz"
f isJust "a"
f isJust "bar/a"
f isJust "/bar/a"
where
g ts p n = H.assertBool ("loading template " ++ n) $ p $
lookupTemplate (B.pack n) ts
------------------------------------------------------------------------------
renderNoNameTest :: H.Assertion
renderNoNameTest = do
ets <- loadT "templates"
either (error "Error loading templates")
(\ts -> do t <- renderTemplate ts ""
H.assertBool "renderNoName" $ t == Nothing
) ets
------------------------------------------------------------------------------
doctypeTest :: H.Assertion
doctypeTest = do
ets <- loadT "templates"
let ts = either (error "Error loading templates") id ets
index <- renderTemplate ts "index"
H.assertBool "doctype test index" $ hasDoctype $ fromJust index
ioc <- renderTemplate ts "ioc"
H.assertBool "doctype test ioc" $ hasDoctype $ fromJust ioc
------------------------------------------------------------------------------
attrSubstTest :: H.Assertion
attrSubstTest = do
ets <- loadT "templates"
let ts = either (error "Error loading templates") id ets
check (setTs "meaning_of_everything" ts) "pre_meaning_of_everything_post"
check ts "pre__post"
where
setTs val = bindSplice "foo" (return [X.Text val])
check ts str = do
res <- renderTemplate ts "attrs"
H.assertBool ("attr subst " ++ (show str)) $
not $ B.null $ snd $ B.breakSubstring str $ fromJust res
H.assertBool ("attr subst foo") $
not $ B.null $ snd $ B.breakSubstring "$(foo)" $ fromJust res
------------------------------------------------------------------------------
bindAttrTest :: H.Assertion
bindAttrTest = do
ets <- loadT "templates"
let ts = either (error "Error loading templates") id ets
check ts "<div id=\"zzzzz\""
where
check ts str = do
res <- renderTemplate ts "bind-attrs"
H.assertBool ("attr subst " ++ (show str)) $
not $ B.null $ snd $ B.breakSubstring str $ fromJust res
H.assertBool ("attr subst bar") $
B.null $ snd $ B.breakSubstring "$(bar)" $ fromJust res
------------------------------------------------------------------------------
htmlExpected :: ByteString
htmlExpected = "<div class=\"markdown\"><p>This <em>is</em> a test.</p></div>"
------------------------------------------------------------------------------
-- | Markdown test on a file
markdownTest :: H.Assertion
markdownTest = do
ets <- loadT "templates"
let ts = either (error "Error loading templates") id ets
check ts htmlExpected
where
check ts str = do
result <- liftM (fmap $ B.filter (/= '\n')) $
renderTemplate ts "markdown"
H.assertEqual ("Should match " ++ (show str)) str (fromJust result)
------------------------------------------------------------------------------
-- | Markdown test on supplied text
markdownTextTest :: H.Assertion
markdownTextTest = do
result <- evalTemplateMonad markdownSplice
(X.Text "This *is* a test.") emptyTemplateState
H.assertEqual "Markdown text" htmlExpected
(B.filter (/= '\n') $ formatList' result)
------------------------------------------------------------------------------
applyTest :: H.Assertion
applyTest = do
let es = emptyTemplateState :: TemplateState IO
res <- evalTemplateMonad applyImpl
(X.Element "apply" [("template", "nonexistant")] []) es
H.assertEqual "apply nothing" [] res
------------------------------------------------------------------------------
ignoreTest :: H.Assertion
ignoreTest = do
let es = emptyTemplateState :: TemplateState IO
res <- evalTemplateMonad ignoreImpl
(X.Element "ignore" [("tag", "ignorable")]
[X.Text "This should be ignored"]) es
H.assertEqual "<ignore> tag" [] res
------------------------------------------------------------------------------
-- Utility functions
isLeft :: Either a b -> Bool
isLeft (Left _) = True
isLeft (Right _) = False
------------------------------------------------------------------------------
loadT :: String -> IO (Either String (TemplateState IO))
loadT s = loadTemplates s emptyTemplateState
------------------------------------------------------------------------------
loadTS :: FilePath -> IO (TemplateState IO)
loadTS baseDir = do
etm <- loadTemplates baseDir emptyTemplateState
return $ either error id etm
------------------------------------------------------------------------------
identStartChar :: [Char]
identStartChar = ['a'..'z']
------------------------------------------------------------------------------
identChar :: [Char]
identChar = '_' : identStartChar
------------------------------------------------------------------------------
textGen :: Gen [Char]
textGen = listOf $ elements ((replicate 5 ' ') ++ identStartChar)
------------------------------------------------------------------------------
limitedDepth :: Int -> Gen Node
limitedDepth 0 = liftM (X.Text . B.pack) textGen
limitedDepth n =
oneof [ liftM (X.Text . B.pack) textGen
, liftM3 X.Element arbitrary
(liftM (take 2) arbitrary)
(liftM (take 3) $ listOf $ limitedDepth (n - 1))
]
------------------------------------------------------------------------------
-- | Returns the number of unique insertion points in the tree.
-- If h = insertAt f n g", the following property holds:
-- insSize h == (insSize f) + (insSize g) - 1
insSize :: [X.Node tag text] -> Int
insSize ns = 1 + (sum $ map nodeSize ns)
where nodeSize (X.Text _) = 1
nodeSize (X.Element _ _ c) = 1 + (insSize c)
------------------------------------------------------------------------------
insertAt :: [Node] -> Int -> [Node] -> [Node]
insertAt elems 0 ns = elems ++ ns
insertAt elems _ [] = elems
insertAt elems n list = maybe [] (toForest . root) $
evalState (processNode elems $ fromJust $ fromForest list) n
------------------------------------------------------------------------------
move :: Insert ()
move = modify (\x -> x - 1)
------------------------------------------------------------------------------
processNode :: [Node] -> Loc -> Insert (Maybe Loc)
processNode elems loc =
liftM2 mplus (move >> goDown loc) (move >> goRight loc)
where
goDown l =
case current l of
X.Text _ -> modify (+1) >> return Nothing
X.Element _ _ _ -> doneCheck (insertManyFirstChild elems)
firstChild
l
goRight = doneCheck (Just . insertManyRight elems) right
doneCheck insertFunc next l = do
s <- get
if s == 0
then return $ insertFunc l
else maybe (return Nothing) (processNode elems) $ next l
------------------------------------------------------------------------------
-- | Reloads the templates from disk and renders the specified
-- template. (Old convenience code.)
quickRender :: FilePath -> ByteString -> IO (Maybe ByteString)
quickRender baseDir name = do
ts <- loadTS baseDir
renderTemplate ts name
------------------------------------------------------------------------------
newtype Name = Name { unName :: B.ByteString } deriving (Show)
instance Arbitrary Name where
arbitrary = do
x <- elements identStartChar
n <- choose (4,10)
rest <- vectorOf n $ elements identChar
return $ Name $ B.pack (x:rest)
instance Arbitrary Node where
arbitrary = limitedDepth 3
shrink (X.Text _) = []
shrink (X.Element _ [] []) = []
shrink (X.Element n [] (_:cs)) = [X.Element n [] cs]
shrink (X.Element n (_:as) []) = [X.Element n as []]
shrink (X.Element n as cs) = [X.Element n as (tail cs), X.Element n (tail as) cs]
instance Arbitrary B.ByteString where
arbitrary = liftM unName arbitrary
--
-- Code for inserting nodes into any point of a tree
--
type Loc = Cursor B.ByteString B.ByteString
type Insert a = State Int a
------------------------------------------------------------------------------
-- <bind> tests
-- Data type encapsulating the parameters for a bind operation
data Bind = Bind
{ _bindElemName :: Name
, _bindChildren :: [Node]
, _bindDoc :: [Node]
, _bindPos :: Int
, _bindRefPos :: Int
} -- deriving (Show)
instance Arbitrary Bind where
arbitrary = do
name <- arbitrary
kids <- liftM (take 3) arbitrary
doc <- liftM (take 5) arbitrary
let s = insSize doc
loc <- choose (0, s - 1)
loc2 <- choose (0, s - loc - 1)
return $ Bind name kids doc loc loc2
shrink (Bind e [c] (_:ds) p r) = [Bind e [c] ds p r]
shrink (Bind e (_:cs) d p r) = [Bind e cs d p r]
shrink _ = []
instance Show Bind where
show b@(Bind e c d p r) = unlines
[ "\n"
, "Bind element name: " ++ (show e)
, "Bind pos: " ++ (show p)
, "Bind ref pos: " ++ (show r)
, "Bind document:"
, L.unpack $ L.concat $ map formatNode d
, "Bind children:"
, L.unpack $ L.concat $ map formatNode c
, "Result:"
, L.unpack $ L.concat $ map formatNode $ buildResult b
, "Splice result:"
, L.unpack $ L.concat $ map formatNode $ unsafePerformIO $
evalTemplateMonad (runNodeList $ buildBindTemplate b)
(X.Text "") emptyTemplateState
, "Template:"
, L.unpack $ L.concat $ map formatNode $ buildBindTemplate b
]
------------------------------------------------------------------------------
buildNode :: B.ByteString -> B.ByteString -> Bind -> Node
buildNode tag attr (Bind s c _ _ _) = X.Element tag [(attr, unName s)] c
------------------------------------------------------------------------------
buildBind :: Bind -> Node
buildBind = buildNode "bind" "tag"
------------------------------------------------------------------------------
empty :: tag -> X.Node tag text
empty n = X.Element n [] []
------------------------------------------------------------------------------
buildBindTemplate :: Bind -> [Node]
buildBindTemplate s@(Bind n _ d b r) =
insertAt [empty $ unName $ n] pos $ withBind
where bind = [buildBind s]
bindSize = insSize bind
withBind = insertAt bind b d
pos = b + bindSize - 1 + r
------------------------------------------------------------------------------
buildResult :: Bind -> [Node]
buildResult (Bind _ c d b r) = insertAt c (b + r) d
------------------------------------------------------------------------------
-- <apply> tests
data Apply = Apply
{ _applyName :: Name
, _applyCaller :: [Node]
, _applyCallee :: Template
, _applyChildren :: [Node]
, _applyPos :: Int
} deriving (Show)
instance Arbitrary Apply where
arbitrary = do
name <- arbitrary
kids <- liftM (take 3) $ listOf $ limitedDepth 2
caller <- liftM (take 5) arbitrary
callee <- liftM (take 1) $ listOf $ limitedDepth 3
let s = insSize caller
loc <- choose (0, s - 1)
return $ Apply name caller callee kids loc
------------------------------------------------------------------------------
buildApplyCaller :: Apply -> [Node]
buildApplyCaller (Apply name caller _ kids pos) =
insertAt [X.Element "apply" [("template", unName name)] kids] pos caller
------------------------------------------------------------------------------
calcCorrect :: Apply -> [Node]
calcCorrect (Apply _ caller callee _ pos) = insertAt callee pos caller
------------------------------------------------------------------------------
calcResult :: (MonadIO m) => Apply -> m [Node]
calcResult apply@(Apply name _ callee _ _) =
evalTemplateMonad (runNodeList $ buildApplyCaller apply)
(X.Text "") ts
where ts = setTemplates (Map.singleton [unName name]
(InternalTemplate Nothing callee))
emptyTemplateState
{-
-- The beginning of some future tests for hook functions.
p :: ByteString -> Node
p t = X.Element "p" [] [X.Text t]
hookG :: Monad m => ByteString -> Template -> m Template
hookG str t = return $ (p str) : t
onLoad = hookG "Inserted on load"
preRun = hookG "Inserted on preRun"
postRun = hookG "Inserted on postRun"
ts :: IO (Either String (TemplateState IO))
ts = loadTemplates "test/templates" $
foldr ($) emptyTemplateState
[setOnLoadHook onLoad
,setPreRunHook preRun
,setPostRunHook postRun
]
r name etm = do
let ts = either (error "Danger Will Robinson!") id etm
ns <- runNodeList ts name
return $ (Just . formatList') =<< ns
-}
{-
- Convenience code for manual ghci experimentation
-}
--html :: [Node] -> Node
--html c = X.Element "html" [] [hhead, body c]
--hhead :: Node
--hhead = X.Element "head" [] [title, X.Element "script" [] []]
--title :: Node
--title = X.Element "title" [] [X.Text "Test Page"]
--body :: [Node] -> Node
--body = X.Element "body" []
--
--para :: Int -> Node
--para n = X.Element "p" [] [X.Text $ B.pack $ "This is paragraph " ++ show n]
--para2 :: B.ByteString -> Node
--para2 c = X.Element "p" [] [X.Text c]
--para3 :: Node
--para3 = X.Element "p" [] [X.Text "AHA!"]
--
--foo :: Int -> [Node]
--foo n = insertAt [X.Element "NEW" [] []] n [html [para 1, para 2]]
--
--tdoc :: [Node]
--tdoc = [para 1, para 2, para 3, para 4]
--
--bindElem :: [Node] -> Int -> Int -> Bind
--bindElem = Bind (Name "mytag") [para2 "bound paragraph"]
--
--addBind :: Bind -> [Node] -> [Node]
--addBind b = insertAt [buildBind b] 0 . insertAt [empty $ unName $ _bindElemName b] 2
--
--prn :: Node -> IO ()
--prn = L.putStrLn . formatNode
--runTests :: IO ()
--runTests = defaultMain tests