on master help sign in
Moved Arbitrary Template instance to tests.

This way, the code for Text.Hakyll.Internal.Template is cleaner, and our library does not depend on QuickCheck anymore.

commit057b7fa4851591309ed7de11d7d748779444d92b authorJasper Van der Jeugt <jaspervdj@gmail.com> dateThu 1 Apr 2010, 12:27 (17 years ago) parent417a9a9
3 files changed+35−36
hakyll.cabal+1−2 src/Text/Hakyll/Internal/Template.hs+2−34 tests/Template.hs+32
hakyll.cabal+1−2
@@ -39,8 +39,7 @@ library
3939 old-locale >= 1,
4040 old-time >= 1,
4141 time >= 1,
42− binary >= 0.5,
43− QuickCheck >= 2
42+ binary >= 0.5
4443 exposed-modules: Network.Hakyll.SimpleServer
4544 Text.Hakyll
4645 Text.Hakyll.Context
src/Text/Hakyll/Internal/Template.hs+2−34
@@ -1,5 +1,5 @@
11module Text.Hakyll.Internal.Template
2− ( Template
2+ ( Template (..)
33 , fromString
44 , readTemplate
55 , substitute
@@ -11,14 +11,11 @@ import qualified Data.Map as M
1111import Data.List (isPrefixOf)
1212import Data.Char (isAlphaNum)
1313import Data.Binary
14−import Control.Monad (liftM, liftM2, replicateM)
15−import Control.Applicative ((<$>))
14+import Control.Monad (liftM, liftM2)
1615import Data.Maybe (fromMaybe)
1716import System.FilePath ((</>))
1817import Control.Monad.Reader (liftIO)
1918
20−import Test.QuickCheck
21−
2219import Text.Hakyll.Context (Context)
2320import Text.Hakyll.HakyllMonad (Hakyll)
2421import Text.Hakyll.Internal.Cache
@@ -91,32 +88,3 @@ instance Binary Template where
9188 2 -> liftM EscapeCharacter get
9289 3 -> return End
9390 _ -> error "Error reading template"
94−
95−-- | Generate arbitrary templates from a given length.
96−arbitraryTemplate :: Int -> Gen Template
97−arbitraryTemplate 0 = return End
98−arbitraryTemplate length' = oneof [ do chunk <- chunk'
99− Chunk chunk <$> template'
100− , do key <- key'
101− Identifier key <$> template'
102− , EscapeCharacter <$> template'
103− ]
104− where
105− template' = arbitraryTemplate (length' - 1)
106− -- Generate keys.
107− key' = do l <- choose (5, 10)
108− replicateM l $ choose ('a', 'z')
109− -- Generate non-empty chunks.
110− chunk' = do string <- arbitrary
111− let sanitized = filter (/= '$') string
112− return $ if null sanitized then "foo"
113− else sanitized
114−
115−-- | Make @Template@ testable.
116−instance Arbitrary Template where
117− arbitrary = choose (0, 20) >>= arbitraryTemplate
118−
119− shrink (Chunk chunk template) = [template, Chunk chunk End]
120− shrink (Identifier key template) = [template, Identifier key End]
121− shrink (EscapeCharacter template) = [template, EscapeCharacter End]
122− shrink End = []
tests/Template.hs+32
@@ -3,12 +3,15 @@ module Template
33 ) where
44
55import qualified Data.Map as M
6+import Control.Applicative ((<$>))
7+import Control.Monad (replicateM)
68
79import Data.Binary
810import Test.Framework (testGroup)
911import Test.Framework.Providers.HUnit
1012import Test.Framework.Providers.QuickCheck2
1113import Test.HUnit
14+import Test.QuickCheck
1215
1316import Text.Hakyll.Internal.Template
1417
@@ -20,6 +23,35 @@ templateGroup = testGroup "Template"
2023 , testCase "test_substitute_2" test_substitute_2
2124 ]
2225
26+-- | Generate arbitrary templates from a given length.
27+arbitraryTemplate :: Int -> Gen Template
28+arbitraryTemplate 0 = return End
29+arbitraryTemplate length' = oneof [ do chunk <- chunk'
30+ Chunk chunk <$> template'
31+ , do key <- key'
32+ Identifier key <$> template'
33+ , EscapeCharacter <$> template'
34+ ]
35+ where
36+ template' = arbitraryTemplate (length' - 1)
37+ -- Generate keys.
38+ key' = do l <- choose (5, 10)
39+ replicateM l $ choose ('a', 'z')
40+ -- Generate non-empty chunks.
41+ chunk' = do string <- arbitrary
42+ let sanitized = filter (/= '$') string
43+ return $ if null sanitized then "foo"
44+ else sanitized
45+
46+-- | Make @Template@ testable.
47+instance Arbitrary Template where
48+ arbitrary = choose (0, 20) >>= arbitraryTemplate
49+
50+ shrink (Chunk chunk template) = [template, Chunk chunk End]
51+ shrink (Identifier key template) = [template, Identifier key End]
52+ shrink (EscapeCharacter template) = [template, EscapeCharacter End]
53+ shrink End = []
54+
2355-- Test encoding/decoding of templates.
2456prop_template_encode_id :: Template -> Bool
2557prop_template_encode_id template = decode (encode template) == template