Rev 165: Add a ConnectionLogger class, which is able to accept signals and log that they occurred. in http://bazaar.launchpad.net/~jameinel/bzr-explorer/basic-tests

John Arbash Meinel john at arbash-meinel.com
Wed Jul 8 21:00:10 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr-explorer/basic-tests

------------------------------------------------------------
revno: 165
revision-id: john at arbash-meinel.com-20090708200005-9lf1fud4k91pi1am
parent: john at arbash-meinel.com-20090708194653-fm9db3a5zwl0bdzt
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: basic-tests
timestamp: Wed 2009-07-08 15:00:05 -0500
message:
  Add a ConnectionLogger class, which is able to accept signals and log that they occurred.
  
  This is generally just a helper class, which allows us to test our signal and slot acceptance.
  It makes it easier to hook up a bunch of potential signals to self.logger, and then
  see when they are triggered.
-------------- next part --------------
=== modified file 'tests/__init__.py'
--- a/tests/__init__.py	2009-07-08 19:46:53 +0000
+++ b/tests/__init__.py	2009-07-08 20:00:05 +0000
@@ -34,6 +34,7 @@
     def setUp(self):
         super(TestCaseWithQt, self).setUp()
         self.ensureApp()
+        self.logger = self.createLogger()
 
     def ensureApp(self):
         if TestCaseWithQt._app is None:
@@ -41,6 +42,27 @@
             TestCaseWithQt._app = QtGui.QApplication(['bzr-explorer-test-app'])
             # It seems we can leave it running.
 
+    def createLogger(self, *args):
+        from PyQt4 import QtCore
+
+        class ConnectionLogger(QtCore.QObject):
+
+            def __init__(self, *args):
+                QtCore.QObject.__init__(self, *args)
+                # A log of actions that have occurrred
+                self.log = []
+
+            def createNamedSlot(self, signal_name):
+                """Create a new slot, which tracks the signal name given.
+
+                :param signal_name: The name of the signal we are connecting
+                :return: A function that can be used as a signal to this logger.
+                """
+                def named_slot(*args):
+                    self.log.append((signal_name,) + args)
+                return named_slot
+        return ConnectionLogger(*args)
+
 
 def load_tests(basic_tests, module, loader):
     suite = loader.suiteClass()

=== modified file 'tests/test_test_suite.py'
--- a/tests/test_test_suite.py	2009-07-08 19:46:53 +0000
+++ b/tests/test_test_suite.py	2009-07-08 20:00:05 +0000
@@ -21,7 +21,35 @@
 from bzrlib.plugins.explorer import tests
 
 
+class _Emitter(QtCore.QObject):
+
+    def trigger_foo(self):
+        self.emit(QtCore.SIGNAL('foo()'))
+
+    def trigger_bar(self, arg1, arg2):
+        self.emit(QtCore.SIGNAL('bar(int, int)'), arg1, arg2)
+
+
 class TestTestCaseWithQt(tests.TestCaseWithQt):
 
     def test_create_qpixmap(self):
         pixmap = QtGui.QPixmap()
+
+    def test_logger_named_slot(self):
+        slot = self.logger.createNamedSlot('trapping_foo')
+        emitter = _Emitter()
+        QtCore.QObject.connect(emitter, QtCore.SIGNAL('foo()'), slot)
+        self.assertEqual([], self.logger.log)
+        emitter.trigger_foo()
+        self.assertEqual([('trapping_foo',)], self.logger.log)
+        emitter.trigger_bar(1, 2) # not connected
+        self.assertEqual([('trapping_foo',)], self.logger.log)
+        emitter.trigger_foo()
+        self.assertEqual([('trapping_foo',), ('trapping_foo',)],
+                         self.logger.log)
+        slot = self.logger.createNamedSlot('trapping_bar')
+        QtCore.QObject.connect(emitter, QtCore.SIGNAL('bar(int, int)'), slot)
+        emitter.trigger_bar(1, 2)
+        self.assertEqual([('trapping_foo',), ('trapping_foo',),
+                          ('trapping_bar', 1, 2),
+                         ], self.logger.log)



More information about the bazaar-commits mailing list