1{-# OPTIONS_GHC -fno-warn-orphans #-}
2{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, ScopedTypeVariables #-}
3-- provides Arbitrary instance for Pandoc types
4module Tests.Arbitrary ()
6import Test.QuickCheck.Gen
7import Test.QuickCheck.Arbitrary
8import Control.Monad (liftM, liftM2)
9import Text.Pandoc.Definition
10import Text.Pandoc.Shared (normalize, escapeURI)
11import Text.Pandoc.Builder
13realString :: Gen String
14realString = resize 8 $ listOf $ frequency [ (9, elements [' '..'\127'])
15 , (1, elements ['\128'..'\9999']) ]
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)
24instance Arbitrary Inlines where
25 arbitrary = liftM (fromList :: [Inline] -> Inlines) arbitrary
27instance Arbitrary Blocks where
28 arbitrary = liftM (fromList :: [Block] -> Blocks) arbitrary
30instance Arbitrary Inline where
31 arbitrary = resize 3 $ arbInline 2
33arbInlines :: Int -> Gen [Inline]
34arbInlines n = listOf1 (arbInline n) `suchThat` (not . startsWithSpace)
35 where startsWithSpace (Space:_) = True
36 startsWithSpace _ = False
38-- restrict to 3 levels of nesting max; otherwise we get
39-- bogged down in indefinitely large structures
40arbInline :: Int -> Gen Inline
41arbInline n = frequency $ [ (60, liftM Str realString)
43 , (10, liftM2 Code arbAttr realString)
44 , (5, elements [ RawInline "html" "<a id=\"eek\">"
45 , RawInline "latex" "\\my{command}" ])
46 ] ++ [ x | x <- nesters, n > 1]
47 where nesters = [ (10, liftM Emph $ arbInlines (n-1))
48 , (10, liftM Strong $ arbInlines (n-1))
49 , (10, liftM Strikeout $ arbInlines (n-1))
50 , (10, liftM Superscript $ arbInlines (n-1))
51 , (10, liftM Subscript $ arbInlines (n-1))
52-- , (10, liftM SmallCaps $ arbInlines (n-1))
53 , (10, do x1 <- arbitrary
54 x2 <- arbInlines (n-1)
55 return $ Quoted x1 x2)
56 , (10, do x1 <- arbitrary
59 , (10, do x1 <- arbInlines (n-1)
61 x2 <- liftM escapeURI realString
62 return $ Link x1 (x2,x3))
63 , (10, do x1 <- arbInlines (n-1)
65 x2 <- liftM escapeURI realString
66 return $ Image x1 (x2,x3))
67 , (2, liftM Note $ resize 3 $ listOf1 $ arbBlock (n-1))
70instance Arbitrary Block where
71 arbitrary = resize 3 $ arbBlock 2
73arbBlock :: Int -> Gen Block
74arbBlock n = frequency $ [ (10, liftM Plain $ arbInlines (n-1))
75 , (15, liftM Para $ arbInlines (n-1))
76 , (5, liftM2 CodeBlock arbAttr realString)
77 , (2, elements [ RawBlock "html"
78 "<div>\n*&*\n</div>"
80 "\\begin[opt]{env}\nhi\n{\\end{env}"
82 , (5, do x1 <- choose (1 :: Int, 6)
83 x2 <- arbInlines (n-1)
84 return (Header x1 x2))
85 , (2, return HorizontalRule)
86 ] ++ [x | x <- nesters, n > 0]
87 where nesters = [ (5, liftM BlockQuote $ listOf1 $ arbBlock (n-1))
88 , (5, do x2 <- arbitrary
90 x1 <- arbitrary `suchThat` (> 0)
91 x4 <- listOf1 $ listOf1 $ arbBlock (n-1)
92 return $ OrderedList (x1,x2,x3) x4 )
93 , (5, liftM BulletList $ (listOf1 $ listOf1 $ arbBlock (n-1)))
94 , (5, do items <- listOf1 $ do
95 x1 <- listOf1 $ listOf1 $ arbBlock (n-1)
96 x2 <- arbInlines (n-1)
98 return $ DefinitionList items)
99 , (2, do rs <- choose (1 :: Int, 4)
100 cs <- choose (1 :: Int, 4)
101 x1 <- arbInlines (n-1)
103 x3 <- vectorOf cs $ elements [0, 0.25]
104 x4 <- vectorOf cs $ listOf $ arbBlock (n-1)
105 x5 <- vectorOf rs $ vectorOf cs
106 $ listOf $ arbBlock (n-1)
107 return (Table x1 x2 x3 x4 x5))
110instance Arbitrary Pandoc where
111 arbitrary = resize 8 $ liftM normalize
112 $ liftM2 Pandoc arbitrary arbitrary
115instance Arbitrary CitationMode where
117 = do x <- choose (0 :: Int, 2)
119 0 -> return AuthorInText
120 1 -> return SuppressAuthor
121 2 -> return NormalCitation
122 _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
124instance Arbitrary Citation where
126 = do x1 <- liftM (filter (`notElem` ",;]@ \t\n")) arbitrary
132 return (Citation x1 x2 x3 x4 x5 x6)
135instance Arbitrary MathType where
137 = do x <- choose (0 :: Int, 1)
139 0 -> return DisplayMath
140 1 -> return InlineMath
141 _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
143instance Arbitrary QuoteType where
145 = do x <- choose (0 :: Int, 1)
147 0 -> return SingleQuote
148 1 -> return DoubleQuote
149 _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
151instance Arbitrary Meta where
154 x2 <- liftM (filter (not . null)) arbitrary
156 return (Meta x1 x2 x3)
158instance Arbitrary Alignment where
160 = do x <- choose (0 :: Int, 3)
162 0 -> return AlignLeft
163 1 -> return AlignRight
164 2 -> return AlignCenter
165 3 -> return AlignDefault
166 _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
168instance Arbitrary ListNumberStyle where
170 = do x <- choose (0 :: Int, 6)
172 0 -> return DefaultStyle
175 3 -> return LowerRoman
176 4 -> return UpperRoman
177 5 -> return LowerAlpha
178 6 -> return UpperAlpha
179 _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
181instance Arbitrary ListNumberDelim where
183 = do x <- choose (0 :: Int, 3)
185 0 -> return DefaultDelim
188 3 -> return TwoParens
189 _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list