feat(sides): Side enum with opposite/from_str

This commit is contained in:
ai-chess
2026-05-14 23:03:29 +03:00
parent c02b43abbf
commit 5fcb620ac2
2 changed files with 36 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
from __future__ import annotations
from enum import Enum
class Side(str, Enum):
WHITE = "white"
BLACK = "black"
def opposite(self) -> Side:
return Side.BLACK if self is Side.WHITE else Side.WHITE
@classmethod
def from_str(cls, value: str) -> Side:
v = value.strip().lower()
if v in ("w", "white"):
return cls.WHITE
if v in ("b", "black"):
return cls.BLACK
raise ValueError(f"unknown side: {value!r}")
+16
View File
@@ -0,0 +1,16 @@
from ai_chess.sides import Side
def test_opposite():
assert Side.WHITE.opposite() is Side.BLACK
assert Side.BLACK.opposite() is Side.WHITE
def test_from_str():
assert Side.from_str("white") is Side.WHITE
assert Side.from_str("black") is Side.BLACK
def test_str_value():
assert Side.WHITE.value == "white"
assert Side.BLACK.value == "black"