def setUp(self): self.game_file = game self.game_file.USE_CURSES = False self.game = self.game_file.Game() files.new_game() self.game.player = player.Player("Test Player") self.game.room.current_room = files.load_room("shore") self.game.validate_object(self.game.room.current_room, "shore")
def test_load_save_game(self): print "" print "TEST load_game functionality, first start a new game." print "Make some changes, save the game, start another new game." print "Then load game and check that the changes we made are as expected." files.new_game() rooms = name_lists.room_info().get_titles() items = name_lists.item_info().get_titles() new_rooms_dir = name_lists.save_info().get_temp_save_dir_rooms() new_items_dir = name_lists.save_info().get_temp_save_dir_items() save_rooms_dir = name_lists.save_info().get_save_dir_rooms() save_items_dir = name_lists.save_info().get_save_dir_items() print "Open each room and edit the visited attribute" for title in rooms: room = files.load_room(title) room['visited'] = "" files.store_room(room) print "Open each item and edit the active attribute" for title in items: item = files.load_item(title) if item['active']: item['active'] = False else: item['active'] = True files.store_item(item) title = random.choice(rooms) current_room = files.load_room(title) files.save_game(self.player, current_room, 0) files.new_game() print "TEST compare the files in temp and save and confirm no match" match, mismatch, errors = filecmp.cmpfiles(save_rooms_dir, new_rooms_dir, rooms) for file_ in match: self.assertNotIn(file_, rooms) self.assertEqual(len(match), 0) self.assertEqual(len(errors), 0) match, mismatch, errors = filecmp.cmpfiles(save_items_dir, new_items_dir, items) for file_ in match: self.assertNotIn(file_, items) self.assertEqual(len(match), 0) self.assertEqual(len(errors), 0) print "TEST that player file exists and that it is a JSON dict" save_dir = name_lists.save_info().get_save_dir() player_file = os.path.join(save_dir, 'player') self.assertEqual(os.path.isfile(player_file), True) try: with open(player_file, 'r') as player_file: player = json.load(player_file) except Exception, e: player = None
def test_save_game(self): print "" print "TEST the save game of file_lib" files.new_game() rooms = name_lists.room_info().get_titles() items = name_lists.item_info().get_titles() title = random.choice(rooms) current_room = files.load_room(title) new_rooms_dir = name_lists.save_info().get_temp_save_dir_rooms() new_items_dir = name_lists.save_info().get_temp_save_dir_items() save_rooms_dir = name_lists.save_info().get_save_dir_rooms() save_items_dir = name_lists.save_info().get_save_dir_items() print "Open each room and edit the visited attribute" for title in rooms: room = files.load_room(title) room['visited'] = True files.store_room(room) print "Open each item and edit the active attribute" for title in items: item = files.load_item(title) if item['active']: item['active'] = False else: item['active'] = True files.store_item(item) files.save_game(self.player, current_room, 0) print "TEST compare the room files in both dirs to check that they match" match, mismatch, errors = filecmp.cmpfiles(save_rooms_dir, new_rooms_dir, rooms) for file_ in match: self.assertIn(file_, rooms) self.assertEqual(len(mismatch), 0) self.assertEqual(len(errors), 0) print "TEST compare the item files in both dirs to check that they match" match, mismatch, errors = filecmp.cmpfiles(save_items_dir, new_items_dir, items) for file_ in match: self.assertIn(file_, items) self.assertEqual(len(mismatch), 0) self.assertEqual(len(errors), 0) print "TEST that player file exists and that it is a JSON dict" save_dir = name_lists.save_info().get_save_dir() player_file = os.path.join(save_dir, 'player') self.assertEqual(os.path.isfile(player_file), True) try: with open(player_file, 'r') as player_file: player = json.load(player_file) except Exception, e: player = None
def newGame(self): """ copies the files over from template dir to the temp save dir and starts a new player """ files.new_game() self.player = self.gen_player() #New games start at the shore self.room.current_room = files.load_room("shore") self.validate_object(self.room.current_room, 'shore') #for testing purposes load a specific room and start from there if LOAD_SPECIFIC_ROOM_ON_NEW_GAME: self.room.current_room = files.load_room(SPECIFIC_ROOM) self.validate_object(self.room.current_room, SPECIFIC_ROOM)
def test_new_game(self): IGNORE = files.IGNORE rooms_dir = os.path.abspath('data/rooms') items_dir = os.path.abspath('data/items') room_files = os.listdir(rooms_dir) item_files = os.listdir(items_dir) result = files.new_game() new_rooms_dir = name_lists.save_info().get_temp_save_dir_rooms() new_items_dir = name_lists.save_info().get_temp_save_dir_items() new_room_files = os.listdir(new_rooms_dir) new_item_files = os.listdir(new_items_dir) print "" print "TEST that files were transferred" self.assertEqual(result, True) for file_ in new_room_files: print "File: " + file_ + " in temp save is in template" self.assertIn(file_, room_files) for file_ in room_files: if file_ not in IGNORE: print "File: " + file_ + " in temp save is in temp save" self.assertIn(file_, new_room_files) for file_ in new_item_files: print "File: " + file_ + " in temp save is in template" self.assertIn(file_, item_files) for file_ in item_files: if file_ not in IGNORE: print "File: " + file_ + " in temp save is in temp save" self.assertIn(file_, new_item_files) print "TEST compare the room files in both dirs to check that they match" match, mismatch, errors = filecmp.cmpfiles(rooms_dir, new_rooms_dir, room_files) for name in match: self.assertIn(name, room_files) self.assertIn(name, new_room_files) for name in mismatch: self.assertIn(name, IGNORE) self.assertEqual(len(mismatch), 0) print "TEST compare the item files in both dirs to check that they match" match, mismatch, errors = filecmp.cmpfiles(items_dir, new_items_dir, item_files) for name in match: self.assertIn(name, item_files) self.assertIn(name, new_item_files) for name in mismatch: self.assertIn(name, IGNORE) self.assertEqual(len(mismatch), 0)
def setUp(self): files.new_game() self.player = player.Player("Bob") print ""