]> gitweb @ CieloNegro.org - Rakka.git/blob - tests/WikiParserTest.hs
Implemented more features
[Rakka.git] / tests / WikiParserTest.hs
1 module WikiParserTest
2     ( testData
3     )
4     where
5
6 import           Rakka.Wiki
7 import           Rakka.Wiki.Parser
8 import           Test.HUnit
9 import           Text.ParserCombinators.Parsec
10
11
12 cmdTypeOf :: String -> Maybe CommandType
13 cmdTypeOf "br"   = Just InlineCommandType
14 cmdTypeOf "i"    = Just InlineCommandType
15 cmdTypeOf "b"    = Just InlineCommandType
16 cmdTypeOf "span" = Just InlineCommandType
17 cmdTypeOf "div"  = Just BlockCommandType
18 cmdTypeOf _      = Nothing
19
20
21 parseWiki :: String -> Either String WikiPage
22 parseWiki src = case parse (wikiPage cmdTypeOf) "" src of
23                   Left  err  -> Left (show err)
24                   Right page -> Right page
25
26
27 testData :: [Test]
28 testData = [ (parseWiki ""
29               ~?=
30               (Right []))
31
32            , (parseWiki "\n"
33               ~?=
34               (Right []))
35
36            , (parseWiki "=heading="
37               ~?=
38               (Right [ Heading 1 "heading" ]))
39
40            , (parseWiki "==      heading  ==  \n"
41               ~?=
42               (Right [ Heading 2 "heading" ]))
43
44            , (parseWiki "===== hello world =====\n"
45               ~?=
46               (Right [ Heading 5 "hello world" ]))
47
48            , (parseWiki "a =not a heading="
49               ~?=
50               (Right [ Paragraph [ Text "a =not a heading=" ]
51                      ]))
52
53            , (parseWiki "=h=\n\n=h="
54               ~?=
55               (Right [ Heading 1 "h"
56                      , Heading 1 "h"
57                      ]))
58            , (parseWiki "foo\nbar"
59               ~?=
60               (Right [ Paragraph [ Text "foo"
61                                  , Text "\n"
62                                  , Text "bar"
63                                  ]
64                      ]))
65            , (parseWiki "foo\nbar\n\nbaz\n"
66               ~?=
67               (Right [ Paragraph [ Text "foo"
68                                  , Text "\n"
69                                  , Text "bar"
70                                  ]
71                      , Paragraph [ Text "baz"
72                                  ]
73                      ]))
74
75            , (parseWiki "foo\n\n\nbar"
76               ~?=
77               (Right [ Paragraph [ Text "foo" ]
78                      , Paragraph [ Text "bar" ]
79                      ]))
80
81            , (parseWiki "foo\n=h="
82               ~?=
83               (Right [ Paragraph [ Text "foo" ]
84                      , Heading 1 "h"
85                      ]))
86
87            , (parseWiki "<!-- comment -->"
88               ~?=
89               (Right []))
90
91            , (parseWiki "<!-- comment -->foo"
92               ~?=
93               (Right [ Paragraph [ Text "foo" ]
94                      ]))
95
96            , (parseWiki "bar<!-- comment -->"
97               ~?=
98               (Right [ Paragraph [ Text "bar" ]
99                      ]))
100
101            , (parseWiki "foo<!-- comment -->bar"
102               ~?=
103               (Right [ Paragraph [ Text "foo"
104                                  , Text "bar"
105                                  ]
106                      ]))
107
108            , (parseWiki "<!-- comment -->=h="
109               ~?=
110               (Right [ Heading 1 "h" ]))
111
112            , (parseWiki "=h= <!---->"
113               ~?=
114               (Right [ Heading 1 "h" ]))
115
116            , (parseWiki "<!-- <!-- nested --> comment -->"
117               ~?=
118               (Right []))
119
120            , (parseWiki "[[Page]]"
121               ~?= 
122               (Right [ Paragraph [ PageLink (Just "Page") Nothing Nothing ]
123                      ]))
124
125            , (parseWiki "[[Page|Link to \"Page\"]]"
126               ~?=
127               (Right [ Paragraph [ PageLink (Just "Page") Nothing (Just "Link to \"Page\"") ]
128                      ]))
129
130            , (parseWiki "[[Page#foo]]"
131               ~?= 
132               (Right [ Paragraph [ PageLink (Just "Page") (Just "foo") Nothing ]
133                      ]))
134
135            , (parseWiki "[[#foo]]"
136               ~?= 
137               (Right [ Paragraph [ PageLink Nothing (Just "foo") Nothing ]
138                      ]))
139
140            , (parseWiki "[[Page#foo|Link to \"Page#foo\"]]"
141               ~?=
142               (Right [ Paragraph [ PageLink (Just "Page") (Just "foo") (Just "Link to \"Page#foo\"") ]
143                      ]))
144
145            , (parseWiki "foo [[Bar]] baz"
146               ~?=
147               (Right [ Paragraph [ Text "foo "
148                                  , PageLink (Just "Bar") Nothing Nothing
149                                  , Text " baz"
150                                  ]
151                      ]))
152
153            , (parseWiki "[[Foo]]\n[[Bar]]"
154               ~?= 
155               (Right [ Paragraph [ PageLink (Just "Foo") Nothing Nothing
156                                  , Text "\n"
157                                  , PageLink (Just "Bar") Nothing Nothing
158                                  ]
159                      ]))
160
161            , (parseWiki " foo"
162               ~?=
163               (Right [ Preformatted [ Text "foo" ] ]))
164
165            , (parseWiki " foo\n  bar\n"
166               ~?=
167               (Right [ Preformatted [ Text "foo"
168                                     , Text "\n"
169                                     , Text " bar"
170                                     ]
171                      ]))
172
173            , (parseWiki "foo\n bar\nbaz"
174               ~?=
175               (Right [ Paragraph    [ Text "foo" ]
176                      , Preformatted [ Text "bar" ]
177                      , Paragraph    [ Text "baz" ]
178                      ]))
179
180            , (parseWiki "----"
181               ~?=
182               (Right [ HorizontalLine ]))
183
184            , (parseWiki "\nfoo\nbar\n----\n"
185               ~?=
186               (Right [ Paragraph [ Text "foo"
187                                  , Text "\n"
188                                  , Text "bar"
189                                  ]
190                      , HorizontalLine
191                      ]))
192
193            , (parseWiki "a----b"
194               ~?=
195               (Right [ Paragraph [ Text "a----b" ] ]))
196
197            , (parseWiki "* a"
198               ~?=
199               (Right [ List (ListElement Bullet [[Right (Text "a")]]) ]))
200
201            , (parseWiki "* a*"
202               ~?=
203               (Right [ List (ListElement Bullet [[Right (Text "a*")]]) ]))
204
205            , (parseWiki "* a\n* b\n"
206               ~?=
207               (Right [ List (ListElement Bullet [ [Right (Text "a")]
208                                                 , [Right (Text "b")]
209                                                 ])
210                      ]))
211
212            , (parseWiki "*a\n*#b\n*#c\n"
213               ~?=
214               (Right [ List (ListElement Bullet [ [ Right (Text "a")
215                                                   , Left (ListElement Numbered [ [Right (Text "b")]
216                                                                                , [Right (Text "c")]
217                                                                                ])
218                                                   ]
219                                                 ])
220                      ]))
221
222            , (parseWiki "*a\n#b"
223               ~?=
224               (Right [ List (ListElement Bullet   [ [Right (Text "a")] ])
225                      , List (ListElement Numbered [ [Right (Text "b")] ])
226                      ]))
227
228            , (parseWiki "*a<!-- comment -->"
229               ~?=
230               (Right [ List (ListElement Bullet [ [Right (Text "a")] ]) ]))
231
232            , (parseWiki "*a<!-- comment -->\n*b"
233               ~?=
234               (Right [ List (ListElement Bullet [ [Right (Text "a")]
235                                                 , [Right (Text "b")]
236                                                 ])
237                      ]))
238
239            , (parseWiki "foo:bar"
240               ~?=
241               (Right [ Paragraph [ Text "foo"
242                                  , Text ":bar"
243                                  ]
244                      ]))
245
246            , (parseWiki "; foo: bar"
247               ~?=
248               (Right [ DefinitionList [Definition [Text "foo"] [Text "bar"]] ]))
249
250            , (parseWiki "; foo: bar\n"
251               ~?=
252               (Right [ DefinitionList [Definition [Text "foo"] [Text "bar"]] ]))
253
254            , (parseWiki "; foo\n: bar\n; bar\n: baz\n: baz"
255               ~?=
256               (Right [ DefinitionList [ Definition [Text "foo"] [ Text "bar" ]
257                                       , Definition [Text "bar"] [ Text "baz"
258                                                                 , Text "\n"
259                                                                 , Text "baz" ]
260                                       ]
261                      ]))
262
263            , (parseWiki "<![CDATA[foo [[bar]] baz]]>"
264               ~?=
265               (Right [ Paragraph [ Text "foo [[bar]] baz" ] ]))
266
267            , (parseWiki "<![PDATA[foo [[bar]] baz]]>"
268               ~?=
269               (Right [ Preformatted [ Text "foo [[bar]] baz" ] ]))
270
271            , (parseWiki "<![PDATA[\nfoo [[bar]] baz\n]]>"
272               ~?=
273               (Right [ Preformatted [ Text "foo [[bar]] baz" ] ]))
274
275            , (parseWiki "foo' bar"
276               ~?=
277               (Right [ Paragraph [ Text "foo"
278                                  , Text "'"
279                                  , Text " bar" ]
280                      ]))
281
282            , (parseWiki "''foo''"
283               ~?=
284               (Right [ Paragraph [ Italic [Text "foo"] ] ]))
285
286            , (parseWiki "'''foo'''"
287               ~?=
288               (Right [ Paragraph [ Bold [Text "foo"] ] ]))
289
290            , (parseWiki "foo''''"
291               ~?=
292               (Right [ Paragraph [ Text "foo"
293                                  , Text "'"
294                                  ]
295                      ]))
296
297            , (parseWiki "'''''foo'''''"
298               ~?=
299               (Right [ Paragraph [ Italic [Bold [Text "foo"]] ] ]))
300
301            , (parseWiki "<br />"
302               ~?=
303               (Right [ Paragraph [ InlineCmd (InlineCommand "br" [] []) ] ]))
304
305            , (parseWiki "<br style=\"clear: both\"/>"
306               ~?=
307               (Right [ Paragraph [ InlineCmd (InlineCommand "br" [("style", "clear: both")] []) ] ]))
308
309            , (parseWiki "<i><b>foo</b></i>"
310               ~?=
311               (Right [ Paragraph [ InlineCmd (InlineCommand "i" []
312                                               [ InlineCmd (InlineCommand "b" [] [ Text "foo" ]) ]) ] ]))
313
314            , (parseWiki "<i>\nfoo\n<!-- comment -->\nbar</i>"
315               ~?=
316               (Right [ Paragraph [ InlineCmd (InlineCommand "i" []
317                                               [ Text "\n"
318                                               , Text "foo"
319                                               , Text "\n"
320                                               , Text "\n"
321                                               , Text "bar"
322                                               ]) ] ]))
323            ]