changeset 2247:d782b0db4757

wikidicts: more comments, minor cleanup, converted tests to py.test
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 22 Jun 2007 14:05:11 +0200
parents 1b8b4e7bfba5
children 0131f0aabb18
files MoinMoin/_tests/test_wikidicts.py MoinMoin/wikidicts.py
diffstat 2 files changed, 47 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikidicts.py	Fri Jun 22 00:46:29 2007 +0200
+++ b/MoinMoin/_tests/test_wikidicts.py	Fri Jun 22 14:05:11 2007 +0200
@@ -2,38 +2,39 @@
 """
     MoinMoin - MoinMoin.wikidicts tests
 
-    @copyright: 2003-2004 by Juergen Hermann <jh@web.de>
+    @copyright: 2003-2004 by Juergen Hermann <jh@web.de>,
+                2007 by MoinMoin:ThomasWaldmann
     @license: GNU GPL, see COPYING for details.
 """
 
-import unittest # LEGACY UNITTEST, PLEASE DO NOT IMPORT unittest IN NEW TESTS, PLEASE CONSULT THE py.test DOCS
+import py
 import re
 
 from MoinMoin import wikidicts
 from MoinMoin import Page
 
-class TestGroupPage(unittest.TestCase):
+class TestGroupPage:
 
     def testCamelCase(self):
         """ wikidicts: initFromText: CamelCase links """
         text = """
  * CamelCase
 """
-        self.assertEqual(self.getMembers(text), ['CamelCase'])
+        assert self.getMembers(text) == ['CamelCase']
 
     def testExtendedName(self):
         """ wikidicts: initFromText: extended names """
         text = """
  * extended name
 """
-        self.assertEqual(self.getMembers(text), ['extended name'])
+        assert self.getMembers(text) == ['extended name']
 
     def testExtendedLink(self):
         """ wikidicts: initFromText: extended link """
         text = """
  * ["extended link"]
 """
-        self.assertEqual(self.getMembers(text), ['extended link'])
+        assert self.getMembers(text) == ['extended link']
 
     def testIgnoreSecondLevelList(self):
         """ wikidicts: initFromText: ignore non first level items """
@@ -43,7 +44,7 @@
     * forth level
      * and then some...
 """
-        self.assertEqual(self.getMembers(text), [])
+        assert self.getMembers(text) == []
 
     def testIgnoreOther(self):
         """ wikidicts: initFromText: ignore anything but first level list itmes """
@@ -53,14 +54,14 @@
 
 Ignore previous line and this text.
 """
-        self.assertEqual(self.getMembers(text), ['take this'])
+        assert self.getMembers(text) == ['take this']
 
     def testStripWhitespace(self):
         """ wikidicts: initFromText: strip whitespace around items """
         text = """
  *   take this  
 """
-        self.assertEqual(self.getMembers(text), ['take this'])
+        assert self.getMembers(text) == ['take this']
 
     def getMembers(self, text):
         group = wikidicts.Group(self.request, '')
@@ -68,7 +69,7 @@
         return group.members()
 
 
-class TestDictPage(unittest.TestCase):
+class TestDictPage:
 
     def testGroupMembers(self):
         """ wikidicts: create dict from keys and values in text """
@@ -86,13 +87,13 @@
 '''
         d = wikidicts.Dict(self.request, '')
         d.initFromText(text)
-        self.assertEqual(d['First'], 'first item')
-        self.assertEqual(d['text with spaces'], 'second item')
-        self.assertEqual(d['Empty string'], '')
-        self.assertEqual(d['Last'], 'last item')
+        assert d['First'] == 'first item'
+        assert d['text with spaces'] == 'second item'
+        assert d['Empty string'] == ''
+        assert d['Last'] == 'last item'
+        assert len(d) == 4
 
-
-class GroupDictTestCase(unittest.TestCase):
+class GroupDictTestCase:
 
     def testSystemPagesGroupInDicts(self):
         """ wikidict: names in SystemPagesGroup should be in request.dicts
--- a/MoinMoin/wikidicts.py	Fri Jun 22 00:46:29 2007 +0200
+++ b/MoinMoin/wikidicts.py	Fri Jun 22 14:05:11 2007 +0200
@@ -2,13 +2,12 @@
 """
     MoinMoin - Dictionary / Group Functions
 
-    @copyright: 2003-2006 MoinMoin:ThomasWaldmann,
-                2003 by Gustavo Niemeyer, http://moin.conectiva.com.br/GustavoNiemeyer
+    @copyright: 2003-2007 MoinMoin:ThomasWaldmann,
+                2003 by Gustavo Niemeyer
     @license: GNU GPL, see COPYING for details.
 """
 import re, time
 
-#import copy #broken, see comments at top of this file:
 from MoinMoin.support import copy
 
 from MoinMoin import caching, wikiutil, Page, logfile
@@ -32,6 +31,7 @@
 
         Create a dict from a wiki page.
         """
+        self._dict = {}
         self.name = name
 
         self.initRegex()
@@ -54,7 +54,7 @@
 
     def values(self):
         return self._dict.values()
-    
+
     def get_dict(self):
         return self._dict
 
@@ -67,9 +67,13 @@
     def __getitem__(self, key):
         return self._dict[key]
 
+    def __len__(self):
+        return len(self._dict)
+
     def __repr__(self):
         return "<DictBase name=%r items=%r>" % (self.name, self._dict.items())
 
+
 class Dict(DictBase):
     """ Mapping of keys to values in a wiki page
 
@@ -91,7 +95,6 @@
 
         Invoked by __init__, also useful for testing without a page.
         """
-        self._dict = {}
         for match in self.regex.finditer(text):
             key, val = match.groups()
             self._dict[key] = val
@@ -99,6 +102,7 @@
     def __repr__(self):
         return "<Dict name=%r items=%r>" % (self.name, self._dict.items())
 
+
 class Group(DictBase):
     """ Group of users, of pages, of whatever
 
@@ -124,21 +128,25 @@
 
         Invoked by __init__, also useful for testing without a page.
         """
-        self._dict = {}
         for match in self.regex.finditer(text):
-            self._dict[match.group('member')] = 1
+            member = match.group('member')
+            self.addmember(member)
 
     def members(self):
+        """ return the group's members """
         return self._dict.keys()
 
     def addmembers(self, members):
+        """ add a list of members to the group """
         for m in members:
             self.addmember(m)
 
     def addmember(self, member):
+        """ add a member to the group """
         self._dict[member] = 1
 
     def has_member(self, member):
+        """ check if the group has member <member> """
         return self._dict.has_key(member)
 
     def _expandgroup(self, groupdict, name):
@@ -186,7 +194,7 @@
 
 
 class DictDict:
-    """a dictionary of Dict objects
+    """ a dictionary of Dict objects
 
        Config:
            cfg.page_dict_regex
@@ -204,11 +212,12 @@
         self.picklever = DICTS_PICKLE_VERSION
 
     def has_key(self, dictname, key):
+        """ check if we have key <key> in dict <dictname> """
         dict = self.dictdict.get(dictname)
         return dict and dict.has_key(key)
 
     def keys(self, dictname):
-        """get keys of dict <dictname>"""
+        """ get keys of dict <dictname> """
         try:
             dict = self.dictdict[dictname]
         except KeyError:
@@ -216,7 +225,7 @@
         return dict.keys()
 
     def values(self, dictname):
-        """get values of dict <dictname>"""
+        """ get values of dict <dictname> """
         try:
             dict = self.dictdict[dictname]
         except KeyError:
@@ -224,7 +233,7 @@
         return dict.values()
 
     def dict(self, dictname):
-        """get dict <dictname>"""
+        """ get dict <dictname> """
         try:
             dict = self.dictdict[dictname]
         except KeyError:
@@ -232,14 +241,15 @@
         return dict
 
     def adddict(self, request, dictname):
-        """add a new dict (will be read from the wiki page)"""
+        """ add a new dict (will be read from the wiki page) """
         self.dictdict[dictname] = Dict(request, dictname)
 
     def has_dict(self, dictname):
+        """ check if we have a dict <dictname> """
         return self.dictdict.has_key(dictname)
 
     def keydict(self, key):
-        """list all dicts that contain key"""
+        """ list all dicts that contain key """
         dictlist = []
         for dict in self.dictdict.values():
             if dict.has_key(key):
@@ -248,7 +258,7 @@
 
 
 class GroupDict(DictDict):
-    """a dictionary of Group objects
+    """ a dictionary of Group objects
 
        Config:
            cfg.page_group_regex
@@ -268,11 +278,12 @@
         self.picklever = DICTS_PICKLE_VERSION
 
     def has_member(self, groupname, member):
+        """ check if we have <member> as a member of group <groupname> """
         group = self.dictdict.get(groupname)
         return group and group.has_member(member)
 
     def members(self, groupname):
-        """get members of group <groupname>"""
+        """ get members of group <groupname> """
         try:
             group = self.dictdict[groupname]
         except KeyError:
@@ -280,16 +291,17 @@
         return group.members()
 
     def addgroup(self, request, groupname):
-        """add a new group (will be read from the wiki page)"""
+        """ add a new group (will be read from the wiki page) """
         grp = Group(request, groupname)
         self.dictdict[groupname] = grp
         self.groupdict[groupname] = grp
 
     def hasgroup(self, groupname):
+        """ check if we have a dict <dictname> """
         return self.groupdict.has_key(groupname)
 
     def membergroups(self, member):
-        """list all groups where member is a member of"""
+        """ list all groups where member is a member of """
         grouplist = []
         for group in self.dictdict.values():
             if group.has_member(member):
@@ -297,7 +309,7 @@
         return grouplist
 
     def scandicts(self):
-        """scan all pages matching the dict / group regex and init the dictdict"""
+        """ scan all pages matching the dict / group regex and init the dictdict """
         dump = 0
         request = self.request