Ejemplo n.º 1
0
class TestRegistry(FocusTestCase):
    def setUp(self):
        super(TestRegistry, self).setUp()
        self.registry = Registry()

    def tearDown(self):
        self.registry = None
        super(TestRegistry, self).tearDown()

    def testEmpty___iter__(self):
        """ Registry.__iter__: returns empty list when registry empty.
            """
        self.assertEqual(list(x for x in self.registry), [])

    def testWithItems___iter__(self):
        """ Registry.__iter__: returns list of (key, callable return value)
            tuples.
            """
        self.registry._actions['foo'] = lambda: 'llama'
        self.registry._actions['bar'] = lambda: 'candy'
        self.assertEqual(list(self.registry),
            [('foo', 'llama'), ('bar', 'candy')])

    def test__clear(self):
        """ Registry.clear: removes all keys.
            """
        self.registry._actions['foo'] = lambda: 'blah'
        self.registry._cache['foo'] = 'blah'
        self.registry._actions['bar'] = lambda: 'blah2'

        self.registry.clear()
        self.assertNotIn('foo', self.registry._actions)
        self.assertNotIn('foo', self.registry._cache)
        self.assertNotIn('bar', self.registry._actions)

    def testExistKey__get(self):
        """ Registry.get: returns (key, callable return value) tuple for
            existing key.
            """
        self.registry._actions['foo'] = lambda: 'blah'
        self.assertEqual(self.registry.get('foo'), 'blah')

    def testNonExistKey__get(self):
        """ Registry.get: returns ``None`` for non-existent key.
            """
        self.assertIsNone(self.registry.get('non-exist'))

    def testNewKey__register(self):
        """ Registry.register: adds new key and value.
            """
        v = lambda: 'bar'
        self.registry.register('foo', v)
        self.assertEqual(self.registry._actions['foo'], v)

    def testExistKeyUpdate__register(self):
        """ Registry.register: updates value for existing key.
            """
        # set first key
        v = lambda: 'bar'
        self.registry.register('foo', v)
        self.assertEqual(self.registry._actions['foo'], v)

        # update value for same key
        v2 = lambda: 'llama'
        self.registry.register('foo', v2)
        self.assertEqual(self.registry._actions['foo'], v2)

    def testExistKey__deregister(self):
        """ Registry.deregister: existing key is removed.
            """
        self.registry._actions['foo'] = lambda: 'bar'
        self.registry._cache['foo'] = 'bar'
        self.assertTrue(self.registry.deregister('foo'))
        self.assertNotIn('foo', self.registry._actions)
        self.assertNotIn('foo', self.registry._cache)

    def testNonExistKey__deregister(self):
        """ Registry.deregister: non-existant key returns False.
            """
        self.assertFalse(self.registry.deregister('non-exist'))
Ejemplo n.º 2
0
class TestRegistry(FocusTestCase):
    def setUp(self):
        super(TestRegistry, self).setUp()
        self.registry = Registry()

    def tearDown(self):
        self.registry = None
        super(TestRegistry, self).tearDown()

    def testEmpty___iter__(self):
        """ Registry.__iter__: returns empty list when registry empty.
            """
        self.assertEqual(list(x for x in self.registry), [])

    def testWithItems___iter__(self):
        """ Registry.__iter__: returns list of (key, callable return value)
            tuples.
            """
        self.registry._actions['foo'] = lambda: 'llama'
        self.registry._actions['bar'] = lambda: 'candy'
        self.assertEqual(list(self.registry), [('foo', 'llama'),
                                               ('bar', 'candy')])

    def test__clear(self):
        """ Registry.clear: removes all keys.
            """
        self.registry._actions['foo'] = lambda: 'blah'
        self.registry._cache['foo'] = 'blah'
        self.registry._actions['bar'] = lambda: 'blah2'

        self.registry.clear()
        self.assertNotIn('foo', self.registry._actions)
        self.assertNotIn('foo', self.registry._cache)
        self.assertNotIn('bar', self.registry._actions)

    def testExistKey__get(self):
        """ Registry.get: returns (key, callable return value) tuple for
            existing key.
            """
        self.registry._actions['foo'] = lambda: 'blah'
        self.assertEqual(self.registry.get('foo'), 'blah')

    def testNonExistKey__get(self):
        """ Registry.get: returns ``None`` for non-existent key.
            """
        self.assertIsNone(self.registry.get('non-exist'))

    def testNewKey__register(self):
        """ Registry.register: adds new key and value.
            """
        v = lambda: 'bar'
        self.registry.register('foo', v)
        self.assertEqual(self.registry._actions['foo'], v)

    def testExistKeyUpdate__register(self):
        """ Registry.register: updates value for existing key.
            """
        # set first key
        v = lambda: 'bar'
        self.registry.register('foo', v)
        self.assertEqual(self.registry._actions['foo'], v)

        # update value for same key
        v2 = lambda: 'llama'
        self.registry.register('foo', v2)
        self.assertEqual(self.registry._actions['foo'], v2)

    def testExistKey__deregister(self):
        """ Registry.deregister: existing key is removed.
            """
        self.registry._actions['foo'] = lambda: 'bar'
        self.registry._cache['foo'] = 'bar'
        self.assertTrue(self.registry.deregister('foo'))
        self.assertNotIn('foo', self.registry._actions)
        self.assertNotIn('foo', self.registry._cache)

    def testNonExistKey__deregister(self):
        """ Registry.deregister: non-existant key returns False.
            """
        self.assertFalse(self.registry.deregister('non-exist'))