Skip to content

Commit

Permalink
Split test_macros (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb authored Jan 12, 2025
1 parent bc1cd8a commit 0fca3a5
Show file tree
Hide file tree
Showing 16 changed files with 641 additions and 404 deletions.
8 changes: 8 additions & 0 deletions tests/unit/test_macros/macro_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import unittest

from inputremapper.configs.preset import Preset
from inputremapper.configs.validation_errors import MacroError
from inputremapper.injection.context import Context
from inputremapper.injection.global_uinputs import GlobalUInputs, UInput
from inputremapper.injection.macros.macro import Macro, macro_variables
from inputremapper.injection.macros.parse import Parser
from inputremapper.injection.mapping_handlers.mapping_parser import MappingParser
from tests.lib.fixtures import fixtures
from tests.lib.logger import logger
Expand Down Expand Up @@ -101,6 +103,12 @@ def count_tasks(self, macro) -> int:
count += self.count_tasks(child_macro)
return count

def expect_string_in_error(self, string: str, macro: str):
with self.assertRaises(MacroError) as cm:
Parser.parse(macro, self.context)
error = str(cm.exception)
self.assertIn(string, error)


class DummyMapping:
macro_key_sleep_ms = 10
Expand Down
71 changes: 71 additions & 0 deletions tests/unit/test_macros/test_add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# input-remapper - GUI for device specific keyboard mappings
# Copyright (C) 2024 sezanzeb <b8x45ygc9@mozmail.com>
#
# This file is part of input-remapper.
#
# input-remapper is free software: you can redistribute it and/or modify
# 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.
#
# input-remapper is distributed in the hope that it will be useful,
# 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
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.


import unittest

from inputremapper.configs.validation_errors import MacroError
from inputremapper.injection.macros.macro import macro_variables
from inputremapper.injection.macros.parse import Parser
from tests.lib.test_setup import test_setup
from tests.unit.test_macros.macro_test_base import DummyMapping, MacroTestBase


@test_setup
class TestAdd(MacroTestBase):
async def test_add(self):
await Parser.parse("set(a, 1).add(a, 1)", self.context, DummyMapping).run(
self.handler
)
self.assertEqual(macro_variables.get("a"), 2)

await Parser.parse("set(b, 1).add(b, -1)", self.context, DummyMapping).run(
self.handler
)
self.assertEqual(macro_variables.get("b"), 0)

await Parser.parse("set(c, -1).add(c, 500)", self.context, DummyMapping).run(
self.handler
)
self.assertEqual(macro_variables.get("c"), 499)

await Parser.parse("add(d, 500)", self.context, DummyMapping).run(self.handler)
self.assertEqual(macro_variables.get("d"), 500)

async def test_add_invalid(self):
# For invalid input it should do nothing (except to log to the console)
await Parser.parse('set(e, "foo").add(e, 1)', self.context, DummyMapping).run(
self.handler
)
self.assertEqual(macro_variables.get("e"), "foo")

await Parser.parse('set(e, "2").add(e, 3)', self.context, DummyMapping).run(
self.handler
)
self.assertEqual(macro_variables.get("e"), "2")

async def test_raises_error(self):
Parser.parse("add(a, 1)", self.context) # no error
self.assertRaises(MacroError, Parser.parse, "add(a, b)", self.context)
self.assertRaises(MacroError, Parser.parse, 'add(a, "1")', self.context)


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions tests/unit/test_macros/test_hold.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from evdev.ecodes import EV_KEY

from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.configs.validation_errors import MacroError
from inputremapper.injection.macros.parse import Parser
from tests.lib.test_setup import test_setup
from tests.unit.test_macros.macro_test_base import MacroTestBase, DummyMapping
Expand Down Expand Up @@ -180,6 +181,11 @@ async def test_hold_variable(self):
],
)

async def test_raises_error(self):
self.assertRaises(MacroError, Parser.parse, "h(1, 1)", self.context)
self.assertRaises(MacroError, Parser.parse, "h(hold(h(1, 1)))", self.context)
self.assertRaises(MacroError, Parser.parse, "hold(key(a)key(b))", self.context)


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions tests/unit/test_macros/test_hold_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from evdev.ecodes import EV_KEY

from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.configs.validation_errors import MacroError
from inputremapper.injection.macros.parse import Parser
from tests.lib.test_setup import test_setup
from tests.unit.test_macros.macro_test_base import MacroTestBase, DummyMapping
Expand Down Expand Up @@ -119,6 +120,11 @@ async def test_run_plus_syntax(self):
self.assertEqual(self.result[6], (EV_KEY, keyboard_layout.get("b"), 0))
self.assertEqual(self.result[7], (EV_KEY, keyboard_layout.get("a"), 0))

async def test_raises_error(self):
self.assertRaises(
MacroError, Parser.parse, "hold_keys(a, broken, b)", self.context
)


if __name__ == "__main__":
unittest.main()
143 changes: 80 additions & 63 deletions tests/unit/test_macros/test_if_eq.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)

from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.configs.validation_errors import MacroError
from inputremapper.injection.macros.macro import macro_variables
from inputremapper.injection.macros.parse import Parser
from tests.lib.logger import logger
Expand All @@ -37,69 +38,6 @@

@test_setup
class TestIfEq(MacroTestBase):
async def test_ifeq_runs(self):
# deprecated ifeq function, but kept for compatibility reasons
macro = Parser.parse(
"set(foo, 2).ifeq(foo, 2, key(a), key(b))",
self.context,
DummyMapping,
)
code_a = keyboard_layout.get("a")
code_b = keyboard_layout.get("b")

await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_KEY, code_a, 1), (EV_KEY, code_a, 0)])
self.assertEqual(self.count_child_macros(macro), 2)

async def test_ifeq_none(self):
code_a = keyboard_layout.get("a")

# first param None
macro = Parser.parse(
"set(foo, 2).ifeq(foo, 2, None, key(b))", self.context, DummyMapping
)
self.assertEqual(self.count_child_macros(macro), 1)
await macro.run(self.handler)
self.assertListEqual(self.result, [])

# second param None
self.result = []
macro = Parser.parse(
"set(foo, 2).ifeq(foo, 2, key(a), None)", self.context, DummyMapping
)
self.assertEqual(self.count_child_macros(macro), 1)
await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_KEY, code_a, 1), (EV_KEY, code_a, 0)])

"""Old syntax, use None instead"""

# first param ""
self.result = []
macro = Parser.parse(
"set(foo, 2).ifeq(foo, 2, , key(b))", self.context, DummyMapping
)
self.assertEqual(self.count_child_macros(macro), 1)
await macro.run(self.handler)
self.assertListEqual(self.result, [])

# second param ""
self.result = []
macro = Parser.parse(
"set(foo, 2).ifeq(foo, 2, key(a), )", self.context, DummyMapping
)
self.assertEqual(self.count_child_macros(macro), 1)
await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_KEY, code_a, 1), (EV_KEY, code_a, 0)])

async def test_ifeq_unknown_key(self):
macro = Parser.parse("ifeq(qux, 2, key(a), key(b))", self.context, DummyMapping)
code_a = keyboard_layout.get("a")
code_b = keyboard_layout.get("b")

await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_KEY, code_b, 1), (EV_KEY, code_b, 0)])
self.assertEqual(self.count_child_macros(macro), 2)

async def test_if_eq(self):
"""new version of ifeq"""
code_a = keyboard_layout.get("a")
Expand Down Expand Up @@ -193,6 +131,85 @@ def set_foo(value):
],
)

async def test_raises_error(self):
Parser.parse("if_eq(2, $a, k(a),)", self.context) # no error
Parser.parse("if_eq(2, $a, , else=k(a))", self.context) # no error
self.assertRaises(MacroError, Parser.parse, "if_eq(2, $a, 1,)", self.context)
self.assertRaises(MacroError, Parser.parse, "if_eq(2, $a, , 2)", self.context)
self.expect_string_in_error("blub", "if_eq(2, $a, key(a), blub=a)")


class TestIfEqDeprecated(MacroTestBase):
async def test_ifeq_runs(self):
# deprecated ifeq function, but kept for compatibility reasons
macro = Parser.parse(
"set(foo, 2).ifeq(foo, 2, key(a), key(b))",
self.context,
DummyMapping,
)
code_a = keyboard_layout.get("a")
code_b = keyboard_layout.get("b")

await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_KEY, code_a, 1), (EV_KEY, code_a, 0)])
self.assertEqual(self.count_child_macros(macro), 2)

async def test_ifeq_none(self):
code_a = keyboard_layout.get("a")

# first param None
macro = Parser.parse(
"set(foo, 2).ifeq(foo, 2, None, key(b))", self.context, DummyMapping
)
self.assertEqual(self.count_child_macros(macro), 1)
await macro.run(self.handler)
self.assertListEqual(self.result, [])

# second param None
self.result = []
macro = Parser.parse(
"set(foo, 2).ifeq(foo, 2, key(a), None)", self.context, DummyMapping
)
self.assertEqual(self.count_child_macros(macro), 1)
await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_KEY, code_a, 1), (EV_KEY, code_a, 0)])

"""Old syntax, use None instead"""

# first param ""
self.result = []
macro = Parser.parse(
"set(foo, 2).ifeq(foo, 2, , key(b))", self.context, DummyMapping
)
self.assertEqual(self.count_child_macros(macro), 1)
await macro.run(self.handler)
self.assertListEqual(self.result, [])

# second param ""
self.result = []
macro = Parser.parse(
"set(foo, 2).ifeq(foo, 2, key(a), )", self.context, DummyMapping
)
self.assertEqual(self.count_child_macros(macro), 1)
await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_KEY, code_a, 1), (EV_KEY, code_a, 0)])

async def test_ifeq_unknown_key(self):
macro = Parser.parse("ifeq(qux, 2, key(a), key(b))", self.context, DummyMapping)
code_a = keyboard_layout.get("a")
code_b = keyboard_layout.get("b")

await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_KEY, code_b, 1), (EV_KEY, code_b, 0)])
self.assertEqual(self.count_child_macros(macro), 2)

async def test_raises_error(self):
Parser.parse("ifeq(a, 2, k(a),)", self.context) # no error
Parser.parse("ifeq(a, 2, , k(a))", self.context) # no error
Parser.parse("ifeq(a, 2, None, k(a))", self.context) # no error
self.assertRaises(MacroError, Parser.parse, "ifeq(a, 2, 1,)", self.context)
self.assertRaises(MacroError, Parser.parse, "ifeq(a, 2, , 2)", self.context)


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions tests/unit/test_macros/test_if_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)

from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.configs.validation_errors import MacroError
from inputremapper.injection.macros.parse import Parser
from inputremapper.input_event import InputEvent
from tests.lib.test_setup import test_setup
Expand Down Expand Up @@ -181,6 +182,11 @@ async def test_if_single_ignores_joystick(self):
self.assertFalse(macro.running)
self.assertListEqual(self.result, [(EV_KEY, code_a, 1), (EV_KEY, code_a, 0)])

async def test_raises_error(self):
Parser.parse("if_single(k(a),)", self.context) # no error
self.assertRaises(MacroError, Parser.parse, "if_single(1,)", self.context)
self.assertRaises(MacroError, Parser.parse, "if_single(,1)", self.context)


if __name__ == "__main__":
unittest.main()
10 changes: 10 additions & 0 deletions tests/unit/test_macros/test_if_tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)

from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.configs.validation_errors import MacroError
from inputremapper.injection.macros.parse import Parser
from tests.lib.test_setup import test_setup
from tests.unit.test_macros.macro_test_base import DummyMapping, MacroTestBase
Expand Down Expand Up @@ -175,6 +176,15 @@ async def test_if_not_tap_named(self):
self.assertListEqual(self.result, [(EV_KEY, y, 1), (EV_KEY, y, 0)])
self.assertFalse(macro.running)

async def test_raises_error(self):
Parser.parse("if_tap(, k(a), 1000)", self.context) # no error
Parser.parse("if_tap(, k(a), timeout=1000)", self.context) # no error
Parser.parse("if_tap(, k(a), $timeout)", self.context) # no error
Parser.parse("if_tap(, k(a), timeout=$t)", self.context) # no error
Parser.parse("if_tap(, key(a))", self.context) # no error
Parser.parse("if_tap(k(a),)", self.context) # no error
self.assertRaises(MacroError, Parser.parse, "if_tap(k(a), b)", self.context)


if __name__ == "__main__":
unittest.main()
Loading

0 comments on commit 0fca3a5

Please sign in to comment.