def test_serialization(self): basedir = util.game_path('test_data_dir') s = serializer.Serializer(basedir=basedir) folder = s.folder_name w = World(s, block_seed=0) b = w.get(0,0) p = b.set_entity(entities.Player, 3, 7) p_x, p_y = p.x, p.y for _ in range(2): w.process() self.assertEqual(w.turn, 2) turn = w.turn self.assertFalse(s.has_settings()) s.save_game(w, p) self.assertTrue(s.has_settings()) s.close_connection() del w del b del p del s s = serializer.Serializer(basedir=basedir, folder=folder) s.load_settings() w = s.init_world() self.assertEqual(turn, w.turn) p = s.init_player(w) self.assertEqual((p_x, p_y), (p.x, p.y)) b = w[(0, 0)] c = util.count_entities(b) self.assertEqual(c[entities.Player], 1) shutil.rmtree(s.serial_path) shutil.rmtree(basedir)
def test_block_process_big(benchmark): s_mock = SerializerMock() w = World(s_mock, block_seed=0) locs = itertools.product([1,0,-1], [1,0,-1]) for loc in locs: w.blocks[loc] = Block(*loc, world=w, tiles=test_block.gen_flat_map(Game.map_size), entities=rand_entities()) benchmark.pedantic(w.process, rounds=1)
def test_set_tile_open(benchmark): game = Game() w = World(None) locs = itertools.product([1,0,-1], [1,0,-1]) flat_map = gen_flat_map(Game.map_size) for loc in locs: w.blocks[loc] = Block(*loc, world=w, tiles=gen_flat_map(Game.map_size), entities=gen_map.gen_empty_entities(Game.map_size)) blk = w[(0,0)] benchmark.pedantic(blk.set_tile, args=(Game.game_width//2, Game.game_height//2, Id.wall), rounds=1)
def test_save_block(benchmark): basedir=util.game_path('test_data_dir') s = serializer.Serializer(basedir=basedir) folder = s.folder_name w = World(s, block_seed=0) b = w.get(0,0) p = b.set_entity(entities.Player, 3, 7) p_x, p_y = p.x, p.y turn = w.turn benchmark.pedantic(s.save_block, args=(b,), rounds=1) shutil.rmtree(s.serial_path) shutil.rmtree(basedir)
def setUp(self): s = SerializerMock() g = HeadlessGame() w = World(s, block_seed=0) start_block = w.get(0, 0) start_loc_x = Game.map_size//2 start_loc_y = Game.map_size//2 player = start_block.set_entity(Player, start_loc_x, start_loc_y) self.start_block = start_block self.player = player self.start_loc_x = start_loc_x self.start_loc_y = start_loc_y
def test_world_draw(benchmark): """Compare current screen with past screenshot""" g = Game() Game.set_sidebar(False) s = SerializerMock() w = World(s, block_seed=0) block = w.get(Game.idx_cur, Game.idy_cur) player = block.set_entity(Player, Game.map_size//2, Game.map_size//2) # Potentially update player location block.reposition_entity(player, avoid_hidden=True) # Update view to player location player.update_view_location() benchmark(w.draw)
def setUp(self): """Set up game/player to allow drawing""" self.__class__.set_game() Game.set_sidebar(False) s = SerializerMock() w = World(s, block_seed=0) start_block = w.get(Game.idx_cur, Game.idy_cur) player = start_block.set_entity(Player, Game.map_size//2, Game.map_size//2) # Potentially update player location start_block.reposition_entity(player, avoid_hidden=True) # Update view to player location player.update_view_location() status_bar_mock = StatusBarMock() self.status_bar_mock = status_bar_mock self.player = player self.w = w self.start_block = start_block self.console_flush = console_flush
def test_seed_float(self): world = World(None) seed_str, seed_float = world.generate_seeds(seed_float=5) self.assertTrue(seed_str is None) self.assertEqual(seed_float, 5)
def test_seed_str(self): world = World(None) seed_str, seed_float = world.generate_seeds("foo") self.assertEqual(seed_str, "foo") self.assertTrue(type(seed_float) is float)
def test_seeds(self): world = World(None) random.seed(0) seed_str, seed_float = world.generate_seeds() self.assertTrue(seed_str is None) self.assertTrue(type(seed_float) is float)
def setUp(self): s_mock = SerializerMock() self.w = World(s_mock, block_seed=0) self.zero_blk = self.w.get(0, 0)
class TestBlock(unittest.TestCase): def setUp(self): s_mock = SerializerMock() self.w = World(s_mock, block_seed=0) self.zero_blk = self.w.get(0, 0) def testMaps(self): self.assertEqual(self.zero_blk.tiles, zero_map) # Where the obstacle map does not match the gen_map obstacle map, # Make sure there is an entity located there obs_entities = [ (x,y) for x in range(len(obs_map)) for y in range(len(obs_map)) if obs_map[x][y] != self.zero_blk.obstacle_map[x][y]] for entity_loc in obs_entities: obstacle_entity = any([entity.is_obstacle for entity in self.zero_blk.entities[entity_loc[0]][entity_loc[1]]]) self.assertTrue(obstacle_entity) self.assertNotEqual(self.zero_blk.obstacle_map, obs_map) def testEntities(self): flat_list = [entity for a_slice in self.zero_blk.entities for a_cell in a_slice for entity in a_cell] flat_list_set = set(flat_list) entity_list_set = set(self.zero_blk.entity_list) self.assertEqual(flat_list_set, entity_list_set) fungus = self.zero_blk.set_entity(entities.Fungus, 0, 0) self.assertTrue(fungus in self.zero_blk.entities[0][0]) # Add an entity n_flat_list = [entity for a_slice in self.zero_blk.entities for a_cell in a_slice for entity in a_cell] n_flat_list_set = set(n_flat_list) n_entity_list_set = set(self.zero_blk.entity_list) self.assertEqual(n_flat_list_set, n_entity_list_set) self.assertNotEqual(flat_list_set, n_flat_list_set) self.assertNotEqual(entity_list_set, n_entity_list_set) # Get entity self.assertEqual(fungus, self.zero_blk.get_entity(0, 0)) self.assertEqual([fungus], self.zero_blk.get_entities(0, 0)) # Remove the entity self.zero_blk.remove_entity(fungus, 0, 0) self.assertFalse(fungus in self.zero_blk.entities[0][0]) with self.assertRaises(ValueError): self.zero_blk.remove_entity(fungus, 0, 0) # Get empty self.assertEqual(None, self.zero_blk.get_entity(0, 0)) self.assertEqual([], self.zero_blk.get_entities(0, 0)) n2_flat_list = [entity for a_slice in self.zero_blk.entities for a_cell in a_slice for entity in a_cell] n2_flat_list_set = set(n2_flat_list) n2_entity_list_set = set(self.zero_blk.entity_list) self.assertEqual(n2_flat_list_set, n2_entity_list_set) self.assertNotEqual(n2_flat_list_set, n_flat_list_set) self.assertNotEqual(n2_entity_list_set, n_entity_list_set) self.assertEqual(n2_flat_list_set, flat_list_set) self.assertEqual(n2_entity_list_set, entity_list_set) def testMoveEntity(self): movements = ((1, 0), (-1, 0), (0, 1), (0, -1)) for move in movements: fungus = self.zero_blk.set_entity(entities.Fungus, Game.map_size//2, Game.map_size//2) self.zero_blk.reposition_entity(fungus) (old_x,old_y) = fungus.x, fungus.y self.zero_blk.move_entity(fungus, *map(operator.add, move, (fungus.x, fungus.y))) self.assertEqual(map(operator.add, move, (old_x, old_y)), [fungus.x, fungus.y]) self.assertFalse(fungus in self.zero_blk.entities[old_x][old_y]) self.assertTrue(fungus in self.zero_blk.entities[fungus.x][fungus.y]) self.assertTrue(fungus in self.zero_blk.entity_list) self.zero_blk.remove_entity(fungus, fungus.x, fungus.y) def testDuplicateBlock(self): """Test duplicate block check with DuplicateBlockError exception""" with self.assertRaises(DuplicateBlockError) as exception: b = Block(0, 0, self.w) ex = exception.exception self.assertEqual(ex.idx, 0) self.assertEqual(ex.idy, 0)