TestEventSubject.py 2.41 KB
import unittest
import mock

from os.path import dirname, realpath
from sys import argv, path
path.append(dirname(dirname(realpath(__file__))) + '/lib')

from Event.Event import Event
from Event.EventSubject import EventSubject

class EventSubject1(EventSubject):
    _EVENTS = {
        'a': 0x01
    }

class EventSubject2(EventSubject1):
    _EVENTS = {
        'b': 0x01
    }

class EventSubject3(EventSubject):
    _EVENTS = {
        'a': 0x01
    }

class TestEventSubject(unittest.TestCase):
    def setUp(self):
        self._subject  = EventSubject()
        self._subject1 = EventSubject1()
        self._subject2 = EventSubject2()
        self._subject3 = EventSubject3()

    def testEventId(self):
        self.assertEqual(EventSubject().eventId('a'), None)
        self.assertNotEqual(EventSubject1().eventId('a'), None)
        self.assertNotEqual(EventSubject2().eventId('a'), None)
        self.assertNotEqual(EventSubject2().eventId('b'), None)
        self.assertNotEqual(EventSubject2.eventId('a'), EventSubject2.eventId('b'))
        self.assertNotEqual(EventSubject1.eventId('a'), EventSubject3.eventId('a'))

    def testEmit(self):
        event = self._subject1.emit('a', None)
        self.assertEqual(event.name, 'a')
        self.assertNotEqual(event.type, None)
        self.assertEqual(event.subject, self._subject1)
        self.assertEqual(event.data, None)
        self.assertEqual(event.sno, 0)

        event = self._subject1.emit('b', None)
        self.assertEqual(event.name, 'b')
        self.assertEqual(event.type, None)
        self.assertEqual(event.subject, self._subject1)
        self.assertEqual(event.data, None)
        self.assertEqual(event.sno, 1)

        event = self._subject2.emit('a', None)
        self.assertEqual(event.name, 'a')
        self.assertNotEqual(event.type, None)
        self.assertEqual(event.subject, self._subject2)
        self.assertEqual(event.data, None)
        self.assertEqual(event.sno, 2)

        event = self._subject2.emit('b', 'data')
        self.assertEqual(event.name, 'b')
        self.assertNotEqual(event.type, None)
        self.assertEqual(event.subject, self._subject2)
        self.assertEqual(event.data, 'data')
        self.assertEqual(event.sno, 3)

def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TestEventSubject)

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())

# vim: set ft=python et ts=8 sw=4 sts=4: