1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# input-remapper - GUI for device specific keyboard mappings
# Copyright (C) 2025 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.argument import Argument, ArgumentConfig
from inputremapper.injection.macros.macro import Macro, macro_variables
from inputremapper.injection.macros.raw_value import RawValue
from inputremapper.injection.macros.variable import Variable
from tests.lib.test_setup import test_setup
from tests.unit.test_macros.macro_test_base import DummyMapping, MacroTestBase
@test_setup
class TestArgument(MacroTestBase):
def test_resolve(self):
self.assertEqual(Variable("a", const=True).get_value(), "a")
self.assertEqual(Variable(1, const=True).get_value(), 1)
self.assertEqual(Variable(None, const=True).get_value(), None)
# $ is part of a custom string here
self.assertEqual(Variable('"$a"', const=True).get_value(), '"$a"')
self.assertEqual(Variable("'$a'", const=True).get_value(), "'$a'")
self.assertEqual(Variable("$a", const=True).get_value(), "$a")
variable = Variable("a", const=False)
self.assertEqual(variable.get_value(), None)
macro_variables["a"] = 1
self.assertEqual(variable.get_value(), 1)
def test_type_check(self):
def test(value, types, name, position):
argument = Argument(
ArgumentConfig(
types=types,
name=name,
position=position,
),
DummyMapping(),
)
argument.initialize_variable(RawValue(value=value))
return argument.get_value()
def test_variable(variable, types, name, position):
argument = Argument(
ArgumentConfig(
types=types,
name=name,
position=position,
),
DummyMapping(),
)
argument._variable = variable
return argument.get_value()
# allows params that can be cast to the target type
self.assertEqual(test("1", [str, None], "foo", 0), "1")
self.assertEqual(test("1.2", [str], "foo", 2), "1.2")
self.assertRaises(
MacroError,
lambda: test("1.2", [int], "foo", 3),
)
self.assertRaises(MacroError, lambda: test("a", [None], "foo", 0))
self.assertRaises(MacroError, lambda: test("a", [int], "foo", 1))
self.assertRaises(
MacroError,
lambda: test("a", [int, float], "foo", 2),
)
self.assertRaises(
MacroError,
lambda: test("a", [int, None], "foo", 3),
)
self.assertEqual(test("a", [int, float, None, str], "foo", 4), "a")
# variables are expected to be of the Variable type here, not a $string
self.assertRaises(
MacroError,
lambda: test("$a", [int], "foo", 4),
)
# We don't cast values that were explicitly set as strings back into numbers.
variable = Variable("a", const=False)
variable.set_value("5")
self.assertRaises(
MacroError,
lambda: test_variable(variable, [int], "foo", 4),
)
self.assertRaises(
MacroError,
lambda: test("a", [Macro], "foo", 0),
)
self.assertRaises(MacroError, lambda: test("1", [Macro], "foo", 0))
def test_validate_variable_name(self):
self.assertRaises(
MacroError,
lambda: Variable("1a", const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable("$a", const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable("a()", const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable("1", const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable("+", const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable("-", const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable("*", const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable("a,b", const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable("a,b", const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable("#", const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable(1, const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable(None, const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable([], const=False).validate_variable_name(),
)
self.assertRaises(
MacroError,
lambda: Variable((), const=False).validate_variable_name(),
)
# doesn't raise
Variable("a", const=False).validate_variable_name()
Variable("_a", const=False).validate_variable_name()
Variable("_A", const=False).validate_variable_name()
Variable("A", const=False).validate_variable_name()
Variable("Abcd", const=False).validate_variable_name()
Variable("Abcd_", const=False).validate_variable_name()
Variable("Abcd_1234", const=False).validate_variable_name()
Variable("Abcd1234_", const=False).validate_variable_name()
if __name__ == "__main__":
unittest.main()
|