http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9199
Revision: 9199 Author: jayvdb Date: 2011-04-25 15:02:45 +0000 (Mon, 25 Apr 2011) Log Message: ----------- Add three unittests that demonstrate replaceCategoryLinks() does not support round-trips - instead it removes the last eol
Modified Paths: -------------- trunk/pywikipedia/tests/test_textlib.py
Modified: trunk/pywikipedia/tests/test_textlib.py =================================================================== --- trunk/pywikipedia/tests/test_textlib.py 2011-04-25 12:53:39 UTC (rev 9198) +++ trunk/pywikipedia/tests/test_textlib.py 2011-04-25 15:02:45 UTC (rev 9199) @@ -5,17 +5,22 @@ __version__ = '$Id$'
import unittest -import tests.test_pywiki +from tests.test_pywiki import PyWikiTestCase
import wikipedia as pywikibot import pywikibot.textlib as textlib import catlib
-class PyWikiTextLibTestCase(tests.test_pywiki.PyWikiTestCase): +class PyWikiTextLibTestCase(PyWikiTestCase):
result1 = '[[Category:Cat1]]\r\n[[Category:Cat2]]\r\n'
+ def setUp(self): + self.site = pywikibot.getSite('en', 'wikipedia') + self.data = [catlib.Category(self.site, 'Category:Cat1'), + catlib.Category(self.site, 'Category:Cat2')] + def test_categoryFormat_raw(self): self.assertEqual(self.result1, textlib.categoryFormat(['[[Category:Cat1]]', @@ -27,14 +32,32 @@ textlib.categoryFormat(['Cat1', 'Cat2'], self.site))
def test_categoryFormat_Category(self): - data = [catlib.Category(self.site, 'Category:Cat1'), - catlib.Category(self.site, 'Category:Cat2')] - self.assertEqual(self.result1, textlib.categoryFormat(data, self.site)) + self.assertEqual(self.result1, + textlib.categoryFormat(self.data, self.site))
def test_categoryFormat_Page(self): data = [pywikibot.Page(self.site, 'Category:Cat1'), pywikibot.Page(self.site, 'Category:Cat2')] - self.assertEqual(self.result1, textlib.categoryFormat(data, self.site)) + self.assertEqual(self.result1, textlib.categoryFormat(self.data, + self.site))
+ def assertEqualCategoryRoundtrip(self, text, catcount): + cats = textlib.getCategoryLinks(text) + self.assertEqual(len(cats), catcount) + self.assertEqual(text, textlib.replaceCategoryLinks(text, + cats, + site = self.site)) + + def test_replaceCategoryLinks(self): + self.assertEqualCategoryRoundtrip(self.result1,2) + + def test_replaceCategoryLinks1(self): + result = 'Blah\r\n\r\n[[Category:Cat1]]\r\n[[Category:Cat2]]\r\n' + self.assertEqualCategoryRoundtrip(result,2) + + def test_replaceCategoryLinks2(self): + result = 'Blah\r\n\r\n[[Category:Cat1]]\r\n[[Category:Cat2]]\r\n\r\n[[fr:Test]]\r\n' + self.assertEqualCategoryRoundtrip(result,2) + if __name__ == "__main__": unittest.main()
pywikipedia-svn@lists.wikimedia.org