class TestTeam(unittest.TestCase): def setUp(self): tiles = [Tile() for _ in range(9)] self.map = Map(3, 3, tiles) self.team = Team(Point(1, 1), self.map) def test_init(self): tiles = [Tile() for _ in range(9)] the_map = Map(3, 3, tiles) team = Team(Point(0, 0), the_map) self.assertEqual(team.deployed, []) self.assertEqual(team.undeployed, []) self.assertEqual(team.home, Point(0, 0)) def test_deployed_and_undeployed_returns_a_copy(self): x = self.team.undeployed x.append(123) self.assertEqual(self.team.undeployed, []) x = self.team.deployed x.append(123) self.assertEqual(self.team.deployed, []) def test_add_player(self): unit = Soldier() self.team.add_player(unit) self.assertEqual(self.team.undeployed, [unit]) def test_add_player_dupe(self): unit = Soldier() self.team.add_player(unit) self.team.add_player(unit) self.assertEqual(self.team.undeployed, [unit]) def test_add_player_multiple_players(self): unit = Soldier() unit_2 = Soldier() self.team.add_player(unit) self.team.add_player(unit_2) self.assertEqual(self.team.undeployed, [unit, unit_2]) def test_unteam_player_undeployed(self): unit = Soldier() unit_2 = Soldier() self.team.add_player(unit) self.team.add_player(unit_2) self.team.unteam_player(unit) self.assertEqual(self.team.undeployed, [unit_2]) def test_unteam_player_deployed(self): unit = Soldier() unit_2 = Soldier() self.team.add_player(unit) self.team.add_player(unit_2) self.team.spawn() self.team.spawn() self.team.unteam_player(unit) self.assertEqual(self.team.deployed, [unit_2]) def test_unteam_player_not_in_team(self): unit = Soldier() self.assertRaises(ValueError, self.team.unteam_player, unit) def test_is_on_team_true(self): unit = Soldier() self.team.add_player(unit) self.assertTrue(self.team.is_on_team(unit)) self.team.spawn() self.assertTrue(self.team.is_on_team(unit)) def test_is_on_team_false(self): unit = Soldier() self.team.add_player(unit) self.assertFalse(self.team.is_on_team(Soldier())) def test_spawn(self): units = [Soldier(), Soldier(), Soldier()] for soldier in units: self.team.add_player(soldier) for _ in range(3): self.team.spawn() self.assertEqual(self.map.get_unit(Point(1, 0)), units[0]) self.assertEqual(self.map.get_unit(Point(0, 1)), units[1]) self.assertEqual(self.map.get_unit(Point(2, 1)), units[2]) self.assertRaises(ValueError, self.team.spawn) def test_spawn_uses_undeployed_as_a_queue(self): units = [Soldier(), Soldier(), Soldier()] for soldier in units: self.team.add_player(soldier) self.assertEqual(self.team.undeployed, units) self.team.spawn() self.assertEqual(self.team.undeployed, units[1:]) self.assertEqual(self.team.deployed, units[0:1]) self.team.spawn() self.assertEqual(self.team.undeployed, units[2:]) self.assertEqual(self.team.deployed, units[0:2]) self.team.spawn() self.assertEqual(self.team.undeployed, []) self.assertEqual(self.team.deployed, units) def test_spawn_no_unplaced_players(self): unit = Soldier() self.team.add_player(unit) self.team.spawn() self.assertRaises(ValueError, self.team.spawn) def test_spawn_no_room_on_map(self): units = [Soldier() for _ in range(10)] for soldier in units: self.team.add_player(soldier) for _ in range(8): self.team.spawn() self.assertRaises(ValueError, self.team.spawn) def test_spawn_with_obstacles_at_distance_one(self): obstacles = self.team.home.at_distance(1) for point in obstacles: self.map.place_unit(Soldier(), point) units = [Soldier() for _ in range(3)] for soldier in units: self.team.add_player(soldier) for _ in range(len(units)): self.team.spawn() expected_points = [Point(0, 0), Point(2, 0), Point(0, 2)] for soldier, point in zip(units, expected_points): self.assertEqual(self.map.get_unit(point), soldier) def test_spawn_with_obstacles_some_at_distance_one_and_some_at_distance_two( self): obstacles = [Point(1, 0), Point(0, 1), Point(0, 0), Point(2, 2)] for point in obstacles: self.map.place_unit(Soldier(), point) units = [Soldier() for _ in range(3)] for soldier in units: self.team.add_player(soldier) for _ in range(len(units)): self.team.spawn() expected_points = [Point(2, 1), Point(1, 2), Point(2, 0)] for soldier, point in zip(units, expected_points): self.assertEqual(self.map.get_unit(point), soldier) def test_spawn_return_value(self): unit = Soldier() self.team.add_player(unit) self.assertEqual(self.team.spawn(), (unit, Point(1, 0))) def test_place_unit_and_then_spawn_raises_error(self): unit = Soldier() self.team.add_player(unit) self.map.place_unit(unit, Point(1, 0)) self.assertRaises(MapPlacementError, self.team.spawn)
class TestMap(unittest.TestCase): def setUp(self): self.unit = Soldier() self.width = 5 self.height = 3 self.tiles = get_tiles_without_points(self.width, self.height) self.map = Map(self.width, self.height, self.tiles) def test_init_pointless_tiles(self): for point in Point(0, 0).to_rectangle(5, 3): self.assertIs(self.map.get_tile(point), self.tiles.pop(0)) def test_init_pointed_tiles(self): width = 5 height = 3 tiles = get_tiles_with_points(width, height) test_map = Map(width, height, tiles) for point in Point(0, 0).to_rectangle(width, height): self.assertIs(test_map.get_tile(point), tiles.pop(0)) def test_init_mixed_tiles(self): width = 2 height = 2 tiles = [Tile(point=Point(0, 0)), Tile(), Tile(), Tile(point=Point(1, 1))] self.assertFalse(tiles[1].has_point()) test_map = Map(width, height, tiles) for tile in tiles: self.assertTrue(tile.has_point()) for point in Point(0, 0).to_rectangle(width, height): self.assertIs(test_map.get_tile(point), tiles.pop(0)) def test_init_raises_error_point_outside_map(self): tiles = get_tiles_with_points(1, 3) self.assertRaises(MapPlacementError, Map, 2, 2, tiles) def test_init_raises_error_two_tiles_same_point(self): tiles = [Tile(point=Point(0, 0)), Tile(point=Point(0, 0))] self.assertRaises(MapPlacementError, Map, 2, 2, tiles) def test_init_raises_error_if_more_tiles_than_points(self): tiles = get_tiles_without_points(5, 5) self.assertRaises(MapPlacementError, Map, 2, 2, tiles) def test_init_if_tiles_do_not_fill_points(self): tiles = get_tiles_without_points(3, 2) test_map = Map(3, 3, tiles) for point in Point(0, 0).to_rectangle(3, 2): self.assertIs(test_map.get_tile(point), tiles.pop(0)) for point in Point(0, 2).to_rectangle(3, 1): self.assertFalse(test_map.has_tile(point)) def test_get_size(self): to_test = Map(3, 5, []) self.assertEqual(to_test.get_size(), (3, 5)) def test_get_elevation(self): elevations = {Point(0, 0): 1, Point(1, 0): 2, Point(0, 1): -1, Point(1, 1): 0} tiles = [Tile(point=point, elevation=elevation) for point, elevation in elevations.items()] map_ = Map(2, 2, tiles) self.assertEqual(map_.get_elevation(Point(0, 0)), 1) self.assertEqual(map_.get_elevation(Point(1, 0)), 2) self.assertEqual(map_.get_elevation(Point(0, 1)), -1) self.assertEqual(map_.get_elevation(Point(1, 1)), 0) def test_get_elevation_empty_tile_and_not_on_map(self): elevations = {Point(0, 0): 1, Point(1, 0): 2, Point(0, 1): -1, } tiles = [Tile(point=point, elevation=elevation) for point, elevation in elevations.items()] map_ = Map(2, 2, tiles) self.assertEqual(map_.get_elevation(Point(1, 1)), float('-inf')) self.assertEqual(map_.get_elevation(Point(-1, -1)), float('-inf')) def test_is_on_map_true(self): points = Point(0, 0).to_rectangle(self.width, self.height) for point in points: self.assertTrue(self.map.is_on_map(point)) def test_is_on_map_false(self): points = [Point(-1, -1), Point(5, 5), Point(1, 10), Point(10, 1)] for point in points: self.assertFalse(self.map.is_on_map(point)) def test_has_tile_off_map(self): self.assertFalse(self.map.has_tile(Point(-1, -1))) def test_has_tile_empty_map_point(self): self.assertFalse(Map(2, 2, []).has_tile(Point(0, 0))) def test_has_tile_true(self): self.assertTrue(self.map.has_tile(Point(0, 0))) def test_get_tile(self): self.assertIs(self.map.get_tile(Point(0, 0)), self.tiles[0]) def test_get_tile_none(self): self.assertIsNone(Map(2, 2, []).get_tile(Point(1, 1))) def test_can_place_unit_true(self): self.assertTrue(self.map.can_place_unit(Point(1, 1))) def test_can_place_unit_false_by_not_on_map(self): self.assertFalse(self.map.can_place_unit(Point(10, 1))) def test_can_place_unit_false_by_no_tile(self): self.assertFalse(Map(2, 2, []).can_place_unit(Point(1, 1))) def test_can_place_unit_false_by_occupied_by_unit(self): self.map.place_unit(self.unit, Point(1, 1)) self.assertFalse(self.map.can_place_unit(Point(1, 1))) def test_get_unit_is_none_when_no_unit(self): self.assertIsNone(self.map.get_unit(Point(1, 1))) def test_get_unit(self): unit = Soldier() point = Point(1, 1) self.map.place_unit(unit, point) self.assertIs(unit, self.map.get_unit(point)) self.assertIsNone(self.map.get_unit(Point(0, 0))) def test_get_point(self): self.map.place_unit(self.unit, Point(0, 0)) self.assertEqual(self.map.get_point(self.unit), Point(0, 0)) self.map.remove_unit(Point(0, 0)) self.map.place_unit(self.unit, Point(0, 1)) self.assertEqual(self.map.get_point(self.unit), Point(0, 1)) def test_get_point_none(self): self.assertEqual(self.map.get_point(self.unit), None) def test_place_unit_error_by_not_on_map(self): self.assertRaises(MapPlacementError, self.map.place_unit, self.unit, Point(10, 1)) def test_place_unit_error_by_no_tile(self): test_map = Map(2, 2, []) self.assertRaises(MapPlacementError, test_map.place_unit, self.unit, Point(1, 1)) def test_place_unit_error_by_occupied_by_unit(self): unit_2 = Soldier() self.map.place_unit(self.unit, Point(1, 1)) self.assertRaises(MapPlacementError, self.map.place_unit, unit_2, Point(1, 1)) def test_place_unit_error_by_unit_placed_twice(self): self.map.place_unit(self.unit, Point(0, 0)) self.assertRaises(MapPlacementError, self.map.place_unit, self.unit, Point(1, 2)) def test_place_unit(self): self.map.place_unit(self.unit, Point(1, 1)) self.assertIs(self.map.get_unit(Point(1, 1)), self.unit) self.assertEqual(self.map.get_point(self.unit), Point(1, 1)) def test_has_unit_point_not_on_map_false(self): self.assertFalse(self.map.has_unit(Point(-1, -1))) def test_has_unit_false(self): self.assertFalse(self.map.has_unit(Point(1, 1))) def test_has_unit_true(self): self.map.place_unit(Soldier(), Point(1, 1)) self.assertTrue(self.map.has_unit(Point(1, 1))) self.assertFalse(self.map.has_unit(Point(2, 1))) def test_remove_unit(self): self.map.place_unit(self.unit, Point(1, 1)) self.map.remove_unit(Point(1, 1)) self.assertTrue(self.map.can_place_unit(Point(1, 1))) self.assertEqual(self.map.get_point(self.unit), None)