From 47fe859e11a6fb2fba07756493fcc6806844b91a Mon Sep 17 00:00:00 2001 From: xs5871 Date: Wed, 8 Mar 2023 20:14:24 +0000 Subject: [PATCH] Speed up unit tests --- tests/keyboard_test.py | 4 +- tests/test_capsword.py | 2 +- tests/test_combos.py | 29 +++++---- tests/{test_hold_tap.py => test_holdtap.py} | 72 +++++++++++++-------- tests/test_oneshot.py | 19 ++++-- tests/test_sticky_mod.py | 10 --- tests/test_string_substitution.py | 45 ++++++++----- 7 files changed, 107 insertions(+), 74 deletions(-) rename tests/{test_hold_tap.py => test_holdtap.py} (80%) diff --git a/tests/keyboard_test.py b/tests/keyboard_test.py index 1a507c5..84a78e3 100644 --- a/tests/keyboard_test.py +++ b/tests/keyboard_test.py @@ -23,6 +23,8 @@ def code2name(code): class KeyboardTest: + loop_delay_ms = 2 + def __init__( self, modules, @@ -128,4 +130,4 @@ class KeyboardTest: def do_main_loop(self): self.keyboard._main_loop() - time.sleep(0.002) + time.sleep(self.loop_delay_ms / 1000) diff --git a/tests/test_capsword.py b/tests/test_capsword.py index 1a8e452..2ab37b9 100644 --- a/tests/test_capsword.py +++ b/tests/test_capsword.py @@ -10,7 +10,7 @@ from tests.keyboard_test import KeyboardTest class TestCapsWord(unittest.TestCase): def setUp(self): self.kb = KeyboardTest( - [CapsWord()], + [CapsWord(timeout=2 * KeyboardTest.loop_delay_ms)], [ [KC.CW, KC.A, KC.Z, KC.N1, KC.N0, KC.SPC], ], diff --git a/tests/test_combos.py b/tests/test_combos.py index 3a2bc32..8d7f79a 100644 --- a/tests/test_combos.py +++ b/tests/test_combos.py @@ -1,28 +1,36 @@ import unittest from kmk.keys import KC -from kmk.modules.combos import Chord, Combos, Sequence +from kmk.modules.combos import Chord, Combo, Combos, Sequence from kmk.modules.layers import Layers from tests.keyboard_test import KeyboardTest class TestCombo(unittest.TestCase): def setUp(self): + self.t_within = 2 * KeyboardTest.loop_delay_ms + self.t_after = 7 * KeyboardTest.loop_delay_ms + timeout = (self.t_after + self.t_within) // 2 + + # overide default timeouts + Combo.timeout = timeout + Sequence.timeout = timeout + combos = Combos() layers = Layers() KCMO = KC.MO(1) combos.combos = [ Chord((KC.A, KC.B, KC.C), KC.Y), Chord((KC.A, KC.B), KC.X), - Chord((KC.C, KC.D), KC.Z, timeout=80), + Chord((KC.C, KC.D), KC.Z, timeout=2 * timeout), Chord((KC.C, KCMO), KC.Z), - Chord((KC.F, KC.G), KC.Z, timeout=130), - Sequence((KC.N1, KC.N2, KC.N3), KC.Y, timeout=50), - Sequence((KC.N1, KC.N2), KC.X, timeout=50), - Sequence((KC.N3, KC.N4), KC.Z, timeout=100), - Sequence((KC.N1, KC.N1, KC.N1), KC.W, timeout=50), - Sequence((KC.N3, KC.N2, KC.N1), KC.Y, timeout=50, fast_reset=False), - Sequence((KC.LEADER, KC.N1), KC.V, timeout=50), + Chord((KC.F, KC.G), KC.Z, timeout=3 * timeout), + Sequence((KC.N1, KC.N2, KC.N3), KC.Y), + Sequence((KC.N1, KC.N2), KC.X), + Sequence((KC.N3, KC.N4), KC.Z, timeout=2 * timeout), + Sequence((KC.N1, KC.N1, KC.N1), KC.W), + Sequence((KC.N3, KC.N2, KC.N1), KC.Y, fast_reset=False), + Sequence((KC.LEADER, KC.N1), KC.V), ] self.keyboard = KeyboardTest( [combos, layers], @@ -33,9 +41,6 @@ class TestCombo(unittest.TestCase): debug_enabled=False, ) - self.t_within = 40 - self.t_after = 60 - def test_chord(self): keyboard = self.keyboard t_within = self.t_within diff --git a/tests/test_hold_tap.py b/tests/test_holdtap.py similarity index 80% rename from tests/test_hold_tap.py rename to tests/test_holdtap.py index 9a27741..52595dc 100644 --- a/tests/test_hold_tap.py +++ b/tests/test_holdtap.py @@ -10,20 +10,37 @@ class TestHoldTap(unittest.TestCase): def setUp(self): KC.clear() + self.t_within = 2 * KeyboardTest.loop_delay_ms + self.t_after = 5 * KeyboardTest.loop_delay_ms + tap_time = (self.t_after + self.t_within) // 2 + + # overide default timeouts + HoldTap.tap_time = tap_time + def test_holdtap(self): + t_within = self.t_within + t_after = self.t_after + keyboard = KeyboardTest( [Layers(), HoldTap()], [ - [KC.HT(KC.A, KC.LCTL), KC.LT(1, KC.B), KC.C, KC.D], + [ + KC.HT(KC.A, KC.LCTL), + KC.LT(1, KC.B), + KC.C, + KC.D, + ], [KC.N1, KC.N2, KC.N3, KC.N4], ], debug_enabled=False, ) - keyboard.test('HT tap behaviour', [(0, True), 100, (0, False)], [{KC.A}, {}]) + keyboard.test( + 'HT tap behaviour', [(0, True), t_within, (0, False)], [{KC.A}, {}] + ) keyboard.test( - 'HT hold behaviour', [(0, True), 350, (0, False)], [{KC.LCTL}, {}] + 'HT hold behaviour', [(0, True), t_after, (0, False)], [{KC.LCTL}, {}] ) # TODO test multiple mods being held @@ -31,74 +48,74 @@ class TestHoldTap(unittest.TestCase): # HT keyboard.test( 'HT within tap time sequential -> tap behavior', - [(0, True), 100, (0, False), (3, True), (3, False)], + [(0, True), t_within, (0, False), (3, True), (3, False)], [{KC.A}, {}, {KC.D}, {}], ) keyboard.test( 'HT within tap time rolling -> hold behavior', - [(0, True), 100, (3, True), 250, (0, False), (3, False)], + [(0, True), t_within, (3, True), t_after, (0, False), (3, False)], [{KC.LCTL}, {KC.LCTL, KC.D}, {KC.D}, {}], ) keyboard.test( 'HT within tap time nested -> hold behavior', - [(0, True), 100, (3, True), (3, False), 250, (0, False)], + [(0, True), t_within, (3, True), (3, False), t_after, (0, False)], [{KC.LCTL}, {KC.LCTL, KC.D}, {KC.LCTL}, {}], ) keyboard.test( 'HT after tap time sequential -> hold behavior', - [(0, True), 350, (0, False), (3, True), (3, False)], + [(0, True), t_after, (0, False), (3, True), (3, False)], [{KC.LCTL}, {}, {KC.D}, {}], ) keyboard.test( 'HT after tap time rolling -> hold behavior', - [(0, True), 350, (3, True), (0, False), (3, False)], + [(0, True), t_after, (3, True), (0, False), (3, False)], [{KC.LCTL}, {KC.LCTL, KC.D}, {KC.D}, {}], ) keyboard.test( 'HT after tap time nested -> hold behavior', - [(0, True), 350, (3, True), (3, False), (0, False)], + [(0, True), t_after, (3, True), (3, False), (0, False)], [{KC.LCTL}, {KC.LCTL, KC.D}, {KC.LCTL}, {}], ) # LT keyboard.test( 'LT within tap time sequential -> tap behavior', - [(1, True), 100, (1, False), (3, True), (3, False)], + [(1, True), t_within, (1, False), (3, True), (3, False)], [{KC.B}, {}, {KC.D}, {}], ) keyboard.test( 'LT within tap time rolling -> tap behavior', - [(1, True), 100, (3, True), 250, (1, False), (3, False)], + [(1, True), t_within, (3, True), t_after, (1, False), (3, False)], [{KC.B}, {KC.B, KC.D}, {KC.D}, {}], ) keyboard.test( 'LT within tap time nested -> tap behavior', - [(1, True), 100, (3, True), (3, False), 250, (1, False)], + [(1, True), t_within, (3, True), (3, False), t_after, (1, False)], [{KC.B}, {KC.B, KC.D}, {KC.B}, {}], ) keyboard.test( 'LT after tap time sequential -> hold behavior', - [(1, True), 350, (1, False), (3, True), (3, False)], + [(1, True), t_after, (1, False), (3, True), (3, False)], [{KC.D}, {}], ) keyboard.test( 'LT after tap time rolling -> hold behavior', - [(1, True), 350, (3, True), (1, False), (3, False)], + [(1, True), t_after, (3, True), (1, False), (3, False)], [{KC.N4}, {}], ) keyboard.test( 'LT after tap time nested -> hold behavior', - [(1, True), 350, (3, True), (3, False), (1, False)], + [(1, True), t_after, (3, True), (3, False), (1, False)], [{KC.N4}, {}], ) @@ -106,9 +123,9 @@ class TestHoldTap(unittest.TestCase): 'LT after tap time nested -> hold behavior', [ (0, True), - 350, + t_after, (1, True), - 350, + t_after, (3, True), (3, False), (1, False), @@ -118,26 +135,25 @@ class TestHoldTap(unittest.TestCase): ) def test_holdtap_chain(self): + t_after = self.t_after + keyboard = KeyboardTest( [HoldTap()], [ [ KC.N0, - KC.HT(KC.N1, KC.LCTL, tap_time=50), - KC.HT(KC.N2, KC.LSFT, tap_interrupted=True, tap_time=50), + KC.HT(KC.N1, KC.LCTL), + KC.HT(KC.N2, KC.LSFT, tap_interrupted=True), KC.HT( KC.N3, KC.LALT, prefer_hold=False, tap_interrupted=True, - tap_time=50, ), ], ], debug_enabled=False, ) - # t_within = 40 - t_after = 60 keyboard.test( 'chained 0', @@ -275,21 +291,21 @@ class TestHoldTap(unittest.TestCase): # TODO test TT def test_holdtap_repeat(self): + t_within = self.t_within + t_after = self.t_after + keyboard = KeyboardTest( [HoldTap()], [ [ - KC.HT(KC.A, KC.B, repeat=HoldTapRepeat.ALL, tap_time=50), - KC.HT(KC.A, KC.B, repeat=HoldTapRepeat.TAP, tap_time=50), - KC.HT(KC.A, KC.B, repeat=HoldTapRepeat.HOLD, tap_time=50), + KC.HT(KC.A, KC.B, repeat=HoldTapRepeat.ALL), + KC.HT(KC.A, KC.B, repeat=HoldTapRepeat.TAP), + KC.HT(KC.A, KC.B, repeat=HoldTapRepeat.HOLD), ] ], debug_enabled=False, ) - t_within = 40 - t_after = 60 - keyboard.test( 'repeat tap', [ diff --git a/tests/test_oneshot.py b/tests/test_oneshot.py index 3cfc4ac..eaba85c 100644 --- a/tests/test_oneshot.py +++ b/tests/test_oneshot.py @@ -6,25 +6,30 @@ from kmk.modules.oneshot import OneShot from tests.keyboard_test import KeyboardTest -class TestHoldTap(unittest.TestCase): +class TestOneshot(unittest.TestCase): def test_oneshot(self): + t_within = 2 * KeyboardTest.loop_delay_ms + t_after = 7 * KeyboardTest.loop_delay_ms + timeout = (t_after + t_within) // 2 + + # overide default timeouts + OneShot.tap_time = timeout + keyboard = KeyboardTest( [Layers(), OneShot()], [ [ - KC.OS(KC.MO(1), tap_time=50), + KC.OS(KC.MO(1)), KC.MO(1), KC.C, KC.D, - KC.OS(KC.E, tap_time=50), - KC.OS(KC.F, tap_time=50), + KC.OS(KC.E), + KC.OS(KC.F), ], - [KC.N0, KC.N1, KC.N2, KC.N3, KC.OS(KC.LSFT, tap_time=50), KC.TRNS], + [KC.N0, KC.N1, KC.N2, KC.N3, KC.OS(KC.LSFT), KC.TRNS], ], debug_enabled=False, ) - t_within = 40 - t_after = 60 keyboard.test( 'OS timed out', diff --git a/tests/test_sticky_mod.py b/tests/test_sticky_mod.py index 92c13eb..0323670 100644 --- a/tests/test_sticky_mod.py +++ b/tests/test_sticky_mod.py @@ -37,11 +37,8 @@ class TestStickyMod(unittest.TestCase): [ (4, True), (4, False), - 100, (4, True), - 200, (4, False), - 100, (1, True), (1, False), ], @@ -61,26 +58,19 @@ class TestStickyMod(unittest.TestCase): (1, True), (1, False), (2, True), - 200, (0, True), - 50, (0, False), - 50, (0, True), - 50, (0, False), (1, True), (1, False), - 50, (1, True), (1, False), (0, True), - 50, (0, False), (3, True), (3, False), (2, False), - 100, (4, True), (4, False), (1, True), diff --git a/tests/test_string_substitution.py b/tests/test_string_substitution.py index ae49d92..4095929 100644 --- a/tests/test_string_substitution.py +++ b/tests/test_string_substitution.py @@ -7,6 +7,7 @@ from tests.keyboard_test import KeyboardTest class TestStringSubstitution(unittest.TestCase): def setUp(self) -> None: + self.delay = KeyboardTest.loop_delay_ms self.symbols = '`-=[]\\;\',./~!@#$%^&*()_+{}|:\"<>?' self.everything = ALL_NUMBERS + ALL_ALPHAS + ALL_ALPHAS.lower() + self.symbols self.test_dictionary = { @@ -46,21 +47,21 @@ class TestStringSubstitution(unittest.TestCase): # that results in a corresponding match, as that key is never sent self.keyboard.test( 'multi-character key, single-character value', - [(0, True), (0, False), (0, True), (0, False), 50], + [(0, True), (0, False), (0, True), (0, False), self.delay], [{KC.A}, {}, {KC.BACKSPACE}, {}, {KC.B}, {}], ) # note: the pressed key is never sent here, as the event is # intercepted and the replacement is sent instead self.keyboard.test( 'multi-character value, single-character key', - [(1, True), (1, False), 50], + [(1, True), (1, False), self.delay], [{KC.A}, {}, {KC.A}, {}], ) # modifiers are force-released if there's a match, # so the keyup event for them isn't sent self.keyboard.test( 'shifted alphanumeric or symbol in key and/or value', - [(3, True), (2, True), (2, False), (3, False), 50], + [(3, True), (2, True), (2, False), (3, False), self.delay], [{KC.LSHIFT}, {KC.LSHIFT, KC.N2}, {}], ) self.keyboard.test( @@ -74,7 +75,7 @@ class TestStringSubstitution(unittest.TestCase): (5, False), (5, True), (5, False), - 10, + self.delay, ], [ {KC.D}, @@ -93,7 +94,7 @@ class TestStringSubstitution(unittest.TestCase): ) self.keyboard.test( 'the presence of non-shift modifiers prevents a multi-character match', - [(4, True), (0, True), (0, False), (0, True), (0, False), (4, False), 50], + [(4, True), (0, True), (0, False), (0, True), (0, False), (4, False)], [ {KC.LCTRL}, {KC.LCTRL, KC.A}, @@ -105,7 +106,7 @@ class TestStringSubstitution(unittest.TestCase): ) self.keyboard.test( 'the presence of non-shift modifiers prevents a single-character match', - [(4, True), (1, True), (1, False), (4, False), 50], + [(4, True), (1, True), (1, False), (4, False)], [ {KC.LCTRL}, {KC.LCTRL, KC.B}, @@ -115,7 +116,7 @@ class TestStringSubstitution(unittest.TestCase): ) self.keyboard.test( 'the presence of non-shift modifiers resets current potential matches', - [(0, True), (0, False), (4, True), (0, True), (0, False), (4, False), 50], + [(0, True), (0, False), (4, True), (0, True), (0, False), (4, False)], [ {KC.A}, {}, @@ -128,7 +129,15 @@ class TestStringSubstitution(unittest.TestCase): self.keyboard.test( 'match found and replaced when there are preceding characters', - [(5, True), (5, False), (0, True), (0, False), (0, True), (0, False), 50], + [ + (5, True), + (5, False), + (0, True), + (0, False), + (0, True), + (0, False), + self.delay, + ], [ {KC.C}, {}, @@ -142,7 +151,15 @@ class TestStringSubstitution(unittest.TestCase): ) self.keyboard.test( 'match found and replaced when there are trailing characters, and the trailing characters are sent', - [(0, True), (0, False), (0, True), (0, False), (5, True), (5, False), 50], + [ + (0, True), + (0, False), + (0, True), + (0, False), + (5, True), + (5, False), + self.delay, + ], [ {KC.A}, {}, @@ -156,7 +173,7 @@ class TestStringSubstitution(unittest.TestCase): ) self.keyboard.test( 'no match', - [(0, True), (0, False), (2, True), (2, False), 50], + [(0, True), (0, False), (2, True), (2, False)], [ {KC.A}, {}, @@ -183,7 +200,7 @@ class TestStringSubstitution(unittest.TestCase): (6, False), (0, True), (0, False), - 50, + 10 * self.delay, ], [ {KC.D}, @@ -241,7 +258,7 @@ class TestStringSubstitution(unittest.TestCase): # send the unreachable match "cccc" after matching "ccc" (5, True), (5, False), - 10, + self.delay, ], [ {KC.C}, @@ -272,7 +289,7 @@ class TestStringSubstitution(unittest.TestCase): (0, True), (0, False), (7, False), - 10, + self.delay, ], [ {KC.RSHIFT}, @@ -303,7 +320,6 @@ class TestStringSubstitution(unittest.TestCase): (0, False), (4, False), (8, False), - 10, ], [ {KC.RALT}, @@ -325,7 +341,6 @@ class TestStringSubstitution(unittest.TestCase): (1, False), (3, False), (8, False), - 10, ], [ {KC.RALT},