settings: Add tests
This commit is contained in:
parent
c9e781e752
commit
89233957dc
|
@ -13,6 +13,7 @@
|
|||
## 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.,
|
||||
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
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):
|
||||
return new
|
||||
|
||||
try:
|
||||
return bool(int(new))
|
||||
except (TypeError, ValueError):
|
||||
new = str(new).lower()
|
||||
|
||||
if new in ("true", "yes", "on", "t", "y"):
|
||||
return True
|
||||
if new in ("false", "no", "off", "f", "n"):
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue