~ubuntu-branches/ubuntu/precise/pandoc/precise

« back to all changes in this revision

Viewing changes to src/Tests/Arbitrary.hs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2011-05-29 10:36:00 UTC
  • mfrom: (3.1.8 sid)
  • Revision ID: james.westby@ubuntu.com-20110529103600-pldc9ra7earow3lg
Tags: 1.8.1.1-1ubuntu1
Change BDI to ghc-haddock explicitly. Can go away once ghc6 is removed and
there is only one package providing haddock again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-# OPTIONS_GHC -fno-warn-orphans #-}
 
2
{-# LANGUAGE TypeSynonymInstances #-}
 
3
-- provides Arbitrary instance for Pandoc types
 
4
module Tests.Arbitrary ()
 
5
where
 
6
import Test.QuickCheck.Gen
 
7
import Test.QuickCheck.Arbitrary
 
8
import Control.Monad (liftM, liftM2)
 
9
import Text.Pandoc.Definition
 
10
import Text.Pandoc.Shared (normalize, escapeURI)
 
11
import Text.Pandoc.Builder
 
12
 
 
13
realString :: Gen String
 
14
realString = resize 8 $ listOf $ frequency [ (9, elements [' '..'\127'])
 
15
                                           , (1, elements ['\128'..'\9999']) ]
 
16
 
 
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)
 
23
 
 
24
instance Arbitrary Inlines where
 
25
  arbitrary = liftM fromList arbitrary
 
26
 
 
27
instance Arbitrary Blocks where
 
28
  arbitrary = liftM fromList arbitrary
 
29
 
 
30
instance Arbitrary Inline where
 
31
  arbitrary = resize 3 $ arbInline 3
 
32
 
 
33
arbInlines :: Int -> Gen [Inline]
 
34
arbInlines n = listOf1 (arbInline n) `suchThat` (not . startsWithSpace)
 
35
  where startsWithSpace (Space:_) = True
 
36
        startsWithSpace        _  = False
 
37
 
 
38
-- restrict to 3 levels of nesting max; otherwise we get
 
39
-- bogged down in indefinitely large structures
 
40
arbInline :: Int -> Gen Inline
 
41
arbInline n = frequency $ [ (60, liftM Str realString)
 
42
                          , (60, return Space)
 
43
                          , (10, liftM2 Code arbAttr realString)
 
44
                          , (5,  return EmDash)
 
45
                          , (5,  return EnDash)
 
46
                          , (5,  return Apostrophe)
 
47
                          , (5,  return Ellipses)
 
48
                          , (5,  elements [ RawInline "html" "<a id=\"eek\">"
 
49
                                          , RawInline "latex" "\\my{command}" ])
 
50
                          ] ++ [ x | x <- nesters, 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))
 
57
                   , (10,  do x1 <- arbitrary
 
58
                              x2 <- arbInlines (n-1)
 
59
                              return $ Quoted x1 x2)
 
60
                   , (10,  do x1 <- arbitrary
 
61
                              x2 <- realString
 
62
                              return $ Math x1 x2)
 
63
                   , (10,  do x1 <- arbInlines (n-1)
 
64
                              x3 <- realString
 
65
                              x2 <- liftM escapeURI realString
 
66
                              return $ Link x1 (x2,x3))
 
67
                   , (10,  do x1 <- arbInlines (n-1)
 
68
                              x3 <- realString
 
69
                              x2 <- liftM escapeURI realString
 
70
                              return $ Image x1 (x2,x3))
 
71
                   , (2,  liftM Note $ resize 3 $ listOf1 $ arbBlock (n-1))
 
72
                   ]
 
73
 
 
74
instance Arbitrary Block where
 
75
  arbitrary = resize 3 $ arbBlock 3
 
76
 
 
77
arbBlock :: Int -> Gen Block
 
78
arbBlock n = frequency $ [ (10, liftM Plain $ arbInlines (n-1))
 
79
                         , (15, liftM Para $ arbInlines (n-1))
 
80
                         , (5,  liftM2 CodeBlock arbAttr realString)
 
81
                         , (2,  elements [ RawBlock "html"
 
82
                                            "<div>\n*&amp;*\n</div>"
 
83
                                         , RawBlock "latex"
 
84
                                            "\\begin[opt]{env}\nhi\n{\\end{env}"
 
85
                                         ])
 
86
                         , (5,  do x1 <- choose (1 :: Int, 6)
 
87
                                   x2 <- arbInlines (n-1)
 
88
                                   return (Header x1 x2))
 
89
                         , (2, return HorizontalRule)
 
90
                         ] ++ [x | x <- nesters, n > 0]
 
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 )
 
97
                   , (5,  liftM BulletList $ (listOf1 $ listOf1 $ arbBlock (n-1)))
 
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)
 
103
                   , (2, do rs <- choose (1 :: Int, 4)
 
104
                            cs <- choose (1 :: Int, 4)
 
105
                            x1 <- arbInlines (n-1)
 
106
                            x2 <- vector cs
 
107
                            x3 <- vectorOf cs $ elements [0, 0.25]
 
108
                            x4 <- vectorOf cs $ listOf $ arbBlock (n-1)
 
109
                            x5 <- vectorOf rs $ vectorOf cs
 
110
                                  $ listOf $ arbBlock (n-1)
 
111
                            return (Table x1 x2 x3 x4 x5))
 
112
                   ]
 
113
 
 
114
instance Arbitrary Pandoc where
 
115
        arbitrary = resize 8 $ liftM normalize
 
116
                             $ liftM2 Pandoc arbitrary arbitrary
 
117
 
 
118
{-
 
119
instance Arbitrary CitationMode where
 
120
        arbitrary
 
121
          = do x <- choose (0 :: Int, 2)
 
122
               case x of
 
123
                   0 -> return AuthorInText
 
124
                   1 -> return SuppressAuthor
 
125
                   2 -> return NormalCitation
 
126
                   _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
 
127
 
 
128
instance Arbitrary Citation where
 
129
        arbitrary
 
130
          = do x1 <- liftM (filter (`notElem` ",;]@ \t\n")) arbitrary
 
131
               x2 <- arbitrary
 
132
               x3 <- arbitrary
 
133
               x4 <- arbitrary
 
134
               x5 <- arbitrary
 
135
               x6 <- arbitrary
 
136
               return (Citation x1 x2 x3 x4 x5 x6)
 
137
-}
 
138
 
 
139
instance Arbitrary MathType where
 
140
        arbitrary
 
141
          = do x <- choose (0 :: Int, 1)
 
142
               case x of
 
143
                   0 -> return DisplayMath
 
144
                   1 -> return InlineMath
 
145
                   _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
 
146
 
 
147
instance Arbitrary QuoteType where
 
148
        arbitrary
 
149
          = do x <- choose (0 :: Int, 1)
 
150
               case x of
 
151
                   0 -> return SingleQuote
 
152
                   1 -> return DoubleQuote
 
153
                   _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
 
154
 
 
155
instance Arbitrary Meta where
 
156
        arbitrary
 
157
          = do x1 <- arbitrary
 
158
               x2 <- liftM (filter (not . null)) arbitrary
 
159
               x3 <- arbitrary
 
160
               return (Meta x1 x2 x3)
 
161
 
 
162
instance Arbitrary Alignment where
 
163
        arbitrary
 
164
          = do x <- choose (0 :: Int, 3)
 
165
               case x of
 
166
                   0 -> return AlignLeft
 
167
                   1 -> return AlignRight
 
168
                   2 -> return AlignCenter
 
169
                   3 -> return AlignDefault
 
170
                   _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
 
171
 
 
172
instance Arbitrary ListNumberStyle where
 
173
        arbitrary
 
174
          = do x <- choose (0 :: Int, 6)
 
175
               case x of
 
176
                   0 -> return DefaultStyle
 
177
                   1 -> return Example
 
178
                   2 -> return Decimal
 
179
                   3 -> return LowerRoman
 
180
                   4 -> return UpperRoman
 
181
                   5 -> return LowerAlpha
 
182
                   6 -> return UpperAlpha
 
183
                   _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
 
184
 
 
185
instance Arbitrary ListNumberDelim where
 
186
        arbitrary
 
187
          = do x <- choose (0 :: Int, 3)
 
188
               case x of
 
189
                   0 -> return DefaultDelim
 
190
                   1 -> return Period
 
191
                   2 -> return OneParen
 
192
                   3 -> return TwoParens
 
193
                   _ -> error "FATAL ERROR: Arbitrary instance, logic bug"
 
194