|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from deebot_client.util.enum import IntEnumWithXml, StrEnumWithXml |
| 6 | + |
| 7 | + |
| 8 | +class _TestStrEnumWithXml(StrEnumWithXml): |
| 9 | + """Simple Enum for testing.""" |
| 10 | + |
| 11 | + ENUM1 = "value1", "xmlvalue1" |
| 12 | + ENUM2 = "value2", "xmlvalue2" |
| 13 | + |
| 14 | + |
| 15 | +class _TestIntEnumWithXml(IntEnumWithXml): |
| 16 | + ENUM1 = 1, "xmlvalue1" |
| 17 | + ENUM2 = 2, "xmlvalue2" |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize( |
| 21 | + ("test_enum", "test_value", "test_xml_value"), |
| 22 | + [ |
| 23 | + (_TestStrEnumWithXml.ENUM1, "value1", "xmlvalue1"), |
| 24 | + (_TestStrEnumWithXml.ENUM2, "value2", "xmlvalue2"), |
| 25 | + ], |
| 26 | +) |
| 27 | +def test_StrEnumWithXml_values( |
| 28 | + test_enum: _TestStrEnumWithXml, test_value: str, test_xml_value: str |
| 29 | +) -> None: |
| 30 | + assert test_enum.value == test_value |
| 31 | + assert test_enum.xml_value == test_xml_value |
| 32 | + assert test_value == _TestStrEnumWithXml.from_xml(test_xml_value).value |
| 33 | + assert test_xml_value == _TestStrEnumWithXml(test_value).xml_value |
| 34 | + |
| 35 | + |
| 36 | +def test_StrEnumWithXml_invalid_value() -> None: |
| 37 | + with pytest.raises(ValueError, match="this_is_invalid"): |
| 38 | + _TestStrEnumWithXml.from_xml("this_is_invalid") |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize( |
| 42 | + ("test_enum", "test_value", "test_xml_value"), |
| 43 | + [ |
| 44 | + (_TestIntEnumWithXml.ENUM1, 1, "xmlvalue1"), |
| 45 | + (_TestIntEnumWithXml.ENUM2, 2, "xmlvalue2"), |
| 46 | + ], |
| 47 | +) |
| 48 | +def test_IntEnumWithXml_values( |
| 49 | + test_enum: _TestIntEnumWithXml, test_value: int, test_xml_value: str |
| 50 | +) -> None: |
| 51 | + assert test_enum.value == test_value |
| 52 | + assert test_enum.xml_value == test_xml_value |
| 53 | + assert test_value == _TestIntEnumWithXml.from_xml(test_xml_value).value |
| 54 | + assert test_xml_value == _TestIntEnumWithXml(test_value).xml_value |
| 55 | + |
| 56 | + |
| 57 | +def test_IntEnumWithXml_invalid_value() -> None: |
| 58 | + with pytest.raises(ValueError, match="this_is_invalid"): |
| 59 | + _TestIntEnumWithXml.from_xml("this_is_invalid") |
0 commit comments