ScreenPyHQ / screenpy

Screenplay pattern base for Python automated UI test suites.
MIT License
28 stars 4 forks source link

support enums in `represent_prop` #144

Open bandophahita opened 4 months ago

bandophahita commented 4 months ago

Consider the following code

class ScheduleStatus(StrEnum):
    DRAFT = "DRAFT"
    POSTED = "POSTED"
    ARCHIVED = "ARCHIVED"

actor.shall(See(Text(SCHEDULE_STATUS), ReadsExactly(ScheduleStatus.POSTED)))

Which works fine but logs as follows:

Marcel sees if the text from the schedule status is <ScheduleStatus.POSTED: 'POSTED'>, verbatim.

What we really want is:

Marcel sees if the text from the schedule status is 'POSTED', verbatim.

To avoid this we can wrap the enum in a string, but could perhaps represent_prop do this for us?

bandophahita commented 4 months ago

The problem is isinstance(ScheduleStatus.POSTED, str) returns true even though type(ScheduleStatus.POSTED) returns <enum 'ScheduleStatus'>.

I'm curious if we can get away with something like this:

def represent_prop(item: str | T | mock.Mock) -> str | mock.Mock:
    """Represent items in a manner suitable for the audience (logging)."""
    if isinstance(item, mock.Mock):
        return item
    if hasmethod(item, "describe_to"):
        return f"{item}"
    if isisntance(item, Enum):
        return repr(f"{item}")
    if isinstance(item, str):
        return repr(item)
    ...