Revision: 6680 Author: nicdumz Date: 2009-04-23 05:57:01 +0000 (Thu, 23 Apr 2009)
Log Message: ----------- Making sure that Link.__hash__ is consistent with Link.__cmp__ Adding a relevant unit test
Modified Paths: -------------- branches/rewrite/pywikibot/page.py branches/rewrite/tests/page_tests.py
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2009-04-23 05:21:30 UTC (rev 6679) +++ branches/rewrite/pywikibot/page.py 2009-04-23 05:57:01 UTC (rev 6680) @@ -1947,7 +1947,9 @@ return self.astext()
def __hash__(self): - return hash(self.astext()) + return hash(u'%s:%s:%s' % (self.site.family.name, + self.site.code, + self.title))
@staticmethod def fromPage(page, source=None):
Modified: branches/rewrite/tests/page_tests.py =================================================================== --- branches/rewrite/tests/page_tests.py 2009-04-23 05:21:30 UTC (rev 6679) +++ branches/rewrite/tests/page_tests.py 2009-04-23 05:57:01 UTC (rev 6680) @@ -24,6 +24,9 @@ """Test cases for Link objects"""
enwiki = pywikibot.Site("en", "wikipedia") + frwiki = pywikibot.Site("fr", "wikipedia") + itwikt = pywikibot.Site("it", "wiktionary") + namespaces = {0: [u""], # en.wikipedia.org namespaces for testing 1: [u"Talk:"], # canonical form first, then others 2: [u"User:"], # must end with : @@ -84,7 +87,25 @@ m = pywikibot.page.Link(":"+self.namespaces[num][0]+title) self.assertEqual(m.title, self.titles[title])
+ def testHashCmp(self): + # All links point to en:wikipedia:Test + l1 = pywikibot.page.Link('Test', source=self.enwiki) + l2 = pywikibot.page.Link('en:Test', source=self.frwiki) + l3 = pywikibot.page.Link('wikipedia:en:Test', source=self.itwikt) + def assertHashCmp(link1, link2): + self.assertEqual(link1, link2) + self.assertEqual(hash(link1), hash(link2))
+ assertHashCmp(l1, l2) + assertHashCmp(l1, l3) + assertHashCmp(l2, l3) + + # fr:wikipedia:Test + other = pywikibot.page.Link('Test', source=self.frwiki) + + self.assertNotEqual(l1, other) + self.assertNotEqual(hash(l1), hash(other)) + class TestPageObject(unittest.TestCase): def testGeneral(self): self.assertEqual(str(mainpage), "[[%s:%s]]"
pywikipedia-svn@lists.wikimedia.org