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

Daniel Martin consume.noise at gmail.com
Thu Jan 2 13:48:00 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 by generating the expected values out
of a dictionary and compare dictionaries at the end.

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

diff --git a/python/evemu/testing/testcase.py b/python/evemu/testing/testcase.py
index f19504a..e33bd5e 100644
--- a/python/evemu/testing/testcase.py
+++ b/python/evemu/testing/testcase.py
@@ -61,3 +61,53 @@ 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
+            # A: 01 0 7200 78 0 0
+            # A: 30 0 9600 200 0 0
+            # A: 31 0 7200 150 0 0
+            # A: 34 0 1 0 0 0
+            # A: 35 0 9600 75 0 0
+            # A: 36 0 7200 78 0 0
+            # ...
+            0x00: {"min":0, "max": 9600, "fuzz": 75,  "flat": 0, "res": 0},
+            0x01: {"min":0, "max": 7200, "fuzz": 78,  "flat": 0, "res": 0},
+            0x30: {"min":0, "max": 9600, "fuzz": 200, "flat": 0, "res": 0},
+            0x31: {"min":0, "max": 7200, "fuzz": 150, "flat": 0, "res": 0},
+            0x34: {"min":0, "max": 1,    "fuzz": 0,   "flat": 0, "res": 0},
+            0x35: {"min":0, "max": 9600, "fuzz": 75,  "flat": 0, "res": 0},
+            0x36: {"min":0, "max": 7200, "fuzz": 78,  "flat": 0, "res": 0}
+            }
+    _expected_key_ntrig_dell_xt2 = {
+            # ...
+            #   Event type 1 (EV_KEY)
+            #     Event code 330 (BTN_TOUCH)
+            # ...
+            330: 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..ceac1b3 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 = {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 = {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 = {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 = {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 = {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 = {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 = {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 = {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