[PATCH evemu 10/19] tests: Fix tests with fixed lists

Daniel Martin consume.noise at gmail.com
Mon Jan 6 09:38:10 PST 2014


Comparing a fixed list of values with a list of values which have been
computed out of an _unordered_ dictionary is a bad idea. A simple fix
would've been to use an OrderedDict.

Instead, replace the fixed lists of values with descriptive dictionaries
and use those dictionaries to generate the expected values in tests for
the comparisions.

v2: Use constants as keys in _expected_{abs,key}_ntrig_dell_xt2 and make
    the dictionary comprehension Python 2.6 compatible.

Signed-off-by: Daniel Martin <consume.noise at gmail.com>
---
 python/evemu/testing/testcase.py  | 51 +++++++++++++++++++++
 python/evemu/tests/test_device.py | 96 ++++++++++++++-------------------------
 2 files changed, 85 insertions(+), 62 deletions(-)

diff --git a/python/evemu/testing/testcase.py b/python/evemu/testing/testcase.py
index f19504a..2603581 100644
--- a/python/evemu/testing/testcase.py
+++ b/python/evemu/testing/testcase.py
@@ -61,3 +61,54 @@ class BaseTestCase(unittest.TestCase):
     def get_events_file(self):
         return os.path.join(self.data_dir, "ntrig-dell-xt2.event")
 
+    _expected_abs_ntrig_dell_xt2 = {
+            # A: 00 0 9600 75 0 0
+            const.absolute_axes["ABS_X"]:
+                {"min": 0, "max": 9600, "fuzz": 75, "flat": 0, "res": 0},
+            # A: 01 0 7200 78 0 0
+            const.absolute_axes["ABS_Y"]:
+                {"min": 0, "max": 7200, "fuzz": 78, "flat": 0, "res": 0},
+            # A: 30 0 9600 200 0 0
+            const.absolute_axes["ABS_MT_TOUCH_MAJOR"]:
+                {"min": 0, "max": 9600, "fuzz": 200, "flat": 0, "res": 0},
+            # A: 31 0 7200 150 0 0
+            const.absolute_axes["ABS_MT_TOUCH_MINOR"]:
+                {"min": 0, "max": 7200, "fuzz": 150, "flat": 0, "res": 0},
+            # A: 34 0 1 0 0 0
+            const.absolute_axes["ABS_MT_ORIENTATION"]:
+                {"min": 0, "max": 1, "fuzz": 0, "flat": 0, "res": 0},
+            # A: 35 0 9600 75 0 0
+            const.absolute_axes["ABS_MT_POSITION_X"]:
+                {"min": 0, "max": 9600, "fuzz": 75, "flat": 0, "res": 0},
+            # A: 36 0 7200 78 0 0
+            const.absolute_axes["ABS_MT_POSITION_Y"]:
+                {"min": 0, "max": 7200, "fuzz": 78,  "flat": 0, "res": 0}
+            }
+    _expected_key_ntrig_dell_xt2 = {
+            const.buttons["BTN_TOUCH"]: True
+            }
+
+    def get_expected_abs(self, sub_key):
+        expected_items = self._expected_abs_ntrig_dell_xt2.items()
+        expected = dict.fromkeys(const.absolute_axes.values(), 0)
+        expected.update((k, v[sub_key]) for (k, v) in expected_items)
+
+        return expected
+
+    def get_expected_ev_abs(self):
+        expected_keys = self._expected_abs_ntrig_dell_xt2.keys()
+        expected = dict.fromkeys(const.absolute_axes.values(), False)
+        expected.update((k, True) for k in expected_keys)
+
+        return expected
+
+    def get_expected_ev_key(self):
+        expected_keys = self._expected_key_ntrig_dell_xt2.keys()
+        expected = dict.fromkeys(const.buttons.values(), False)
+        expected.update((k, True) for k in expected_keys)
+
+        return expected
+
+    def get_expected_prop(self):
+        # no props for N-Trig-MultiTouch-Virtual-Device
+        return dict.fromkeys(const.absolute_axes.values(), False)
diff --git a/python/evemu/tests/test_device.py b/python/evemu/tests/test_device.py
index 4c7bf65..727a1b1 100644
--- a/python/evemu/tests/test_device.py
+++ b/python/evemu/tests/test_device.py
@@ -159,82 +159,54 @@ class DevicePropertiesTestCase(testcase.BaseTestCase):
         self.assertEqual(self._device.id_version, 272)
 
     def test_get_abs_minimum(self):
-        expected = [
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            ]
-        results = [self._device.get_abs_minimum(x) 
-                   for x in evemu.const.absolute_axes.values()]
-        self.assertEqual(results, expected)
+        keys = evemu.const.absolute_axes.values()
+        results = dict((x, self._device.get_abs_minimum(x)) for x in keys)
+
+        self.assertEqual(results, self.get_expected_abs("min"))
 
     def test_get_abs_maximum(self):
-        expected = [
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9600, 7200, 0, 0, 0, 0,
-            7200, 1, 7200, 0, 9600, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9600,
-            0, 0,
-            ]
-        # Skipping the entry for ABS_CNT; some times it's 0, sometimes a very
-        # large negative number.
-        results = [self._device.get_abs_maximum(x) 
-                   for x in evemu.const.absolute_axes.values()]
-        self.assertEqual(results[:-1], expected[:-1])
+        keys = evemu.const.absolute_axes.values()
+        results = dict((x, self._device.get_abs_maximum(x)) for x in keys)
+
+        self.assertEqual(results, self.get_expected_abs("max"))
 
     def test_get_abs_fuzz(self):
-        expected = [
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 78, 0, 0, 0, 0, 150,
-            0, 78, 0, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 
-            ]
-        results = [self._device.get_abs_fuzz(x) 
-                   for x in evemu.const.absolute_axes.values()]
-        self.assertEqual(results, expected)
+        keys = evemu.const.absolute_axes.values()
+        results = dict((x, self._device.get_abs_fuzz(x)) for x in keys)
+
+        self.assertEqual(results, self.get_expected_abs("fuzz"))
 
     def test_get_abs_flat(self):
-        expected = [
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            ]
-        results = [self._device.get_abs_flat(x) 
-                   for x in evemu.const.absolute_axes.values()]
-        self.assertEqual(results, expected)
+        keys = evemu.const.absolute_axes.values()
+        results = dict((x, self._device.get_abs_flat(x)) for x in keys)
+
+        self.assertEqual(results, self.get_expected_abs("flat"))
 
     def test_get_abs_resolution(self):
-        expected = [
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            ]
-        results = [self._device.get_abs_resolution(x) 
-                   for x in evemu.const.absolute_axes.values()]
-        self.assertEqual(results, expected)
+        keys = evemu.const.absolute_axes.values()
+        results = dict((x, self._device.get_abs_resolution(x)) for x in keys)
+
+        self.assertEqual(results, self.get_expected_abs("res"))
 
     def test_has_prop(self):
-        expected = [
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            ]
-        results = [self._device.has_prop(x) 
-                   for x in evemu.const.absolute_axes.values()]
-        self.assertEqual(results, expected)
+        keys = evemu.const.absolute_axes.values()
+        results = dict((x, bool(self._device.has_prop(x))) for x in keys)
+
+        self.assertEqual(results, self.get_expected_prop())
 
     def test_has_event_ev_abs(self):
-        expected = [
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1,
-            1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
-            ]
-        results = [self._device.has_event(evemu.const.event_types["EV_ABS"], x) 
-                   for x in evemu.const.absolute_axes.values()]
-        self.assertEqual(results, expected)
+        ev_abs = evemu.const.event_types["EV_ABS"]
+        keys = evemu.const.absolute_axes.values()
+        results = dict((x, bool(self._device.has_event(ev_abs, x))) for x in keys)
+
+        self.assertEqual(results, self.get_expected_ev_abs())
 
     def test_has_event_ev_key(self):
-        expected = [
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-            ]
-        results = [self._device.has_event(evemu.const.event_types["EV_KEY"], x) 
-                   for x in evemu.const.buttons.values()]
-        self.assertEqual(results, expected)
+        ev_key = evemu.const.event_types["EV_KEY"]
+        keys = evemu.const.buttons.values()
+        results = dict((x, bool(self._device.has_event(ev_key, x))) for x in keys)
+
+        self.assertEqual(results, self.get_expected_ev_key())
 
 
 if __name__ == "__main__":
-- 
1.8.5.2



More information about the Input-tools mailing list