TestEventSubject.py
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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: