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
|
module Template
( templateGroup
) where
import qualified Data.Map as M
import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.Binary
import Test.Framework (testGroup)
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
import Test.HUnit
import Test.QuickCheck
import Text.Hakyll.Internal.Template
-- Template test group.
templateGroup = testGroup "Template"
[ testProperty "prop_template_encode_id" prop_template_encode_id
, testProperty "prop_substitute_id" prop_substitute_id
, testCase "test_substitute_1" test_substitute_1
, testCase "test_substitute_2" test_substitute_2
]
-- | Generate arbitrary templates from a given length.
arbitraryTemplate :: Int -> Gen Template
arbitraryTemplate 0 = return End
arbitraryTemplate length' = oneof [ do chunk <- chunk'
Chunk chunk <$> template'
, do key <- key'
Identifier key <$> template'
, EscapeCharacter <$> template'
]
where
template' = arbitraryTemplate (length' - 1)
-- Generate keys.
key' = do l <- choose (5, 10)
replicateM l $ choose ('a', 'z')
-- Generate non-empty chunks.
chunk' = do string <- arbitrary
let sanitized = filter (/= '$') string
return $ if null sanitized then "foo"
else sanitized
-- | Make @Template@ testable.
instance Arbitrary Template where
arbitrary = choose (0, 20) >>= arbitraryTemplate
shrink (Chunk chunk template) = [template, Chunk chunk End]
shrink (Identifier key template) = [template, Identifier key End]
shrink (EscapeCharacter template) = [template, EscapeCharacter End]
shrink End = []
-- Test encoding/decoding of templates.
prop_template_encode_id :: Template -> Bool
prop_template_encode_id template = decode (encode template) == template
-- Check we get the same sting with empty substitutions.
prop_substitute_id string =
regularSubstitute (fromString string) M.empty == string
-- substitute test case 1.
test_substitute_1 =
finalSubstitute template context @?= "Banana costs $4."
where
template = fromString "$product costs $$$price."
context = M.fromList [("product", "Banana"), ("price", "4")]
-- substitute test case 2.
test_substitute_2 =
regularSubstitute template context @?= "$$root is a special key."
where
template = fromString "$$root is a special $thing."
context = M.fromList [("root", "foo"), ("thing", "key")]
|