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

Daniel Martin consume.noise at gmail.com
Tue Jan 7 01:15:58 PST 2014


On Tue, Jan 07, 2014 at 09:17:39AM +1000, Peter Hutterer wrote:
> On Mon, Jan 06, 2014 at 06:38:10PM +0100, Daniel Martin wrote:
> > 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):
> 
> sorry, should've seen that in the first version: I think the naming here
> would be better as get_expected_absbits and get_expected_keybits,
> .propbits.

What would be a proper renaming of the tests
    test_has_event_ev_abs -> test_has_event_absbits
or
    test_has_event_ev_abs -> test_has_absbits?

> > +        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)
> 
> this isn't a comment on your patch, more for the archives and future work:
> I'd really prefer if for the future the BaseTestCase is abstracted in a way
> that the n-trig-specific data is in an n-trig specifica class and object so
> that we can add/remove devices and have device-specific data as such.
> the code would roughly be something like this:
> 
>   class BaseTestClass:
>       def setUp():
>           self.sourcedevice = new TestDevice("ntrig-dell-xt2")
> 
>       def get_device_file(self):
>           return self.sourcedevice.get_source_file()
> 
>       def get_expected_keybits(self):
>           return self.sourcedevice.keybits()
>
> if anyone thinks of expanding the test suite, this would be a good next step
> because it would allow adding more devices easily and just re-running the
> tests for multiple devices.

I thought about that too. Would it be sufficient to specify the expected
values for a device as I did in a dictionary?
The alternative would be to have another parser in the test suite. This
would make it very easy to add more tests for devices, because only the
property file to use would need to be specified. But, it requires more
work if the property file format changes.

So, more work to do when adding a new device (transform a property file
to a dictionary) or more work if the file format changes?

> 
> Cheers,
>    Peter
> 
> > 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
> > 
> > _______________________________________________
> > Input-tools mailing list
> > Input-tools at lists.freedesktop.org
> > http://lists.freedesktop.org/mailman/listinfo/input-tools


More information about the Input-tools mailing list