git.iko.soy/typeable/pandoc
on master help sign in
Improved Arbitrary instance.
commit714303b210b249292b83eb13ed37dd897999380d authorJohn MacFarlane <jgm@berkeley.edu> dateFri 4 Feb 2011, 18:32 (16 years ago) parent99677ac
1 file changed+45−32
src/Tests/Arbitrary.hs+45−32
src/Tests/Arbitrary.hs+45−32
@@ -6,17 +6,20 @@ where
66import Test.QuickCheck.Gen
77import Test.QuickCheck.Arbitrary
88import Control.Monad (liftM, liftM2)
9−import Text.Pandoc
10−import Text.Pandoc.Shared
9+import Text.Pandoc.Definition
10+import Text.Pandoc.Shared (normalize, escapeURI)
1111import Text.Pandoc.Builder
1212
1313realString :: Gen String
14−realString = resize 8 arbitrary -- elements wordlist
14+realString = resize 8 $ listOf $ frequency [ (9, elements [' '..'\127'])
15+ , (1, elements ['\128'..'\9999']) ]
1516
16−{-
17−wordlist :: [String]
18−wordlist = ["foo","Bar","baz","\\","/",":","\"","'","féé"]
19−-}
17+arbAttr :: Gen Attr
18+arbAttr = do
19+ id' <- elements ["","loc"]
20+ classes <- elements [[],["haskell"],["c","numberLines"]]
21+ keyvals <- elements [[],[("start","22")],[("a","11"),("b_2","a b c")]]
22+ return (id',classes,keyvals)
2023
2124instance Arbitrary Inlines where
2225 arbitrary = liftM fromList arbitrary
@@ -27,69 +30,79 @@ instance Arbitrary Blocks where
2730instance Arbitrary Inline where
2831 arbitrary = resize 3 $ arbInline 3
2932
33+arbInlines :: Int -> Gen [Inline]
34+arbInlines n = listOf1 (arbInline n) `suchThat` (not . startsWithSpace)
35+ where startsWithSpace (Space:_) = True
36+ startsWithSpace _ = False
37+
3038-- restrict to 3 levels of nesting max; otherwise we get
3139-- bogged down in indefinitely large structures
3240arbInline :: Int -> Gen Inline
3341arbInline n = frequency $ [ (60, liftM Str realString)
3442 , (60, return Space)
35− , (10, liftM2 Code arbitrary realString)
43+ , (10, liftM2 Code arbAttr realString)
3644 , (5, return EmDash)
3745 , (5, return EnDash)
3846 , (5, return Apostrophe)
3947 , (5, return Ellipses)
40− , (5, elements [ RawInline "html" "<a>*&amp;*</a>"
48+ , (5, elements [ RawInline "html" "<a id=\"eek\">"
4149 , RawInline "latex" "\\my{command}" ])
4250 ] ++ [ x | x <- nesters, n > 1]
43− where nesters = [ (10, liftM Emph $ listOf $ arbInline (n-1))
44− , (10, liftM Strong $ listOf $ arbInline (n-1))
45− , (10, liftM Strikeout $ listOf $ arbInline (n-1))
46− , (10, liftM Superscript $ listOf $ arbInline (n-1))
47− , (10, liftM Subscript $ listOf $ arbInline (n-1))
48− , (10, liftM SmallCaps $ listOf $ arbInline (n-1))
51+ where nesters = [ (10, liftM Emph $ arbInlines (n-1))
52+ , (10, liftM Strong $ arbInlines (n-1))
53+ , (10, liftM Strikeout $ arbInlines (n-1))
54+ , (10, liftM Superscript $ arbInlines (n-1))
55+ , (10, liftM Subscript $ arbInlines (n-1))
56+-- , (10, liftM SmallCaps $ arbInlines (n-1))
4957 , (10, do x1 <- arbitrary
50− x2 <- listOf $ arbInline (n-1)
58+ x2 <- arbInlines (n-1)
5159 return $ Quoted x1 x2)
5260 , (10, do x1 <- arbitrary
5361 x2 <- realString
5462 return $ Math x1 x2)
55− , (10, do x1 <- listOf $ arbInline (n-1)
63+ , (10, do x1 <- arbInlines (n-1)
5664 x3 <- realString
57− x2 <- realString
65+ x2 <- liftM escapeURI realString
5866 return $ Link x1 (x2,x3))
59− , (10, do x1 <- listOf $ arbInline (n-1)
67+ , (10, do x1 <- arbInlines (n-1)
6068 x3 <- realString
61− x2 <- realString
69+ x2 <- liftM escapeURI realString
6270 return $ Image x1 (x2,x3))
63− , (2, liftM Note $ resize 3 $ listOf1 arbitrary)
71+ , (2, liftM Note $ resize 3 $ listOf1 $ arbBlock (n-1))
6472 ]
6573
6674instance Arbitrary Block where
6775 arbitrary = resize 3 $ arbBlock 3
6876
6977arbBlock :: Int -> Gen Block
70−arbBlock n = frequency $ [ (10, liftM Plain arbitrary)
71− , (15, liftM Para arbitrary)
72− , (5, liftM2 CodeBlock arbitrary realString)
78+arbBlock n = frequency $ [ (10, liftM Plain $ arbInlines (n-1))
79+ , (15, liftM Para $ arbInlines (n-1))
80+ , (5, liftM2 CodeBlock arbAttr realString)
7381 , (2, elements [ RawBlock "html"
7482 "<div>\n*&amp;*\n</div>"
7583 , RawBlock "latex"
7684 "\\begin[opt]{env}\nhi\n{\\end{env}"
7785 ])
7886 , (5, do x1 <- choose (1 :: Int, 6)
79− x2 <- arbitrary
87+ x2 <- arbInlines (n-1)
8088 return (Header x1 x2))
8189 , (2, return HorizontalRule)
8290 ] ++ [x | x <- nesters, n > 0]
83− where nesters = [ (5, liftM BlockQuote $ listOf $ arbBlock (n-1))
84− , (5, liftM2 OrderedList arbitrary
85− $ (listOf1 $ listOf1 $ arbBlock (n-1)))
91+ where nesters = [ (5, liftM BlockQuote $ listOf1 $ arbBlock (n-1))
92+ , (5, do x2 <- arbitrary
93+ x3 <- arbitrary
94+ x1 <- arbitrary `suchThat` (> 0)
95+ x4 <- listOf1 $ listOf1 $ arbBlock (n-1)
96+ return $ OrderedList (x1,x2,x3) x4 )
8697 , (5, liftM BulletList $ (listOf1 $ listOf1 $ arbBlock (n-1)))
87− , (5, do x1 <- listOf $ listOf1 $ listOf1 $ arbBlock (n-1)
88− x2 <- arbitrary
89− return (DefinitionList $ zip x2 x1))
98+ , (5, do items <- listOf1 $ do
99+ x1 <- listOf1 $ listOf1 $ arbBlock (n-1)
100+ x2 <- arbInlines (n-1)
101+ return (x2,x1)
102+ return $ DefinitionList items)
90103 , (2, do rs <- choose (1 :: Int, 4)
91104 cs <- choose (1 :: Int, 4)
92− x1 <- arbitrary
105+ x1 <- arbInlines (n-1)
93106 x2 <- vector cs
94107 x3 <- vectorOf cs $ elements [0, 0.25]
95108 x4 <- vectorOf cs $ listOf $ arbBlock (n-1)