comparison MoinMoin/parser/_tests/test_text_moin_wiki.py @ 2624:3d30e923448f

wiki parser tests: convert to py.test
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 10 Aug 2007 12:10:20 +0200
parents 5c6d61bf88c5
children 388204baf00f
comparison
equal deleted inserted replaced
2623:5c6d61bf88c5 2624:3d30e923448f
7 7
8 @copyright: 2003-2004 by Juergen Hermann <jh@web.de> 8 @copyright: 2003-2004 by Juergen Hermann <jh@web.de>
9 @license: GNU GPL, see COPYING for details. 9 @license: GNU GPL, see COPYING for details.
10 """ 10 """
11 11
12 import unittest # LEGACY UNITTEST, PLEASE DO NOT IMPORT unittest IN NEW TESTS, PLEASE CONSULT THE py.test DOCS
13 import re 12 import re
14 from StringIO import StringIO 13 from StringIO import StringIO
15 14
16 import py 15 import py
17 16
18 from MoinMoin.Page import Page 17 from MoinMoin.Page import Page
19 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser 18 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
20 from MoinMoin.formatter.text_html import Formatter as HtmlFormatter 19 from MoinMoin.formatter.text_html import Formatter as HtmlFormatter
21 20
22 21
23 class ParserTestCase(unittest.TestCase): 22 class ParserTestCase(object):
24 """ Helper class that provide a parsing method """ 23 """ Helper class that provide a parsing method """
25 24
26 def parse(self, body): 25 def parse(self, body):
27 """Parse body and return html 26 """Parse body and return html
28 27
53 """ 52 """
54 53
55 def testFirstParagraph(self): 54 def testFirstParagraph(self):
56 """ parser.wiki: first paragraph should be in <p> """ 55 """ parser.wiki: first paragraph should be in <p> """
57 result = self.parse('First') 56 result = self.parse('First')
58 expected = re.compile(r'<p.*?>\s*First\s*') 57 assert re.search(r'<p.*?>\s*First\s*', result)
59 self.assert_(expected.search(result),
60 '"%s" not in "%s"' % (expected.pattern, result))
61 58
62 def testEmptyLineBetweenParagraphs(self): 59 def testEmptyLineBetweenParagraphs(self):
63 """ parser.wiki: empty line separates paragraphs """ 60 """ parser.wiki: empty line separates paragraphs """
64 result = self.parse('First\n\nSecond') 61 result = self.parse('First\n\nSecond')
65 expected = re.compile(r'<p.*?>\s*Second\s*') 62 assert re.search(r'<p.*?>\s*Second\s*', result)
66 self.assert_(expected.search(result),
67 '"%s" not in "%s"' % (expected.pattern, result))
68 63
69 def testParagraphAfterBlockMarkup(self): 64 def testParagraphAfterBlockMarkup(self):
70 """ parser.wiki: create paragraph after block markup """ 65 """ parser.wiki: create paragraph after block markup """
71 66
72 markup = ( 67 markup = (
80 # '[[en]]\n', XXX crashes 75 # '[[en]]\n', XXX crashes
81 ) 76 )
82 for item in markup: 77 for item in markup:
83 text = item + 'Paragraph' 78 text = item + 'Paragraph'
84 result = self.parse(text) 79 result = self.parse(text)
85 expected = re.compile(r'<p.*?>\s*Paragraph\s*') 80 assert re.search(r'<p.*?>\s*Paragraph\s*', result)
86 self.assert_(expected.search(result),
87 '"%s" not in "%s"' % (expected.pattern, result))
88 81
89 82
90 class TestHeadings(ParserTestCase): 83 class TestHeadings(ParserTestCase):
91 """ Test various heading problems """ 84 """ Test various heading problems """
92 85
93 def setUp(self): 86 def class_setup(self):
94 """ Require show_section_numbers = 0 to workaround counter 87 """ Require show_section_numbers = 0 to workaround counter
95 global state saved in request. 88 global state saved in request.
96 """ 89 """
97 self.config = self.TestConfig(show_section_numbers=0) 90 self.config = self.TestConfig(show_section_numbers=0)
98 91
99 def tearDown(self): 92 def class_teardown(self):
100 del self.config 93 del self.config
101 94
102 def testIgnoreWhiteSpaceAroundHeadingText(self): 95 def testIgnoreWhiteSpaceAroundHeadingText(self):
103 """ parser.wiki: ignore white space around heading text 96 """ parser.wiki: ignore white space around heading text
104 97
112 '= head =\n' # both 105 '= head =\n' # both
113 ) 106 )
114 expected = self.parse('= head =') 107 expected = self.parse('= head =')
115 for test in tests: 108 for test in tests:
116 result = self.parse(test) 109 result = self.parse(test)
117 self.assertEqual(result, expected, 110 assert result == expected
118 'Expected "%(expected)s" but got "%(result)s"' % locals())
119 111
120 112
121 class TestTOC(ParserTestCase): 113 class TestTOC(ParserTestCase):
122 114
123 def setUp(self): 115 def class_setup(self):
124 """ Require show_section_numbers = 0 to workaround counter 116 """ Require show_section_numbers = 0 to workaround counter
125 global state saved in request. 117 global state saved in request.
126 """ 118 """
127 self.config = self.TestConfig(show_section_numbers=0) 119 self.config = self.TestConfig(show_section_numbers=0)
128 120
129 def tearDown(self): 121 def class_teardown(self):
130 del self.config 122 del self.config
131 123
132 def testHeadingWithWhiteSpace(self): 124 def testHeadingWithWhiteSpace(self):
133 """ parser.wiki: TOC links to headings with white space 125 """ parser.wiki: TOC links to headings with white space
134 126
147 = heading = 139 = heading =
148 Text 140 Text
149 """ 141 """
150 expected = self.parse(standard) 142 expected = self.parse(standard)
151 result = self.parse(withWhitespace) 143 result = self.parse(withWhitespace)
152 self.assertEqual(result, expected, 144 assert result == expected
153 'Expected "%(expected)s" but got "%(result)s"' % locals())
154 145
155 146
156 class TestDateTimeMacro(ParserTestCase): 147 class TestDateTimeMacro(ParserTestCase):
157 """ Test DateTime macro 148 """ Test DateTime macro
158 149
180 (u'[[DateTime(2000-01-01T00:00:00Z)]]', '2000-01-01 00:00:00'), # works for Europe/Vilnius 171 (u'[[DateTime(2000-01-01T00:00:00Z)]]', '2000-01-01 00:00:00'), # works for Europe/Vilnius
181 (u'[[Date(2002-02-02T01:02:03Z)]]', '2002-02-02'), 172 (u'[[Date(2002-02-02T01:02:03Z)]]', '2002-02-02'),
182 (u'[[DateTime(1970-01-06T00:00:00)]]', '1970-01-06 00:00:00'), # fails e.g. for Europe/Vilnius 173 (u'[[DateTime(1970-01-06T00:00:00)]]', '1970-01-06 00:00:00'), # fails e.g. for Europe/Vilnius
183 ) 174 )
184 175
185 def setUp(self): 176 def class_setup(self):
186 """ Require default date and time format config values """ 177 """ Require default date and time format config values """
187 self.config = self.TestConfig(defaults=('date_fmt', 'datetime_fmt')) 178 self.config = self.TestConfig(defaults=('date_fmt', 'datetime_fmt'))
188 179
189 def tearDown(self): 180 def class_teardown(self):
190 del self.config 181 del self.config
191 182
192 def testDateTimeMacro(self): 183 def testDateTimeMacro(self):
193 """ parser.wiki: DateTime macro """ 184 """ parser.wiki: DateTime macro """
194 note = """ 185 note = """
200 """ 191 """
201 192
202 for test, expected in self._tests: 193 for test, expected in self._tests:
203 html = self.parse(self.text % test) 194 html = self.parse(self.text % test)
204 result = self.needle.search(html).group(1) 195 result = self.needle.search(html).group(1)
205 self.assertEqual(result, expected, 196 assert result == expected
206 'Expected "%(expected)s" but got "%(result)s"; %(note)s' % locals())
207 197
208 198
209 class TestTextFormatingTestCase(ParserTestCase): 199 class TestTextFormatingTestCase(ParserTestCase):
210 """ Test wiki markup """ 200 """ Test wiki markup """
211 201
226 def testTextFormating(self): 216 def testTextFormating(self):
227 """ parser.wiki: text formating """ 217 """ parser.wiki: text formating """
228 for test, expected in self._tests: 218 for test, expected in self._tests:
229 html = self.parse(self.text % test) 219 html = self.parse(self.text % test)
230 result = self.needle.search(html).group(1) 220 result = self.needle.search(html).group(1)
231 self.assertEqual(result, expected, 221 assert result == expected
232 'Expected "%(expected)s" but got "%(result)s"' % locals())
233 222
234 223
235 class TestCloseInlineTestCase(ParserTestCase): 224 class TestCloseInlineTestCase(ParserTestCase):
236 225
237 def testCloseOneInline(self): 226 def testCloseOneInline(self):
245 ("text ''em '''em strong __em strong underline", 234 ("text ''em '''em strong __em strong underline",
246 r"text <em>em <strong>em strong <span class=\"u\">em strong underline" 235 r"text <em>em <strong>em strong <span class=\"u\">em strong underline"
247 r"\s*</span></strong></em></p>"), 236 r"\s*</span></strong></em></p>"),
248 ) 237 )
249 for test, expected in cases: 238 for test, expected in cases:
250 needle = re.compile(expected)
251 result = self.parse(test) 239 result = self.parse(test)
252 self.assert_(needle.search(result), 240 assert re.search(expected, result)
253 'Expected "%(expected)s" but got "%(result)s"' % locals())
254 241
255 242
256 class TestInlineCrossing(ParserTestCase): 243 class TestInlineCrossing(ParserTestCase):
257 """ 244 """
258 This test case fail with current parser/formatter and should be fixed in 2.0 245 This test case fail with current parser/formatter and should be fixed in 2.0
261 def disabled_testInlineCrossing(self): 248 def disabled_testInlineCrossing(self):
262 """ parser.wiki: prevent inline crossing <a><b></a></b> """ 249 """ parser.wiki: prevent inline crossing <a><b></a></b> """
263 250
264 expected = (r"<p><em>a<strong>ab</strong></em><strong>b</strong>\s*</p>") 251 expected = (r"<p><em>a<strong>ab</strong></em><strong>b</strong>\s*</p>")
265 test = "''a'''ab''b'''\n" 252 test = "''a'''ab''b'''\n"
266 needle = re.compile(expected)
267 result = self.parse(test) 253 result = self.parse(test)
268 self.assert_(needle.search(result), 254 assert re.search(expected, result)
269 'Expected "%(expected)s" but got "%(result)s"' % locals())
270 255
271 256
272 class TestEscapeHTML(ParserTestCase): 257 class TestEscapeHTML(ParserTestCase):
273 258
274 def testEscapeInTT(self): 259 def testEscapeInTT(self):
326 self._test(test) 311 self._test(test)
327 312
328 def _test(self, test): 313 def _test(self, test):
329 expected = r'&lt;escape-me&gt;' 314 expected = r'&lt;escape-me&gt;'
330 result = self.parse(test) 315 result = self.parse(test)
331 self.assert_(re.search(expected, result), 316 assert re.search(expected, result)
332 'Expected "%(expected)s" but got "%(result)s"' % locals())
333 317
334 318
335 class TestEscapeWikiTableMarkup(ParserTestCase): 319 class TestEscapeWikiTableMarkup(ParserTestCase):
336 320
337 def testEscapeInTT(self): 321 def testEscapeInTT(self):
369 self.do(test) 353 self.do(test)
370 354
371 def do(self, test): 355 def do(self, test):
372 expected = r'&lt;tablewidth="80"&gt;' 356 expected = r'&lt;tablewidth="80"&gt;'
373 result = self.parse(test) 357 result = self.parse(test)
374 self.assert_(re.search(expected, result), 358 assert re.search(expected, result)
375 'Expected "%(expected)s" but got "%(result)s"' % locals())
376 359
377 360
378 class TestRule(ParserTestCase): 361 class TestRule(ParserTestCase):
379 """ Test rules markup """ 362 """ Test rules markup """
380 363
381 def testNotRule(self): 364 def testNotRule(self):
382 """ parser.wiki: --- is no rule """ 365 """ parser.wiki: --- is no rule """
383 result = self.parse('---') 366 result = self.parse('---')
384 expected = '---' # inside <p> 367 expected = '---' # inside <p>
385 self.assert_(expected in result, 368 assert expected in result
386 'Expected "%(expected)s" but got "%(result)s"' % locals())
387 369
388 def testStandardRule(self): 370 def testStandardRule(self):
389 """ parser.wiki: ---- is standard rule """ 371 """ parser.wiki: ---- is standard rule """
390 result = self.parse('----') 372 result = self.parse('----')
391 expected = re.compile(r'<hr.*?>') 373 assert re.search(r'<hr.*?>', result)
392 self.assert_(expected.search(result),
393 'Expected "%(expected)s" but got "%(result)s"' % locals())
394 374
395 def testVariableRule(self): 375 def testVariableRule(self):
396 """ parser.wiki: ----- rules with size """ 376 """ parser.wiki: ----- rules with size """
397 377
398 for size in range(5, 11): 378 for size in range(5, 11):
399 test = '-' * size 379 test = '-' * size
400 result = self.parse(test) 380 result = self.parse(test)
401 expected = re.compile(r'<hr class="hr%d".*?>' % (size - 4)) 381 assert re.search(r'<hr class="hr%d".*?>' % (size - 4), result)
402 self.assert_(expected.search(result),
403 'Expected "%(expected)s" but got "%(result)s"' % locals())
404 382
405 def testLongRule(self): 383 def testLongRule(self):
406 """ parser.wiki: ------------ long rule shortened to hr6 """ 384 """ parser.wiki: ------------ long rule shortened to hr6 """
407 test = '-' * 254 385 test = '-' * 254
408 result = self.parse(test) 386 result = self.parse(test)
409 expected = re.compile(r'<hr class="hr6".*?>') 387 assert re.search(r'<hr class="hr6".*?>', result)
410 self.assert_(expected.search(result),
411 'Expected "%(expected)s" but got "%(result)s"' % locals())
412 388
413 389
414 class TestBlock(ParserTestCase): 390 class TestBlock(ParserTestCase):
415 cases = ( 391 cases = (
416 # test, block start 392 # test, block start
432 for test, blockstart in self.cases: 408 for test, blockstart in self.cases:
433 # We dont test here formatter white space generation 409 # We dont test here formatter white space generation
434 expected = r'<p.*?>AAA\s*\n*%s' % blockstart 410 expected = r'<p.*?>AAA\s*\n*%s' % blockstart
435 needle = re.compile(expected, re.MULTILINE) 411 needle = re.compile(expected, re.MULTILINE)
436 result = self.parse(text % test) 412 result = self.parse(text % test)
437 match = needle.search(result) 413 assert needle.search(result)
438 self.assert_(match is not None,
439 'Expected "%(expected)s" but got "%(result)s"' % locals())
440 414
441 def testEmptyLineBeforeBlock(self): 415 def testEmptyLineBeforeBlock(self):
442 """ parser.wiki: empty lines before block element ignored 416 """ parser.wiki: empty lines before block element ignored
443 417
444 Empty lines separate paragraphs, but should be ignored if a block 418 Empty lines separate paragraphs, but should be ignored if a block
453 """ 427 """
454 for test, blockstart in self.cases: 428 for test, blockstart in self.cases:
455 expected = r'<p.*?>AAA.*?(<p.*?>\s*)*%s' % blockstart # XXX ignores addtl. <p> 429 expected = r'<p.*?>AAA.*?(<p.*?>\s*)*%s' % blockstart # XXX ignores addtl. <p>
456 needle = re.compile(expected, re.MULTILINE) 430 needle = re.compile(expected, re.MULTILINE)
457 result = self.parse(text % test) 431 result = self.parse(text % test)
458 match = needle.search(result) 432 assert needle.search(result)
459 self.assert_(match is not None,
460 'Expected "%(expected)s" but got "%(result)s"' % locals())
461 433
462 def testUrlAfterBlock(self): 434 def testUrlAfterBlock(self):
463 """ parser.wiki: tests url after block element """ 435 """ parser.wiki: tests url after block element """
464 case = 'some text {{{some block text\n}}} and a URL http://moinmo.in/' 436 case = 'some text {{{some block text\n}}} and a URL http://moinmo.in/'
465 437
591 result = self.needle.search(html).group(1) 563 result = self.needle.search(html).group(1)
592 assert result == expected 564 assert result == expected
593 565
594 566
595 coverage_modules = ['MoinMoin.parser.text_moin_wiki'] 567 coverage_modules = ['MoinMoin.parser.text_moin_wiki']
568