2022-04-17 23:52:24 +02:00
|
|
|
import unittest
|
|
|
|
|
2022-04-23 09:01:20 +02:00
|
|
|
from kmk.keys import KC, Key, ModifierKey, make_key
|
2022-04-17 23:52:24 +02:00
|
|
|
from tests.keyboard_test import KeyboardTest
|
|
|
|
|
|
|
|
|
|
|
|
class TestKmkKeys(unittest.TestCase):
|
|
|
|
def test_basic_kmk_keyboard(self):
|
|
|
|
keyboard = KeyboardTest(
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
KC.HASH,
|
|
|
|
KC.RALT(KC.HASH),
|
2022-04-19 02:15:27 +02:00
|
|
|
KC.RALT(KC.LSFT(KC.N3)),
|
2022-04-17 23:52:24 +02:00
|
|
|
KC.RALT(KC.LSFT),
|
2022-04-19 02:15:27 +02:00
|
|
|
# Note: this is correct, if unusual, syntax. It's a useful test because it failed silently on previous builds.
|
|
|
|
KC.RALT(KC.LSFT)(KC.N3),
|
2022-04-17 23:52:24 +02:00
|
|
|
KC.RALT,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
)
|
|
|
|
keyboard.test(
|
|
|
|
'Shifted key',
|
|
|
|
[(0, True), (0, False)],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
KC.N3,
|
|
|
|
KC.LSFT,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
keyboard.test(
|
2022-04-19 02:15:27 +02:00
|
|
|
'AltGr+Shifted key',
|
2022-04-17 23:52:24 +02:00
|
|
|
[(1, True), (1, False)],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
KC.N3,
|
|
|
|
KC.LSFT,
|
|
|
|
KC.RALT,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
keyboard.test(
|
2022-04-19 02:15:27 +02:00
|
|
|
'AltGr+Shift+key',
|
2022-04-17 23:52:24 +02:00
|
|
|
[(2, True), (2, False)],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
KC.N3,
|
|
|
|
KC.LSFT,
|
|
|
|
KC.RALT,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
keyboard.test(
|
|
|
|
'Shift+AltGr',
|
|
|
|
[(3, True), (3, False)],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
KC.LSFT,
|
|
|
|
KC.RALT,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
keyboard.test(
|
2022-04-19 02:15:27 +02:00
|
|
|
'AltGr+Shift+key, alternate chaining',
|
2022-04-17 23:52:24 +02:00
|
|
|
[(4, True), (4, False)],
|
2022-04-19 02:15:27 +02:00
|
|
|
[
|
|
|
|
{
|
|
|
|
KC.N3,
|
|
|
|
KC.LSFT,
|
|
|
|
KC.RALT,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
keyboard.test(
|
|
|
|
'AltGr',
|
|
|
|
[(5, True), (5, False)],
|
2022-04-17 23:52:24 +02:00
|
|
|
[
|
|
|
|
{
|
|
|
|
KC.RALT,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
assert isinstance(KC.RGUI(no_press=True), ModifierKey)
|
|
|
|
assert isinstance(KC.RALT(KC.RGUI), ModifierKey)
|
|
|
|
assert isinstance(KC.Q(no_press=True), Key)
|
|
|
|
assert not isinstance(KC.Q(no_press=True), ModifierKey)
|
|
|
|
assert isinstance(KC.RALT(KC.Q), Key)
|
|
|
|
assert not isinstance(KC.RALT(KC.Q), ModifierKey)
|
|
|
|
|
|
|
|
|
2022-04-22 20:16:30 +02:00
|
|
|
class TestKeys_dot(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2022-04-23 08:57:39 +02:00
|
|
|
KC.clear()
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
def test_expected_code_uppercase(self):
|
|
|
|
assert 4 == KC.A.code
|
|
|
|
|
|
|
|
def test_expected_code_lowercase(self):
|
|
|
|
assert 4 == KC.a.code
|
|
|
|
|
|
|
|
def test_case_ignored_alpha(self):
|
|
|
|
upper_key = KC.A
|
|
|
|
lower_key = KC.a
|
|
|
|
assert upper_key is lower_key
|
|
|
|
|
|
|
|
def test_case_requested_order_irrelevant(self):
|
|
|
|
lower_key = KC.a
|
|
|
|
upper_key = KC.A
|
|
|
|
assert upper_key is lower_key
|
|
|
|
|
|
|
|
def test_secondary_name(self):
|
|
|
|
primary_key = KC.NO
|
|
|
|
secondary_key = KC.XXXXXXX
|
|
|
|
assert primary_key is secondary_key
|
|
|
|
|
|
|
|
def test_invalid_key_upper(self):
|
2023-03-06 21:31:16 +01:00
|
|
|
assert KC.INVALID_KEY == KC.NO
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
def test_invalid_key_lower(self):
|
2023-03-06 21:31:16 +01:00
|
|
|
assert KC.invalid_key == KC.NO
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
def test_custom_key(self):
|
|
|
|
created = make_key(
|
|
|
|
KC.N2.code,
|
|
|
|
names=(
|
|
|
|
'EURO',
|
|
|
|
'€',
|
|
|
|
),
|
|
|
|
has_modifiers={KC.LSFT.code, KC.ROPT.code},
|
|
|
|
)
|
|
|
|
assert created is KC.get('EURO')
|
|
|
|
assert created is KC.get('€')
|
|
|
|
|
|
|
|
def test_match_exactly_case(self):
|
|
|
|
created = make_key(names=('ThIs_Is_A_StRaNgE_kEy',))
|
|
|
|
assert created is KC.get('ThIs_Is_A_StRaNgE_kEy')
|
|
|
|
|
|
|
|
|
|
|
|
class TestKeys_index(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2022-04-23 08:57:39 +02:00
|
|
|
KC.clear()
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
def test_expected_code_uppercase(self):
|
|
|
|
assert 4 == KC['A'].code
|
|
|
|
|
|
|
|
def test_expected_code_lowercase(self):
|
|
|
|
assert 4 == KC['a'].code
|
|
|
|
|
|
|
|
def test_case_ignored_alpha(self):
|
|
|
|
upper_key = KC['A']
|
|
|
|
lower_key = KC['a']
|
|
|
|
assert upper_key is lower_key
|
|
|
|
|
|
|
|
def test_case_requested_order_irrelevant(self):
|
|
|
|
lower_key = KC['a']
|
|
|
|
upper_key = KC['A']
|
|
|
|
assert upper_key is lower_key
|
|
|
|
|
|
|
|
def test_invalid_key_upper(self):
|
2023-03-06 21:31:16 +01:00
|
|
|
assert KC.INVALID_KEY == KC.NO
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
def test_invalid_key_lower(self):
|
2023-03-06 21:31:16 +01:00
|
|
|
assert KC.invalid_key == KC.NO
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
def test_custom_key(self):
|
|
|
|
created = make_key(
|
|
|
|
KC['N2'].code,
|
|
|
|
names=(
|
|
|
|
'EURO',
|
|
|
|
'€',
|
|
|
|
),
|
|
|
|
has_modifiers={KC['LSFT'].code, KC['ROPT'].code},
|
|
|
|
)
|
|
|
|
assert created is KC['EURO']
|
|
|
|
assert created is KC['€']
|
|
|
|
|
|
|
|
def test_match_exactly_case(self):
|
|
|
|
created = make_key(names=('ThIs_Is_A_StRaNgE_kEy',))
|
|
|
|
assert created is KC['ThIs_Is_A_StRaNgE_kEy']
|
|
|
|
|
|
|
|
|
|
|
|
class TestKeys_get(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2022-04-23 08:57:39 +02:00
|
|
|
KC.clear()
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
def test_expected_code_uppercase(self):
|
|
|
|
assert 4 == KC.get('A').code
|
|
|
|
|
|
|
|
def test_expected_code_lowercase(self):
|
|
|
|
assert 4 == KC.get('a').code
|
|
|
|
|
|
|
|
def test_case_ignored_alpha(self):
|
|
|
|
upper_key = KC.get('A')
|
|
|
|
lower_key = KC.get('a')
|
|
|
|
assert upper_key is lower_key
|
|
|
|
|
|
|
|
def test_case_requested_order_irrelevant(self):
|
|
|
|
lower_key = KC.get('a')
|
|
|
|
upper_key = KC.get('A')
|
|
|
|
assert upper_key is lower_key
|
|
|
|
|
|
|
|
def test_secondary_name(self):
|
|
|
|
primary_key = KC.NO
|
|
|
|
secondary_key = KC.XXXXXXX
|
|
|
|
assert primary_key is secondary_key
|
|
|
|
|
|
|
|
def test_invalid_key_upper(self):
|
2023-03-06 21:31:16 +01:00
|
|
|
assert KC.get('INVALID_KEY') is KC.NO
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
def test_invalid_key_lower(self):
|
2023-03-06 21:31:16 +01:00
|
|
|
assert KC.get('not_a_valid_key') is KC.NO
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
def test_custom_key(self):
|
|
|
|
created = make_key(
|
|
|
|
KC.get('N2').code,
|
|
|
|
names=(
|
|
|
|
'EURO',
|
|
|
|
'€',
|
|
|
|
),
|
|
|
|
has_modifiers={KC.get('LSFT').code, KC.get('ROPT').code},
|
|
|
|
)
|
|
|
|
assert created is KC.get('EURO')
|
|
|
|
assert created is KC.get('€')
|
|
|
|
|
|
|
|
def test_match_exactly_case(self):
|
|
|
|
created = make_key(names=('ThIs_Is_A_StRaNgE_kEy',))
|
|
|
|
assert created is KC.get('ThIs_Is_A_StRaNgE_kEy')
|
|
|
|
|
2022-07-02 10:08:59 +02:00
|
|
|
def test_underscore(self):
|
|
|
|
assert KC.get('_')
|
|
|
|
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
# Some of these test appear silly, but they're testing we get the
|
|
|
|
# same, single, instance back when requested through KC and that
|
|
|
|
# order of request doesn't matter
|
|
|
|
class TestKeys_instances(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2022-04-23 08:57:39 +02:00
|
|
|
KC.clear()
|
2022-04-22 20:16:30 +02:00
|
|
|
|
|
|
|
def test_make_key_new_instance(self):
|
|
|
|
key1 = make_key(code=1)
|
|
|
|
key2 = make_key(code=1)
|
|
|
|
assert key1 is not key2
|
|
|
|
assert key1.code == key2.code
|
|
|
|
|
|
|
|
def test_index_is_index(self):
|
|
|
|
assert KC['A'] is KC['A']
|
|
|
|
|
|
|
|
def test_index_is_dot(self):
|
|
|
|
assert KC['A'] is KC.A
|
|
|
|
|
|
|
|
def test_index_is_get(self):
|
|
|
|
assert KC['A'] is KC.get('A')
|
|
|
|
|
|
|
|
def test_dot_is_dot(self):
|
|
|
|
assert KC.A is KC.A
|
|
|
|
|
|
|
|
def test_dot_is_index(self):
|
|
|
|
assert KC.A is KC['A']
|
|
|
|
|
|
|
|
def test_dot_is_get(self):
|
|
|
|
assert KC.A is KC.get('A')
|
|
|
|
|
|
|
|
def test_get_is_get(self):
|
|
|
|
assert KC.get('A') is KC.get('A')
|
|
|
|
|
|
|
|
def test_get_is_index(self):
|
|
|
|
assert KC.get('A') is KC['A']
|
|
|
|
|
|
|
|
def test_get_is_dot(self):
|
|
|
|
assert KC.get('A') is KC.A
|
|
|
|
|
|
|
|
|
2022-04-17 23:52:24 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|