settings: Add tests

This commit is contained in:
MattHag 2024-09-28 21:04:30 +02:00 committed by Peter F. Patel-Schneider
parent c9e781e752
commit 89233957dc
2 changed files with 29 additions and 1 deletions

View File

@ -13,6 +13,7 @@
## You should have received a copy of the GNU General Public License along ## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc., ## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from __future__ import annotations
import logging import logging
import math import math
@ -42,13 +43,15 @@ KIND = NamedInts(
) )
def bool_or_toggle(current, new): def bool_or_toggle(current: bool | str, new: bool | str) -> bool:
if isinstance(new, bool): if isinstance(new, bool):
return new return new
try: try:
return bool(int(new)) return bool(int(new))
except (TypeError, ValueError): except (TypeError, ValueError):
new = str(new).lower() new = str(new).lower()
if new in ("true", "yes", "on", "t", "y"): if new in ("true", "yes", "on", "t", "y"):
return True return True
if new in ("false", "no", "off", "f", "n"): if new in ("false", "no", "off", "f", "n"):

View File

@ -0,0 +1,25 @@
import pytest
from logitech_receiver import settings
@pytest.mark.parametrize(
"current, new, expected",
[
(False, "toggle", True),
(True, "~", False),
("don't care", True, True),
("don't care", "true", True),
("don't care", "false", False),
("don't care", "no", False),
("don't care", "off", False),
("don't care", "True", True),
("don't care", "yes", True),
("don't care", "on", True),
("anything", "anything", None),
],
)
def test_bool_or_toggle(current, new, expected):
result = settings.bool_or_toggle(current=current, new=new)
assert result == expected