Skip to content

Commit

Permalink
0.4.2 - Add more safety and improve test coverage (#9)
Browse files Browse the repository at this point in the history
* Add argument safety to several public methods

* Implement test for non existing tag in PGN file tags
  • Loading branch information
DaniruKun committed Nov 28, 2019
1 parent d320088 commit 0311a11
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
25 changes: 19 additions & 6 deletions pypgn/game.py
Expand Up @@ -24,13 +24,23 @@ class Game:
def __init__(self, path: str = None):
"""
:param path: path to pgn file or Lichess game ID or
:param path: path to pgn file or Lichess game ID
:type path: str
"""
self.pgn: list = _get_pgn_list(path)
if path is not None:
self.pgn: list = _get_pgn_list(path)
else:
self.pgn = None
self.tags: dict = _get_tags(self.pgn)
self.moves: List[Move] = _get_moves(self.pgn)

def open_pgn(self, path: str) -> None:
"""Sets the pgn attribute
:param path: path to pgn file or Lichess game ID
"""
self.pgn = _get_pgn_list(path)

def get_pgn_list(self) -> list:
"""Gets and returns a list of lines of the PGN
Expand All @@ -47,7 +57,10 @@ def get_tag(self, name: str) -> str:
:return: Value of a tag
:rtype: str
"""
return self.tags[name]
if name in self.tags:
return self.tags[name]
else:
raise KeyError(f"This tag does not exist: {name}")

def get_tags(self) -> dict:
"""Gets and returns a map of metadata tags of the PGN
Expand Down Expand Up @@ -108,13 +121,13 @@ def get_date(self) -> str:
:return: Date of the game in format YYYY.MM.DD
"""
return self.get_tag('Date')
return self.get_tag('Date' if 'Date' in self.tags else 'UTCDate')

def get_move_range(self, start: int, end: int) -> List[Move]:
def get_move_range(self, start: int = 1, end: int = None) -> List[Move]:
"""Gets and returns a range of moves
:param start: Start index of moves to get
:param end: End index of moves to get
:return: List of moves in given range
"""
return self.moves[start - 1:end]
return self.moves[start - 1:(end if end is not None else -1)]
1 change: 1 addition & 0 deletions pypgn/game_utils.py
Expand Up @@ -29,6 +29,7 @@ def _get_tags(pgn: list) -> dict:


def _get_moves(pgn: list) -> List[Move]:
movetext = ''
for line in pgn:
if re.search(r'^1\. ', line):
movetext: str = line
Expand Down
9 changes: 6 additions & 3 deletions test/test_game.py
Expand Up @@ -19,11 +19,15 @@ def test_get_pgn_list(self, game_pgn):
def test_get_tags(self, game_pgn):
assert type(game_pgn.get_tags()) == dict

def test_get_tag_value(self, game_pgn):
def test_get_tag(self, game_pgn):
assert game_pgn.get_tag('Event') == "Rated Blitz game"
assert game_pgn.get_tag('Site') == "https://lichess.org/#"
assert game_pgn.get_tag('UTCDate') == "2019.11.06"

def test_get_non_existing_tag(self, game_pgn):
with pytest.raises(KeyError):
game_pgn.get_tag('SomeTag')

def test_get_move(self, game_pgn):
assert game_pgn.get_move(4) == ["4.", "d4", "Nc6"]

Expand All @@ -35,8 +39,7 @@ def test_get_moves(self, game_pgn):
def test_get_ply(self, game_pgn):
for i in range(1, game_pgn.get_move_count()):
move = game_pgn.get_move(i)
assert move[1] == game_pgn.get_ply(i, 'w') \
and move[2] == game_pgn.get_ply(i, 'b'), \
assert move[1] == game_pgn.get_ply(i, 'w') and move[2] == game_pgn.get_ply(i, 'b'), \
"Non-matching ply pair at move %s !" % str(i)

def test_get_move_count(self, game_pgn):
Expand Down

0 comments on commit 0311a11

Please sign in to comment.