macro: add unit tests for serialize/deserialize

main
Ilya Zhuravlev 2021-03-24 08:41:29 -04:00
parent cd5383135a
commit f645c6163f
2 changed files with 68 additions and 0 deletions

View File

@ -24,6 +24,9 @@ class BasicAction:
self, self.tag, act[0] self, self.tag, act[0]
)) ))
def __eq__(self, other):
return self.tag == other.tag
class ActionText(BasicAction): class ActionText(BasicAction):
@ -43,6 +46,9 @@ class ActionText(BasicAction):
super().restore(act) super().restore(act)
self.text = act[1] self.text = act[1]
def __eq__(self, other):
return super().__eq__(other) and self.text == other.text
class ActionSequence(BasicAction): class ActionSequence(BasicAction):
@ -77,6 +83,9 @@ class ActionSequence(BasicAction):
for qmk_id in act[1:]: for qmk_id in act[1:]:
self.sequence.append(Keycode.find_by_qmk_id(qmk_id)) self.sequence.append(Keycode.find_by_qmk_id(qmk_id))
def __eq__(self, other):
return super().__eq__(other) and self.sequence == other.sequence
class ActionDown(ActionSequence): class ActionDown(ActionSequence):
@ -122,3 +131,6 @@ class ActionDelay(BasicAction):
def restore(self, act): def restore(self, act):
super().restore(act) super().restore(act)
self.delay = act[1] self.delay = act[1]
def __eq__(self, other):
return super().__eq__(other) and self.delay == other.delay

View File

@ -1,7 +1,9 @@
# SPDX-License-Identifier: GPL-2.0-or-later # SPDX-License-Identifier: GPL-2.0-or-later
import unittest import unittest
from keyboard_comm import DummyKeyboard
from keycodes import Keycode from keycodes import Keycode
from macro_action import ActionTap, ActionDown, ActionText, ActionDelay
from macro_key import KeyDown, KeyTap, KeyUp, KeyString from macro_key import KeyDown, KeyTap, KeyUp, KeyString
from macro_optimizer import remove_repeats, replace_with_tap, replace_with_string from macro_optimizer import remove_repeats, replace_with_tap, replace_with_string
@ -27,3 +29,57 @@ class TestMacro(unittest.TestCase):
def test_replace_string(self): def test_replace_string(self):
self.assertEqual(replace_with_string([KeyTap(KC_A), KeyTap(KC_B)]), [KeyString("ab")]) self.assertEqual(replace_with_string([KeyTap(KC_A), KeyTap(KC_B)]), [KeyString("ab")])
def test_serialize_v1(self):
kb = DummyKeyboard(None)
kb.vial_protocol = 1
data = kb.macro_serialize([ActionText("Hello"), ActionTap([KC_A, KC_B, KC_C]), ActionText("World"),
ActionDown([KC_C, KC_B, KC_A])])
self.assertEqual(data, b"Hello\x01\x04\x01\x05\x01\x06World\x02\x06\x02\x05\x02\x04")
def test_deserialize_v1(self):
kb = DummyKeyboard(None)
kb.vial_protocol = 1
macro = kb.macro_deserialize(b"Hello\x01\x04\x01\x05\x01\x06World\x02\x06\x02\x05\x02\x04")
self.assertEqual(macro, [ActionText("Hello"), ActionTap([KC_A, KC_B, KC_C]), ActionText("World"),
ActionDown([KC_C, KC_B, KC_A])])
def test_serialize_v2(self):
kb = DummyKeyboard(None)
kb.vial_protocol = 2
data = kb.macro_serialize([ActionText("Hello"), ActionTap([KC_A, KC_B, KC_C]), ActionText("World"),
ActionDown([KC_C, KC_B, KC_A]), ActionDelay(1000)])
self.assertEqual(data, b"Hello\x01\x01\x04\x01\x01\x05\x01\x01\x06World\x01\x02\x06\x01\x02\x05\x01\x02\x04"
b"\x01\x04\xEC\x04")
data = kb.macro_serialize([ActionText("Hello"), ActionTap([KC_A, KC_B, KC_C]), ActionText("World"),
ActionDown([KC_C, KC_B, KC_A]), ActionDelay(0)])
self.assertEqual(data, b"Hello\x01\x01\x04\x01\x01\x05\x01\x01\x06World\x01\x02\x06\x01\x02\x05\x01\x02\x04"
b"\x01\x04\x01\x01")
data = kb.macro_serialize([ActionText("Hello"), ActionTap([KC_A, KC_B, KC_C]), ActionText("World"),
ActionDown([KC_C, KC_B, KC_A]), ActionDelay(1)])
self.assertEqual(data, b"Hello\x01\x01\x04\x01\x01\x05\x01\x01\x06World\x01\x02\x06\x01\x02\x05\x01\x02\x04"
b"\x01\x04\x02\x01")
data = kb.macro_serialize([ActionText("Hello"), ActionTap([KC_A, KC_B, KC_C]), ActionText("World"),
ActionDown([KC_C, KC_B, KC_A]), ActionDelay(256)])
self.assertEqual(data, b"Hello\x01\x01\x04\x01\x01\x05\x01\x01\x06World\x01\x02\x06\x01\x02\x05\x01\x02\x04"
b"\x01\x04\x02\x02")
def test_deserialize_v2(self):
kb = DummyKeyboard(None)
kb.vial_protocol = 2
macro = kb.macro_deserialize(b"Hello\x01\x01\x04\x01\x01\x05\x01\x01\x06World\x01\x02\x06\x01\x02\x05"
b"\x01\x02\x04\x01\x04\xEC\x04")
self.assertEqual(macro, [ActionText("Hello"), ActionTap([KC_A, KC_B, KC_C]), ActionText("World"),
ActionDown([KC_C, KC_B, KC_A]), ActionDelay(1000)])
macro = kb.macro_deserialize(b"Hello\x01\x01\x04\x01\x01\x05\x01\x01\x06World\x01\x02\x06\x01\x02\x05"
b"\x01\x02\x04\x01\x04\x01\x01")
self.assertEqual(macro, [ActionText("Hello"), ActionTap([KC_A, KC_B, KC_C]), ActionText("World"),
ActionDown([KC_C, KC_B, KC_A]), ActionDelay(0)])
macro = kb.macro_deserialize(b"Hello\x01\x01\x04\x01\x01\x05\x01\x01\x06World\x01\x02\x06\x01\x02\x05"
b"\x01\x02\x04\x01\x04\x02\x01")
self.assertEqual(macro, [ActionText("Hello"), ActionTap([KC_A, KC_B, KC_C]), ActionText("World"),
ActionDown([KC_C, KC_B, KC_A]), ActionDelay(1)])
macro = kb.macro_deserialize(b"Hello\x01\x01\x04\x01\x01\x05\x01\x01\x06World\x01\x02\x06\x01\x02\x05"
b"\x01\x02\x04\x01\x04\x02\x02")
self.assertEqual(macro, [ActionText("Hello"), ActionTap([KC_A, KC_B, KC_C]), ActionText("World"),
ActionDown([KC_C, KC_B, KC_A]), ActionDelay(256)])