test/unittest_registry.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Mon, 09 Jun 2008 13:39:44 +0200
changeset 4 6ff5e87d288a
parent 0 7710b138d4eb
permissions -rw-r--r--
change position on __future__ import for compatibility

"""unit tests for the plugin registry"""

__revision__ = '$Id: unittest_registry.py,v 1.2 2004-11-10 09:54:45 adim Exp $'

import unittest

from oobrother.registry import PlugInsRegistry
from oobrother.filelib.basefile import FileWrapper
from oobrother.plugins import AbstractPlugIn

class DummyPlugin(AbstractPlugIn):
    def __init__(self, mt, name):
        AbstractPlugIn.__init__(self)
        self.name = name
        self.mimetypes = (mt,)

    def __cmp__(self, other):
        name = getattr(other, 'name', other)
        return cmp(self.name, name)
                   
class PlugInsRegistryTC(unittest.TestCase):

    def setUp(self):
        # hack: the PlugInsRegistry class is deleted to enforce singleton
        self.reg = PlugInsRegistry(appl = None)

    def test_wildcard_1(self):
        self.reg.register(DummyPlugin('text/*', 2))
        self.reg.register(DummyPlugin('text/plain', 3))
        self.reg.register(DummyPlugin('text/html', 4))
        self.assertEquals(self.reg.get_plugins(FileWrapper('toto.txt')), [2, 3])
        self.assertEquals(self.reg.get_plugins(FileWrapper('toto.html')), [2, 4])
        self.assertEquals(self.reg.get_plugins(FileWrapper('toto.py')), [2])
        self.assertEquals(self.reg.get_plugins(FileWrapper('toto.pdf')), [])

    def test_wildcard_2(self):
        self.reg.register(DummyPlugin('*', 1))
        self.reg.register(DummyPlugin('text/*', 2))
        self.reg.register(DummyPlugin('text/plain', 3))
        self.reg.register(DummyPlugin('text/html', 4))
        self.assertEquals(self.reg.get_plugins(FileWrapper('toto.txt')), [1, 2, 3])
        self.assertEquals(self.reg.get_plugins(FileWrapper('toto.html')), [1, 2, 4])
        self.assertEquals(self.reg.get_plugins(FileWrapper('toto.py')), [1, 2])
        self.assertEquals(self.reg.get_plugins(FileWrapper('toto.pdf')), [1])
        
    def test_wildcard_3(self):
        self.reg.register(DummyPlugin('*', 1))
        self.reg.register(DummyPlugin('text/*', 2))
        self.reg.register(DummyPlugin('text/plain', 3))
        self.reg.register(DummyPlugin('text/html', 4))
        self.assertEquals(self.reg.get_plugins(FileWrapper('toto')), [1])
        
    def test_plugins(self):
        self.reg.register(DummyPlugin('*', 1))
        self.reg.register(DummyPlugin('text/*', 2))
        self.reg.register(DummyPlugin('text/plain', 3))
        self.reg.register(DummyPlugin('text/html', 4))
        plugins = [p.name for p in self.reg.plugins]
        self.assertEquals(plugins, [1, 2, 3, 4])

if __name__ == '__main__': 
    unittest.main()