-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy path02-Generators.purs
280 lines (239 loc) · 9.64 KB
/
02-Generators.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
module Test.ToC.ParserLogic.Generators
( MarkdownParserResult(..)
, genMarkdownParserResult
, PureScriptParserResult(..)
) where
import Prelude
import Control.Alt ((<|>))
import Control.Comonad.Cofree (head, tail, (:<))
import Control.Monad.Gen (chooseBool, oneOf)
import Control.Monad.Rec.Class (Step(..), tailRecM)
import Data.Array (cons, foldM)
import Data.Char.Gen (genAlpha, genAsciiChar, genDigitChar, genUnicodeChar)
import Data.Char.Unicode (isAlphaNum, isControl)
import Data.Foldable (fold, foldl)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.List (List(..), intercalate, reverse, (:))
import Data.Monoid (power)
import Data.NonEmpty ((:|))
import Data.String.Gen (genString)
import Data.Tree (Forest, Tree)
import Data.Tuple (Tuple(..))
import Test.QuickCheck (class Arbitrary)
import Test.QuickCheck.Gen (Gen, chooseInt, frequency, listOf, shuffle, suchThat, vectorOf)
import ToC.Core.FileTypes (HeaderInfo)
newtype MarkdownParserResult =
MarkdownParserResult { expectedHeaders :: Forest HeaderInfo
, lines :: Array String
}
instance arbitraryMarkdownParserResult :: Arbitrary MarkdownParserResult where
arbitrary = genMarkdownParserResult
genMarkdownParserResult :: Gen MarkdownParserResult
genMarkdownParserResult = do
headerForest <- genMarkdownHeaderForest
lines <- genRenderedMarkdownLines headerForest
pure $ MarkdownParserResult { expectedHeaders: headerForest
, lines: lines
}
newtype PureScriptParserResult =
PureScriptParserResult { expectedHeaders :: Forest HeaderInfo
, lines :: Array String
}
-- TODO: The generators for this data type require keeping track of the
-- number of lines that precede a given header
instance arbitraryPureScriptParserResult :: Arbitrary PureScriptParserResult where
arbitrary = genPureScriptParserResult
-- TODO: The generators for this data type require keeping track of the
-- number of lines that precede a given header
genPureScriptParserResult :: Gen PureScriptParserResult
genPureScriptParserResult = do
pure $ PureScriptParserResult { expectedHeaders: Nil
, lines: []
}
data AnchorFragment
= Word String
| Hyphen
| UnderScore
| WhiteSpace String
| NonAnchorContent String
derive instance genericAnchorFragment :: Generic AnchorFragment _
instance showAnchorFragment :: Show AnchorFragment where
show x = genericShow x
-- | Unboxes the AnchorFragment data constructor
renderHeaderText :: AnchorFragment -> String
renderHeaderText = case _ of
Word x -> x
Hyphen -> "-"
UnderScore -> "_"
WhiteSpace x -> x
NonAnchorContent x -> x
-- | Unboxes the AnchorFragment data constructor except for NonAnchorContent,
-- | which is not rendered
renderAnchorText :: AnchorFragment -> String
renderAnchorText = case _ of
Word x -> x
Hyphen -> "-"
UnderScore -> "_"
WhiteSpace _ -> "-"
NonAnchorContent _ -> ""
-- | Generates a alphanumerical String
genWordFragment :: Gen AnchorFragment
genWordFragment = do
totalChars <- chooseInt 1 5
wordList <- listOf totalChars $
genString $
oneOf $ genAlpha :| [genDigitChar]
pure $ Word $ fold wordList
-- | Generates symbolic characters and non-alphanum non-whitespace characters
genNonAnchorContent :: Gen AnchorFragment
genNonAnchorContent = do
totalChars <- chooseInt 1 4
somethingElseList <- listOf totalChars $ genString genNonAnchorChar
pure $ NonAnchorContent $ fold somethingElseList
where
genNonAnchorChar :: Gen Char
genNonAnchorChar =
genUnicodeChar `suchThat` (\c ->
not ( isControl c
|| isAlphaNum c
|| c == ' '
|| c == '\t'
|| c == '-'
|| c == '_'
)
)
-- | Generates the non-whitespace characters
genAnchorFragmentContent :: Gen AnchorFragment
genAnchorFragmentContent =
frequency $ Tuple 4.0 genWordFragment
:| ( Tuple 2.0 genNonAnchorContent
: Tuple 2.0 (pure Hyphen)
: Tuple 1.0 (pure UnderScore)
: Nil
)
-- | Generates a sequence of 1 or more spaces and/or tabs
genWhitespace :: Gen String
genWhitespace = do
totalChars <- chooseInt 1 4
fold <$> (listOf totalChars $ oneOf $ pure " " :| [pure "\t"])
-- | Generates 1 header for a Markdown file
genMarkdownHeaderInfo :: Int -> Gen HeaderInfo
genMarkdownHeaderInfo level = do
-- generate all anchor fragments
guaranteeAtLeastOneWord <- genWordFragment
totalFragments <- chooseInt 0 6
allOtherFragments <- vectorOf totalFragments genAnchorFragmentContent
fragmentContents <- shuffle (guaranteeAtLeastOneWord `cons` allOtherFragments)
-- add some whitespace between them
fragments <- intercalateGen (WhiteSpace <$> genWhitespace) fragmentContents
-- create the corresponding anchor and text info
let rec = foldl (\acc next ->
{ anchor: acc.anchor <> renderAnchorText next
, text: acc.text <> renderHeaderText next
}) { anchor: "", text: "" } fragments
pure { anchor: "#" <> rec.anchor, text: rec.text, level: level }
where
-- | Simulates 'intercalate whiteSpaceSeparation arrayOfGeneratedContent'
-- | by building up the list backwards (for speed)
-- | and then reversing the final output (for correct order)
intercalateGen :: Gen AnchorFragment -> Array AnchorFragment -> Gen (List AnchorFragment)
intercalateGen genSeparator foldable = do
result <- foldM (\acc next ->
if acc.init
then
pure { init: false, entity: next : acc.entity }
else do
separation <- genSeparator
pure $ acc { entity = next : separation : acc.entity }
) { init: true, entity: Nil } foldable
pure $ reverse result.entity
-- | Recursively generates a tree of markdown headers that may
-- | or may not have children. However, the tree's depth is guaranteed
-- | to be at least less than the max depth.
genMarkdownHeaderInfoTree :: Int -> Int -> Gen (Tree HeaderInfo)
genMarkdownHeaderInfoTree maxPossibleDepth headerLevel
| maxPossibleDepth == headerLevel =
(genMarkdownHeaderInfo headerLevel) <#> (\headerInfo -> headerInfo :< Nil)
| otherwise = do
header <- genMarkdownHeaderInfo headerLevel
children <- ifM chooseBool
(do
amount <- chooseInt 1 4
listOf amount $ genMarkdownHeaderInfoTree maxPossibleDepth (headerLevel + 1)
)
(pure Nil)
pure $ header :< children
genMarkdownHeaderForest :: Gen (Forest HeaderInfo)
genMarkdownHeaderForest = do
amount <- chooseInt 1 6
maxPossibleDepth <- chooseInt 1 4
-- since we only parse headers that are level 2 or higher
-- we increase `maxPossibleDepth` by one
listOf amount $ genMarkdownHeaderInfoTree (maxPossibleDepth + 1) 2
genRenderedMarkdownLines :: Forest HeaderInfo -> Gen (Array String)
genRenderedMarkdownLines forest =
tailRecM renderForest { forest: forest, array: [] }
where
renderForest :: { forest :: Forest HeaderInfo, array :: Array String }
-> Gen (Step { forest :: Forest HeaderInfo, array :: Array String } (Array String))
renderForest { forest: Nil, array: array } = pure $ Done array
renderForest { forest: (head:remaining), array: array } = do
section <- renderSection head
pure $ Loop { forest: remaining, array: array <|> section }
renderSection :: Tree HeaderInfo -> Gen (Array String)
renderSection headerTree = do
randomContentBefore <- genLinesWithRandomContent
wsSeparator <- genWhitespace
let headerLine = renderHeader (head headerTree) wsSeparator
randomContentAfter <- genLinesWithRandomContent
let initialValue = { forest: tail headerTree, array: [] }
renderedChildHeaderSections <- tailRecM renderForest initialValue
pure $ randomContentBefore <|> (headerLine `cons` (randomContentAfter <|> renderedChildHeaderSections))
renderHeader :: HeaderInfo -> String -> String
renderHeader header wsSeparator =
let
headerPrefix = power "#" header.level
in
headerPrefix <> wsSeparator <> header.text
genLinesWithRandomContent :: Gen (Array String)
genLinesWithRandomContent = do
numberOfContentBlocks <- chooseInt 0 8
array_of_array <- vectorOf numberOfContentBlocks $ oneOf $
genBlankLine
:| [ genBulletListSection
, genCodeBlockSection
, genLineOfGibberish
]
let flattenedArray = join array_of_array
pure flattenedArray
where
genBlankLine :: Gen (Array String)
genBlankLine = pure [""]
genBulletListSection :: Gen (Array String)
genBulletListSection = do
listSize <- chooseInt 1 4
vectorOf listSize do
gibberish <- genGibberishPhrase
pure $ "- " <> gibberish
genGibberishWord :: Gen String
genGibberishWord = do
numberOfChars <- chooseInt 1 15
charList <- listOf numberOfChars $ genString
(genAsciiChar `suchThat` \c ->
not ( isControl c
|| c == '#'
)
)
pure $ fold charList
genGibberishPhrase :: Gen String
genGibberishPhrase = do
numberOfWords <- chooseInt 1 15
(intercalate " ") <$> listOf numberOfWords genGibberishWord
genCodeBlockSection :: Gen (Array String)
genCodeBlockSection = do
numberOfLines <- chooseInt 1 8
gibberishLines <- vectorOf numberOfLines genGibberishPhrase
pure $ ["```"] <|> gibberishLines <|> ["```"]
genLineOfGibberish :: Gen (Array String)
genLineOfGibberish = vectorOf 1 genGibberishPhrase