|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TypeVar |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from deebot_client.util.enum import IntEnumWithXml, StrEnumWithXml |
| 8 | + |
| 9 | + |
| 10 | +class _TestStrEnumWithXml(StrEnumWithXml): |
| 11 | + ENUM1 = "value1", "xmlvalue1" |
| 12 | + ENUM2 = "value2", "xmlvalue2" |
| 13 | + |
| 14 | + def assert_self_conversion(self) -> None: |
| 15 | + assert self == _TestStrEnumWithXml(self.value) |
| 16 | + assert self == _TestStrEnumWithXml.from_xml(self.xml_value) |
| 17 | + |
| 18 | + |
| 19 | +class _TestIntEnumWithXml(IntEnumWithXml): |
| 20 | + ENUM1 = 1, "xmlvalue1" |
| 21 | + ENUM2 = 2, "xmlvalue2" |
| 22 | + |
| 23 | + def assert_self_conversion(self) -> None: |
| 24 | + assert self == _TestIntEnumWithXml(self.value) |
| 25 | + assert self == _TestIntEnumWithXml.from_xml(self.xml_value) |
| 26 | + |
| 27 | + |
| 28 | +T = TypeVar("T", _TestStrEnumWithXml, _TestIntEnumWithXml) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize( |
| 32 | + "test_enum", list(_TestStrEnumWithXml) + list(_TestIntEnumWithXml) |
| 33 | +) |
| 34 | +def test_EnumWithXml_conversion( |
| 35 | + test_enum: T, |
| 36 | +) -> None: |
| 37 | + test_enum.assert_self_conversion() |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize( |
| 41 | + ("test_enum_cls", "invalid_value"), |
| 42 | + [ |
| 43 | + (_TestStrEnumWithXml, "this_is_invalid"), |
| 44 | + (_TestStrEnumWithXml, None), |
| 45 | + (_TestIntEnumWithXml, "this_is_invalid"), |
| 46 | + (_TestIntEnumWithXml, None), |
| 47 | + ], |
| 48 | +) |
| 49 | +def test_EnumWithXml_invalid_value( |
| 50 | + test_enum_cls: type[T], invalid_value: str | None |
| 51 | +) -> None: |
| 52 | + with pytest.raises(ValueError, match=str(invalid_value)): |
| 53 | + test_enum_cls.from_xml(invalid_value) |
0 commit comments