Repository: google/mobly

Issue:
`SnippetEvent` should be loggable
Right now logging event object directly does not show the content of the event, which makes debugging difficult.
`logging.info(event)` should log the full content of the event.


Discussion:

Resolving patch:
diff --git a/mobly/controllers/android_device_lib/snippet_event.py b/mobly/controllers/android_device_lib/snippet_event.py
--- a/mobly/controllers/android_device_lib/snippet_event.py
+++ b/mobly/controllers/android_device_lib/snippet_event.py
@@ -3,9 +3,9 @@
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
-# 
+#
 #     http://www.apache.org/licenses/LICENSE-2.0
-# 
+#
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -45,3 +45,8 @@ def __init__(self, callback_id, name, creation_time, data):
         self.name = name
         self.creation_time = creation_time
         self.data = data
+
+    def __repr__(self):
+        return ('SnippetEvent(callback_id: %s, name: %s, creation_time: %s, '
+                'data: %s)') % (self.callback_id, self.name,
+                                self.creation_time, self.data)
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -70,7 +70,7 @@ def main():
         packages=setuptools.find_packages(),
         include_package_data=False,
         scripts=['tools/sl4a_shell.py', 'tools/snippet_shell.py'],
-        tests_require=['pytest'],
+        tests_require=['pytest', 'testfixtures'],
         install_requires=install_requires,
         cmdclass={'test': PyTest}, )
 


Test patch:
diff --git a/tests/mobly/controllers/android_device_lib/snippet_event_test.py b/tests/mobly/controllers/android_device_lib/snippet_event_test.py
new file mode 100755
--- /dev/null
+++ b/tests/mobly/controllers/android_device_lib/snippet_event_test.py
@@ -0,0 +1,45 @@
+# Copyright 2018 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import logging
+import time
+
+from future.tests.base import unittest
+
+from mobly.controllers.android_device_lib import snippet_event
+
+import testfixtures
+
+MOCK_CALLBACK_ID = 'myCallbackId'
+MOCK_EVENT_NAME = 'onXyzEvent'
+MOCK_CREATION_TIME = '12345678'
+MOCK_DATA = {'foo': 'bar'}
+
+
+class SnippetEventTest(unittest.TestCase):
+    def test_basic(self):
+        """Verifies that an event object can be created and logged properly.
+        """
+        event = snippet_event.SnippetEvent(MOCK_CALLBACK_ID, MOCK_EVENT_NAME,
+                                           MOCK_CREATION_TIME, MOCK_DATA)
+        with testfixtures.LogCapture() as l:
+            logger = logging.getLogger()
+            logger.info(event)
+        l.check(('root', 'INFO',
+                 "SnippetEvent(callback_id: myCallbackId, name: onXyzEvent, "
+                 "creation_time: 12345678, data: {'foo': 'bar'})"))
+
+
+if __name__ == "__main__":
+    unittest.main()