Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Dec 20, 2021
1 parent c659f3c commit 2dcffbe
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
2 changes: 1 addition & 1 deletion enum_tools/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class StatusFlags(IntFlag):
Stopped = 2 # doc: The system has stopped.
Error = 4 # doc: An error has occurred.

def has_errored(self) -> bool:
def has_errored(self) -> bool: # pragma: no cover
"""
Returns whether the operation has errored.
"""
Expand Down
9 changes: 8 additions & 1 deletion enum_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,16 @@ def get_base_object(enum: Type[HasMRO]) -> Type:
If the members are of indeterminate type then the :class:`object` class is returned.
:param enum:
:rtype:
:raises TypeError: If ``enum`` is not an Enum.
"""

mro = inspect.getmro(enum)
try:
mro = inspect.getmro(enum)
except AttributeError:
raise TypeError("not an Enum")

if Flag in mro:
mro = mro[:mro.index(Flag)]
Expand Down
83 changes: 83 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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, result):
assert is_enum(obj) == result


@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, result):
assert is_enum_member(obj) == result


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, result):
assert is_flag(obj) == result


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]

0 comments on commit 2dcffbe

Please sign in to comment.