changeset 2143:b97040fcd8e1

added some new tests, move util tests to MoinMoin/util/_tests
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 18 Jun 2007 21:21:06 +0200
parents d84535cb9f65
children 93e8ba421a88
files MoinMoin/_tests/test_pysupport.py MoinMoin/_tests/test_util_lock.py MoinMoin/_tests/test_util_web.py MoinMoin/util/_tests/test_diff3.py MoinMoin/util/_tests/test_diff_text.py MoinMoin/util/_tests/test_lock.py MoinMoin/util/_tests/test_pysupport.py MoinMoin/util/_tests/test_web.py
diffstat 8 files changed, 503 insertions(+), 351 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_pysupport.py	Mon Jun 18 00:13:15 2007 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,126 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-"""
-    MoinMoin - MoinMoin.util.pysupport Tests
-
-    @copyright: 2004 Oliver Graf <ograf@bitart.de>
-    @license: GNU GPL, see COPYING for details.
-"""
-
-import unittest, os, errno # LEGACY UNITTEST, PLEASE DO NOT IMPORT unittest IN NEW TESTS, PLEASE CONSULT THE py.test DOCS
-
-import py
-
-from MoinMoin.util import pysupport
-
-
-class TestImportNameFromMoin(unittest.TestCase):
-    """ Test importName of MoinMoin modules
-
-    We don't make any testing for files, assuming that moin package is
-    not broken.
-    """
-
-    def testNonExistingModule(self):
-        """ pysupport: import nonexistent module raises ImportError """
-        self.assertRaises(ImportError, pysupport.importName,
-                          'MoinMoin.parser.abcdefghijkl', 'Parser')
-
-    def testNonExistingAttribute(self):
-        """ pysupport: import nonexistent attritbue raises AttributeError """
-        self.assertRaises(AttributeError, pysupport.importName,
-                          'MoinMoin.parser.text_moin_wiki', 'NoSuchParser')
-
-    def testExisting(self):
-        """ pysupport: import name from existing module """
-        from MoinMoin.parser import text_moin_wiki
-        Parser = pysupport.importName('MoinMoin.parser.text_moin_wiki', 'Parser')
-        self.failUnless(Parser is text_moin_wiki.Parser)
-
-
-class TestImportNameFromPlugin(unittest.TestCase):
-    """ Base class for import plugin tests """
-
-    name = 'Parser'
-
-    def setUp(self):
-        """ Check for valid plugin package """
-        self.pluginDirectory = os.path.join(self.request.cfg.data_dir, 'plugin', 'parser')
-        self.checkPackage(self.pluginDirectory)
-        self.pluginModule = (self.request.cfg.siteid + '.plugin.parser.' + self.plugin)
-
-    def checkPackage(self, path):
-        for item in (path, os.path.join(path, '__init__.py')):
-            if not os.path.exists(item):
-                py.test.skip("Missing or wrong permissions: %s" % item)
-
-    def pluginExists(self):
-        return (os.path.exists(self.pluginFilePath('.py')) or
-                os.path.exists(self.pluginFilePath('.pyc')))
-
-    def pluginFilePath(self, suffix):
-        return os.path.join(self.pluginDirectory, self.plugin + suffix)
-
-
-class TestImportNonExisiting(TestImportNameFromPlugin):
-
-    plugin = 'NonExistingWikiPlugin'
-
-    def testNonEsisting(self):
-        """ pysupport: import nonexistent wiki plugin fail """
-        if self.pluginExists():
-            py.test.skip('plugin exists: %s' % self.plugin)
-        self.assertRaises(ImportError, pysupport.importName,
-                          self.pluginModule, self.name)
-
-
-class TestImportExisting(TestImportNameFromPlugin):
-
-    plugin = 'AutoCreatedMoinMoinTestPlugin'
-    shouldDeleteTestPlugin = True
-
-    def testExisting(self):
-        """ pysupport: import existing wiki plugin
-        
-        Tests if a module can be imported from an arbitrary path
-        like it is done in moin for plugins. Some strange bug
-        in the old implementation failed on an import of os,
-        cause os does a from os.path import that will stumble
-        over a poisoned sys.modules.
-        """
-        try:
-            self.createTestPlugin()
-            plugin = pysupport.importName(self.pluginModule, self.name)
-            self.assertEqual(getattr(plugin, '__name__', None), self.name,
-                            'Failed to import the test plugin')
-        finally:
-            self.deleteTestPlugin()
-
-    def createTestPlugin(self):
-        """ Create test plugin, skiping if plugin exists """
-        if self.pluginExists():
-            self.shouldDeleteTestPlugin = False
-            py.test.skip("Won't overwrite exiting plugin: %s" % self.plugin)
-        data = '''
-# If you find this file in your wiki plugin directory, you can safely
-# delete it.
-import sys, os
-
-class Parser:
-    pass
-'''
-        try:
-            file(self.pluginFilePath('.py'), 'w').write(data)
-        except Exception, err:
-            py.test.skip("Can't create test plugin: %s" % str(err))
-
-    def deleteTestPlugin(self):
-        """ Delete plugin files ignoring missing files errors """
-        if not self.shouldDeleteTestPlugin:
-            return
-        for suffix in ('.py', '.pyc'):
-            try:
-                os.unlink(self.pluginFilePath(suffix))
-            except OSError, err:
-                if err.errno != errno.ENOENT:
-                    raise
-
--- a/MoinMoin/_tests/test_util_lock.py	Mon Jun 18 00:13:15 2007 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,130 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-    MoinMoin - MoinMoin.module_tested Tests
-
-    Module names must start with 'test_' to be included in the tests.
-
-    @copyright: 2005 by Florian Festi
-    @license: GNU GPL, see COPYING for details.
-"""
-
-import unittest, tempfile, os, time, shutil # LEGACY UNITTEST, PLEASE DO NOT IMPORT unittest IN NEW TESTS, PLEASE CONSULT THE py.test DOCS
-
-import py
-
-from MoinMoin.util.lock import ExclusiveLock
-
-
-class TestExclusiveLock(unittest.TestCase):
-    
-    def setUp(self):
-        self.test_dir = tempfile.mkdtemp('', 'lock_')
-        self.test_dir_mtime_goal = time.time()
-        self.test_dir_mtime_reported = os.stat(self.test_dir).st_mtime
-        self.lock_dir = os.path.join(self.test_dir, "lock")
-        
-    def tearDown(self):
-        shutil.rmtree(self.test_dir)
-
-    def testBrokenTimeAPI(self):
-        """ util.lock: os.stat().mtime consistency with time.time()
-        
-            the timestamp os.stat reports as st_mtime on a fresh file should
-            be the same (or at least almost the same) as the time time.time()
-            reported at this time.
-            Differences of n*3600s are usually operating system bugs / limitations,
-            Win32 (as of Win XP SP2 + hotfixes 2006-04-30) is broken if you set
-            TZ to a different value than the rest of the system uses.
-            E.g. if you set "TZ=GMT1EDT" (and the rest of the system is setup
-            on german/berlin timezone), it will report 7200s difference in the
-            summer.
-        """
-        diff = self.test_dir_mtime_reported - self.test_dir_mtime_goal # diff should be 0 or near 0
-        self.failUnless(abs(diff) <= 2,
-                        "Operating System time.time / os.stat inconsistent (diff==%.1fs). Locking will NOT work correctly." % float(diff))
-
-    def testTimeout(self):
-        """ util.lock: ExclusiveLock: raise ValueError for timeout < 2.0 """
-        self.assertRaises(ValueError, ExclusiveLock, self.lock_dir, timeout=1.0)
-        
-    def testAcquire(self):
-        """ util.lock: ExclusiveLock: acquire """
-        lock = ExclusiveLock(self.lock_dir)
-        self.failUnless(lock.acquire(0.1), "Could not acquire lock")
-
-    def testRelease(self):
-        """ util.lock: ExclusiveLock: release 
-        
-        After releasing a lock, new one could be acquired.
-        """
-        lock = ExclusiveLock(self.lock_dir)
-        if not lock.acquire(0.1):
-            py.test.skip("can't acquire lock")
-        lock.release()
-        self.failUnless(lock.acquire(0.1), 
-                        "Could not acquire lock after release")
-
-    def testIsLocked(self):
-        """ util.lock: ExclusiveLock: isLocked """
-        lock = ExclusiveLock(self.lock_dir)
-        if not lock.acquire(0.1):
-            py.test.skip("can't acquire lock")
-        self.failUnless(lock.isLocked(), "lock state wrong")
-        lock.release()
-        self.failIf(lock.isLocked(), "lock state wrong")
-
-    def testExists(self):
-        """ util.lock: ExclusiveLock: exists """
-        lock = ExclusiveLock(self.lock_dir)
-        if not lock.acquire(0.1):
-            py.test.skip("can't acquire lock")
-        self.failUnless(lock.exists(), "lock should exists")
-
-    def testIsExpired(self):
-        """ util.lock: ExclusiveLock: isExpired """
-        timeout = 2.0
-        lock = ExclusiveLock(self.lock_dir, timeout=timeout)
-        if not lock.acquire(0.1):
-            py.test.skip("can't acquire lock")
-        self.failIf(lock.isExpired(), "lock should not be expired yet")
-        time.sleep(timeout)
-        self.failUnless(lock.isExpired(), "lock should be expired")
-
-    def testExpire(self):
-        """ util.lock: ExclusiveLock: expire """
-        timeout = 2.0
-        lock = ExclusiveLock(self.lock_dir, timeout=timeout)
-        if not lock.acquire(0.1):
-            py.test.skip("can't acquire lock")
-        self.failIf(lock.expire(), "lock should not be expired yet")
-        time.sleep(timeout)
-        self.failUnless(lock.expire(), "lock should be expired")
-
-    def testExclusive(self):
-        """ util.lock: ExclusiveLock: lock is exclusive """
-        first = ExclusiveLock(self.lock_dir)
-        second = ExclusiveLock(self.lock_dir)
-        if not first.acquire(0.1):
-            py.test.skip("can't acquire lock")
-        self.failIf(second.acquire(0.1), "first lock is not exclusive")                
-
-    def testAcquireAfterTimeout(self):
-        """ util.lock: ExclusiveLock: acquire after timeout
-        
-        Lock with one lock, try to acquire another after timeout.
-        """
-        timeout = 2.0 # minimal timout
-        first = ExclusiveLock(self.lock_dir, timeout)
-        second = ExclusiveLock(self.lock_dir, timeout)
-        if not first.acquire(0.1):
-            py.test.skip("can't acquire lock")
-        if second.acquire(0.1):
-            py.test.skip("first lock is not exclusive")
-        # Second lock should be acquired after timeout
-        self.failUnless(second.acquire(timeout + 0.1), 
-                        "can't acquire after timeout")
-            
-    def unlock(self, lock, delay):
-        time.sleep(delay)
-        lock.release()
-
--- a/MoinMoin/_tests/test_util_web.py	Mon Jun 18 00:13:15 2007 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,95 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-"""
-    MoinMoin - MoinMoin.util.web Tests
-
-    @copyright: 2003-2004 by Juergen Hermann <jh@web.de>
-    @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
-from MoinMoin import wikiutil
-from MoinMoin.util import web
-from MoinMoin.widget import html
-
-
-class TestMakeQueryString(unittest.TestCase):
-    """util.web: making query string"""
-
-    def testMakeQueryStringFromArgument(self):
-        """ util.web: make query sting from argument """
-        tests = (
-            # description,          arg,                expected
-            ('string unchanged',    'a=b',              'a=b'),
-            ('string value',        {'a': 'b'},         'a=b'),
-            ('integer value',       {'a': 1},           'a=1'),
-            ('multiply values',     {'a': 1, 'b': 2},   'a=1&b=2'),
-            )
-
-        for description, arg, expected in tests:
-            result = wikiutil.makeQueryString(arg)
-            self.assertEqual(result, expected,
-                             ('%(description)s: expected "%(expected)s" '
-                              'but got "%(result)s"') % locals())
-
-    def testMakeQueryStringFromKeywords(self):
-        """ util.web: make query sting from keywords """
-        expected = 'a=1&b=string'
-        result = wikiutil.makeQueryString(a=1, b='string')
-        self.assertEqual(result, expected,
-                         'Expected "%(expected)s" but got "%(result)s"' % locals())
-
-    def testMakeQueryStringFromArgumentAndKeywords(self):
-        """ util.web: make query sting from argument and keywords """        
-        
-        tests = (
-            # description,      arg,                    expected
-            ('kw ignored',      'a=1',                  'a=1'),             
-            ('kw added to arg', {'a': 1},               'a=1&b=kw'),             
-            ('kw override arg',  {'a': 1, 'b': 'arg'},   'a=1&b=kw'),
-            )
-        
-        for description, arg, expected in tests:
-            # Call makeQueryString with both arg and keyword
-            result = wikiutil.makeQueryString(arg, b='kw')
-            self.assertEqual(result, expected,
-                             ('%(description)s: expected "%(expected)s" '
-                              'but got "%(result)s"') % locals())
-        
-
-class TestMakeSelection(unittest.TestCase):
-    """util.web: creating html select"""
-
-    values = ('one', 'two', 'simple', ('complex', 'A tuple & <escaped text>'))
-
-    def setUp(self):
-        html._SORT_ATTRS = 1
-        self.expected = (
-        u'<select name="test" size="1">'
-        u'<option value="one">one</option>'
-        u'<option value="two">two</option>'
-        u'<option value="simple">simple</option>'
-        u'<option value="complex">A tuple &amp; &lt;escaped text&gt;</option>'
-        u'</select>'
-        )
-        
-    def testMakeSelectNoSelection(self):
-        """util.web: creating html select with no selection"""
-        expected = self.expected
-        result = unicode(web.makeSelection('test', self.values, size=1))
-        self.assertEqual(result, expected,
-                         'Expected "%(expected)s" but got "%(result)s"' % locals())
-
-    def testMakeSelectNoSelection2(self):
-        """util.web: creating html select with non existing selection"""
-        expected = self.expected
-        result = unicode(web.makeSelection('test', self.values, 'three', size=1))
-        self.assertEqual(result, expected,
-                         'Expected "%(expected)s" but got "%(result)s"' % locals())
-        
-    def testMakeSelectWithSelectedItem(self):
-        """util.web: creating html select with selected item"""
-        expected = self.expected.replace('value="two"', 'selected value="two"')
-        result = unicode(web.makeSelection('test', self.values, 'two', size=1))
-        self.assertEqual(result, expected,
-                         'Expected "%(expected)s" but got "%(result)s"' % locals())
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/util/_tests/test_diff3.py	Mon Jun 18 21:21:06 2007 +0200
@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+"""
+    MoinMoin - MoinMoin.util.diff3 Tests
+
+    @copyright: 2007 MoinMoin:ThomasWaldmann
+    @license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin.util import diff3
+
+class TestDiff3:
+
+    def testTextMerge(self):
+        """ util.diff3.text_merge: test correct merging """
+        in1 = """AAA 001
+AAA 002
+AAA 003
+AAA 004
+AAA 005
+AAA 006
+AAA 007
+AAA 008
+AAA 009
+AAA 010
+AAA 011
+AAA 012
+AAA 013
+AAA 014
+"""
+
+        in2 = """AAA 001
+AAA 002
+AAA 005
+AAA 006
+AAA 007
+AAA 008
+BBB 001
+BBB 002
+AAA 009
+AAA 010
+BBB 003
+"""
+
+        in3 = """AAA 001
+AAA 002
+AAA 003
+AAA 004
+AAA 005
+AAA 006
+AAA 007
+AAA 008
+CCC 001
+CCC 002
+CCC 003
+AAA 012
+AAA 013
+AAA 014
+"""
+        result = diff3.text_merge(in1, in2, in3)
+
+        expected = """AAA 001
+AAA 002
+AAA 005
+AAA 006
+AAA 007
+AAA 008
+<<<<<<<<<<<<<<<<<<<<<<<<<
+BBB 001
+BBB 002
+AAA 009
+AAA 010
+BBB 003
+=========================
+CCC 001
+CCC 002
+CCC 003
+AAA 012
+AAA 013
+AAA 014
+>>>>>>>>>>>>>>>>>>>>>>>>>
+"""
+        assert result == expected, ('Expected "%(expected)s" but got "%(result)s"') % locals()
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/util/_tests/test_diff_text.py	Mon Jun 18 21:21:06 2007 +0200
@@ -0,0 +1,68 @@
+# -*- coding: utf-8 -*-
+"""
+    MoinMoin - MoinMoin.util.diff_text Tests
+
+    @copyright: 2007 MoinMoin:ThomasWaldmann
+    @license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin.util import diff_text
+
+class TestDiffText:
+
+    def testDiff(self):
+        """ util.diff_text.diff: test correct diff calculation """
+        in1 = """AAA 001
+AAA 002
+AAA 003
+AAA 004
+AAA 005
+AAA 006
+AAA 007
+AAA 008
+AAA 009
+AAA 010
+AAA 011
+AAA 012
+AAA 013
+AAA 014
+"""
+
+        in2 = """AAA 001
+AAA 002
+AAA 005
+AAA 006
+AAA 007
+AAA 008
+BBB 001
+BBB 002
+AAA 009
+AAA 010
+BBB 003
+"""
+
+        result = diff_text.diff(in1.splitlines(), in2.splitlines())
+        result = "\n".join(result)
+
+        expected = """\
+  AAA 001
+  AAA 002
+- AAA 003
+- AAA 004
+  AAA 005
+  AAA 006
+  AAA 007
+  AAA 008
++ BBB 001
++ BBB 002
+  AAA 009
+  AAA 010
++ BBB 003
+- AAA 011
+- AAA 012
+- AAA 013
+- AAA 014"""
+
+        assert result == expected, ('Expected "%(expected)s" but got "%(result)s"') % locals()
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/util/_tests/test_lock.py	Mon Jun 18 21:21:06 2007 +0200
@@ -0,0 +1,130 @@
+# -*- coding: utf-8 -*-
+"""
+    MoinMoin - MoinMoin.module_tested Tests
+
+    Module names must start with 'test_' to be included in the tests.
+
+    @copyright: 2005 by Florian Festi
+    @license: GNU GPL, see COPYING for details.
+"""
+
+import unittest, tempfile, os, time, shutil # LEGACY UNITTEST, PLEASE DO NOT IMPORT unittest IN NEW TESTS, PLEASE CONSULT THE py.test DOCS
+
+import py
+
+from MoinMoin.util.lock import ExclusiveLock
+
+
+class TestExclusiveLock(unittest.TestCase):
+
+    def setUp(self):
+        self.test_dir = tempfile.mkdtemp('', 'lock_')
+        self.test_dir_mtime_goal = time.time()
+        self.test_dir_mtime_reported = os.stat(self.test_dir).st_mtime
+        self.lock_dir = os.path.join(self.test_dir, "lock")
+
+    def tearDown(self):
+        shutil.rmtree(self.test_dir)
+
+    def testBrokenTimeAPI(self):
+        """ util.lock: os.stat().mtime consistency with time.time()
+        
+            the timestamp os.stat reports as st_mtime on a fresh file should
+            be the same (or at least almost the same) as the time time.time()
+            reported at this time.
+            Differences of n*3600s are usually operating system bugs / limitations,
+            Win32 (as of Win XP SP2 + hotfixes 2006-04-30) is broken if you set
+            TZ to a different value than the rest of the system uses.
+            E.g. if you set "TZ=GMT1EDT" (and the rest of the system is setup
+            on german/berlin timezone), it will report 7200s difference in the
+            summer.
+        """
+        diff = self.test_dir_mtime_reported - self.test_dir_mtime_goal # diff should be 0 or near 0
+        self.failUnless(abs(diff) <= 2,
+                        "Operating System time.time / os.stat inconsistent (diff==%.1fs). Locking will NOT work correctly." % float(diff))
+
+    def testTimeout(self):
+        """ util.lock: ExclusiveLock: raise ValueError for timeout < 2.0 """
+        self.assertRaises(ValueError, ExclusiveLock, self.lock_dir, timeout=1.0)
+
+    def testAcquire(self):
+        """ util.lock: ExclusiveLock: acquire """
+        lock = ExclusiveLock(self.lock_dir)
+        self.failUnless(lock.acquire(0.1), "Could not acquire lock")
+
+    def testRelease(self):
+        """ util.lock: ExclusiveLock: release 
+        
+        After releasing a lock, new one could be acquired.
+        """
+        lock = ExclusiveLock(self.lock_dir)
+        if not lock.acquire(0.1):
+            py.test.skip("can't acquire lock")
+        lock.release()
+        self.failUnless(lock.acquire(0.1),
+                        "Could not acquire lock after release")
+
+    def testIsLocked(self):
+        """ util.lock: ExclusiveLock: isLocked """
+        lock = ExclusiveLock(self.lock_dir)
+        if not lock.acquire(0.1):
+            py.test.skip("can't acquire lock")
+        self.failUnless(lock.isLocked(), "lock state wrong")
+        lock.release()
+        self.failIf(lock.isLocked(), "lock state wrong")
+
+    def testExists(self):
+        """ util.lock: ExclusiveLock: exists """
+        lock = ExclusiveLock(self.lock_dir)
+        if not lock.acquire(0.1):
+            py.test.skip("can't acquire lock")
+        self.failUnless(lock.exists(), "lock should exists")
+
+    def testIsExpired(self):
+        """ util.lock: ExclusiveLock: isExpired """
+        timeout = 2.0
+        lock = ExclusiveLock(self.lock_dir, timeout=timeout)
+        if not lock.acquire(0.1):
+            py.test.skip("can't acquire lock")
+        self.failIf(lock.isExpired(), "lock should not be expired yet")
+        time.sleep(timeout)
+        self.failUnless(lock.isExpired(), "lock should be expired")
+
+    def testExpire(self):
+        """ util.lock: ExclusiveLock: expire """
+        timeout = 2.0
+        lock = ExclusiveLock(self.lock_dir, timeout=timeout)
+        if not lock.acquire(0.1):
+            py.test.skip("can't acquire lock")
+        self.failIf(lock.expire(), "lock should not be expired yet")
+        time.sleep(timeout)
+        self.failUnless(lock.expire(), "lock should be expired")
+
+    def testExclusive(self):
+        """ util.lock: ExclusiveLock: lock is exclusive """
+        first = ExclusiveLock(self.lock_dir)
+        second = ExclusiveLock(self.lock_dir)
+        if not first.acquire(0.1):
+            py.test.skip("can't acquire lock")
+        self.failIf(second.acquire(0.1), "first lock is not exclusive")
+
+    def testAcquireAfterTimeout(self):
+        """ util.lock: ExclusiveLock: acquire after timeout
+        
+        Lock with one lock, try to acquire another after timeout.
+        """
+        timeout = 2.0 # minimal timout
+        first = ExclusiveLock(self.lock_dir, timeout)
+        second = ExclusiveLock(self.lock_dir, timeout)
+        if not first.acquire(0.1):
+            py.test.skip("can't acquire lock")
+        if second.acquire(0.1):
+            py.test.skip("first lock is not exclusive")
+        # Second lock should be acquired after timeout
+        self.failUnless(second.acquire(timeout + 0.1),
+                        "can't acquire after timeout")
+
+    def unlock(self, lock, delay):
+        time.sleep(delay)
+        lock.release()
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/util/_tests/test_pysupport.py	Mon Jun 18 21:21:06 2007 +0200
@@ -0,0 +1,126 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - MoinMoin.util.pysupport Tests
+
+    @copyright: 2004 Oliver Graf <ograf@bitart.de>
+    @license: GNU GPL, see COPYING for details.
+"""
+
+import unittest, os, errno # LEGACY UNITTEST, PLEASE DO NOT IMPORT unittest IN NEW TESTS, PLEASE CONSULT THE py.test DOCS
+
+import py
+
+from MoinMoin.util import pysupport
+
+
+class TestImportNameFromMoin(unittest.TestCase):
+    """ Test importName of MoinMoin modules
+
+    We don't make any testing for files, assuming that moin package is
+    not broken.
+    """
+
+    def testNonExistingModule(self):
+        """ pysupport: import nonexistent module raises ImportError """
+        self.assertRaises(ImportError, pysupport.importName,
+                          'MoinMoin.parser.abcdefghijkl', 'Parser')
+
+    def testNonExistingAttribute(self):
+        """ pysupport: import nonexistent attritbue raises AttributeError """
+        self.assertRaises(AttributeError, pysupport.importName,
+                          'MoinMoin.parser.text_moin_wiki', 'NoSuchParser')
+
+    def testExisting(self):
+        """ pysupport: import name from existing module """
+        from MoinMoin.parser import text_moin_wiki
+        Parser = pysupport.importName('MoinMoin.parser.text_moin_wiki', 'Parser')
+        self.failUnless(Parser is text_moin_wiki.Parser)
+
+
+class TestImportNameFromPlugin(unittest.TestCase):
+    """ Base class for import plugin tests """
+
+    name = 'Parser'
+
+    def setUp(self):
+        """ Check for valid plugin package """
+        self.pluginDirectory = os.path.join(self.request.cfg.data_dir, 'plugin', 'parser')
+        self.checkPackage(self.pluginDirectory)
+        self.pluginModule = (self.request.cfg.siteid + '.plugin.parser.' + self.plugin)
+
+    def checkPackage(self, path):
+        for item in (path, os.path.join(path, '__init__.py')):
+            if not os.path.exists(item):
+                py.test.skip("Missing or wrong permissions: %s" % item)
+
+    def pluginExists(self):
+        return (os.path.exists(self.pluginFilePath('.py')) or
+                os.path.exists(self.pluginFilePath('.pyc')))
+
+    def pluginFilePath(self, suffix):
+        return os.path.join(self.pluginDirectory, self.plugin + suffix)
+
+
+class TestImportNonExisiting(TestImportNameFromPlugin):
+
+    plugin = 'NonExistingWikiPlugin'
+
+    def testNonEsisting(self):
+        """ pysupport: import nonexistent wiki plugin fail """
+        if self.pluginExists():
+            py.test.skip('plugin exists: %s' % self.plugin)
+        self.assertRaises(ImportError, pysupport.importName,
+                          self.pluginModule, self.name)
+
+
+class TestImportExisting(TestImportNameFromPlugin):
+
+    plugin = 'AutoCreatedMoinMoinTestPlugin'
+    shouldDeleteTestPlugin = True
+
+    def testExisting(self):
+        """ pysupport: import existing wiki plugin
+        
+        Tests if a module can be imported from an arbitrary path
+        like it is done in moin for plugins. Some strange bug
+        in the old implementation failed on an import of os,
+        cause os does a from os.path import that will stumble
+        over a poisoned sys.modules.
+        """
+        try:
+            self.createTestPlugin()
+            plugin = pysupport.importName(self.pluginModule, self.name)
+            self.assertEqual(getattr(plugin, '__name__', None), self.name,
+                            'Failed to import the test plugin')
+        finally:
+            self.deleteTestPlugin()
+
+    def createTestPlugin(self):
+        """ Create test plugin, skiping if plugin exists """
+        if self.pluginExists():
+            self.shouldDeleteTestPlugin = False
+            py.test.skip("Won't overwrite exiting plugin: %s" % self.plugin)
+        data = '''
+# If you find this file in your wiki plugin directory, you can safely
+# delete it.
+import sys, os
+
+class Parser:
+    pass
+'''
+        try:
+            file(self.pluginFilePath('.py'), 'w').write(data)
+        except Exception, err:
+            py.test.skip("Can't create test plugin: %s" % str(err))
+
+    def deleteTestPlugin(self):
+        """ Delete plugin files ignoring missing files errors """
+        if not self.shouldDeleteTestPlugin:
+            return
+        for suffix in ('.py', '.pyc'):
+            try:
+                os.unlink(self.pluginFilePath(suffix))
+            except OSError, err:
+                if err.errno != errno.ENOENT:
+                    raise
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/util/_tests/test_web.py	Mon Jun 18 21:21:06 2007 +0200
@@ -0,0 +1,95 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - MoinMoin.util.web Tests
+
+    @copyright: 2003-2004 by Juergen Hermann <jh@web.de>
+    @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
+from MoinMoin import wikiutil
+from MoinMoin.util import web
+from MoinMoin.widget import html
+
+
+class TestMakeQueryString(unittest.TestCase):
+    """util.web: making query string"""
+
+    def testMakeQueryStringFromArgument(self):
+        """ util.web: make query sting from argument """
+        tests = (
+            # description,          arg,                expected
+            ('string unchanged',    'a=b',              'a=b'),
+            ('string value',        {'a': 'b'},         'a=b'),
+            ('integer value',       {'a': 1},           'a=1'),
+            ('multiply values',     {'a': 1, 'b': 2},   'a=1&b=2'),
+            )
+
+        for description, arg, expected in tests:
+            result = wikiutil.makeQueryString(arg)
+            self.assertEqual(result, expected,
+                             ('%(description)s: expected "%(expected)s" '
+                              'but got "%(result)s"') % locals())
+
+    def testMakeQueryStringFromKeywords(self):
+        """ util.web: make query sting from keywords """
+        expected = 'a=1&b=string'
+        result = wikiutil.makeQueryString(a=1, b='string')
+        self.assertEqual(result, expected,
+                         'Expected "%(expected)s" but got "%(result)s"' % locals())
+
+    def testMakeQueryStringFromArgumentAndKeywords(self):
+        """ util.web: make query sting from argument and keywords """
+
+        tests = (
+            # description,      arg,                    expected
+            ('kw ignored',      'a=1',                  'a=1'),
+            ('kw added to arg', {'a': 1},               'a=1&b=kw'),
+            ('kw override arg', {'a': 1, 'b': 'arg'},   'a=1&b=kw'),
+            )
+
+        for description, arg, expected in tests:
+            # Call makeQueryString with both arg and keyword
+            result = wikiutil.makeQueryString(arg, b='kw')
+            self.assertEqual(result, expected,
+                             ('%(description)s: expected "%(expected)s" '
+                              'but got "%(result)s"') % locals())
+
+
+class TestMakeSelection(unittest.TestCase):
+    """util.web: creating html select"""
+
+    values = ('one', 'two', 'simple', ('complex', 'A tuple & <escaped text>'))
+
+    def setUp(self):
+        html._SORT_ATTRS = 1
+        self.expected = (
+        u'<select name="test" size="1">'
+        u'<option value="one">one</option>'
+        u'<option value="two">two</option>'
+        u'<option value="simple">simple</option>'
+        u'<option value="complex">A tuple &amp; &lt;escaped text&gt;</option>'
+        u'</select>'
+        )
+
+    def testMakeSelectNoSelection(self):
+        """util.web: creating html select with no selection"""
+        expected = self.expected
+        result = unicode(web.makeSelection('test', self.values, size=1))
+        self.assertEqual(result, expected,
+                         'Expected "%(expected)s" but got "%(result)s"' % locals())
+
+    def testMakeSelectNoSelection2(self):
+        """util.web: creating html select with non existing selection"""
+        expected = self.expected
+        result = unicode(web.makeSelection('test', self.values, 'three', size=1))
+        self.assertEqual(result, expected,
+                         'Expected "%(expected)s" but got "%(result)s"' % locals())
+
+    def testMakeSelectWithSelectedItem(self):
+        """util.web: creating html select with selected item"""
+        expected = self.expected.replace('value="two"', 'selected value="two"')
+        result = unicode(web.makeSelection('test', self.values, 'two', size=1))
+        self.assertEqual(result, expected,
+                         'Expected "%(expected)s" but got "%(result)s"' % locals())
+