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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
module Tests.Writers.BBCode (tests) where
import Data.Maybe (isNothing)
import Data.Text as T
import Test.Tasty
import Test.Tasty.HUnit (HasCallStack)
import Test.Tasty.QuickCheck
import Tests.Helpers
import Text.Pandoc
import Text.Pandoc.Arbitrary ()
import Text.Pandoc.Builder
import Text.Pandoc.Shared (tshow)
import Text.Read (readMaybe)
bbcodeDefault
, bbcodeSteam
, bbcodePhpBB
, bbcodeFluxBB
, bbcodeHubzilla
, bbcodeXenforo ::
(ToPandoc a) => a -> Text
bbcodeDefault = purely (writeBBCode def) . toPandoc
bbcodeSteam = purely (writeBBCodeSteam def) . toPandoc
bbcodePhpBB = purely (writeBBCodePhpBB def) . toPandoc
bbcodeFluxBB = purely (writeBBCodeFluxBB def) . toPandoc
bbcodeHubzilla = purely (writeBBCodeHubzilla def) . toPandoc
bbcodeXenforo = purely (writeBBCodeXenforo def) . toPandoc
infix 4 =:, `steam`, `phpbb`, `fluxbb`, `hubzilla`, `xenforo`
(=:)
, steam
, phpbb
, fluxbb
, hubzilla
, xenforo ::
(ToString a, ToPandoc a, HasCallStack) =>
String ->
(a, Text) ->
TestTree
(=:) = test bbcodeDefault
steam = test bbcodeSteam
phpbb = test bbcodePhpBB
fluxbb = test bbcodeFluxBB
hubzilla = test bbcodeHubzilla
xenforo = test bbcodeXenforo
spanClasses :: [Text] -> Inlines -> Inlines
spanClasses cls = spanWith ("", cls, [])
spanAttrs :: [(Text, Text)] -> Inlines -> Inlines
spanAttrs kvList = spanWith ("", [], kvList)
divClasses :: [Text] -> Blocks -> Blocks
divClasses cls = divWith ("", cls, [])
divAttrs :: [(Text, Text)] -> Blocks -> Blocks
divAttrs kvList = divWith ("", [], kvList)
tests :: [TestTree]
tests =
[ testGroup
"spans classes"
[ "left" =: spanClasses ["left"] "foo" =?> "foo"
, "center" =: spanClasses ["center"] "foo" =?> "foo"
, "right" =: spanClasses ["right"] "foo" =?> "foo"
, "spoiler" =: spanClasses ["spoiler"] "foo" =?> "foo"
]
, testGroup
"spans attributes"
[ testProperty "incorrect size ignored" . property $ do
n <- arbitrary @String
let nInt = readMaybe @Int n
let actual = bbcodeDefault (spanAttrs [("size", T.pack n)] "foo")
pure $ isNothing nInt ==> actual === "foo"
, testProperty "size<=0 ignored" . property $ do
NonPositive n <- arbitrary @(NonPositive Int)
let actual = bbcodeDefault (spanAttrs [("size", tshow n)] "foo")
pure $ actual === "foo"
, testProperty "size>0" . property $ do
Positive n <- arbitrary @(Positive Int)
let actual = bbcodeDefault (spanAttrs [("size", tshow n)] "foo")
let expected = "[size=" <> tshow n <> "]" <> "foo[/size]"
pure $ actual === expected
, "size=20" =: spanAttrs [("size", "20")] "foo" =?> "[size=20]foo[/size]"
, "color=#AAAAAA"
=: spanAttrs [("color", "#AAAAAA")] "foo"
=?> "[color=#AAAAAA]foo[/color]"
, "spoiler ignored"
=: spanAttrs [("spoiler", "name with spaces and ]brackets[]")] "foo"
=?> "foo"
]
, testGroup
"divs classes"
[ "left"
=: divClasses ["left"] (para "foo")
=?> "[left]foo[/left]"
, "center"
=: divClasses ["center"] (para "foo")
=?> "[center]foo[/center]"
, "right"
=: divClasses ["right"] (para "foo")
=?> "[right]foo[/right]"
, "spoiler"
=: divClasses ["spoiler"] (para "foo")
=?> "[spoiler]foo[/spoiler]"
]
, testGroup
"divs attributes"
[ testProperty "incorrect size ignored" . property $ do
n <- arbitrary @String
let nInt = readMaybe @Int n
let actual = bbcodeDefault (divAttrs [("size", T.pack n)] $ para "foo")
pure $ isNothing nInt ==> actual === "foo"
, testProperty "size<=0 ignored" . property $ do
NonPositive n <- arbitrary @(NonPositive Int)
let actual = bbcodeDefault (divAttrs [("size", tshow n)] $ para "foo")
pure $ actual === "foo"
, testProperty "size>0" . property $ do
Positive n <- arbitrary @(Positive Int)
let actual = bbcodeDefault (divAttrs [("size", tshow n)] $ para "foo")
let expected = "[size=" <> tshow n <> "]" <> "foo[/size]"
pure $ actual === expected
, "size=20"
=: divAttrs [("size", "20")] (para "foo")
=?> "[size=20]foo[/size]"
, "color=#AAAAAA"
=: divAttrs [("color", "#AAAAAA")] (para "foo")
=?> "[color=#AAAAAA]foo[/color]"
, "spoiler"
=: divAttrs
[("spoiler", "name with spaces and ]brackets[]")]
(para "foo")
=?> "[spoiler=name with spaces and brackets]foo[/spoiler]"
]
, testGroup
"default flavor"
[ "link"
=: link "https://example.com" "title" "label"
=?> "[url=https://example.com]label[/url]"
, "autolink"
=: link "https://example.com" "title" "https://example.com"
=?> "[url]https://example.com[/url]"
, "email autolink"
=: link
"mailto:[email protected]"
"title"
"[email protected]"
=?> "[email][email protected][/email]"
, "named email"
=: link "mailto:[email protected]" "title" "example email"
=?> "[[email protected]]example email[/email]"
, "h0" =: header 0 "heading 0" =?> "[u][b]heading 0[/b][/u]"
, "h1" =: header 1 "heading 1" =?> "[u][b]heading 1[/b][/u]"
, "h2" =: header 2 "heading 2" =?> "[b]heading 2[/b]"
, "h3" =: header 3 "heading 3" =?> "[u]heading 3[/u]"
, "h4" =: header 4 "heading 4" =?> "heading 4"
, "h5" =: header 5 "heading 5" =?> "heading 5"
]
, testGroup
"steam"
[ test bbcodeSteam "dename spoiler" $
divAttrs [("spoiler", "bar")] (para "foo")
=?> ("[spoiler]foo[/spoiler]" :: Text)
, testProperty "ordered list styleless" . property $ do
let listItems = [para "foo", para "bar", para "baz"]
attrsRand <- (,,) <$> arbitrary <*> arbitrary <*> arbitrary
let actual = bbcodeSteam $ orderedListWith attrsRand listItems
let expected = "[olist]\n[*]foo\n[*]bar\n[*]baz\n[/olist]"
pure $ actual === expected
, "h0" `steam` header 0 "heading 0" =?> "[h1]heading 0[/h1]"
, "h1" `steam` header 1 "heading 1" =?> "[h1]heading 1[/h1]"
, "h2" `steam` header 2 "heading 2" =?> "[h2]heading 2[/h2]"
, "h3" `steam` header 3 "heading 3" =?> "[h3]heading 3[/h3]"
, "h4" `steam` header 4 "heading 4" =?> "[h3]heading 4[/h3]"
, "code"
`steam` codeWith ("id", ["haskell"], []) "map (2^) [1..5]"
=?> "[noparse]map (2^) [1..5][/noparse]"
]
, testGroup
"phpBB"
[ "image"
`phpbb` imageWith
("id", [], [("width", "100")])
"https://example.com"
"title"
"alt text"
=?> "[img]https://example.com[/img]"
]
, testGroup
"FluxBB"
[ "image"
`fluxbb` imageWith
("id", [], [("width", "100")])
"https://example.com"
"title"
"alt text"
=?> "[img=alt text]https://example.com[/img]"
, testProperty "ordered list" . property $ do
let listItems = [para "foo", para "bar", para "baz"]
attrsRand <- (,,) <$> arbitrary <*> arbitrary <*> arbitrary
let actual = bbcodeFluxBB $ orderedListWith attrsRand listItems
let opening = case attrsRand of
(_, LowerAlpha, _) -> "[list=a]"
(_, UpperAlpha, _) -> "[list=a]"
_ -> "[list=1]"
let expected = opening <> "\n[*]foo\n[*]bar\n[*]baz\n[/list]"
pure $ actual === expected
, "ulist > BlockQuote not rendered"
`fluxbb` bulletList [blockQuote (para "foo") <> para "bar"]
=?> "[list]\n[*]bar\n[/list]"
, "code block"
`fluxbb` codeBlockWith
("id", ["haskell"], [])
( T.intercalate "\n" $
[ "vals ="
, " take 10"
, " . filter (\\x -> (x - 5) `mod` 3 == 0)"
, " $ map (2 ^) [1 ..]"
]
)
=?> T.intercalate
"\n"
[ "[code]vals ="
, " take 10"
, " . filter (\\x -> (x - 5) `mod` 3 == 0)"
, " $ map (2 ^) [1 ..]"
, "[/code]"
]
]
, testGroup
"Hubzilla"
[ "unordered list"
`hubzilla` bulletList [para "foo", para "bar", para "baz"]
=?> "[ul]\n[*]foo\n[*]bar\n[*]baz\n[/ul]"
, testProperty "ordered list" . property $ do
let listItems = [para "foo", para "bar", para "baz"]
attrsRand <- (,,) <$> arbitrary <*> arbitrary <*> arbitrary
let actual = bbcodeHubzilla $ orderedListWith attrsRand listItems
let (opening, closing) = case attrsRand of
(_, Decimal, _) -> ("[list=1]", "[/list]")
(_, DefaultStyle, _) -> ("[ol]", "[/ol]")
(_, Example, _) -> ("[ol]", "[/ol]")
(_, LowerAlpha, _) -> ("[list=a]", "[/list]")
(_, UpperAlpha, _) -> ("[list=A]", "[/list]")
(_, LowerRoman, _) -> ("[list=i]", "[/list]")
(_, UpperRoman, _) -> ("[list=I]", "[/list]")
let expected =
opening <> "\n[*]foo\n[*]bar\n[*]baz\n" <> closing
pure $ actual === expected
, "definition list"
`hubzilla` definitionList
[ ("term_foo", [para "def_foo1", para "def_foo2"])
, ("term_bar", [para "def_bar1", para "def_bar2"])
, ("term_baz", [para "def_baz1", para "def_baz2"])
]
=?> mconcat
[ "[dl terms=\"b\"]\n"
, "[*= term_foo]\ndef_foo1\ndef_foo2\n"
, "[*= term_bar]\ndef_bar1\ndef_bar2\n"
, "[*= term_baz]\ndef_baz1\ndef_baz2\n"
, "[/dl]"
]
, "h0" `hubzilla` header 0 "heading 0" =?> "[h1]heading 0[/h1]"
, "h1" `hubzilla` header 1 "heading 1" =?> "[h1]heading 1[/h1]"
, "h2" `hubzilla` header 2 "heading 2" =?> "[h2]heading 2[/h2]"
, "h3" `hubzilla` header 3 "heading 3" =?> "[h3]heading 3[/h3]"
, "h4" `hubzilla` header 4 "heading 4" =?> "[h4]heading 4[/h4]"
, "h5" `hubzilla` header 5 "heading 5" =?> "[h5]heading 5[/h5]"
, "h6" `hubzilla` header 6 "heading 6" =?> "[h6]heading 6[/h6]"
, "h7" `hubzilla` header 7 "heading 7" =?> "[h6]heading 7[/h6]"
, "link"
`hubzilla` link "https://example.com" "title" "label"
=?> "[url=https://example.com]label[/url]"
, "autolink"
`hubzilla` link "https://example.com" "title" "https://example.com"
=?> "[url]https://example.com[/url]"
, "email autolink"
`hubzilla` link
"mailto:[email protected]"
"title"
"[email protected]"
=?> "[url=mailto:[email protected]][email protected][/url]"
, "named email"
`hubzilla` link "mailto:[email protected]" "title" "example email"
=?> "[url=mailto:[email protected]]example email[/url]"
, "inline code"
`hubzilla` ( "inline code: "
<> codeWith ("id", ["haskell"], []) "map (2^) [1..5]"
)
=?> "inline code: [code]map (2^) [1..5][/code]"
, "font"
`hubzilla` divAttrs [("font", "serif")] (para "foo")
=?> "[font=serif]foo[/font]"
]
, testGroup
"xenForo"
[ "unordered list"
`xenforo` bulletList [para "foo", para "bar", para "baz"]
=?> "[list]\n[*]foo\n[*]bar\n[*]baz\n[/list]"
, testProperty "ordered list styleless" . property $ do
let listItems = [para "foo", para "bar", para "baz"]
attrsRand <- (,,) <$> arbitrary <*> arbitrary <*> arbitrary
let actual = bbcodeXenforo $ orderedListWith attrsRand listItems
let expected = "[list=1]\n[*]foo\n[*]bar\n[*]baz\n[/list]"
pure $ actual === expected
, "h0" `xenforo` header 0 "heading 0" =?> "[heading=1]heading 0[/heading]"
, "h1" `xenforo` header 1 "heading 1" =?> "[heading=1]heading 1[/heading]"
, "h2" `xenforo` header 2 "heading 2" =?> "[heading=2]heading 2[/heading]"
, "h3" `xenforo` header 3 "heading 3" =?> "[heading=3]heading 3[/heading]"
, "h4" `xenforo` header 4 "heading 4" =?> "[heading=4]heading 4[/heading]"
, "link"
`xenforo` link "https://example.com" "title" "label"
=?> "[url=https://example.com]label[/url]"
, "autolink"
`xenforo` link "https://example.com" "title" "https://example.com"
=?> "[url]https://example.com[/url]"
, "email autolink"
`xenforo` link
"mailto:[email protected]"
"title"
"[email protected]"
=?> "[email][email protected][/email]"
, "named email"
`xenforo` link "mailto:[email protected]" "title" "example email"
=?> "[[email protected]]example email[/email]"
, "inline code"
`xenforo` ( "inline code: "
<> codeWith ("id", ["haskell"], []) "map (2^) [1..5]"
)
=?> "inline code: [icode]map (2^) [1..5][/icode]"
, "font"
`xenforo` divAttrs [("font", "serif")] (para "foo")
=?> "[font=serif]foo[/font]"
, "inline spoiler"
`xenforo` ("It was " <> spanClasses ["spoiler"] ("DNS") <> "!")
=?> "It was [ispoiler]DNS[/ispoiler]!"
, "image w=50% h=50%"
`xenforo` imageWith
("", [], [("width", "50%"), ("height", "50%")])
"https://example.com"
"title text"
"alt text"
=?> "[img alt=\"alt text\" title=\"title text\" width=50%]https://example.com[/img]"
, "image w=50 h=50"
`xenforo` imageWith
("", [], [("width", "50"), ("height", "50")])
"https://example.com"
""
""
=?> "[img]https://example.com[/img]"
]
]
|