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
|
# stdlib
import enum
import http
# 3rd party
import pytest
# this package
from enum_tools import StrEnum
from enum_tools.utils import get_base_object, is_enum, is_enum_member, is_flag
@pytest.mark.parametrize(
"obj, result",
[
(enum.Enum, True),
(http.HTTPStatus, True),
(http.HTTPStatus.NOT_ACCEPTABLE, False),
(123, False),
("abc", False),
]
)
def test_is_enum(obj: object, result: bool):
assert is_enum(obj) == result # type: ignore[arg-type]
@pytest.mark.parametrize(
"obj, result",
[
(enum.Enum, False),
(http.HTTPStatus, False),
(http.HTTPStatus.NOT_ACCEPTABLE, True),
(123, False),
("abc", False),
]
)
def test_is_enum_member(obj: object, result: bool):
assert is_enum_member(obj) == result # type: ignore[arg-type]
class Colours(enum.Flag):
RED = 1
BLUE = 2
PURPLE = Colours.RED | Colours.BLUE
@pytest.mark.parametrize(
"obj, result",
[
(enum.Enum, False),
(http.HTTPStatus, False),
(http.HTTPStatus.NOT_ACCEPTABLE, False),
(123, False),
("abc", False),
(Colours, True),
(Colours.RED, False),
(PURPLE, False),
]
)
def test_is_flag(obj: object, result: bool):
assert is_flag(obj) == result # type: ignore[arg-type]
def test_get_base_object():
# TODO: report issue to mypy
assert get_base_object(enum.Enum) is object # type: ignore[arg-type]
assert get_base_object(Colours) is object # type: ignore[arg-type]
assert get_base_object(enum.IntFlag) is int # type: ignore[arg-type]
assert get_base_object(StrEnum) is str # type: ignore[arg-type]
with pytest.raises(TypeError, match="not an Enum"):
get_base_object("abc") # type: ignore[arg-type]
with pytest.raises(TypeError, match="not an Enum"):
get_base_object(123) # type: ignore[arg-type]
with pytest.raises(TypeError, match="not an Enum"):
get_base_object(str) # type: ignore[arg-type]
with pytest.raises(TypeError, match="not an Enum"):
get_base_object(int) # type: ignore[arg-type]
|