class TestGameStateManager(unittest.TestCase): """ Teste les différentes fonctionnalités du GameStateManager """ def setUp(self): self.game = Game() self.referee = Referee self.game.set_referee(self.referee) self.tcsvc = TeamColorService(TeamColor.BLUE) self.game_world_OK = ReferenceTransferObject(self.game) self.game_world_OK.set_team_color_svc(self.tcsvc) self.GameStateManager1 = GameState() self.GameStateManager2 = GameState() self.GameStateManager1.set_reference(self.game_world_OK) def test_singleton(self): """ Teste si le Manager est un singleton, i.e. s'il ne peut y avoir qu'une seule instance du manager """ self.assertTrue(self.GameStateManager1 is self.GameStateManager2) self.assertIs(self.GameStateManager1, self.GameStateManager2) def test_set_reference(self): self.GameStateManager1.set_reference(self.game_world_OK) self.assertIs(self.GameStateManager1.game.referee, self.game_world_OK.game.referee) self.assertIs(self.GameStateManager1.field, self.game_world_OK.game.field) self.assertIs(self.GameStateManager1.game.our_team_color, self.game.our_team_color) game_state_manager = GameState() self.assertRaises(AssertionError, game_state_manager.set_reference, None) game = Game() game_world_nok = ReferenceTransferObject(game) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game_world_nok.game.set_referee(self.referee) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game = Game() game_world_nok = ReferenceTransferObject(game) game_world_nok.set_team_color_svc(self.tcsvc) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok)
class TestRaycast(unittest.TestCase): def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game_state.set_reference(game_world) self.game_state.game.ball.set_position(Position(100, 0), 0) def test_raycast(self): self.assertTrue( raycast(self.game_state, Position(100, 100), 200, -pi / 2, BALL_RADIUS, [], [], False)) def test_raycast2(self): pass
class WorldState: def __init__(self, mode_debug_active: bool=True): """ initialisation du worldstate :param mode_debug_active: (bool) indique si le mode debug est activé """ self.module_state = ModuleState() self.play_state = PlayState() self.game_state = GameState() # pour passer une interface de debug deja recuperer self.debug_interface = DebugInterface() def set_reference(self, world_reference: GameWorld) -> None: """ Passe le data transfert object GameWorld au game state pour qu'il prenne ses références. :param world_reference: GameWorld instance avec les références dedans :return: None """ self.game_state.set_reference(world_reference)
class WorldState: def __init__(self, mode_debug_active: bool = True): """ initialisation du worldstate :param mode_debug_active: (bool) indique si le mode debug est activé """ self.module_state = ModuleState() self.play_state = PlayState() self.game_state = GameState() # pour passer une interface de debug deja recuperer self.debug_interface = DebugInterface() def set_reference(self, world_reference: ReferenceTransferObject) -> None: """ Passe le data transfert object GameWorld au game state pour qu'il prenne ses références. :param world_reference: GameWorld instance avec les références dedans :return: None """ self.game_state.set_reference(world_reference)
class TestNode(unittest.TestCase): def setUp(self): self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = GameWorld(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.game_state.game.friends.players[0].update(Pose(Position(-4450, 0), 0)) self.tactic1 = GoalKeeper(self.game_state, 0) self.tactic2 = Stop(self.game_state, 1) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(0, foo) self.vertex2 = Vertex(1, foo2) def test_init(self): self.assertRaises(AssertionError, Node, "not a tactic") self.assertIsInstance(self.node2.tactic, Tactic) self.assertEqual(len(self.node2.vertices), 0) def test_add_vertex(self): self.assertRaises(AssertionError, self.node1.add_vertex, "not a vertex") self.node1.add_vertex(self.vertex1) self.assertEqual(len(self.node1.vertices), 1) self.node1.add_vertex(self.vertex1) self.assertEqual(len(self.node1.vertices), 1) # il ne peut y avoir qu'un vertex entre deux noeuds dans un sens self.node1.add_vertex(self.vertex2) self.assertEqual(len(self.node1.vertices), 2) def test_remove_vertex(self): self.assertRaises(AssertionError, self.node1.remove_vertex, "not an int") self.assertRaises(AssertionError, self.node1.remove_vertex, -1) self.node1.add_vertex(self.vertex1) self.node1.remove_vertex(420) self.assertEqual(len(self.node1.vertices), 1) self.node1.remove_vertex(0) self.assertEqual(len(self.node1.vertices), 0) @unittest.skip("thinkin we should have generic tactic for test purpose, this is infuriating") def test_exec(self): self.node1.add_vertex(self.vertex1) self.node1.add_vertex(self.vertex2) next_ai_command, next_node = self.node1.exec() self.assertEqual(next_node, 0) expected_aicmd = AICommand(0, AICommandType.MOVE, **{"pose_goal": Pose(Position(-4000, 0), 0)}) self.assertEqual(next_ai_command, expected_aicmd) self.node2.add_vertex(self.vertex2) expected_aicmd = AICommand(0, AICommandType.STOP)#, **{"pose_goal": Pose(Position(-4000, 0), 0)}) next_ai_command, next_node = self.node2.exec() self.assertEqual(next_ai_command, expected_aicmd) self.assertEqual(next_node, -1) def test_str(self): self.node1.add_vertex(self.vertex1) self.node1.add_vertex(self.vertex2) expected_string = "Tactic: GoalKeeper Vertices: " for vertex in self.node1.vertices: expected_string += "\n " + str(vertex) self.assertEqual(str(self.node1), expected_string) def test_set_flag(self): self.assertRaises(AssertionError, self.node1.set_flag, "not a flag") self.node1.set_flag(Flags.SUCCESS) self.assertEqual(self.node1.tactic.status_flag, Flags.SUCCESS)
class TestActions(unittest.TestCase): def setUp(self): # ToDo : Use mock instead of actual objects self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = GameWorld(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.player_id = 1 # random integer @unittest.skip("I don't know whuy the f**k it is broken here.") def test_move_to(self): self.pose = Pose(Position(0, 0, 0), orientation=0.0) self.move = MoveToPosition(self.game_state, self.player_id, self.pose) self.assertEqual(self.move.exec(), AICommand(1, AICommandType.MOVE, **{"pose_goal": self.pose})) self.pose = Pose(Position(0.5, 0.3, 0.2), orientation=3.2) self.move = MoveToPosition(self.game_state, self.player_id, self.pose) self.assertEqual(MoveToPosition.exec(self.move), AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": self.pose})) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_idle(self): self.idle = Idle(self.game_state, self.player_id) current_pose = None current_pose_string = AICommand(self.player_id, AICommandType.STOP) self.assertEqual(Idle.exec(self.idle), current_pose_string) @unittest.skip("I don't know what the f**k is happening here.") def test_GrabBall(self): self.grab_ball = GetBall(self.game_state, self.player_id) self.game_state.game.ball.set_position(Position(5, 0), 0) ai_cmd = self.grab_ball.exec() ball_position = self.game_state.get_ball_position() destination_orientation = get_angle(self.game_state.get_player_pose(self.player_id).position, ball_position) destination_pose = {"pose_goal": Pose(ball_position, destination_orientation)} ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": destination_pose}) self.assertEqual(ai_cmd, ai_cmd_expected) self.game_state.game.ball.set_position(Position(-5, 5), 0) ai_cmd = self.grab_ball.exec() ai_cmd_expected = AICommand(Pose(Position(-5, 5), 3*pi/4), 0) self.assertEqual(ai_cmd, ai_cmd_expected) @unittest.skip("LAZY ME OH HELL") def test_MoveWithBall(self): self.move_with_ball = MoveToDribblingBall(self.game_state, self.player_id, Position(100, 0)) self.game_state.game.ball.set_position(Position(5, 0), 0) ai_cmd = self.move_with_ball.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), 0)}) self.assertEqual(ai_cmd, ai_cmd_expected) self.game_state.game.ball.set_position(Position(5, 2), 0) ai_cmd = self.move_with_ball.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), atan(2/5))}) self.assertEqual(ai_cmd, ai_cmd_expected) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_GoBetween(self): # test avec une droite verticale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(100, -100), Position(200, 0)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), 0)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite horizontale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(-100, 100), Position(0, 200)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(0, 100), pi/2)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite quelconque self.go_between = GoBetween(self.game_state, self.player_id, Position(0, 500), Position(500, 0), Position(-300, -300)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(250, 250), -3*pi/4)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position1 self.go_between = GoBetween(self.game_state, self.player_id, Position(1000, 75), Position(1500, -250), Position(0, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(1150, -23), 3.1215)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position2 self.go_between = GoBetween(self.game_state, self.player_id, Position(-100, 50), Position(-50, 50), Position(-60.0 + sqrt(3), 51.0), 10) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-60, 50), 0.5235)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test correction pour respecter la distance minimale self.go_between = GoBetween(self.game_state, self.player_id, Position(-500, 25), Position(1, 25), Position(-179, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-179, 25), -pi/2)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test distance entre les positions insuffisantes self.assertRaises(AssertionError, GoBetween, self.game_state, self.player_id, Position(1, 1), Position(-1, -1), 50) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_GoBehind(self): # TODO: faire davantage de cas de test distance_behind = 500 # test avec une droite quelconque self.go_behind = GoBehind(self.game_state, self.player_id, Position(1.5, 2.3), Position(18.3, 27.8), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-273, -415), 0.9882)}) # AICommand(Pose(Position(-273, -415), 0.9882), 0) self.assertEqual(aicmd_obtenu, aicmd_cible) # test avec une droite verticale self.go_behind = GoBehind(self.game_state, self.player_id, Position(1000, 250.3), Position(1000, 725.8), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(1000, -249), 1.5707)}) # AICommand(Pose(Position(1000, -249), 1.5707), 0) self.assertEqual(aicmd_obtenu, aicmd_cible) # test avec une droite horizontale self.go_behind = GoBehind(self.game_state, self.player_id, Position(175.8, -200.34), Position(-276.8, -200.34), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(675, -200), -3.1415)}) self.assertEqual(aicmd_obtenu, aicmd_cible) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_kick(self): # test avec la valeur 0 (nulle) self.kick = Kick(self.game_state, self.player_id, 0) current_pose = self.game_state.get_player_pose(self.player_id) current_pose_string = AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose}) self.assertEqual(Kick.exec(self.kick), current_pose_string) # test avec la valeur 1 (force maximale) self.kick = Kick(self.game_state, self.player_id, 1) current_pose = self.game_state.get_player_pose(self.player_id) self.assertEqual(Kick.exec(self.kick), AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose})) # test avec la valeur 0.3 (force intermediaire) self.kick = Kick(self.game_state, self.player_id, 0.3) current_pose = self.game_state.get_player_pose(self.player_id) self.assertEqual(Kick.exec(self.kick), AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose})) @unittest.skip("I got lazy, didn't want to review all of the protectgoal.") def test_ProtectGoal(self): # test de base self.game_state.game.friends.players[0].update(Pose(Position(4450, 10))) self.game_state.game.ball.set_position(Position(0, 0), 0) self.protectGoal = ProtectGoal(self.game_state, 0) aicmd_obtenu = self.protectGoal.exec() aicmd_cible = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(4000, 0), -pi)}) self.assertEqual(aicmd_obtenu, aicmd_cible) # test distance max < distance min self.assertRaises(AssertionError, ProtectGoal, self.game_state, 0, True, 50, 40)
class TestGraph(unittest.TestCase): def setUp(self): self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW)) self.game_state.set_reference(game_world) self.game_state = GameState() self.empty_graph = Graph() self.graph1 = Graph() self.a_player = OurPlayer(TeamColor.YELLOW, A_PLAYER_ID) self.tactic1 = Stop(self.game_state, self.a_player) self.tactic2 = GoToPositionNoPathfinder(self.game_state, self.a_player, Pose(Position(500, 0), 0)) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(1, foo) self.graph1.add_node(self.node1) self.graph1.add_node(self.node2) self.graph1.add_vertex(0, 1, foo) def test_init(self): self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(len(self.empty_graph.nodes), 0) def test_get_current_tactic_name(self): self.assertEqual(self.graph1.get_current_tactic_name(), "Stop") self.assertEqual(self.empty_graph.get_current_tactic_name(), None) self.empty_graph.add_node(self.node2) self.assertEqual(self.empty_graph.get_current_tactic_name(), "GoToPositionNoPathfinder") def test_get_current_tactic(self): self.assertIsInstance(self.graph1.get_current_tactic(), Stop) self.assertEqual(self.empty_graph.get_current_tactic(), None) self.empty_graph.add_node(self.node2) self.assertIsInstance(self.empty_graph.get_current_tactic(), GoToPositionNoPathfinder) def test_add_node(self): self.assertEqual(len(self.graph1.nodes), 2) self.assertRaises(AssertionError, self.graph1.add_node, "not a node") def test_remove_node(self): self.assertRaises(AssertionError, self.graph1.remove_node, "not an int") self.assertRaises(AssertionError, self.graph1.remove_node, -1) self.assertRaises(AssertionError, self.graph1.remove_node, 420) self.graph1.remove_node(1) self.assertEqual(len(self.graph1.nodes), 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) def test_add_vertex(self): self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.assertRaises(AssertionError, self.graph1.add_vertex, "not an int", 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, -1, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 420, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, "not an int", foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, -1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 420, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 1, "not a callable") self.graph1.add_vertex(0, 1, foo) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) def test_remove_vertex(self): self.assertRaises(AssertionError, self.graph1.remove_vertex, "not an int", 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, -1, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 420, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, "not an int") self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, -1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, 420) self.graph1.add_node(self.node2) self.graph1.remove_vertex(0, 2) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.graph1.remove_vertex(0, 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) def test_exec(self): next_ai_command = self.graph1.exec() expected_ai_command = Idle(self.game_state, self.a_player).exec() self.assertEqual(self.graph1.current_node, 1) self.assertEqual(next_ai_command, expected_ai_command) self.assertRaises(EmptyGraphException, self.empty_graph.exec) self.empty_graph.add_node(self.node2) self.empty_graph.add_node(self.node1) self.empty_graph.add_vertex(0, 1, foo2) next_ai_command = self.empty_graph.exec() expected_ai_command = GoToPositionNoPathfinder( self.game_state, self.a_player, Pose(Position(500, 0), 0)).exec() self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) next_ai_command = self.empty_graph.exec() expected_ai_command = GoToPositionNoPathfinder( self.game_state, self.a_player, Pose(Position(500, 0), 0)).exec() self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) def test_set_current_node(self): self.assertRaises(AssertionError, self.graph1.set_current_node, "not an int") self.assertRaises(AssertionError, self.graph1.set_current_node, -1) self.assertRaises(AssertionError, self.graph1.set_current_node, 420) self.graph1.nodes[0].set_flag(Flags.WIP) self.graph1.set_current_node(1) self.assertEqual(self.graph1.current_node, 1) self.assertEqual(self.graph1.nodes[0].tactic.status_flag, Flags.INIT)
class TestGraph(unittest.TestCase): def setUp(self): self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = GameWorld(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.game_state = GameState() self.empty_graph = Graph() self.graph1 = Graph() self.tactic1 = Stop(self.game_state, 1) self.tactic2 = GoToPositionNoPathfinder(self.game_state, 0, Pose(Position(500, 0), 0)) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(1, foo) self.graph1.add_node(self.node1) self.graph1.add_node(self.node2) self.graph1.add_vertex(0, 1, foo) def test_init(self): self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(len(self.empty_graph.nodes), 0) def test_get_current_tactic_name(self): self.assertEqual(self.graph1.get_current_tactic_name(), "Stop") self.assertEqual(self.empty_graph.get_current_tactic_name(), None) self.empty_graph.add_node(self.node2) self.assertEqual(self.empty_graph.get_current_tactic_name(), "GoToPositionNoPathfinder") def test_get_current_tactic(self): self.assertIsInstance(self.graph1.get_current_tactic(), Stop) self.assertEqual(self.empty_graph.get_current_tactic(), None) self.empty_graph.add_node(self.node2) self.assertIsInstance(self.empty_graph.get_current_tactic(), GoToPositionNoPathfinder) def test_add_node(self): self.assertEqual(len(self.graph1.nodes), 2) self.assertRaises(AssertionError, self.graph1.add_node, "not a node") def test_remove_node(self): self.assertRaises(AssertionError, self.graph1.remove_node, "not an int") self.assertRaises(AssertionError, self.graph1.remove_node, -1) self.assertRaises(AssertionError, self.graph1.remove_node, 420) self.graph1.remove_node(1) self.assertEqual(len(self.graph1.nodes), 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) def test_add_vertex(self): self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.assertRaises(AssertionError, self.graph1.add_vertex, "not an int", 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, -1, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 420, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, "not an int", foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, -1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 420, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 1, "not a callable") self.graph1.add_vertex(0, 1, foo) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) def test_remove_vertex(self): self.assertRaises(AssertionError, self.graph1.remove_vertex, "not an int", 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, -1, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 420, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, "not an int") self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, -1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, 420) self.graph1.add_node(self.node2) self.graph1.remove_vertex(0, 2) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.graph1.remove_vertex(0, 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_exec(self): next_ai_command = self.graph1.exec() expected_ai_command = AICommand(1, AICommandType.STOP) self.assertEqual(self.graph1.current_node, 1) self.assertEqual(next_ai_command, expected_ai_command) self.assertRaises(EmptyGraphException, self.empty_graph.exec) self.empty_graph.add_node(self.node2) self.empty_graph.add_node(self.node1) self.empty_graph.add_vertex(0, 1, foo2) next_ai_command = self.empty_graph.exec() expected_ai_command = AICommand(0, AICommandType.MOVE, **{"pose_goal": Pose(Position(500, 0))}) self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) next_ai_command = self.empty_graph.exec() expected_ai_command = AICommand(0, AICommandType.MOVE, **{"pose_goal": Pose(Position(500, 0))}) self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) def test_set_current_node(self): self.assertRaises(AssertionError, self.graph1.set_current_node, "not an int") self.assertRaises(AssertionError, self.graph1.set_current_node, -1) self.assertRaises(AssertionError, self.graph1.set_current_node, 420) self.graph1.nodes[0].set_flag(Flags.WIP) self.graph1.set_current_node(1) self.assertEqual(self.graph1.current_node, 1) self.assertEqual(self.graph1.nodes[0].tactic.status_flag, Flags.INIT) def test_str(self): expected_string = "" for i in range(len(self.graph1.nodes)): expected_string += "Node " + str(i) + ": " + str(self.graph1.nodes[i]) + "\n" self.assertEqual(str(self.graph1), expected_string)
class TestNode(unittest.TestCase): def setUp(self): self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW)) self.game_state.set_reference(game_world) self.game_state.game.friends.players[0].pose = Pose( Position(-4450, 0), 0) self.tactic1 = GoalKeeper( self.game_state, self.game_state.game.friends.players[A_GOAL_PLAYER_ID]) self.tactic2 = Stop(self.game_state, self.game_state.game.friends.players[A_PLAYER_ID]) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(0, foo) self.vertex2 = Vertex(1, foo2) self.a_player = OurPlayer(TeamColor.BLUE, 0) def test_init(self): self.assertRaises(AssertionError, Node, "not a tactic") self.assertIsInstance(self.node2.tactic, Tactic) self.assertEqual(len(self.node2.vertices), 0) def test_add_vertex(self): self.assertRaises(AssertionError, self.node1.add_vertex, "not a vertex") self.node1.add_vertex(self.vertex1) self.assertEqual(len(self.node1.vertices), 1) self.node1.add_vertex(self.vertex1) self.assertEqual( len(self.node1.vertices), 1 ) # il ne peut y avoir qu'un vertex entre deux noeuds dans un sens self.node1.add_vertex(self.vertex2) self.assertEqual(len(self.node1.vertices), 2) def test_remove_vertex(self): self.assertRaises(AssertionError, self.node1.remove_vertex, "not an int") self.assertRaises(AssertionError, self.node1.remove_vertex, -1) self.node1.add_vertex(self.vertex1) self.node1.remove_vertex(420) self.assertEqual(len(self.node1.vertices), 1) self.node1.remove_vertex(0) self.assertEqual(len(self.node1.vertices), 0) @unittest.skip( "With the current implemention of aiCommand, testing this is more or less impossible" ) def test_exec(self): self.node1.add_vertex(self.vertex1) self.node1.add_vertex(self.vertex2) next_ai_command, next_node = self.node1.exec() self.assertEqual(next_node, 0) expected_aicmd = AICommand( self.a_player, AICommandType.MOVE, **{"pose_goal": Pose(Position(-4000, 0), 0)}) self.assertEqual(next_ai_command, expected_aicmd) self.node2.add_vertex(self.vertex2) expected_aicmd = AICommand( self.a_player, AICommandType.STOP ) #, **{"pose_goal": Pose(Position(-4000, 0), 0)}) next_ai_command, next_node = self.node2.exec() self.assertEqual(next_ai_command, expected_aicmd) self.assertEqual(next_node, -1) def test_set_flag(self): self.assertRaises(AssertionError, self.node1.set_flag, "not a flag") self.node1.set_flag(Flags.SUCCESS) self.assertEqual(self.node1.tactic.status_flag, Flags.SUCCESS)
class TestGameStateManager(unittest.TestCase): """ Teste les différentes fonctionnalités du GameStateManager """ def setUp(self): self.game = Game() self.referee = Referee self.game.set_referee(self.referee) self.tcsvc = TeamColorService(TeamColor.YELLOW_TEAM) self.game.set_our_team_color(self.tcsvc.OUR_TEAM_COLOR) self.game_world_OK = GameWorld(self.game) self.game_world_OK.set_team_color_svc(self.tcsvc) self.GameStateManager1 = GameState() self.GameStateManager2 = GameState() self.GameStateManager1.set_reference(self.game_world_OK) def test_singleton(self): """ Teste si le Manager est un singleton, i.e. s'il ne peut y avoir qu'une seule instance du manager """ self.assertTrue(self.GameStateManager1 is self.GameStateManager2) self.assertIs(self.GameStateManager1, self.GameStateManager2) def test_set_reference(self): self.GameStateManager1.set_reference(self.game_world_OK) self.assertIs(self.GameStateManager1.game.referee, self.game_world_OK.game.referee) self.assertIs(self.GameStateManager1.field, self.game_world_OK.game.field) self.assertIs(self.GameStateManager2.our_team_color, self.game.our_team_color) game_state_manager = GameState() self.assertRaises(AssertionError, game_state_manager.set_reference, None) game = Game() game_world_nok = GameWorld(game) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game_world_nok.game.set_referee(self.referee) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game = Game() game_world_nok = GameWorld(game) game_world_nok.set_team_color_svc(self.tcsvc) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) def test_get_player_pose(self): self.assertIs(self.GameStateManager1.get_player_pose(0, True), self.game.friends.players[0].pose) self.assertIs(self.GameStateManager2.get_player_pose(0, False), self.game.enemies.players[0].pose) self.assertIsNot(self.GameStateManager1.get_player_pose(0, True), self.game.friends.players[1].pose) self.assertIsNot(self.GameStateManager2.get_player_pose(0, False), self.game.enemies.players[1].pose) self.assertIsNot(self.GameStateManager1.get_player_pose(0, True), self.game.enemies.players[0].pose) self.assertIsNot(self.GameStateManager2.get_player_pose(0, False), self.game.friends.players[0].pose) def test_get_player_position(self): self.assertIs(self.GameStateManager1.get_player_position(0, True), self.game.friends.players[0].pose.position) self.assertIs(self.GameStateManager2.get_player_position(0, False), self.game.enemies.players[0].pose.position)
class TestActions(unittest.TestCase): def setUp(self): # ToDo : Use mock instead of actual objects self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.player_id = 1 # random integer @unittest.skip("I don't know whuy the f**k it is broken here.") def test_move_to(self): self.pose = Pose(Position(0, 0, 0), orientation=0.0) self.move = MoveToPosition(self.game_state, self.player_id, self.pose) self.assertEqual( self.move.exec(), AICommand(1, AICommandType.MOVE, **{"pose_goal": self.pose})) self.pose = Pose(Position(0.5, 0.3, 0.2), orientation=3.2) self.move = MoveToPosition(self.game_state, self.player_id, self.pose) self.assertEqual( MoveToPosition.exec(self.move), AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": self.pose})) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_idle(self): self.idle = Idle(self.game_state, self.player_id) current_pose = None current_pose_string = AICommand(self.player_id, AICommandType.STOP) self.assertEqual(Idle.exec(self.idle), current_pose_string) @unittest.skip("I don't know what the f**k is happening here.") def test_GrabBall(self): self.grab_ball = GetBall(self.game_state, self.player_id) self.game_state.game.ball.set_position(Position(5, 0), 0) ai_cmd = self.grab_ball.exec() ball_position = self.game_state.get_ball_position() destination_orientation = get_angle( self.game_state.get_player_pose(self.player_id).position, ball_position) destination_pose = { "pose_goal": Pose(ball_position, destination_orientation) } ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": destination_pose}) self.assertEqual(ai_cmd, ai_cmd_expected) self.game_state.game.ball.set_position(Position(-5, 5), 0) ai_cmd = self.grab_ball.exec() ai_cmd_expected = AICommand(Pose(Position(-5, 5), 3 * pi / 4), 0) self.assertEqual(ai_cmd, ai_cmd_expected) @unittest.skip("LAZY ME OH HELL") def test_MoveWithBall(self): self.move_with_ball = MoveToDribblingBall(self.game_state, self.player_id, Position(100, 0)) self.game_state.game.ball.set_position(Position(5, 0), 0) ai_cmd = self.move_with_ball.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), 0)}) self.assertEqual(ai_cmd, ai_cmd_expected) self.game_state.game.ball.set_position(Position(5, 2), 0) ai_cmd = self.move_with_ball.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), atan(2 / 5))}) self.assertEqual(ai_cmd, ai_cmd_expected) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_GoBetween(self): # test avec une droite verticale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(100, -100), Position(200, 0)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), 0)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite horizontale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(-100, 100), Position(0, 200)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(0, 100), pi / 2)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite quelconque self.go_between = GoBetween(self.game_state, self.player_id, Position(0, 500), Position(500, 0), Position(-300, -300)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(250, 250), -3 * pi / 4)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position1 self.go_between = GoBetween(self.game_state, self.player_id, Position(1000, 75), Position(1500, -250), Position(0, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(1150, -23), 3.1215)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position2 self.go_between = GoBetween(self.game_state, self.player_id, Position(-100, 50), Position(-50, 50), Position(-60.0 + sqrt(3), 51.0), 10) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-60, 50), 0.5235)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test correction pour respecter la distance minimale self.go_between = GoBetween(self.game_state, self.player_id, Position(-500, 25), Position(1, 25), Position(-179, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-179, 25), -pi / 2)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test distance entre les positions insuffisantes self.assertRaises(AssertionError, GoBetween, self.game_state, self.player_id, Position(1, 1), Position(-1, -1), 50) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_GoBehind(self): # TODO: faire davantage de cas de test distance_behind = 500 # test avec une droite quelconque self.go_behind = GoBehind(self.game_state, self.player_id, Position(1.5, 2.3), Position(18.3, 27.8), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-273, -415), 0.9882)}) # AICommand(Pose(Position(-273, -415), 0.9882), 0) self.assertEqual(aicmd_obtenu, aicmd_cible) # test avec une droite verticale self.go_behind = GoBehind(self.game_state, self.player_id, Position(1000, 250.3), Position(1000, 725.8), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(1000, -249), 1.5707)}) # AICommand(Pose(Position(1000, -249), 1.5707), 0) self.assertEqual(aicmd_obtenu, aicmd_cible) # test avec une droite horizontale self.go_behind = GoBehind(self.game_state, self.player_id, Position(175.8, -200.34), Position(-276.8, -200.34), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(675, -200), -3.1415)}) self.assertEqual(aicmd_obtenu, aicmd_cible) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_kick(self): # test avec la valeur 0 (nulle) self.kick = Kick(self.game_state, self.player_id, 0) current_pose = self.game_state.get_player_pose(self.player_id) current_pose_string = AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose}) self.assertEqual(Kick.exec(self.kick), current_pose_string) # test avec la valeur 1 (force maximale) self.kick = Kick(self.game_state, self.player_id, 1) current_pose = self.game_state.get_player_pose(self.player_id) self.assertEqual( Kick.exec(self.kick), AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose})) # test avec la valeur 0.3 (force intermediaire) self.kick = Kick(self.game_state, self.player_id, 0.3) current_pose = self.game_state.get_player_pose(self.player_id) self.assertEqual( Kick.exec(self.kick), AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose})) @unittest.skip("I got lazy, didn't want to review all of the protectgoal.") def test_ProtectGoal(self): # test de base self.game_state.game.friends.players[0].update(Pose(Position(4450, 10))) self.game_state.game.ball.set_position(Position(0, 0), 0) self.protectGoal = ProtectGoal(self.game_state, 0) aicmd_obtenu = self.protectGoal.exec() aicmd_cible = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(4000, 0), -pi)}) self.assertEqual(aicmd_obtenu, aicmd_cible) # test distance max < distance min self.assertRaises(AssertionError, ProtectGoal, self.game_state, 0, True, 50, 40)
class TestActions(unittest.TestCase): def setUp(self): # ToDo : Use mock instead of actual objects self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW)) self.game_state.set_reference(game_world) self.a_player = OurPlayer(TeamColor.YELLOW, A_PLAYER_ID) def test_move_to(self): A_CRUISE_SPEED = 0.1 self.pose = Pose(Position(0, 0), 0.0) self.move = MoveToPosition(self.game_state, self.a_player, self.pose, False, A_CRUISE_SPEED) return_cmd = self.move.exec() expected_cmd = AICommand( self.a_player, AICommandType.MOVE, **{ "pose_goal": self.pose, "pathfinder_on": False, "cruise_speed": A_CRUISE_SPEED }) self.assertEqual(return_cmd, expected_cmd) self.pose = Pose(Position(0.5, 0.3), 3.2) self.move = MoveToPosition(self.game_state, self.a_player, self.pose, False, A_CRUISE_SPEED) self.assertEqual( MoveToPosition.exec(self.move), AICommand( self.a_player, AICommandType.MOVE, **{ "pose_goal": self.pose, "pathfinder_on": False, "cruise_speed": A_CRUISE_SPEED })) def test_idle(self): idle = Idle(self.game_state, self.a_player) expected_command = AICommand( self.a_player, AICommandType.STOP, control_loop_type=AIControlLoopType.POSITION) actual_command = Idle.exec(idle) self.assertEqual(actual_command, expected_command) def test_GrabBall(self): self.grab_ball = GetBall(self.game_state, self.a_player) self.game_state.set_ball_position(Position(5, 0), A_DELTA_T) ai_cmd = self.grab_ball.exec() ball_position = self.game_state.get_ball_position() destination_orientation = (ball_position - self.a_player.pose.position).angle() destination_pose = Pose(ball_position, destination_orientation) ai_cmd_expected = AICommand(self.a_player, AICommandType.MOVE, **{"pose_goal": destination_pose}) self.assertEqual(ai_cmd, ai_cmd_expected) grab_pose = Pose(Position(-5, 5), 3 * pi / 4) self.game_state.set_ball_position(Position(-5, 5), A_DELTA_T) ai_cmd = self.grab_ball.exec() ai_cmd_expected = AICommand(self.a_player, AICommandType.MOVE, **{"pose_goal": grab_pose}) self.assertEqual(ai_cmd, ai_cmd_expected) @unittest.skip("GoBetween does not actually go in between") def test_GoBetween(self): # test avec une droite verticale POS_TOP = Position(100, 100) POS_BOTTOM = Position(100, -100) POS_TARGET = Position(200, 0) POS_INBETWEEN = Position(100, 0) self.go_between = GoBetween(self.game_state, self.a_player, POS_TOP, POS_BOTTOM, POS_TARGET) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(POS_INBETWEEN, 0) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite horizontale self.go_between = GoBetween(self.game_state, self.a_player, Position(100, 100), Position(-100, 100), Position(0, 200)) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(Position(0, 100), pi / 2) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite quelconque self.go_between = GoBetween(self.game_state, self.a_player, Position(0, 500), Position(500, 0), Position(-300, -300)) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(Position(250, 250), -3 * pi / 4) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position1 self.go_between = GoBetween(self.game_state, self.a_player, Position(1000, 75), Position(1500, -250), Position(0, 0), 0) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(Position(1000, 75), -3.067) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position2 self.go_between = GoBetween(self.game_state, self.a_player, Position(-100, 50), Position(-50, 50), Position(-60.0 + sqrt(3), 51.0), 10) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(Position(-60, 50), 0.5235) self.assertEqual(ai_cmd, ai_cmd_expected) # test correction pour respecter la distance minimale self.go_between = GoBetween(self.game_state, self.a_player, Position(-500, 25), Position(1, 25), Position(-179, 0), 180) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(Position(-179, 25), -pi / 2) self.assertEqual(ai_cmd, ai_cmd_expected) # test distance entre les positions insuffisantes self.assertRaises(AssertionError, GoBetween, self.game_state, self.a_player, Position(1, 1), Position(-1, -1), 50) def test_GoBehind(self): # TODO: faire davantage de cas de test distance_behind = 500 # test avec une droite verticale self.go_behind = GoBehind(self.game_state, self.a_player, Position(1000, 250.3), Position(1000, 725.8), distance_behind) aicmd_obtenu = self.go_behind.exec() aicmd_expected = AICommand( self.a_player, AICommandType.MOVE, **{"pose_goal": Pose(Position(1000, -249.700), 1.5707)}) self.assertEqual(aicmd_obtenu, aicmd_expected) # test avec une droite quelconque self.go_behind = GoBehind(self.game_state, self.a_player, Position(1.5, 2.3), Position(18.3, 27.8), distance_behind) aicmd_obtenu = self.go_behind.exec() aicmd_expected = AICommand( self.a_player, AICommandType.MOVE, **{"pose_goal": Pose(Position(-273.579, -415.230), 0.9882)}) self.assertEqual(aicmd_obtenu, aicmd_expected) # test avec une droite horizontale self.go_behind = GoBehind(self.game_state, self.a_player, Position(175.8, -200.34), Position(-276.8, -200.34), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand( self.a_player, AICommandType.MOVE, **{"pose_goal": Pose(Position(675.800, 99.660), -2.601)}) self.assertEqual(aicmd_obtenu, aicmd_cible) def test_kick(self): # test avec la valeur 0 (nulle) target = Pose(Position(1, 1)) ball_position = Position(5, 0) self.game_state.set_ball_position(ball_position, A_DELTA_T) expected_cmd = AICommand( self.a_player, AICommandType.MOVE, **{ "pose_goal": Pose(ball_position, 0.785), "charge_kick": True, "kick": True, "pathfinder_on": True, "cruise_speed": 0.1, "end_speed": 0 }) return_cmd = Kick(self.game_state, self.a_player, force=0, target=target).exec() self.assertEqual(expected_cmd, return_cmd) # test avec la valeur 1 (force maximale) expected_cmd.kick_strength = 1 return_cmd = Kick(self.game_state, self.a_player, 1, target=target).exec() self.assertEqual(return_cmd, expected_cmd) # test avec la valeur 0.3 (force intermediaire) expected_cmd.kick_strength = 0.3 return_cmd = Kick(self.game_state, self.a_player, 0.3, target=target).exec() self.assertEqual(return_cmd, expected_cmd) @unittest.skip("I got lazy, didn't want to review all of the protectgoal.") def test_ProtectGoal(self): # test de base self.game_state.game.friends.players[0].update(Pose(Position(4450, 10))) self.game_state.game.ball.set_position(Position(0, 0), 0) self.protectGoal = ProtectGoal(self.game_state, 0) aicmd_obtenu = self.protectGoal.exec() aicmd_cible = AICommand(self.a_player, AICommandType.MOVE, **{"pose_goal": Pose(Position(4000, 0), -pi)}) self.assertEqual(aicmd_obtenu, aicmd_cible) # test distance max < distance min self.assertRaises(AssertionError, ProtectGoal, self.game_state, 0, True, 50, 40)
class TestNode(unittest.TestCase): def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game_state.set_reference(game_world) self.game_state.game.friends.players[0].update( Pose(Position(-4450, 0), 0)) self.tactic1 = GoalKeeper(self.game_state, 0) self.tactic2 = Stop(self.game_state, 1) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(0, foo) self.vertex2 = Vertex(1, foo2) def test_init(self): self.assertRaises(AssertionError, Node, "not a tactic") self.assertIsInstance(self.node2.tactic, Tactic) self.assertEqual(len(self.node2.vertices), 0) def test_add_vertex(self): self.assertRaises(AssertionError, self.node1.add_vertex, "not a vertex") self.node1.add_vertex(self.vertex1) self.assertEqual(len(self.node1.vertices), 1) self.node1.add_vertex(self.vertex1) self.assertEqual( len(self.node1.vertices), 1 ) # il ne peut y avoir qu'un vertex entre deux noeuds dans un sens self.node1.add_vertex(self.vertex2) self.assertEqual(len(self.node1.vertices), 2) def test_remove_vertex(self): self.assertRaises(AssertionError, self.node1.remove_vertex, "not an int") self.assertRaises(AssertionError, self.node1.remove_vertex, -1) self.node1.add_vertex(self.vertex1) self.node1.remove_vertex(420) self.assertEqual(len(self.node1.vertices), 1) self.node1.remove_vertex(0) self.assertEqual(len(self.node1.vertices), 0) @unittest.skip( "thinkin we should have generic tactic for test purpose, this is infuriating" ) def test_exec(self): self.node1.add_vertex(self.vertex1) self.node1.add_vertex(self.vertex2) next_ai_command, next_node = self.node1.exec() self.assertEqual(next_node, 0) expected_aicmd = AICommand( 0, AICommandType.MOVE, **{"pose_goal": Pose(Position(-4000, 0), 0)}) self.assertEqual(next_ai_command, expected_aicmd) self.node2.add_vertex(self.vertex2) expected_aicmd = AICommand( 0, AICommandType.STOP ) #, **{"pose_goal": Pose(Position(-4000, 0), 0)}) next_ai_command, next_node = self.node2.exec() self.assertEqual(next_ai_command, expected_aicmd) self.assertEqual(next_node, -1) def test_str(self): self.node1.add_vertex(self.vertex1) self.node1.add_vertex(self.vertex2) expected_string = "Tactic: GoalKeeper Vertices: " for vertex in self.node1.vertices: expected_string += "\n " + str(vertex) self.assertEqual(str(self.node1), expected_string) def test_set_flag(self): self.assertRaises(AssertionError, self.node1.set_flag, "not a flag") self.node1.set_flag(Flags.SUCCESS) self.assertEqual(self.node1.tactic.status_flag, Flags.SUCCESS)
class TestGraph(unittest.TestCase): def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game_state.set_reference(game_world) self.game_state = GameState() self.empty_graph = Graph() self.graph1 = Graph() self.tactic1 = Stop(self.game_state, 1) self.tactic2 = GoToPositionNoPathfinder(self.game_state, 0, Pose(Position(500, 0), 0)) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(1, foo) self.graph1.add_node(self.node1) self.graph1.add_node(self.node2) self.graph1.add_vertex(0, 1, foo) def test_init(self): self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(len(self.empty_graph.nodes), 0) def test_get_current_tactic_name(self): self.assertEqual(self.graph1.get_current_tactic_name(), "Stop") self.assertEqual(self.empty_graph.get_current_tactic_name(), None) self.empty_graph.add_node(self.node2) self.assertEqual(self.empty_graph.get_current_tactic_name(), "GoToPositionNoPathfinder") def test_get_current_tactic(self): self.assertIsInstance(self.graph1.get_current_tactic(), Stop) self.assertEqual(self.empty_graph.get_current_tactic(), None) self.empty_graph.add_node(self.node2) self.assertIsInstance(self.empty_graph.get_current_tactic(), GoToPositionNoPathfinder) def test_add_node(self): self.assertEqual(len(self.graph1.nodes), 2) self.assertRaises(AssertionError, self.graph1.add_node, "not a node") def test_remove_node(self): self.assertRaises(AssertionError, self.graph1.remove_node, "not an int") self.assertRaises(AssertionError, self.graph1.remove_node, -1) self.assertRaises(AssertionError, self.graph1.remove_node, 420) self.graph1.remove_node(1) self.assertEqual(len(self.graph1.nodes), 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) def test_add_vertex(self): self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.assertRaises(AssertionError, self.graph1.add_vertex, "not an int", 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, -1, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 420, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, "not an int", foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, -1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 420, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 1, "not a callable") self.graph1.add_vertex(0, 1, foo) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) def test_remove_vertex(self): self.assertRaises(AssertionError, self.graph1.remove_vertex, "not an int", 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, -1, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 420, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, "not an int") self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, -1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, 420) self.graph1.add_node(self.node2) self.graph1.remove_vertex(0, 2) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.graph1.remove_vertex(0, 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_exec(self): next_ai_command = self.graph1.exec() expected_ai_command = AICommand(1, AICommandType.STOP) self.assertEqual(self.graph1.current_node, 1) self.assertEqual(next_ai_command, expected_ai_command) self.assertRaises(EmptyGraphException, self.empty_graph.exec) self.empty_graph.add_node(self.node2) self.empty_graph.add_node(self.node1) self.empty_graph.add_vertex(0, 1, foo2) next_ai_command = self.empty_graph.exec() expected_ai_command = AICommand( 0, AICommandType.MOVE, **{"pose_goal": Pose(Position(500, 0))}) self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) next_ai_command = self.empty_graph.exec() expected_ai_command = AICommand( 0, AICommandType.MOVE, **{"pose_goal": Pose(Position(500, 0))}) self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) def test_set_current_node(self): self.assertRaises(AssertionError, self.graph1.set_current_node, "not an int") self.assertRaises(AssertionError, self.graph1.set_current_node, -1) self.assertRaises(AssertionError, self.graph1.set_current_node, 420) self.graph1.nodes[0].set_flag(Flags.WIP) self.graph1.set_current_node(1) self.assertEqual(self.graph1.current_node, 1) self.assertEqual(self.graph1.nodes[0].tactic.status_flag, Flags.INIT) def test_str(self): expected_string = "" for i in range(len(self.graph1.nodes)): expected_string += "Node " + str(i) + ": " + str( self.graph1.nodes[i]) + "\n" self.assertEqual(str(self.graph1), expected_string)
class TestGameStateManager(unittest.TestCase): """ Teste les différentes fonctionnalités du GameStateManager """ def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game = Game() self.referee = Referee self.game.set_referee(self.referee) self.tcsvc = TeamColorService(TeamColor.BLUE_TEAM) self.game.set_our_team_color(self.tcsvc.OUR_TEAM_COLOR) self.game_world_OK = ReferenceTransferObject(self.game) self.game_world_OK.set_team_color_svc(self.tcsvc) self.GameStateManager1 = GameState() self.GameStateManager2 = GameState() self.GameStateManager1.set_reference(self.game_world_OK) def test_singleton(self): """ Teste si le Manager est un singleton, i.e. s'il ne peut y avoir qu'une seule instance du manager """ self.assertTrue(self.GameStateManager1 is self.GameStateManager2) self.assertIs(self.GameStateManager1, self.GameStateManager2) def test_set_reference(self): self.GameStateManager1.set_reference(self.game_world_OK) self.assertIs(self.GameStateManager1.game.referee, self.game_world_OK.game.referee) self.assertIs(self.GameStateManager1.field, self.game_world_OK.game.field) self.assertIs(self.GameStateManager1.game.our_team_color, self.game.our_team_color) game_state_manager = GameState() self.assertRaises(AssertionError, game_state_manager.set_reference, None) game = Game() game_world_nok = ReferenceTransferObject(game) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game_world_nok.game.set_referee(self.referee) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game = Game() game_world_nok = ReferenceTransferObject(game) game_world_nok.set_team_color_svc(self.tcsvc) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) def test_get_player_pose(self): self.assertIs(self.GameStateManager1.get_player_pose(0, True), self.game.friends.players[0].pose) self.assertIs(self.GameStateManager2.get_player_pose(0, False), self.game.enemies.players[0].pose) self.assertIsNot(self.GameStateManager1.get_player_pose(0, True), self.game.friends.players[1].pose) self.assertIsNot(self.GameStateManager2.get_player_pose(0, False), self.game.enemies.players[1].pose) self.assertIsNot(self.GameStateManager1.get_player_pose(0, True), self.game.enemies.players[0].pose) self.assertIsNot(self.GameStateManager2.get_player_pose(0, False), self.game.friends.players[0].pose) def test_get_player_position(self): self.assertIs(self.GameStateManager1.get_player_position(0, True), self.game.friends.players[0].pose.position) self.assertIs(self.GameStateManager2.get_player_position(0, False), self.game.enemies.players[0].pose.position)