Skip to content

Commit

Permalink
test_typing: also add test for writing to file with StrEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodong-Yang committed Oct 21, 2024
1 parent 0fd92f5 commit 85b0584
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tests/test_otaclient_common/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,31 @@

from __future__ import annotations

from pathlib import Path
from subprocess import check_output

from otaclient_common.typing import StrEnum


class EnumForTest(StrEnum):
A = "A"


def test_str_enum():
def test_str_enum(tmp_path: Path):
# str enum should be able to compare with string instance directly.
assert EnumForTest.A == EnumForTest.A.value
# str enum's __format__ should be the str's one, returning the str value.
assert f"{EnumForTest.A}" == EnumForTest.A.value
# our version of str enum for < 3.11 fully aligns with >= 3.11, which __str__
# is the str type's one.
assert str(EnumForTest.A) == EnumForTest.A.value

# When directly use as str, StrEnum should be behaved like a normal string.
test_file = tmp_path / "file_written"
test_file.write_text(EnumForTest.A)

assert (
check_output(["cat", str(test_file)]).decode()
== EnumForTest.A
== EnumForTest.A.value
)

0 comments on commit 85b0584

Please sign in to comment.