Commits

Boris FELD committed 01bcca8

Add Messenger Files

Comments (0)

Files changed (4)

lib/core/EventDispatcher.py

+from lib.core.Messenger import Messenger
 from lib.exceptions.notCalleableException import notCalleableException
 from lib.exceptions.undeclaredEventName import undeclaredEventName
 
     def call(self, name):
         if not name in self.events.keys():
             return False
+        messenger = Messenger()
         for callback in self.events[name]:
-            callback()
+            callback(messenger)
 
     def declare(self, name):
         if name in self.events.keys():

lib/core/Messenger.py

+class Messenger():
+
+    values = {}
+
+    def __getattr__(self, name):
+        return self.values[name]
+
+    def __setattr__(self, name, value):
+        print "call"
+        if name in self.values:
+            print str(name) + " is in " + str(self.values.keys()) + "/" + str(self.values)
+            raise AttributeError()
+        self.values[name] = value

test/lib/core/EventDispatcherTest.py

-
 import sys
 import os.path
 sys.path.append(os.path.realpath('../../..'))
 from lib.core.EventDispatcher import EventDispatcher
 from lib.exceptions.undeclaredEventName import undeclaredEventName
 from lib.exceptions.notCalleableException import notCalleableException
+from lib.core.Messenger import Messenger
 import unittest
 
 class EventDispatcherTest(unittest.TestCase):
         eventName = 'eventTest'
         
         self.eventDispatcher.declare(eventName)
-        self.assertEqual(eventName in self.eventDispatcher.declaredEvents(), True)
+        self.assertTrue(eventName in self.eventDispatcher.declaredEvents())
 
     def testFailNoDeclaration(self):
         def call():
             pass
 
-        self.assertRaises(undeclaredEventName, lambda: self.eventDispatcher.attach('undeclaredEvent', call))
+        eventName = 'undeclaredEvent'
+        self.assertRaises(undeclaredEventName, lambda: self.eventDispatcher.attach(eventName, call))
 
     def testFailNotCalleable(self):
-        self.eventDispatcher.declare('notCalleable')
-        self.assertRaises(notCalleableException, lambda: self.eventDispatcher.attach('notCalleable', 'uncalleable'))
+        eventName = 'notCalleable'
+        self.eventDispatcher.declare(eventName)
+        self.assertRaises(notCalleableException, lambda: self.eventDispatcher.attach(eventName, 'uncalleable'))
 
     def testEventConnection(self):
         class SimpleSlot():
             self.called = False
 
-            def call(self):
+            def call(self, argument):
                 self.called = True
 
         eventName = 'simpleEvent'
         self.eventDispatcher.attach(eventName, slot.call)
         self.eventDispatcher.call(eventName)
 
-        self.assertEqual(slot.called, True)
+        self.assertTrue(slot.called)
+
+    def testSendMessenger(self):
+        class SimpleSlot():
+
+            def call(self, argument):
+                self.argument = argument
+
+        eventName = 'messengerArgument'
+        slot = SimpleSlot()
+
+        self.eventDispatcher.declare(eventName)
+        self.eventDispatcher.attach(eventName, slot.call)
+        self.eventDispatcher.call(eventName)
+
+        self.assertTrue(isinstance(slot.argument, Messenger))
 
 unittest.main()

test/lib/core/MessengerTest.py

+import sys
+import os.path
+sys.path.append(os.path.realpath('../../..'))
+
+from lib.core.Messenger import Messenger
+import unittest
+
+class MessengerTest(unittest.TestCase):
+
+    def setUp(self):
+        self.messenger = Messenger()
+
+    def tearDown(self):
+        self.messenger = None
+
+    def testSet(self):
+        value = 'value'
+        self.messenger.simple = value
+        self.assertEquals(self.messenger.simple, value)
+
+    def testNoModification(self):
+        value = 'value'
+        value2 = 'value2'
+
+        self.messenger.noModification = value
+#        self.assertRaises(AttributeError, lambda: self.messenger.__set__('noModification', value2))
+        self.assertEqual(value, self.messenger.noModification)
+
+    def testNoDeletion(self):
+        value = 'noModification'
+        self.messenger.noModification = value
+
+unittest.main()