on master help sign in
Changed test system. Wrote some test cases.
commitd97a53b6b490bc997cfee6d2f16ab7cba9499347 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSun 24 Jan 2010, 19:00 (17 years ago) parent42bacee
6 files changed+114−147
hakyll.cabal+6−5 src/Text/Hakyll/Internal/Template.hs+37−2 tests/Main.hs+23 tests/Template.hs+25 tests/Tests.hs−140 tests/Util.hs+23
hakyll.cabal+6−5
@@ -34,7 +34,8 @@ library
3434 mtl >= 1.1,
3535 old-locale >= 1,
3636 time >= 1,
37− binary >= 0.5
37+ binary >= 0.5,
38+ QuickCheck >= 2
3839 exposed-modules: Network.Hakyll.SimpleServer
3940 Text.Hakyll
4041 Text.Hakyll.Context
@@ -47,7 +48,7 @@ library
4748 Text.Hakyll.Page
4849 Text.Hakyll.Util
4950 Text.Hakyll.Tags
50− other-modules: Text.Hakyll.Internal.Cache
51− Text.Hakyll.Internal.CompressCSS
52− Text.Hakyll.Internal.Render
53− Text.Hakyll.Internal.Template
51+ Text.Hakyll.Internal.Cache
52+ Text.Hakyll.Internal.CompressCSS
53+ Text.Hakyll.Internal.Render
54+ Text.Hakyll.Internal.Template
src/Text/Hakyll/Internal/Template.hs+37−2
@@ -11,21 +11,25 @@ import qualified Data.Map as M
1111import Data.List (isPrefixOf)
1212import Data.Char (isAlphaNum)
1313import Data.Binary
14−import Control.Monad (liftM, liftM2)
14+import Control.Monad (liftM, liftM2, replicateM)
1515import Data.Maybe (fromMaybe)
1616import System.FilePath ((</>))
1717import Control.Monad.Reader (liftIO)
1818
19+import Test.QuickCheck
20+
1921import Text.Hakyll.Hakyll (Hakyll)
2022import Text.Hakyll.Context (Context)
2123import Text.Hakyll.Internal.Cache
2224
25+-- | Datatype used for template substitutions.
2326data Template = Chunk String Template
2427 | Identifier String Template
2528 | EscapeCharacter Template
2629 | End
27− deriving (Show, Read)
30+ deriving (Show, Read, Eq)
2831
32+-- | Construct a "Template" from a string.
2933fromString :: String -> Template
3034fromString [] = End
3135fromString string
@@ -37,6 +41,8 @@ fromString string
3741 where
3842 tail' = tail string
3943
44+-- | Read a "Template" from a file. This function might fetch the "Template"
45+-- from the cache, if available.
4046readTemplate :: FilePath -> Hakyll Template
4147readTemplate path = do
4248 isCacheMoreRecent' <- isCacheMoreRecent fileName [path]
@@ -84,3 +90,32 @@ instance Binary Template where
8490 2 -> liftM EscapeCharacter get
8591 3 -> return End
8692 _ -> error "Error reading template"
93+
94+-- | Generate arbitrary templates from a given length.
95+arbitraryTemplate :: Int -> Gen Template
96+arbitraryTemplate 0 = return End
97+arbitraryTemplate length' = oneof [ do chunk <- chunk'
98+ template' >>= return . Chunk chunk
99+ , do key <- key'
100+ template' >>= return . Identifier key
101+ , template' >>= return . EscapeCharacter
102+ ]
103+ where
104+ template' = arbitraryTemplate (length' - 1)
105+ -- Generate keys.
106+ key' = do l <- choose (5, 10)
107+ replicateM l $ choose ('a', 'z')
108+ -- Generate non-empty chunks.
109+ chunk' = do string <- arbitrary
110+ let sanitized = filter (/= '$') string
111+ return $ if null sanitized then "foo"
112+ else sanitized
113+
114+-- | Make "Template" testable.
115+instance Arbitrary Template where
116+ arbitrary = choose (0, 20) >>= arbitraryTemplate
117+
118+ shrink (Chunk chunk template) = [template, Chunk chunk End]
119+ shrink (Identifier key template) = [template, Identifier key End]
120+ shrink (EscapeCharacter template) = [template, EscapeCharacter End]
121+ shrink End = []
tests/Main.hsnew file+23
@@ -0,0 +1,23 @@
1+module Main where
2+
3+import Control.Monad (mapM_)
4+import Test.QuickCheck
5+
6+import Template
7+import Util
8+
9+main = do
10+ runTests "Template" $ do
11+ quickCheck prop_template_encode_id
12+ quickCheck prop_substitute_id
13+ quickCheck prop_substitute_case1
14+
15+ runTests "Util" $ do
16+ quickCheck prop_trim_length
17+ quickCheck prop_trim_id
18+ quickCheck prop_stripHTML_length
19+ quickCheck prop_stripHTML_id
20+
21+ where
22+ runTests name action = do putStrLn name
23+ action
tests/Template.hsnew file+25
@@ -0,0 +1,25 @@
1+module Template where
2+
3+import qualified Data.Map as M
4+
5+import Test.QuickCheck
6+import Data.Binary
7+
8+import Text.Hakyll.Internal.Template
9+
10+-- Test encoding/decoding of templates.
11+prop_template_encode_id :: Template -> Bool
12+prop_template_encode_id template = decode (encode template) == template
13+
14+-- Check we get the same sting with empty substitutions.
15+prop_substitute_id string =
16+ regularSubstitute (fromString string) M.empty == string
17+
18+-- substitute test case 1.
19+prop_substitute_case1 string1 string2 =
20+ finalSubstitute template context == string1 ++ " costs $" ++ string2 ++ "."
21+ where
22+ template = fromString "$product costs $$$price."
23+ context = M.fromList [ ("product", string1)
24+ , ("price", string2)
25+ ]
tests/Tests.hsdeleted−140
@@ -1,140 +0,0 @@
1−import Data.Char
2−import qualified Data.Map as M
3−
4−import Test.Framework (defaultMain, testGroup)
5−import Test.Framework.Providers.QuickCheck2
6−import Test.Framework.Providers.HUnit
7−import Test.QuickCheck
8−import Test.HUnit
9−
10−import Text.Hakyll.CompressCSS
11−import Text.Hakyll.Util
12−import Text.Hakyll.Regex
13−import Text.Hakyll.Context
14−import Text.Hakyll.File
15−
16−main = defaultMain tests
17−
18−tests = [ testGroup "Util group"
19− [ testProperty "trim length" prop_trim_length
20− -- , testProperty "trim id" prop_trim_id
21− -- , testProperty "trim empty" prop_trim_empty
22− , testCase "stripHTML 1" test_strip_html1
23− , testCase "stripHTML 2" test_strip_html2
24− , testCase "stripHTML 3" test_strip_html3
25− , testCase "link 1" test_link1
26− , testCase "link 2" test_link2
27− ]
28−
29− , testGroup "Regex group"
30− [ testCase "splitRegex 1" test_split_regex1
31− , testCase "splitRegex 2" test_split_regex2
32− ]
33−
34− , testGroup "CompressCSS group"
35− [ testProperty "compressCSS length" prop_compress_css_length
36− , testCase "compressCSS 1" test_compress_css1
37− , testCase "compressCSS 2" test_compress_css2
38− , testCase "compressCSS 3" test_compress_css3
39− , testCase "compressCSS 4" test_compress_css4
40− ]
41−
42− , testGroup "Context group"
43− [ testCase "renderDate 1" test_render_date1
44− , testCase "renderDate 2" test_render_date1
45− , testCase "changeExtension 1" test_change_extension1
46− ]
47−
48− , testGroup "File group"
49− [ testCase "toRoot 1" test_to_root1
50− , testCase "toRoot 2" test_to_root2
51− , testCase "toRoot 3" test_to_root3
52− , testCase "removeSpaces 1" test_remove_spaces1
53− , testCase "removeSpaces 2" test_remove_spaces2
54− -- , testProperty "havingExtension count" prop_having_extension_count
55− , testCase "havingExtension 1" test_having_extension1
56− , testCase "havingExtension 2" test_having_extension2
57− ]
58− ]
59−
60−-- Test that a string always becomes shorter when trimmed.
61−prop_trim_length str = length str >= length (trim str)
62−
63−-- Check that a string which does not start or end with a space is not trimmed.
64−prop_trim_id str = isAlreadyTrimmed ==> str == (trim str)
65− where
66− isAlreadyTrimmed :: Bool
67− isAlreadyTrimmed = (not $ isSpace $ head str) && (not $ isSpace $ last str)
68−
69−-- An string of only spaces should be reduced to an empty string.
70−prop_trim_empty str = (all isSpace str) ==> null (trim str)
71−
72−-- Strip HTML test cases.
73−test_strip_html1 = stripHTML "<b>text</b>" @?= "text"
74−test_strip_html2 = stripHTML "text" @?= "text"
75−test_strip_html3 =
76− stripHTML "<b>Hakyll</b>, a <i>website</i> generator<img src=\"foo.png\" />"
77− @?= "Hakyll, a website generator"
78−
79−-- Link test cases.
80−test_link1 = link "foo bar" "/foo/bar.html"
81− @?= "<a href=\"/foo/bar.html\">foo bar</a>"
82−test_link2 = link "back home" "/" @?= "<a href=\"/\">back home</a>"
83−
84−-- Split Regex test cases.
85−test_split_regex1 = splitRegex "," "1,2,3" @?= ["1", "2", "3"]
86−test_split_regex2 = splitRegex "," ",1,2," @?= ["1", "2"]
87−
88−-- CSS compression should always decrease the text length.
89−prop_compress_css_length str = length str >= length (compressCSS str)
90−
91−-- Compress CSS test cases.
92−test_compress_css1 = compressCSS "a { \n color : red; }" @?= "a{color:red}"
93−test_compress_css2 = compressCSS "img {border :none;;;; }"
94− @?= "img{border:none}"
95−test_compress_css3 =
96− compressCSS "p {font-size : 90%;} h1 {color :white;;; }"
97− @?= "p{font-size:90%}h1{color:white}"
98−test_compress_css4 = compressCSS "a { /* /* red is pretty cool */ color: red; }"
99− @?= "a{color:red}"
100−
101−-- Date rendering test cases.
102−test_render_date1 =
103− M.lookup "date" rendered @?= Just "December 30, 2009"
104− where
105− rendered = renderDate "date" "%B %e, %Y" "Unknown date"
106− (M.singleton "path" "2009-12-30-a-title.markdown")
107−
108−test_render_date2 = M.lookup "date" rendered @?= Just "Unknown date"
109− where
110− rendered = renderDate "date" "%B %e, %Y" "Unknown date" $
111− M.singleton "path" "2009-badness-30-a-title.markdown"
112−
113−-- changeExtension test cases.
114−test_change_extension1 = M.lookup "url" rendered @?= Just "foo.php"
115− where
116− rendered = changeExtension "php" (M.singleton "url" "foo.html")
117−
118−-- toRoot test cases
119−test_to_root1 = toRoot "/posts/foo.html" @?= ".."
120−test_to_root2 = toRoot "posts/foo.html" @?= ".."
121−test_to_root3 = toRoot "foo.html" @?= "."
122−
123−-- removeSpaces test cases
124−test_remove_spaces1 = removeSpaces "$root/tags/random crap.html"
125− @?= "$root/tags/random-crap.html"
126−test_remove_spaces2 = removeSpaces "another simple example.zip"
127− @?= "another-simple-example.zip"
128−
129−-- Add an extension, and test that they have that extension
130−prop_having_extension_count names extension =
131− not (any ('.' `elem`) names || any (`elem` extension) "./\\")
132− ==> havingExtension fullExtension withExtensions == withExtensions
133− where
134− fullExtension = '.' : extension
135− withExtensions = map (++ fullExtension) names
136−
137−-- Having extension test cases
138−test_having_extension1 = havingExtension ".foo" ["file.bar", "file.txt"] @?= []
139−test_having_extension2 = havingExtension ".foo" ["file.foo", "file.txt"]
140− @?= ["file.foo"]
tests/Util.hsnew file+23
@@ -0,0 +1,23 @@
1+module Util where
2+
3+import Data.Char
4+
5+import Test.QuickCheck
6+
7+import Text.Hakyll.Util
8+
9+-- Test that a string always becomes shorter when trimmed.
10+prop_trim_length str = length str >= length (trim str)
11+
12+-- Check that a string which does not start or end with a space is not trimmed.
13+prop_trim_id str = (not $ null str) && isAlreadyTrimmed ==> str == (trim str)
14+ where
15+ isAlreadyTrimmed :: Bool
16+ isAlreadyTrimmed = (not $ isSpace $ head str) && (not $ isSpace $ last str)
17+
18+-- Check that a stripped string is shorter.
19+prop_stripHTML_length str = length str >= length (stripHTML str)
20+
21+-- Check that strings without tags remain untouched.
22+prop_stripHTML_id str = (not $ any (`elem` ['>', '<']) str)
23+ ==> str == stripHTML str