Test loading wrong saves

This commit is contained in:
Yohann D'ANELLO 2020-11-27 18:16:54 +01:00
parent 5faebfe556
commit b0e352444b
2 changed files with 32 additions and 1 deletions

View File

@ -139,7 +139,15 @@ class Game:
"""
Loads the game from a dictionary
"""
try:
self.map.load_state(d)
except KeyError:
self.message = "Some keys are missing in your save file.\n" \
"Your save seems to be corrupt. It got deleted."
os.unlink(ResourceManager.get_config_path("save.json"))
self.display_actions(DisplayActions.UPDATE)
return
players = self.map.find_entities(Player)
if not players:
self.message = "No player was found on this map!\n" \

View File

@ -4,6 +4,8 @@
import os
import unittest
from squirrelbattle.resources import ResourceManager
from squirrelbattle.enums import DisplayActions
from squirrelbattle.bootstrap import Bootstrap
@ -43,6 +45,27 @@ class TestGame(unittest.TestCase):
new_state = self.game.save_state()
self.assertEqual(old_state, new_state)
# Error on loading save
with open(ResourceManager.get_config_path("save.json"), "w") as f:
f.write("I am not a JSON file")
self.assertIsNone(self.game.message)
self.game.load_game()
self.assertIsNotNone(self.game.message)
self.game.message = None
with open(ResourceManager.get_config_path("save.json"), "w") as f:
f.write("{}")
self.assertIsNone(self.game.message)
self.game.load_game()
self.assertIsNotNone(self.game.message)
self.game.message = None
# Load game with a dead player
self.game.map.remove_entity(self.game.player)
self.game.save_game()
self.game.load_game()
self.assertIsNotNone(self.game.message)
def test_bootstrap_fail(self) -> None:
"""
Ensure that the test can't play the game,