input-remapper/tests/unit/test_key.py

118 lines
4.4 KiB
Python
Raw Normal View History

2021-01-01 13:09:28 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
2022-01-01 12:00:49 +00:00
# input-remapper - GUI for device specific keyboard mappings
2022-01-01 12:52:33 +00:00
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
2021-01-01 13:09:28 +00:00
#
2022-01-01 12:00:49 +00:00
# This file is part of input-remapper.
2021-01-01 13:09:28 +00:00
#
2022-01-01 12:00:49 +00:00
# input-remapper is free software: you can redistribute it and/or modify
2021-01-01 13:09:28 +00:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2022-01-01 12:00:49 +00:00
# input-remapper is distributed in the hope that it will be useful,
2021-01-01 13:09:28 +00:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2022-01-01 12:00:49 +00:00
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
2021-01-01 13:09:28 +00:00
import unittest
from evdev.ecodes import KEY_LEFTSHIFT, KEY_RIGHTALT, KEY_LEFTCTRL
2022-01-01 12:00:49 +00:00
from inputremapper.key import Key
2021-01-01 13:09:28 +00:00
class TestKey(unittest.TestCase):
def test_key(self):
# its very similar to regular tuples, but with some extra stuff
key_1 = Key((1, 3, 1), (1, 5, 1))
2021-09-26 10:44:56 +00:00
self.assertEqual(str(key_1), "Key((1, 3, 1), (1, 5, 1))")
2021-01-01 13:09:28 +00:00
self.assertEqual(len(key_1), 2)
self.assertEqual(key_1[0], (1, 3, 1))
self.assertEqual(key_1[1], (1, 5, 1))
self.assertEqual(hash(key_1), hash(((1, 3, 1), (1, 5, 1))))
2021-01-01 13:09:28 +00:00
key_2 = Key((1, 3, 1))
2021-09-26 10:44:56 +00:00
self.assertEqual(str(key_2), "Key((1, 3, 1),)")
2021-01-01 13:09:28 +00:00
self.assertEqual(len(key_2), 1)
self.assertNotEqual(key_2, key_1)
self.assertNotEqual(hash(key_2), hash(key_1))
key_3 = Key(1, 3, 1)
2021-09-26 10:44:56 +00:00
self.assertEqual(str(key_3), "Key((1, 3, 1),)")
2021-01-01 13:09:28 +00:00
self.assertEqual(len(key_3), 1)
self.assertEqual(key_3, key_2)
self.assertEqual(key_3, (1, 3, 1))
self.assertEqual(hash(key_3), hash(key_2))
self.assertEqual(hash(key_3), hash((1, 3, 1)))
key_4 = Key(key_3)
2021-09-26 10:44:56 +00:00
self.assertEqual(str(key_4), "Key((1, 3, 1),)")
2021-01-01 13:09:28 +00:00
self.assertEqual(len(key_4), 1)
self.assertEqual(key_4, key_3)
self.assertEqual(hash(key_4), hash(key_3))
key_5 = Key(key_4, key_4, (1, 7, 1))
2021-09-26 10:44:56 +00:00
self.assertEqual(str(key_5), "Key((1, 3, 1), (1, 3, 1), (1, 7, 1))")
2021-01-01 13:09:28 +00:00
self.assertEqual(len(key_5), 3)
self.assertNotEqual(key_5, key_4)
self.assertNotEqual(hash(key_5), hash(key_4))
self.assertEqual(key_5, ((1, 3, 1), (1, 3, 1), (1, 7, 1)))
self.assertEqual(hash(key_5), hash(((1, 3, 1), (1, 3, 1), (1, 7, 1))))
def test_get_permutations(self):
key_1 = Key((1, 3, 1))
self.assertEqual(len(key_1.get_permutations()), 1)
self.assertEqual(key_1.get_permutations()[0], key_1)
key_2 = Key((1, 3, 1), (1, 5, 1))
self.assertEqual(len(key_2.get_permutations()), 1)
self.assertEqual(key_2.get_permutations()[0], key_2)
key_3 = Key((1, 3, 1), (1, 5, 1), (1, 7, 1))
self.assertEqual(len(key_3.get_permutations()), 2)
self.assertEqual(
2021-09-26 10:44:56 +00:00
key_3.get_permutations()[0], Key((1, 3, 1), (1, 5, 1), (1, 7, 1))
2021-01-01 13:09:28 +00:00
)
2021-09-26 10:44:56 +00:00
self.assertEqual(key_3.get_permutations()[1], ((1, 5, 1), (1, 3, 1), (1, 7, 1)))
2021-01-01 13:09:28 +00:00
def test_is_problematic(self):
key_1 = Key((1, KEY_LEFTSHIFT, 1), (1, 5, 1))
self.assertTrue(key_1.is_problematic())
key_2 = Key((1, KEY_RIGHTALT, 1), (1, 5, 1))
self.assertTrue(key_2.is_problematic())
key_3 = Key((1, 3, 1), (1, KEY_LEFTCTRL, 1))
self.assertTrue(key_3.is_problematic())
key_4 = Key(1, 3, 1)
self.assertFalse(key_4.is_problematic())
key_5 = Key((1, 3, 1), (1, 5, 1))
self.assertFalse(key_5.is_problematic())
2021-04-02 14:33:20 +00:00
def test_raises(self):
self.assertRaises(ValueError, lambda: Key(1))
self.assertRaises(ValueError, lambda: Key(None))
self.assertRaises(ValueError, lambda: Key([1]))
self.assertRaises(ValueError, lambda: Key((1,)))
self.assertRaises(ValueError, lambda: Key((1, 2)))
2021-09-26 10:44:56 +00:00
self.assertRaises(ValueError, lambda: Key(("1", "2", "3")))
self.assertRaises(ValueError, lambda: Key("1"))
self.assertRaises(ValueError, lambda: Key("(1,2,3)"))
self.assertRaises(ValueError, lambda: Key((1, 2, 3), (1, 2, "3")))
2021-04-02 14:33:20 +00:00
self.assertRaises(ValueError, lambda: Key((1, 2, 3), (1, 2, 3), None))
# those don't raise errors
Key((1, 2, 3), (1, 2, 3))
Key((1, 2, 3))
2021-01-01 13:09:28 +00:00
if __name__ == "__main__":
unittest.main()